221 for (i = 0; i < size; i++) { |
221 for (i = 0; i < size; i++) { |
222 PyObject *elt = PyList_GetItem(list, i); |
222 PyObject *elt = PyList_GetItem(list, i); |
223 char *name = PyString_AsString(PyTuple_GET_ITEM(elt, 0)); |
223 char *name = PyString_AsString(PyTuple_GET_ITEM(elt, 0)); |
224 PyObject *py_st = NULL; |
224 PyObject *py_st = NULL; |
225 PyObject *py_kind = PyTuple_GET_ITEM(elt, 1); |
225 PyObject *py_kind = PyTuple_GET_ITEM(elt, 1); |
|
226 struct listdir_stat *st; |
|
227 struct stat buf; |
|
228 struct stat *stp = &buf; |
226 int kind; |
229 int kind; |
227 |
230 |
228 kind = py_kind == Py_None ? -1 : PyInt_AsLong(py_kind); |
231 kind = py_kind == Py_None ? -1 : PyInt_AsLong(py_kind); |
229 |
232 |
230 if (kind != -1 && !do_stat) |
233 if (kind != -1 && !do_stat) |
232 |
235 |
233 strncat(full_path + path_len + 1, name, PATH_MAX - path_len); |
236 strncat(full_path + path_len + 1, name, PATH_MAX - path_len); |
234 full_path[PATH_MAX] = 0; |
237 full_path[PATH_MAX] = 0; |
235 |
238 |
236 if (do_stat) { |
239 if (do_stat) { |
237 struct listdir_stat *st; |
|
238 |
|
239 if (!ctor_args) { |
240 if (!ctor_args) { |
240 ctor_args = PyTuple_New(0); |
241 ctor_args = PyTuple_New(0); |
241 if (!ctor_args) |
242 if (!ctor_args) |
242 goto bail; |
243 goto bail; |
243 } |
244 } |
244 |
245 |
245 st = (struct listdir_stat *) |
246 py_st = PyObject_CallObject((PyObject *)&listdir_stat_type, |
246 PyObject_CallObject((PyObject *)&listdir_stat_type, |
|
247 ctor_args); |
247 ctor_args); |
248 |
248 st = (struct listdir_stat *)py_st; |
249 if (!st) |
249 if (!st) |
250 goto bail; |
250 goto bail; |
|
251 stp = &st->st; |
|
252 PyTuple_SET_ITEM(elt, 2, py_st); |
|
253 } |
|
254 |
251 #ifdef AT_SYMLINK_NOFOLLOW |
255 #ifdef AT_SYMLINK_NOFOLLOW |
252 ret = fstatat(dfd, name, &st->st, AT_SYMLINK_NOFOLLOW); |
256 ret = fstatat(dfd, name, stp, AT_SYMLINK_NOFOLLOW); |
253 #else |
257 #else |
254 ret = lstat(full_path, &st->st); |
258 ret = lstat(full_path, stp); |
255 #endif |
259 #endif |
256 if (ret == -1) { |
260 if (ret == -1) { |
257 list = PyErr_SetFromErrnoWithFilename(PyExc_OSError, |
261 list = PyErr_SetFromErrnoWithFilename(PyExc_OSError, |
258 full_path); |
262 full_path); |
259 goto bail; |
263 goto bail; |
260 } |
264 } |
261 if (kind == -1) |
265 if (kind == -1) |
262 kind = mode_to_kind(st->st.st_mode); |
266 kind = mode_to_kind(stp->st_mode); |
263 py_st = (PyObject *)st; |
|
264 } else { |
|
265 struct stat buf; |
|
266 #ifdef AT_SYMLINK_NOFOLLOW |
|
267 ret = fstatat(dfd, ent->d_name, &buf, AT_SYMLINK_NOFOLLOW); |
|
268 #else |
|
269 ret = lstat(full_path, &buf); |
|
270 #endif |
|
271 if (ret == -1) { |
|
272 list = PyErr_SetFromErrnoWithFilename(PyExc_OSError, |
|
273 full_path); |
|
274 goto bail; |
|
275 } |
|
276 if (kind == -1) |
|
277 kind = mode_to_kind(buf.st_mode); |
|
278 } |
|
279 |
267 |
280 if (py_kind == Py_None && kind != -1) { |
268 if (py_kind == Py_None && kind != -1) { |
281 py_kind = PyInt_FromLong(kind); |
269 py_kind = PyInt_FromLong(kind); |
282 if (!py_kind) |
270 if (!py_kind) |
283 goto bail; |
271 goto bail; |
284 Py_XDECREF(Py_None); |
272 Py_XDECREF(Py_None); |
285 PyTuple_SET_ITEM(elt, 1, py_kind); |
273 PyTuple_SET_ITEM(elt, 1, py_kind); |
286 } |
274 } |
287 |
|
288 if (do_stat) { |
|
289 if (!py_st) { |
|
290 py_st = Py_None; |
|
291 Py_INCREF(Py_None); |
|
292 } |
|
293 PyTuple_SET_ITEM(elt, 2, py_st); |
|
294 } |
|
295 } |
275 } |
296 |
276 |
297 goto done; |
277 goto done; |
298 |
278 |
299 bail: |
279 bail: |