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