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