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