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