##// END OF EJS Templates
osutil: move file list loop to its own function
Matt Mackall -
r5427:dae6188e default
parent child Browse files
Show More
@@ -1,299 +1,307 b''
1 /*
1 /*
2 osutil.c - native operating system services
2 osutil.c - native operating system services
3
3
4 Copyright 2007 Matt Mackall and others
4 Copyright 2007 Matt Mackall and others
5
5
6 This software may be used and distributed according to the terms of
6 This software may be used and distributed according to the terms of
7 the GNU General Public License, incorporated herein by reference.
7 the GNU General Public License, incorporated herein by reference.
8 */
8 */
9
9
10 #define _ATFILE_SOURCE
10 #define _ATFILE_SOURCE
11 #include <Python.h>
11 #include <Python.h>
12 #include <alloca.h>
12 #include <alloca.h>
13 #include <dirent.h>
13 #include <dirent.h>
14 #include <fcntl.h>
14 #include <fcntl.h>
15 #include <string.h>
15 #include <string.h>
16 #include <sys/stat.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
17 #include <sys/types.h>
18 #include <unistd.h>
18 #include <unistd.h>
19
19
20 struct listdir_stat {
20 struct listdir_stat {
21 PyObject_HEAD
21 PyObject_HEAD
22 struct stat st;
22 struct stat st;
23 };
23 };
24
24
25 #define listdir_slot(name) \
25 #define listdir_slot(name) \
26 static PyObject *listdir_stat_##name(PyObject *self, void *x) \
26 static PyObject *listdir_stat_##name(PyObject *self, void *x) \
27 { \
27 { \
28 return PyInt_FromLong(((struct listdir_stat *)self)->st.name); \
28 return PyInt_FromLong(((struct listdir_stat *)self)->st.name); \
29 }
29 }
30
30
31 listdir_slot(st_dev);
31 listdir_slot(st_dev);
32 listdir_slot(st_mode);
32 listdir_slot(st_mode);
33 listdir_slot(st_nlink);
33 listdir_slot(st_nlink);
34 listdir_slot(st_size);
34 listdir_slot(st_size);
35 listdir_slot(st_mtime);
35 listdir_slot(st_mtime);
36 listdir_slot(st_ctime);
36 listdir_slot(st_ctime);
37
37
38 static struct PyGetSetDef listdir_stat_getsets[] = {
38 static struct PyGetSetDef listdir_stat_getsets[] = {
39 {"st_dev", listdir_stat_st_dev, 0, 0, 0},
39 {"st_dev", listdir_stat_st_dev, 0, 0, 0},
40 {"st_mode", listdir_stat_st_mode, 0, 0, 0},
40 {"st_mode", listdir_stat_st_mode, 0, 0, 0},
41 {"st_nlink", listdir_stat_st_nlink, 0, 0, 0},
41 {"st_nlink", listdir_stat_st_nlink, 0, 0, 0},
42 {"st_size", listdir_stat_st_size, 0, 0, 0},
42 {"st_size", listdir_stat_st_size, 0, 0, 0},
43 {"st_mtime", listdir_stat_st_mtime, 0, 0, 0},
43 {"st_mtime", listdir_stat_st_mtime, 0, 0, 0},
44 {"st_ctime", listdir_stat_st_ctime, 0, 0, 0},
44 {"st_ctime", listdir_stat_st_ctime, 0, 0, 0},
45 {0, 0, 0, 0, 0}
45 {0, 0, 0, 0, 0}
46 };
46 };
47
47
48 static PyObject *listdir_stat_new(PyTypeObject *t, PyObject *a, PyObject *k)
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 static void listdir_stat_dealloc(PyObject *o)
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 static PyTypeObject listdir_stat_type = {
58 static PyTypeObject listdir_stat_type = {
59 PyObject_HEAD_INIT(NULL)
59 PyObject_HEAD_INIT(NULL)
60 0, /*ob_size*/
60 0, /*ob_size*/
61 "osutil.stat", /*tp_name*/
61 "osutil.stat", /*tp_name*/
62 sizeof(struct listdir_stat), /*tp_basicsize*/
62 sizeof(struct listdir_stat), /*tp_basicsize*/
63 0, /*tp_itemsize*/
63 0, /*tp_itemsize*/
64 (destructor)listdir_stat_dealloc, /*tp_dealloc*/
64 (destructor)listdir_stat_dealloc, /*tp_dealloc*/
65 0, /*tp_print*/
65 0, /*tp_print*/
66 0, /*tp_getattr*/
66 0, /*tp_getattr*/
67 0, /*tp_setattr*/
67 0, /*tp_setattr*/
68 0, /*tp_compare*/
68 0, /*tp_compare*/
69 0, /*tp_repr*/
69 0, /*tp_repr*/
70 0, /*tp_as_number*/
70 0, /*tp_as_number*/
71 0, /*tp_as_sequence*/
71 0, /*tp_as_sequence*/
72 0, /*tp_as_mapping*/
72 0, /*tp_as_mapping*/
73 0, /*tp_hash */
73 0, /*tp_hash */
74 0, /*tp_call*/
74 0, /*tp_call*/
75 0, /*tp_str*/
75 0, /*tp_str*/
76 0, /*tp_getattro*/
76 0, /*tp_getattro*/
77 0, /*tp_setattro*/
77 0, /*tp_setattro*/
78 0, /*tp_as_buffer*/
78 0, /*tp_as_buffer*/
79 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
79 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
80 "stat objects", /* tp_doc */
80 "stat objects", /* tp_doc */
81 0, /* tp_traverse */
81 0, /* tp_traverse */
82 0, /* tp_clear */
82 0, /* tp_clear */
83 0, /* tp_richcompare */
83 0, /* tp_richcompare */
84 0, /* tp_weaklistoffset */
84 0, /* tp_weaklistoffset */
85 0, /* tp_iter */
85 0, /* tp_iter */
86 0, /* tp_iternext */
86 0, /* tp_iternext */
87 0, /* tp_methods */
87 0, /* tp_methods */
88 0, /* tp_members */
88 0, /* tp_members */
89 listdir_stat_getsets, /* tp_getset */
89 listdir_stat_getsets, /* tp_getset */
90 0, /* tp_base */
90 0, /* tp_base */
91 0, /* tp_dict */
91 0, /* tp_dict */
92 0, /* tp_descr_get */
92 0, /* tp_descr_get */
93 0, /* tp_descr_set */
93 0, /* tp_descr_set */
94 0, /* tp_dictoffset */
94 0, /* tp_dictoffset */
95 0, /* tp_init */
95 0, /* tp_init */
96 0, /* tp_alloc */
96 0, /* tp_alloc */
97 listdir_stat_new, /* tp_new */
97 listdir_stat_new, /* tp_new */
98 };
98 };
99
99
100 static inline int mode_to_kind(int mode)
100 static inline int mode_to_kind(int mode)
101 {
101 {
102 if (S_ISREG(mode)) return S_IFREG;
102 if (S_ISREG(mode)) return S_IFREG;
103 if (S_ISDIR(mode)) return S_IFDIR;
103 if (S_ISDIR(mode)) return S_IFDIR;
104 if (S_ISLNK(mode)) return S_IFLNK;
104 if (S_ISLNK(mode)) return S_IFLNK;
105 if (S_ISBLK(mode)) return S_IFBLK;
105 if (S_ISBLK(mode)) return S_IFBLK;
106 if (S_ISCHR(mode)) return S_IFCHR;
106 if (S_ISCHR(mode)) return S_IFCHR;
107 if (S_ISFIFO(mode)) return S_IFIFO;
107 if (S_ISFIFO(mode)) return S_IFIFO;
108 if (S_ISSOCK(mode)) return S_IFSOCK;
108 if (S_ISSOCK(mode)) return S_IFSOCK;
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 {
115 struct stat buf;
175 struct stat buf;
116 struct stat *stp = &buf;
176 struct stat *stp = &buf;
117 int kind;
177 int kind;
118 int ret;
178 int ret;
119 ssize_t i;
179 ssize_t i;
120 ssize_t size = PyList_Size(list);
180 ssize_t size = PyList_Size(list);
121 #ifdef AT_SYMLINK_NOFOLLOW
181 #ifdef AT_SYMLINK_NOFOLLOW
122 int dfd = dirfd(dir);
182 int dfd = dirfd(dir);
123 #endif
183 #endif
124
184
125 for (i = 0; i < size; i++) {
185 for (i = 0; i < size; i++) {
126 PyObject *elt = PyList_GetItem(list, i);
186 PyObject *elt = PyList_GetItem(list, i);
127 char *name = PyString_AsString(PyTuple_GET_ITEM(elt, 0));
187 char *name = PyString_AsString(PyTuple_GET_ITEM(elt, 0));
128 PyObject *py_st = NULL;
188 PyObject *py_st = NULL;
129 PyObject *py_kind = PyTuple_GET_ITEM(elt, 1);
189 PyObject *py_kind = PyTuple_GET_ITEM(elt, 1);
130
190
131 kind = py_kind == Py_None ? -1 : PyInt_AsLong(py_kind);
191 kind = py_kind == Py_None ? -1 : PyInt_AsLong(py_kind);
132 if (kind != -1 && !keep)
192 if (kind != -1 && !keep)
133 continue;
193 continue;
134
194
135 strncat(path + len + 1, name, PATH_MAX - len);
195 strncat(path + len + 1, name, PATH_MAX - len);
136 path[PATH_MAX] = 0;
196 path[PATH_MAX] = 0;
137
197
138 if (keep) {
198 if (keep) {
139 py_st = PyObject_CallObject(
199 py_st = PyObject_CallObject(
140 (PyObject *)&listdir_stat_type, ctor_args);
200 (PyObject *)&listdir_stat_type, ctor_args);
141 if (!py_st)
201 if (!py_st)
142 return PyErr_NoMemory();
202 return PyErr_NoMemory();
143 stp = &((struct listdir_stat *)py_st)->st;
203 stp = &((struct listdir_stat *)py_st)->st;
144 PyTuple_SET_ITEM(elt, 2, py_st);
204 PyTuple_SET_ITEM(elt, 2, py_st);
145 }
205 }
146
206
147 #ifdef AT_SYMLINK_NOFOLLOW
207 #ifdef AT_SYMLINK_NOFOLLOW
148 ret = fstatat(dfd, name, stp, AT_SYMLINK_NOFOLLOW);
208 ret = fstatat(dfd, name, stp, AT_SYMLINK_NOFOLLOW);
149 #else
209 #else
150 ret = lstat(path, stp);
210 ret = lstat(path, stp);
151 #endif
211 #endif
152 if (ret == -1)
212 if (ret == -1)
153 return PyErr_SetFromErrnoWithFilename(PyExc_OSError,
213 return PyErr_SetFromErrnoWithFilename(PyExc_OSError,
154 path);
214 path);
155
215
156 if (kind == -1)
216 if (kind == -1)
157 kind = mode_to_kind(stp->st_mode);
217 kind = mode_to_kind(stp->st_mode);
158
218
159 if (py_kind == Py_None && kind != -1) {
219 if (py_kind == Py_None && kind != -1) {
160 py_kind = PyInt_FromLong(kind);
220 py_kind = PyInt_FromLong(kind);
161 if (!py_kind)
221 if (!py_kind)
162 return PyErr_NoMemory();
222 return PyErr_NoMemory();
163 Py_XDECREF(Py_None);
223 Py_XDECREF(Py_None);
164 PyTuple_SET_ITEM(elt, 1, py_kind);
224 PyTuple_SET_ITEM(elt, 1, py_kind);
165 }
225 }
166 }
226 }
167
227
168 return 0;
228 return 0;
169 }
229 }
170
230
171 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs)
231 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs)
172 {
232 {
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;
180 int all_kinds = 1;
239 int all_kinds = 1;
181 char full_path[PATH_MAX + 10];
240 char full_path[PATH_MAX + 10];
182 int path_len;
241 int path_len;
183 int do_stat;
242 int do_stat;
184 char *path;
243 char *path;
185
244
186 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:listdir", kwlist,
245 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:listdir", kwlist,
187 &path, &path_len, &statobj))
246 &path, &path_len, &statobj))
188 goto bail;
247 goto bail;
189
248
190 do_stat = statobj && PyObject_IsTrue(statobj);
249 do_stat = statobj && PyObject_IsTrue(statobj);
191
250
192 dir = opendir(path);
251 dir = opendir(path);
193 if (!dir) {
252 if (!dir) {
194 list = PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
253 list = PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
195 goto bail;
254 goto bail;
196 }
255 }
197
256
198 list = PyList_New(0);
257 list = PyList_New(0);
199 if (!list)
258 if (!list)
200 goto bail;
259 goto bail;
201
260
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 goto bail;
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
262 if (do_stat) {
270 if (do_stat) {
263 ctor_args = PyTuple_New(0);
271 ctor_args = PyTuple_New(0);
264 if (!ctor_args)
272 if (!ctor_args)
265 goto bail;
273 goto bail;
266 }
274 }
267
275
268 if (do_stat || !all_kinds)
276 if (do_stat || !all_kinds)
269 if (statfiles(list, ctor_args, do_stat, full_path, path_len,
277 if (statfiles(list, ctor_args, do_stat, full_path, path_len,
270 dir))
278 dir))
271 goto bail;
279 goto bail;
272 goto done;
280 goto done;
273
281
274 bail:
282 bail:
275 Py_XDECREF(list);
283 Py_XDECREF(list);
276
284
277 done:
285 done:
278 Py_XDECREF(ctor_args);
286 Py_XDECREF(ctor_args);
279 if (dir)
287 if (dir)
280 closedir(dir);
288 closedir(dir);
281 return err ? err : list;
289 return err ? err : list;
282 }
290 }
283
291
284
292
285 static char osutil_doc[] = "Native operating system services.";
293 static char osutil_doc[] = "Native operating system services.";
286
294
287 static PyMethodDef methods[] = {
295 static PyMethodDef methods[] = {
288 {"listdir", (PyCFunction)listdir, METH_VARARGS | METH_KEYWORDS,
296 {"listdir", (PyCFunction)listdir, METH_VARARGS | METH_KEYWORDS,
289 "list a directory\n"},
297 "list a directory\n"},
290 {NULL, NULL}
298 {NULL, NULL}
291 };
299 };
292
300
293 PyMODINIT_FUNC initosutil(void)
301 PyMODINIT_FUNC initosutil(void)
294 {
302 {
295 if (PyType_Ready(&listdir_stat_type) == -1)
303 if (PyType_Ready(&listdir_stat_type) == -1)
296 return;
304 return;
297
305
298 Py_InitModule3("osutil", methods, osutil_doc);
306 Py_InitModule3("osutil", methods, osutil_doc);
299 }
307 }
General Comments 0
You need to be logged in to leave comments. Login now