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