Show More
@@ -1,323 +1,323 b'' | |||
|
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 |
|
|
22 | 22 |
|
|
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 *) |
|
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 |
|
|
40 | 40 |
|
|
41 | 41 |
|
|
42 | 42 |
|
|
43 | 43 |
|
|
44 | 44 |
|
|
45 | 45 |
|
|
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 |
|
|
60 | 60 |
|
|
61 | 61 |
|
|
62 | 62 |
|
|
63 | 63 |
|
|
64 | 64 |
|
|
65 | 65 |
|
|
66 | 66 |
|
|
67 | 67 |
|
|
68 | 68 |
|
|
69 | 69 |
|
|
70 | 70 |
|
|
71 | 71 |
|
|
72 | 72 |
|
|
73 | 73 |
|
|
74 | 74 |
|
|
75 | 75 |
|
|
76 | 76 |
|
|
77 | 77 |
|
|
78 | 78 |
|
|
79 | 79 |
|
|
80 | 80 |
|
|
81 | 81 |
|
|
82 | 82 |
|
|
83 | 83 |
|
|
84 | 84 |
|
|
85 | 85 |
|
|
86 | 86 |
|
|
87 | 87 |
|
|
88 | 88 |
|
|
89 | 89 |
|
|
90 | 90 |
|
|
91 | 91 |
|
|
92 | 92 |
|
|
93 | 93 |
|
|
94 | 94 |
|
|
95 | 95 |
|
|
96 | 96 |
|
|
97 | 97 |
|
|
98 | 98 | }; |
|
99 | 99 | |
|
100 | 100 | static inline int mode_to_kind(int mode) |
|
101 | 101 | { |
|
102 | 102 |
|
|
103 | 103 |
|
|
104 | 104 |
|
|
105 | 105 |
|
|
106 | 106 |
|
|
107 | 107 |
|
|
108 | 108 |
|
|
109 | 109 |
|
|
110 | 110 | } |
|
111 | 111 | |
|
112 | 112 | static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs) |
|
113 | 113 | { |
|
114 | static char *kwlist[] = { "path", "stat", NULL }; | |
|
115 | PyObject *statobj = NULL; | |
|
114 | 116 |
|
|
115 | 117 |
|
|
116 | 118 |
|
|
117 | 119 |
|
|
118 | 120 |
|
|
119 | 121 |
|
|
120 | 122 |
|
|
121 | 123 |
|
|
122 | 124 |
|
|
123 | 125 |
|
|
124 | ||
|
125 | { | |
|
126 | static char *kwlist[] = { "path", "stat", NULL }; | |
|
127 | PyObject *statobj = NULL; | |
|
126 | ssize_t size; | |
|
127 | ssize_t i; | |
|
128 | int dfd; | |
|
128 | 129 | |
|
129 | 130 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:listdir", kwlist, |
|
130 | 131 | &path, &path_len, &statobj)) |
|
131 | 132 |
|
|
132 | 133 | |
|
133 | 134 | do_stat = statobj && PyObject_IsTrue(statobj); |
|
134 | } | |
|
135 | 135 | |
|
136 |
|
|
|
136 | dir = opendir(path); | |
|
137 | if (!dir) { | |
|
137 | 138 | list = PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
|
138 | 139 |
|
|
139 | 140 | } |
|
140 | 141 | |
|
141 |
|
|
|
142 | list = PyList_New(0); | |
|
143 | if (!list) | |
|
142 | 144 |
|
|
143 | 145 | |
|
144 | 146 |
|
|
145 | 147 |
|
|
146 | 148 |
|
|
147 | 149 | |
|
148 | while ((ent = readdir(dir))) { | |
|
150 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
|
149 | 151 |
|
|
150 | 152 |
|
|
151 | 153 |
|
|
152 | 154 |
|
|
153 | 155 |
|
|
154 | 156 | |
|
155 |
|
|
|
157 | if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) | |
|
156 | 158 | continue; |
|
157 | 159 | |
|
158 | 160 | #ifdef DT_REG |
|
159 | 161 | if (do_stat) |
|
160 | 162 |
|
|
161 | 163 | else |
|
162 | 164 |
|
|
163 | 165 | #else |
|
164 | 166 |
|
|
165 | 167 | #endif |
|
166 | 168 | |
|
167 | 169 |
|
|
168 | 170 | #ifdef DT_REG |
|
169 | 171 |
|
|
170 | 172 |
|
|
171 | 173 |
|
|
172 | 174 |
|
|
173 | 175 |
|
|
174 | 176 |
|
|
175 | 177 |
|
|
176 | 178 | #endif |
|
177 | 179 | default: |
|
178 | 180 |
|
|
179 | 181 | all_kinds = 0; |
|
180 | 182 |
|
|
181 | 183 | } |
|
182 | 184 | |
|
183 | 185 |
|
|
184 | 186 | if (kind != -1) |
|
185 | 187 |
|
|
186 | 188 | else { |
|
187 | 189 |
|
|
188 | 190 |
|
|
189 | 191 | } |
|
190 | 192 | |
|
191 | 193 |
|
|
192 | 194 | |
|
193 |
|
|
|
195 | if (!name || !py_kind || !val) { | |
|
194 | 196 |
|
|
195 | 197 |
|
|
196 | 198 |
|
|
197 | ||
|
198 | 199 | goto bail; |
|
199 | 200 | } |
|
200 | 201 | |
|
201 | 202 |
|
|
202 | 203 |
|
|
203 | 204 | if (do_stat) { |
|
204 | 205 |
|
|
205 | 206 |
|
|
206 | 207 | } |
|
207 | 208 | |
|
208 | 209 |
|
|
209 | 210 |
|
|
210 | 211 | } |
|
211 | 212 | |
|
212 | 213 |
|
|
214 | size = PyList_Size(list); | |
|
215 | #ifdef AT_SYMLINK_NOFOLLOW | |
|
216 | dfd = dirfd(dir); | |
|
217 | #endif | |
|
213 | 218 | |
|
214 | 219 |
|
|
215 | ssize_t size = PyList_Size(list); | |
|
216 | ssize_t i; | |
|
217 | #ifdef AT_SYMLINK_NOFOLLOW | |
|
218 | int dfd = dirfd(dir); | |
|
219 | #endif | |
|
220 | ||
|
221 | 220 | for (i = 0; i < size; i++) { |
|
222 | 221 |
|
|
223 | 222 |
|
|
224 | 223 |
|
|
225 | 224 |
|
|
226 | 225 |
|
|
227 | 226 | |
|
228 | 227 |
|
|
229 | 228 | |
|
230 | 229 |
|
|
231 | 230 | continue; |
|
232 | 231 | |
|
233 | 232 |
|
|
234 | 233 | |
|
235 | 234 |
|
|
236 | 235 | struct listdir_stat *st; |
|
237 | 236 | |
|
238 |
if (ctor_args |
|
|
237 | if (!ctor_args) { | |
|
239 | 238 |
|
|
240 |
|
|
|
239 | if (!ctor_args) | |
|
241 | 240 | goto bail; |
|
242 | 241 | } |
|
243 | 242 | |
|
244 | 243 | st = (struct listdir_stat *) |
|
245 | 244 |
|
|
246 | 245 | ctor_args); |
|
247 | if (st == NULL) | |
|
246 | ||
|
247 | if (!st) | |
|
248 | 248 |
|
|
249 | 249 | #ifdef AT_SYMLINK_NOFOLLOW |
|
250 | 250 | ret = fstatat(dfd, name, &st->st, AT_SYMLINK_NOFOLLOW); |
|
251 | 251 | #else |
|
252 | 252 | ret = lstat(full_path, &st->st); |
|
253 | 253 | #endif |
|
254 | 254 | if (ret == -1) { |
|
255 | 255 |
|
|
256 | 256 | full_path); |
|
257 | 257 |
|
|
258 | 258 | } |
|
259 | 259 | if (kind == -1) |
|
260 | 260 |
|
|
261 | 261 |
py_st = (PyObject *) |
|
262 | 262 |
|
|
263 | 263 | struct stat buf; |
|
264 | 264 | #ifdef AT_SYMLINK_NOFOLLOW |
|
265 | 265 | ret = fstatat(dfd, ent->d_name, &buf, AT_SYMLINK_NOFOLLOW); |
|
266 | 266 | #else |
|
267 | 267 | ret = lstat(full_path, &buf); |
|
268 | 268 | #endif |
|
269 | 269 | if (ret == -1) { |
|
270 | 270 |
|
|
271 | 271 | full_path); |
|
272 | 272 |
|
|
273 | 273 | } |
|
274 | 274 | if (kind == -1) |
|
275 | 275 |
|
|
276 | 276 | } |
|
277 | 277 | |
|
278 | 278 |
|
|
279 | 279 | py_kind = PyInt_FromLong(kind); |
|
280 |
if (py_kind |
|
|
280 | if (!py_kind) | |
|
281 | 281 |
|
|
282 | 282 | Py_XDECREF(Py_None); |
|
283 | 283 | PyTuple_SET_ITEM(elt, 1, py_kind); |
|
284 | 284 | } |
|
285 | 285 | |
|
286 | 286 |
|
|
287 |
if (py_st |
|
|
287 | if (!py_st) { | |
|
288 | 288 |
|
|
289 | 289 |
|
|
290 | 290 | } |
|
291 | 291 | PyTuple_SET_ITEM(elt, 2, py_st); |
|
292 | 292 | } |
|
293 | 293 | } |
|
294 | 294 | } |
|
295 | 295 | |
|
296 | 296 |
|
|
297 | 297 | |
|
298 | 298 | bail: |
|
299 | 299 |
|
|
300 | 300 | |
|
301 | 301 | done: |
|
302 | 302 |
|
|
303 | 303 |
|
|
304 | 304 |
|
|
305 | ||
|
306 | 305 |
|
|
307 | 306 | } |
|
308 | 307 | |
|
308 | ||
|
309 | 309 | static char osutil_doc[] = "Native operating system services."; |
|
310 | 310 | |
|
311 | 311 | static PyMethodDef methods[] = { |
|
312 | 312 |
|
|
313 | 313 |
|
|
314 | 314 |
|
|
315 | 315 | }; |
|
316 | 316 | |
|
317 | 317 | PyMODINIT_FUNC initosutil(void) |
|
318 | 318 | { |
|
319 | 319 |
|
|
320 | 320 | return; |
|
321 | 321 | |
|
322 | 322 |
|
|
323 | 323 | } |
General Comments 0
You need to be logged in to leave comments.
Login now