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