Show More
@@ -47,12 +47,12 static struct PyGetSetDef listdir_stat_g | |||||
47 |
|
47 | |||
48 | static PyObject *listdir_stat_new(PyTypeObject *t, PyObject *a, PyObject *k) |
|
48 | static PyObject *listdir_stat_new(PyTypeObject *t, PyObject *a, PyObject *k) | |
49 | { |
|
49 | { | |
50 |
|
|
50 | return t->tp_alloc(t, 0); | |
51 | } |
|
51 | } | |
52 |
|
52 | |||
53 | static void listdir_stat_dealloc(PyObject *o) |
|
53 | static void listdir_stat_dealloc(PyObject *o) | |
54 | { |
|
54 | { | |
55 |
|
|
55 | o->ob_type->tp_free(o); | |
56 | } |
|
56 | } | |
57 |
|
57 | |||
58 | static PyTypeObject listdir_stat_type = { |
|
58 | static PyTypeObject listdir_stat_type = { | |
@@ -111,6 +111,8 static inline int mode_to_kind(int mode) | |||||
111 |
|
111 | |||
112 | static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs) |
|
112 | static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs) | |
113 | { |
|
113 | { | |
|
114 | static char *kwlist[] = { "path", "stat", NULL }; | |||
|
115 | PyObject *statobj = NULL; | |||
114 |
|
|
116 | DIR *dir = NULL; | |
115 |
|
|
117 | struct dirent *ent; | |
116 |
|
|
118 | PyObject *list = NULL; | |
@@ -121,38 +123,38 static PyObject *listdir(PyObject *self, | |||||
121 |
|
|
123 | int do_stat; | |
122 |
|
|
124 | char *path; | |
123 |
|
|
125 | int ret; | |
124 |
|
126 | ssize_t size; | ||
125 | { |
|
127 | ssize_t i; | |
126 | static char *kwlist[] = { "path", "stat", NULL }; |
|
128 | int dfd; | |
127 | PyObject *statobj = NULL; |
|
|||
128 |
|
129 | |||
129 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:listdir", kwlist, |
|
130 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:listdir", kwlist, | |
130 | &path, &path_len, &statobj)) |
|
131 | &path, &path_len, &statobj)) | |
131 |
|
|
132 | goto bail; | |
132 |
|
133 | |||
133 | do_stat = statobj && PyObject_IsTrue(statobj); |
|
134 | do_stat = statobj && PyObject_IsTrue(statobj); | |
134 | } |
|
|||
135 |
|
135 | |||
136 |
|
|
136 | dir = opendir(path); | |
|
137 | if (!dir) { | |||
137 | list = PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
|
138 | list = PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); | |
138 |
|
|
139 | goto bail; | |
139 | } |
|
140 | } | |
140 |
|
141 | |||
141 |
|
|
142 | list = PyList_New(0); | |
|
143 | if (!list) | |||
142 |
|
|
144 | goto bail; | |
143 |
|
145 | |||
144 |
|
|
146 | full_path = alloca(path_len + PATH_MAX + 2); | |
145 |
|
|
147 | memcpy(full_path, path, path_len); | |
146 |
|
|
148 | full_path[path_len] = '/'; | |
147 |
|
149 | |||
148 | while ((ent = readdir(dir))) { |
|
150 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
149 |
|
|
151 | PyObject *name = NULL; | |
150 |
|
|
152 | PyObject *py_kind = NULL; | |
151 |
|
|
153 | PyObject *val = NULL; | |
152 |
|
|
154 | unsigned char d_type; | |
153 |
|
|
155 | int kind = -1; | |
154 |
|
156 | |||
155 |
|
|
157 | if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) | |
156 | continue; |
|
158 | continue; | |
157 |
|
159 | |||
158 | #ifdef DT_REG |
|
160 | #ifdef DT_REG | |
@@ -190,11 +192,10 static PyObject *listdir(PyObject *self, | |||||
190 |
|
192 | |||
191 |
|
|
193 | val = PyTuple_New(do_stat ? 3 : 2); | |
192 |
|
194 | |||
193 |
|
|
195 | if (!name || !py_kind || !val) { | |
194 |
|
|
196 | Py_XDECREF(name); | |
195 |
|
|
197 | Py_XDECREF(py_kind); | |
196 |
|
|
198 | Py_XDECREF(val); | |
197 |
|
||||
198 | goto bail; |
|
199 | goto bail; | |
199 | } |
|
200 | } | |
200 |
|
201 | |||
@@ -210,14 +211,12 static PyObject *listdir(PyObject *self, | |||||
210 | } |
|
211 | } | |
211 |
|
212 | |||
212 |
|
|
213 | PyList_Sort(list); | |
|
214 | size = PyList_Size(list); | |||
|
215 | #ifdef AT_SYMLINK_NOFOLLOW | |||
|
216 | dfd = dirfd(dir); | |||
|
217 | #endif | |||
213 |
|
218 | |||
214 |
|
|
219 | if (do_stat || !all_kinds) { | |
215 | ssize_t size = PyList_Size(list); |
|
|||
216 | ssize_t i; |
|
|||
217 | #ifdef AT_SYMLINK_NOFOLLOW |
|
|||
218 | int dfd = dirfd(dir); |
|
|||
219 | #endif |
|
|||
220 |
|
||||
221 | for (i = 0; i < size; i++) { |
|
220 | for (i = 0; i < size; i++) { | |
222 |
|
|
221 | PyObject *elt = PyList_GetItem(list, i); | |
223 |
|
|
222 | char *name = PyString_AsString(PyTuple_GET_ITEM(elt, 0)); | |
@@ -235,16 +234,17 static PyObject *listdir(PyObject *self, | |||||
235 |
|
|
234 | if (do_stat) { | |
236 | struct listdir_stat *st; |
|
235 | struct listdir_stat *st; | |
237 |
|
236 | |||
238 |
if (ctor_args |
|
237 | if (!ctor_args) { | |
239 |
|
|
238 | ctor_args = PyTuple_New(0); | |
240 |
|
|
239 | if (!ctor_args) | |
241 | goto bail; |
|
240 | goto bail; | |
242 | } |
|
241 | } | |
243 |
|
242 | |||
244 | st = (struct listdir_stat *) |
|
243 | st = (struct listdir_stat *) | |
245 |
|
|
244 | PyObject_CallObject((PyObject *)&listdir_stat_type, | |
246 | ctor_args); |
|
245 | ctor_args); | |
247 | if (st == NULL) |
|
246 | ||
|
247 | if (!st) | |||
248 |
|
|
248 | goto bail; | |
249 | #ifdef AT_SYMLINK_NOFOLLOW |
|
249 | #ifdef AT_SYMLINK_NOFOLLOW | |
250 | ret = fstatat(dfd, name, &st->st, AT_SYMLINK_NOFOLLOW); |
|
250 | ret = fstatat(dfd, name, &st->st, AT_SYMLINK_NOFOLLOW); | |
@@ -277,14 +277,14 static PyObject *listdir(PyObject *self, | |||||
277 |
|
277 | |||
278 |
|
|
278 | if (py_kind == Py_None && kind != -1) { | |
279 | py_kind = PyInt_FromLong(kind); |
|
279 | py_kind = PyInt_FromLong(kind); | |
280 |
if (py_kind |
|
280 | if (!py_kind) | |
281 |
|
|
281 | goto bail; | |
282 | Py_XDECREF(Py_None); |
|
282 | Py_XDECREF(Py_None); | |
283 | PyTuple_SET_ITEM(elt, 1, py_kind); |
|
283 | PyTuple_SET_ITEM(elt, 1, py_kind); | |
284 | } |
|
284 | } | |
285 |
|
285 | |||
286 |
|
|
286 | if (do_stat) { | |
287 |
if (py_st |
|
287 | if (!py_st) { | |
288 |
|
|
288 | py_st = Py_None; | |
289 |
|
|
289 | Py_INCREF(Py_None); | |
290 | } |
|
290 | } | |
@@ -302,10 +302,10 done: | |||||
302 |
|
|
302 | Py_XDECREF(ctor_args); | |
303 |
|
|
303 | if (dir) | |
304 |
|
|
304 | closedir(dir); | |
305 |
|
||||
306 |
|
|
305 | return list; | |
307 | } |
|
306 | } | |
308 |
|
307 | |||
|
308 | ||||
309 | static char osutil_doc[] = "Native operating system services."; |
|
309 | static char osutil_doc[] = "Native operating system services."; | |
310 |
|
310 | |||
311 | static PyMethodDef methods[] = { |
|
311 | static PyMethodDef methods[] = { |
General Comments 0
You need to be logged in to leave comments.
Login now