##// END OF EJS Templates
osutil: cleanups...
Matt Mackall -
r5421:9b5d626b default
parent child Browse files
Show More
@@ -47,12 +47,12 static struct PyGetSetDef listdir_stat_g
47 47
48 48 static PyObject *listdir_stat_new(PyTypeObject *t, PyObject *a, PyObject *k)
49 49 {
50 return (*t->tp_alloc)(t, 0);
50 return t->tp_alloc(t, 0);
51 51 }
52 52
53 53 static void listdir_stat_dealloc(PyObject *o)
54 54 {
55 (*o->ob_type->tp_free)(o);
55 o->ob_type->tp_free(o);
56 56 }
57 57
58 58 static PyTypeObject listdir_stat_type = {
@@ -111,6 +111,8 static inline int mode_to_kind(int mode)
111 111
112 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
125 {
126 static char *kwlist[] = { "path", "stat", NULL };
127 PyObject *statobj = NULL;
126 ssize_t size;
127 ssize_t i;
128 int dfd;
128 129
129 130 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:listdir", kwlist,
130 131 &path, &path_len, &statobj))
131 132 goto bail;
132 133
133 134 do_stat = statobj && PyObject_IsTrue(statobj);
134 }
135 135
136 if ((dir = opendir(path)) == NULL) {
136 dir = opendir(path);
137 if (!dir) {
137 138 list = PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
138 139 goto bail;
139 140 }
140 141
141 if ((list = PyList_New(0)) == NULL)
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 if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
157 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
156 158 continue;
157 159
158 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 if (name == NULL || py_kind == NULL || val == NULL) {
195 if (!name || !py_kind || !val) {
194 196 Py_XDECREF(name);
195 197 Py_XDECREF(py_kind);
196 198 Py_XDECREF(val);
197
198 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 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 235 struct listdir_stat *st;
237 236
238 if (ctor_args == NULL) {
237 if (!ctor_args) {
239 238 ctor_args = PyTuple_New(0);
240 if (ctor_args == NULL)
239 if (!ctor_args)
241 240 goto bail;
242 241 }
243 242
244 243 st = (struct listdir_stat *)
245 244 PyObject_CallObject((PyObject *) &listdir_stat_type,
246 245 ctor_args);
247 if (st == NULL)
246
247 if (!st)
248 248 goto bail;
249 249 #ifdef AT_SYMLINK_NOFOLLOW
250 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 279 py_kind = PyInt_FromLong(kind);
280 if (py_kind == NULL)
280 if (!py_kind)
281 281 goto bail;
282 282 Py_XDECREF(Py_None);
283 283 PyTuple_SET_ITEM(elt, 1, py_kind);
284 284 }
285 285
286 286 if (do_stat) {
287 if (py_st == NULL) {
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 309 static char osutil_doc[] = "Native operating system services.";
310 310
311 311 static PyMethodDef methods[] = {
General Comments 0
You need to be logged in to leave comments. Login now