##// END OF EJS Templates
osutil: more tidying...
Matt Mackall -
r5428:eb1b6aae default
parent child Browse files
Show More
@@ -109,11 +109,18 b' static inline int mode_to_kind(int mode)'
109 109 return mode;
110 110 }
111 111
112 static PyObject *listfiles(PyObject *list, DIR *dir, int stat, int *all)
112 static PyObject *listfiles(PyObject *list, DIR *dir,
113 int keep_stat, int *need_stat)
113 114 {
114 115 struct dirent *ent;
115 116 PyObject *name, *py_kind, *val;
116 117
118 #ifdef DT_REG
119 *need_stat = 0;
120 #else
121 *need_stat = 1;
122 #endif
123
117 124 for (ent = readdir(dir); ent; ent = readdir(dir)) {
118 125 int kind = -1;
119 126
@@ -121,7 +128,7 b' static PyObject *listfiles(PyObject *lis'
121 128 continue;
122 129
123 130 #ifdef DT_REG
124 if (!stat)
131 if (!keep_stat)
125 132 switch (ent->d_type) {
126 133 case DT_REG: kind = S_IFREG; break;
127 134 case DT_DIR: kind = S_IFDIR; break;
@@ -131,11 +138,9 b' static PyObject *listfiles(PyObject *lis'
131 138 case DT_FIFO: kind = S_IFIFO; break;
132 139 case DT_SOCK: kind = S_IFSOCK; break;
133 140 default:
134 *all = 0;
141 *need_stat = 0;
135 142 break;
136 143 }
137 #else
138 *all = 0;
139 144 #endif
140 145
141 146 if (kind != -1)
@@ -145,7 +150,7 b' static PyObject *listfiles(PyObject *lis'
145 150 Py_INCREF(Py_None);
146 151 }
147 152
148 val = PyTuple_New(stat ? 3 : 2);
153 val = PyTuple_New(keep_stat ? 3 : 2);
149 154 name = PyString_FromString(ent->d_name);
150 155
151 156 if (!name || !py_kind || !val) {
@@ -157,7 +162,7 b' static PyObject *listfiles(PyObject *lis'
157 162
158 163 PyTuple_SET_ITEM(val, 0, name);
159 164 PyTuple_SET_ITEM(val, 1, py_kind);
160 if (stat) {
165 if (keep_stat) {
161 166 PyTuple_SET_ITEM(val, 2, Py_None);
162 167 Py_INCREF(Py_None);
163 168 }
@@ -231,53 +236,48 b' static PyObject *statfiles(PyObject *lis'
231 236 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs)
232 237 {
233 238 static char *kwlist[] = { "path", "stat", NULL };
239 DIR *dir = NULL;
234 240 PyObject *statobj = NULL;
235 DIR *dir = NULL;
236 241 PyObject *list = NULL;
237 242 PyObject *err = NULL;
238 243 PyObject *ctor_args = NULL;
239 int all_kinds = 1;
244 char *path;
240 245 char full_path[PATH_MAX + 10];
241 246 int path_len;
242 int do_stat;
243 char *path;
247 int need_stat, keep_stat;
244 248
245 249 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:listdir", kwlist,
246 250 &path, &path_len, &statobj))
247 251 goto bail;
248 252
249 do_stat = statobj && PyObject_IsTrue(statobj);
253 keep_stat = statobj && PyObject_IsTrue(statobj);
250 254
251 255 dir = opendir(path);
252 256 if (!dir) {
253 list = PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
257 err = PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
254 258 goto bail;
255 259 }
256 260
257 261 list = PyList_New(0);
258 if (!list)
262 ctor_args = PyTuple_New(0);
263 if (!list || !ctor_args)
259 264 goto bail;
260 265
261 266 strncpy(full_path, path, PATH_MAX);
262 267 full_path[path_len] = '/';
263 268
264 err = listfiles(list, dir, do_stat, &all_kinds);
269 err = listfiles(list, dir, keep_stat, &need_stat);
265 270 if (err)
266 271 goto bail;
267 272
268 273 PyList_Sort(list);
269 274
270 if (do_stat) {
271 ctor_args = PyTuple_New(0);
272 if (!ctor_args)
273 goto bail;
274 }
275 if (!keep_stat && !need_stat)
276 goto done;
275 277
276 if (do_stat || !all_kinds)
277 if (statfiles(list, ctor_args, do_stat, full_path, path_len,
278 dir))
279 goto bail;
280 goto done;
278 err = statfiles(list, ctor_args, keep_stat, full_path, path_len, dir);
279 if (!err)
280 goto done;
281 281
282 282 bail:
283 283 Py_XDECREF(list);
General Comments 0
You need to be logged in to leave comments. Login now