##// END OF EJS Templates
osutil: more tidying...
Matt Mackall -
r5428:eb1b6aae default
parent child Browse files
Show More
@@ -1,307 +1,307
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 static PyObject *listfiles(PyObject *list, DIR *dir, int stat, int *all)
112 static PyObject *listfiles(PyObject *list, DIR *dir,
113 int keep_stat, int *need_stat)
113 114 {
114 115 struct dirent *ent;
115 116 PyObject *name, *py_kind, *val;
116 117
118 #ifdef DT_REG
119 *need_stat = 0;
120 #else
121 *need_stat = 1;
122 #endif
123
117 124 for (ent = readdir(dir); ent; ent = readdir(dir)) {
118 125 int kind = -1;
119 126
120 127 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
121 128 continue;
122 129
123 130 #ifdef DT_REG
124 if (!stat)
131 if (!keep_stat)
125 132 switch (ent->d_type) {
126 133 case DT_REG: kind = S_IFREG; break;
127 134 case DT_DIR: kind = S_IFDIR; break;
128 135 case DT_LNK: kind = S_IFLNK; break;
129 136 case DT_BLK: kind = S_IFBLK; break;
130 137 case DT_CHR: kind = S_IFCHR; break;
131 138 case DT_FIFO: kind = S_IFIFO; break;
132 139 case DT_SOCK: kind = S_IFSOCK; break;
133 140 default:
134 *all = 0;
141 *need_stat = 0;
135 142 break;
136 143 }
137 #else
138 *all = 0;
139 144 #endif
140 145
141 146 if (kind != -1)
142 147 py_kind = PyInt_FromLong(kind);
143 148 else {
144 149 py_kind = Py_None;
145 150 Py_INCREF(Py_None);
146 151 }
147 152
148 val = PyTuple_New(stat ? 3 : 2);
153 val = PyTuple_New(keep_stat ? 3 : 2);
149 154 name = PyString_FromString(ent->d_name);
150 155
151 156 if (!name || !py_kind || !val) {
152 157 Py_XDECREF(name);
153 158 Py_XDECREF(py_kind);
154 159 Py_XDECREF(val);
155 160 return PyErr_NoMemory();
156 161 }
157 162
158 163 PyTuple_SET_ITEM(val, 0, name);
159 164 PyTuple_SET_ITEM(val, 1, py_kind);
160 if (stat) {
165 if (keep_stat) {
161 166 PyTuple_SET_ITEM(val, 2, Py_None);
162 167 Py_INCREF(Py_None);
163 168 }
164 169
165 170 PyList_Append(list, val);
166 171 Py_DECREF(val);
167 172 }
168 173
169 174 return 0;
170 175 }
171 176
172 177 static PyObject *statfiles(PyObject *list, PyObject *ctor_args, int keep,
173 178 char *path, int len, DIR *dir)
174 179 {
175 180 struct stat buf;
176 181 struct stat *stp = &buf;
177 182 int kind;
178 183 int ret;
179 184 ssize_t i;
180 185 ssize_t size = PyList_Size(list);
181 186 #ifdef AT_SYMLINK_NOFOLLOW
182 187 int dfd = dirfd(dir);
183 188 #endif
184 189
185 190 for (i = 0; i < size; i++) {
186 191 PyObject *elt = PyList_GetItem(list, i);
187 192 char *name = PyString_AsString(PyTuple_GET_ITEM(elt, 0));
188 193 PyObject *py_st = NULL;
189 194 PyObject *py_kind = PyTuple_GET_ITEM(elt, 1);
190 195
191 196 kind = py_kind == Py_None ? -1 : PyInt_AsLong(py_kind);
192 197 if (kind != -1 && !keep)
193 198 continue;
194 199
195 200 strncat(path + len + 1, name, PATH_MAX - len);
196 201 path[PATH_MAX] = 0;
197 202
198 203 if (keep) {
199 204 py_st = PyObject_CallObject(
200 205 (PyObject *)&listdir_stat_type, ctor_args);
201 206 if (!py_st)
202 207 return PyErr_NoMemory();
203 208 stp = &((struct listdir_stat *)py_st)->st;
204 209 PyTuple_SET_ITEM(elt, 2, py_st);
205 210 }
206 211
207 212 #ifdef AT_SYMLINK_NOFOLLOW
208 213 ret = fstatat(dfd, name, stp, AT_SYMLINK_NOFOLLOW);
209 214 #else
210 215 ret = lstat(path, stp);
211 216 #endif
212 217 if (ret == -1)
213 218 return PyErr_SetFromErrnoWithFilename(PyExc_OSError,
214 219 path);
215 220
216 221 if (kind == -1)
217 222 kind = mode_to_kind(stp->st_mode);
218 223
219 224 if (py_kind == Py_None && kind != -1) {
220 225 py_kind = PyInt_FromLong(kind);
221 226 if (!py_kind)
222 227 return PyErr_NoMemory();
223 228 Py_XDECREF(Py_None);
224 229 PyTuple_SET_ITEM(elt, 1, py_kind);
225 230 }
226 231 }
227 232
228 233 return 0;
229 234 }
230 235
231 236 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs)
232 237 {
233 238 static char *kwlist[] = { "path", "stat", NULL };
239 DIR *dir = NULL;
234 240 PyObject *statobj = NULL;
235 DIR *dir = NULL;
236 241 PyObject *list = NULL;
237 242 PyObject *err = NULL;
238 243 PyObject *ctor_args = NULL;
239 int all_kinds = 1;
244 char *path;
240 245 char full_path[PATH_MAX + 10];
241 246 int path_len;
242 int do_stat;
243 char *path;
247 int need_stat, keep_stat;
244 248
245 249 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:listdir", kwlist,
246 250 &path, &path_len, &statobj))
247 251 goto bail;
248 252
249 do_stat = statobj && PyObject_IsTrue(statobj);
253 keep_stat = statobj && PyObject_IsTrue(statobj);
250 254
251 255 dir = opendir(path);
252 256 if (!dir) {
253 list = PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
257 err = PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
254 258 goto bail;
255 259 }
256 260
257 261 list = PyList_New(0);
258 if (!list)
262 ctor_args = PyTuple_New(0);
263 if (!list || !ctor_args)
259 264 goto bail;
260 265
261 266 strncpy(full_path, path, PATH_MAX);
262 267 full_path[path_len] = '/';
263 268
264 err = listfiles(list, dir, do_stat, &all_kinds);
269 err = listfiles(list, dir, keep_stat, &need_stat);
265 270 if (err)
266 271 goto bail;
267 272
268 273 PyList_Sort(list);
269 274
270 if (do_stat) {
271 ctor_args = PyTuple_New(0);
272 if (!ctor_args)
273 goto bail;
274 }
275 if (!keep_stat && !need_stat)
276 goto done;
275 277
276 if (do_stat || !all_kinds)
277 if (statfiles(list, ctor_args, do_stat, full_path, path_len,
278 dir))
279 goto bail;
278 err = statfiles(list, ctor_args, keep_stat, full_path, path_len, dir);
279 if (!err)
280 280 goto done;
281 281
282 282 bail:
283 283 Py_XDECREF(list);
284 284
285 285 done:
286 286 Py_XDECREF(ctor_args);
287 287 if (dir)
288 288 closedir(dir);
289 289 return err ? err : list;
290 290 }
291 291
292 292
293 293 static char osutil_doc[] = "Native operating system services.";
294 294
295 295 static PyMethodDef methods[] = {
296 296 {"listdir", (PyCFunction)listdir, METH_VARARGS | METH_KEYWORDS,
297 297 "list a directory\n"},
298 298 {NULL, NULL}
299 299 };
300 300
301 301 PyMODINIT_FUNC initosutil(void)
302 302 {
303 303 if (PyType_Ready(&listdir_stat_type) == -1)
304 304 return;
305 305
306 306 Py_InitModule3("osutil", methods, osutil_doc);
307 307 }
General Comments 0
You need to be logged in to leave comments. Login now