Show More
@@ -109,6 +109,66 b' static inline int mode_to_kind(int mode)' | |||||
109 | return mode; |
|
109 | return mode; | |
110 | } |
|
110 | } | |
111 |
|
111 | |||
|
112 | static PyObject *listfiles(PyObject *list, DIR *dir, int stat, int *all) | |||
|
113 | { | |||
|
114 | struct dirent *ent; | |||
|
115 | PyObject *name, *py_kind, *val; | |||
|
116 | ||||
|
117 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |||
|
118 | int kind = -1; | |||
|
119 | ||||
|
120 | if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) | |||
|
121 | continue; | |||
|
122 | ||||
|
123 | #ifdef DT_REG | |||
|
124 | if (!stat) | |||
|
125 | switch (ent->d_type) { | |||
|
126 | case DT_REG: kind = S_IFREG; break; | |||
|
127 | case DT_DIR: kind = S_IFDIR; break; | |||
|
128 | case DT_LNK: kind = S_IFLNK; break; | |||
|
129 | case DT_BLK: kind = S_IFBLK; break; | |||
|
130 | case DT_CHR: kind = S_IFCHR; break; | |||
|
131 | case DT_FIFO: kind = S_IFIFO; break; | |||
|
132 | case DT_SOCK: kind = S_IFSOCK; break; | |||
|
133 | default: | |||
|
134 | *all = 0; | |||
|
135 | break; | |||
|
136 | } | |||
|
137 | #else | |||
|
138 | *all = 0; | |||
|
139 | #endif | |||
|
140 | ||||
|
141 | if (kind != -1) | |||
|
142 | py_kind = PyInt_FromLong(kind); | |||
|
143 | else { | |||
|
144 | py_kind = Py_None; | |||
|
145 | Py_INCREF(Py_None); | |||
|
146 | } | |||
|
147 | ||||
|
148 | val = PyTuple_New(stat ? 3 : 2); | |||
|
149 | name = PyString_FromString(ent->d_name); | |||
|
150 | ||||
|
151 | if (!name || !py_kind || !val) { | |||
|
152 | Py_XDECREF(name); | |||
|
153 | Py_XDECREF(py_kind); | |||
|
154 | Py_XDECREF(val); | |||
|
155 | return PyErr_NoMemory(); | |||
|
156 | } | |||
|
157 | ||||
|
158 | PyTuple_SET_ITEM(val, 0, name); | |||
|
159 | PyTuple_SET_ITEM(val, 1, py_kind); | |||
|
160 | if (stat) { | |||
|
161 | PyTuple_SET_ITEM(val, 2, Py_None); | |||
|
162 | Py_INCREF(Py_None); | |||
|
163 | } | |||
|
164 | ||||
|
165 | PyList_Append(list, val); | |||
|
166 | Py_DECREF(val); | |||
|
167 | } | |||
|
168 | ||||
|
169 | return 0; | |||
|
170 | } | |||
|
171 | ||||
112 | static PyObject *statfiles(PyObject *list, PyObject *ctor_args, int keep, |
|
172 | static PyObject *statfiles(PyObject *list, PyObject *ctor_args, int keep, | |
113 | char *path, int len, DIR *dir) |
|
173 | char *path, int len, DIR *dir) | |
114 | { |
|
174 | { | |
@@ -173,7 +233,6 b' static PyObject *listdir(PyObject *self,' | |||||
173 | static char *kwlist[] = { "path", "stat", NULL }; |
|
233 | static char *kwlist[] = { "path", "stat", NULL }; | |
174 | PyObject *statobj = NULL; |
|
234 | PyObject *statobj = NULL; | |
175 | DIR *dir = NULL; |
|
235 | DIR *dir = NULL; | |
176 | struct dirent *ent; |
|
|||
177 | PyObject *list = NULL; |
|
236 | PyObject *list = NULL; | |
178 | PyObject *err = NULL; |
|
237 | PyObject *err = NULL; | |
179 | PyObject *ctor_args = NULL; |
|
238 | PyObject *ctor_args = NULL; | |
@@ -202,60 +261,9 b' static PyObject *listdir(PyObject *self,' | |||||
202 | strncpy(full_path, path, PATH_MAX); |
|
261 | strncpy(full_path, path, PATH_MAX); | |
203 | full_path[path_len] = '/'; |
|
262 | full_path[path_len] = '/'; | |
204 |
|
263 | |||
205 | for (ent = readdir(dir); ent; ent = readdir(dir)) { |
|
264 | err = listfiles(list, dir, do_stat, &all_kinds); | |
206 | PyObject *name = NULL; |
|
265 | if (err) | |
207 | PyObject *py_kind = NULL; |
|
|||
208 | PyObject *val = NULL; |
|
|||
209 | int kind = -1; |
|
|||
210 |
|
||||
211 | if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) |
|
|||
212 | continue; |
|
|||
213 |
|
||||
214 | #ifdef DT_REG |
|
|||
215 | if (!do_stat) |
|
|||
216 | switch (ent->d_type) { |
|
|||
217 | case DT_REG: kind = S_IFREG; break; |
|
|||
218 | case DT_DIR: kind = S_IFDIR; break; |
|
|||
219 | case DT_LNK: kind = S_IFLNK; break; |
|
|||
220 | case DT_BLK: kind = S_IFBLK; break; |
|
|||
221 | case DT_CHR: kind = S_IFCHR; break; |
|
|||
222 | case DT_FIFO: kind = S_IFIFO; break; |
|
|||
223 | case DT_SOCK: kind = S_IFSOCK; break; |
|
|||
224 | default: |
|
|||
225 | all_kinds = 0; |
|
|||
226 | break; |
|
|||
227 | } |
|
|||
228 | #else |
|
|||
229 | all_kinds = 0; |
|
|||
230 | #endif |
|
|||
231 |
|
||||
232 | name = PyString_FromString(ent->d_name); |
|
|||
233 | if (kind != -1) |
|
|||
234 | py_kind = PyInt_FromLong(kind); |
|
|||
235 | else { |
|
|||
236 | py_kind = Py_None; |
|
|||
237 | Py_INCREF(Py_None); |
|
|||
238 | } |
|
|||
239 |
|
||||
240 | val = PyTuple_New(do_stat ? 3 : 2); |
|
|||
241 |
|
||||
242 | if (!name || !py_kind || !val) { |
|
|||
243 | Py_XDECREF(name); |
|
|||
244 | Py_XDECREF(py_kind); |
|
|||
245 | Py_XDECREF(val); |
|
|||
246 |
|
|
266 | goto bail; | |
247 | } |
|
|||
248 |
|
||||
249 | PyTuple_SET_ITEM(val, 0, name); |
|
|||
250 | PyTuple_SET_ITEM(val, 1, py_kind); |
|
|||
251 | if (do_stat) { |
|
|||
252 | PyTuple_SET_ITEM(val, 2, Py_None); |
|
|||
253 | Py_INCREF(Py_None); |
|
|||
254 | } |
|
|||
255 |
|
||||
256 | PyList_Append(list, val); |
|
|||
257 | Py_DECREF(val); |
|
|||
258 | } |
|
|||
259 |
|
267 | |||
260 | PyList_Sort(list); |
|
268 | PyList_Sort(list); | |
261 |
|
269 |
General Comments 0
You need to be logged in to leave comments.
Login now