Show More
@@ -1,1422 +1,1419 | |||
|
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 | #define PY_SSIZE_T_CLEAN |
|
12 | 12 | #include <Python.h> |
|
13 | 13 | #include <errno.h> |
|
14 | 14 | #include <fcntl.h> |
|
15 | 15 | #include <stdio.h> |
|
16 | 16 | #include <stdlib.h> |
|
17 | 17 | #include <string.h> |
|
18 | 18 | |
|
19 | 19 | #ifdef _WIN32 |
|
20 | 20 | #include <io.h> |
|
21 | 21 | #include <windows.h> |
|
22 | 22 | #else |
|
23 | 23 | #include <dirent.h> |
|
24 | 24 | #include <signal.h> |
|
25 | 25 | #include <sys/socket.h> |
|
26 | 26 | #include <sys/stat.h> |
|
27 | 27 | #include <sys/types.h> |
|
28 | 28 | #include <unistd.h> |
|
29 | 29 | #ifdef HAVE_LINUX_STATFS |
|
30 | 30 | #include <linux/magic.h> |
|
31 | 31 | #include <sys/vfs.h> |
|
32 | 32 | #endif |
|
33 | 33 | #ifdef HAVE_BSD_STATFS |
|
34 | 34 | #include <sys/mount.h> |
|
35 | 35 | #include <sys/param.h> |
|
36 | 36 | #endif |
|
37 | 37 | #endif |
|
38 | 38 | |
|
39 | 39 | #ifdef __APPLE__ |
|
40 | 40 | #include <sys/attr.h> |
|
41 | 41 | #include <sys/vnode.h> |
|
42 | 42 | #endif |
|
43 | 43 | |
|
44 | 44 | #include "util.h" |
|
45 | 45 | |
|
46 | 46 | /* some platforms lack the PATH_MAX definition (eg. GNU/Hurd) */ |
|
47 | 47 | #ifndef PATH_MAX |
|
48 | 48 | #define PATH_MAX 4096 |
|
49 | 49 | #endif |
|
50 | 50 | |
|
51 | 51 | #ifdef _WIN32 |
|
52 | 52 | /* |
|
53 | 53 | stat struct compatible with hg expectations |
|
54 | 54 | Mercurial only uses st_mode, st_size and st_mtime |
|
55 | 55 | the rest is kept to minimize changes between implementations |
|
56 | 56 | */ |
|
57 | 57 | struct hg_stat { |
|
58 | 58 | int st_dev; |
|
59 | 59 | int st_mode; |
|
60 | 60 | int st_nlink; |
|
61 | 61 | __int64 st_size; |
|
62 | 62 | int st_mtime; |
|
63 | 63 | int st_ctime; |
|
64 | 64 | }; |
|
65 | 65 | struct listdir_stat { |
|
66 | 66 | PyObject_HEAD |
|
67 | 67 | struct hg_stat st; |
|
68 | 68 | }; |
|
69 | 69 | #else |
|
70 | 70 | struct listdir_stat { |
|
71 | 71 | PyObject_HEAD |
|
72 | 72 | struct stat st; |
|
73 | 73 | }; |
|
74 | 74 | #endif |
|
75 | 75 | |
|
76 | 76 | #ifdef IS_PY3K |
|
77 | 77 | #define listdir_slot(name) \ |
|
78 | 78 | static PyObject *listdir_stat_##name(PyObject *self, void *x) \ |
|
79 | 79 | { \ |
|
80 | 80 | return PyLong_FromLong(((struct listdir_stat *)self)->st.name); \ |
|
81 | 81 | } |
|
82 | 82 | #else |
|
83 | 83 | #define listdir_slot(name) \ |
|
84 | 84 | static PyObject *listdir_stat_##name(PyObject *self, void *x) \ |
|
85 | 85 | { \ |
|
86 | 86 | return PyInt_FromLong(((struct listdir_stat *)self)->st.name); \ |
|
87 | 87 | } |
|
88 | 88 | #endif |
|
89 | 89 | |
|
90 | 90 | listdir_slot(st_dev) |
|
91 | 91 | listdir_slot(st_mode) |
|
92 | 92 | listdir_slot(st_nlink) |
|
93 | 93 | #ifdef _WIN32 |
|
94 | 94 | static PyObject *listdir_stat_st_size(PyObject *self, void *x) |
|
95 | 95 | { |
|
96 | 96 | return PyLong_FromLongLong( |
|
97 | 97 | (PY_LONG_LONG)((struct listdir_stat *)self)->st.st_size); |
|
98 | 98 | } |
|
99 | 99 | #else |
|
100 | 100 | listdir_slot(st_size) |
|
101 | 101 | #endif |
|
102 | 102 | listdir_slot(st_mtime) |
|
103 | 103 | listdir_slot(st_ctime) |
|
104 | 104 | |
|
105 | 105 | static struct PyGetSetDef listdir_stat_getsets[] = { |
|
106 | 106 | {"st_dev", listdir_stat_st_dev, 0, 0, 0}, |
|
107 | 107 | {"st_mode", listdir_stat_st_mode, 0, 0, 0}, |
|
108 | 108 | {"st_nlink", listdir_stat_st_nlink, 0, 0, 0}, |
|
109 | 109 | {"st_size", listdir_stat_st_size, 0, 0, 0}, |
|
110 | 110 | {"st_mtime", listdir_stat_st_mtime, 0, 0, 0}, |
|
111 | 111 | {"st_ctime", listdir_stat_st_ctime, 0, 0, 0}, |
|
112 | 112 | {0, 0, 0, 0, 0} |
|
113 | 113 | }; |
|
114 | 114 | |
|
115 | 115 | static PyObject *listdir_stat_new(PyTypeObject *t, PyObject *a, PyObject *k) |
|
116 | 116 | { |
|
117 | 117 | return t->tp_alloc(t, 0); |
|
118 | 118 | } |
|
119 | 119 | |
|
120 | 120 | static void listdir_stat_dealloc(PyObject *o) |
|
121 | 121 | { |
|
122 | 122 | o->ob_type->tp_free(o); |
|
123 | 123 | } |
|
124 | 124 | |
|
125 | 125 | static PyObject *listdir_stat_getitem(PyObject *self, PyObject *key) |
|
126 | 126 | { |
|
127 | 127 | long index = PyLong_AsLong(key); |
|
128 | 128 | if (index == -1 && PyErr_Occurred()) { |
|
129 | 129 | return NULL; |
|
130 | 130 | } |
|
131 | 131 | if (index != 8) { |
|
132 | 132 | PyErr_Format(PyExc_IndexError, "osutil.stat objects only " |
|
133 | 133 | "support stat.ST_MTIME in " |
|
134 | 134 | "__getitem__"); |
|
135 | 135 | return NULL; |
|
136 | 136 | } |
|
137 | 137 | return listdir_stat_st_mtime(self, NULL); |
|
138 | 138 | } |
|
139 | 139 | |
|
140 | 140 | static PyMappingMethods listdir_stat_type_mapping_methods = { |
|
141 | 141 | (lenfunc)NULL, /* mp_length */ |
|
142 | 142 | (binaryfunc)listdir_stat_getitem, /* mp_subscript */ |
|
143 | 143 | (objobjargproc)NULL, /* mp_ass_subscript */ |
|
144 | 144 | }; |
|
145 | 145 | |
|
146 | 146 | static PyTypeObject listdir_stat_type = { |
|
147 | 147 | PyVarObject_HEAD_INIT(NULL, 0) /* header */ |
|
148 | 148 | "osutil.stat", /*tp_name*/ |
|
149 | 149 | sizeof(struct listdir_stat), /*tp_basicsize*/ |
|
150 | 150 | 0, /*tp_itemsize*/ |
|
151 | 151 | (destructor)listdir_stat_dealloc, /*tp_dealloc*/ |
|
152 | 152 | 0, /*tp_print*/ |
|
153 | 153 | 0, /*tp_getattr*/ |
|
154 | 154 | 0, /*tp_setattr*/ |
|
155 | 155 | 0, /*tp_compare*/ |
|
156 | 156 | 0, /*tp_repr*/ |
|
157 | 157 | 0, /*tp_as_number*/ |
|
158 | 158 | 0, /*tp_as_sequence*/ |
|
159 | 159 | &listdir_stat_type_mapping_methods, /*tp_as_mapping*/ |
|
160 | 160 | 0, /*tp_hash */ |
|
161 | 161 | 0, /*tp_call*/ |
|
162 | 162 | 0, /*tp_str*/ |
|
163 | 163 | 0, /*tp_getattro*/ |
|
164 | 164 | 0, /*tp_setattro*/ |
|
165 | 165 | 0, /*tp_as_buffer*/ |
|
166 | 166 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
|
167 | 167 | "stat objects", /* tp_doc */ |
|
168 | 168 | 0, /* tp_traverse */ |
|
169 | 169 | 0, /* tp_clear */ |
|
170 | 170 | 0, /* tp_richcompare */ |
|
171 | 171 | 0, /* tp_weaklistoffset */ |
|
172 | 172 | 0, /* tp_iter */ |
|
173 | 173 | 0, /* tp_iternext */ |
|
174 | 174 | 0, /* tp_methods */ |
|
175 | 175 | 0, /* tp_members */ |
|
176 | 176 | listdir_stat_getsets, /* tp_getset */ |
|
177 | 177 | 0, /* tp_base */ |
|
178 | 178 | 0, /* tp_dict */ |
|
179 | 179 | 0, /* tp_descr_get */ |
|
180 | 180 | 0, /* tp_descr_set */ |
|
181 | 181 | 0, /* tp_dictoffset */ |
|
182 | 182 | 0, /* tp_init */ |
|
183 | 183 | 0, /* tp_alloc */ |
|
184 | 184 | listdir_stat_new, /* tp_new */ |
|
185 | 185 | }; |
|
186 | 186 | |
|
187 | 187 | #ifdef _WIN32 |
|
188 | 188 | |
|
189 | 189 | static int to_python_time(const FILETIME *tm) |
|
190 | 190 | { |
|
191 | 191 | /* number of seconds between epoch and January 1 1601 */ |
|
192 | 192 | const __int64 a0 = (__int64)134774L * (__int64)24L * (__int64)3600L; |
|
193 | 193 | /* conversion factor from 100ns to 1s */ |
|
194 | 194 | const __int64 a1 = 10000000; |
|
195 | 195 | /* explicit (int) cast to suspend compiler warnings */ |
|
196 | 196 | return (int)((((__int64)tm->dwHighDateTime << 32) |
|
197 | 197 | + tm->dwLowDateTime) / a1 - a0); |
|
198 | 198 | } |
|
199 | 199 | |
|
200 | 200 | static PyObject *make_item(const WIN32_FIND_DATAA *fd, int wantstat) |
|
201 | 201 | { |
|
202 | 202 | PyObject *py_st; |
|
203 | 203 | struct hg_stat *stp; |
|
204 | 204 | |
|
205 | 205 | int kind = (fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
|
206 | 206 | ? _S_IFDIR : _S_IFREG; |
|
207 | 207 | |
|
208 | 208 | if (!wantstat) |
|
209 | 209 | return Py_BuildValue(PY23("si", "yi"), fd->cFileName, kind); |
|
210 | 210 | |
|
211 | 211 | py_st = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL); |
|
212 | 212 | if (!py_st) |
|
213 | 213 | return NULL; |
|
214 | 214 | |
|
215 | 215 | stp = &((struct listdir_stat *)py_st)->st; |
|
216 | 216 | /* |
|
217 | 217 | use kind as st_mode |
|
218 | 218 | rwx bits on Win32 are meaningless |
|
219 | 219 | and Hg does not use them anyway |
|
220 | 220 | */ |
|
221 | 221 | stp->st_mode = kind; |
|
222 | 222 | stp->st_mtime = to_python_time(&fd->ftLastWriteTime); |
|
223 | 223 | stp->st_ctime = to_python_time(&fd->ftCreationTime); |
|
224 | 224 | if (kind == _S_IFREG) |
|
225 | 225 | stp->st_size = ((__int64)fd->nFileSizeHigh << 32) |
|
226 | 226 | + fd->nFileSizeLow; |
|
227 | 227 | return Py_BuildValue(PY23("siN", "yiN"), fd->cFileName, |
|
228 | 228 | kind, py_st); |
|
229 | 229 | } |
|
230 | 230 | |
|
231 | 231 | static PyObject *_listdir(char *path, Py_ssize_t plen, int wantstat, char *skip) |
|
232 | 232 | { |
|
233 | 233 | PyObject *rval = NULL; /* initialize - return value */ |
|
234 | 234 | PyObject *list; |
|
235 | 235 | HANDLE fh; |
|
236 | 236 | WIN32_FIND_DATAA fd; |
|
237 | 237 | char *pattern; |
|
238 | 238 | |
|
239 | 239 | /* build the path + \* pattern string */ |
|
240 | 240 | pattern = PyMem_Malloc(plen + 3); /* path + \* + \0 */ |
|
241 | 241 | if (!pattern) { |
|
242 | 242 | PyErr_NoMemory(); |
|
243 | 243 | goto error_nomem; |
|
244 | 244 | } |
|
245 | 245 | memcpy(pattern, path, plen); |
|
246 | 246 | |
|
247 | 247 | if (plen > 0) { |
|
248 | 248 | char c = path[plen-1]; |
|
249 | 249 | if (c != ':' && c != '/' && c != '\\') |
|
250 | 250 | pattern[plen++] = '\\'; |
|
251 | 251 | } |
|
252 | 252 | pattern[plen++] = '*'; |
|
253 | 253 | pattern[plen] = '\0'; |
|
254 | 254 | |
|
255 | 255 | fh = FindFirstFileA(pattern, &fd); |
|
256 | 256 | if (fh == INVALID_HANDLE_VALUE) { |
|
257 | 257 | PyErr_SetFromWindowsErrWithFilename(GetLastError(), path); |
|
258 | 258 | goto error_file; |
|
259 | 259 | } |
|
260 | 260 | |
|
261 | 261 | list = PyList_New(0); |
|
262 | 262 | if (!list) |
|
263 | 263 | goto error_list; |
|
264 | 264 | |
|
265 | 265 | do { |
|
266 | 266 | PyObject *item; |
|
267 | 267 | |
|
268 | 268 | if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
|
269 | 269 | if (!strcmp(fd.cFileName, ".") |
|
270 | 270 | || !strcmp(fd.cFileName, "..")) |
|
271 | 271 | continue; |
|
272 | 272 | |
|
273 | 273 | if (skip && !strcmp(fd.cFileName, skip)) { |
|
274 | 274 | rval = PyList_New(0); |
|
275 | 275 | goto error; |
|
276 | 276 | } |
|
277 | 277 | } |
|
278 | 278 | |
|
279 | 279 | item = make_item(&fd, wantstat); |
|
280 | 280 | if (!item) |
|
281 | 281 | goto error; |
|
282 | 282 | |
|
283 | 283 | if (PyList_Append(list, item)) { |
|
284 | 284 | Py_XDECREF(item); |
|
285 | 285 | goto error; |
|
286 | 286 | } |
|
287 | 287 | |
|
288 | 288 | Py_XDECREF(item); |
|
289 | 289 | } while (FindNextFileA(fh, &fd)); |
|
290 | 290 | |
|
291 | 291 | if (GetLastError() != ERROR_NO_MORE_FILES) { |
|
292 | 292 | PyErr_SetFromWindowsErrWithFilename(GetLastError(), path); |
|
293 | 293 | goto error; |
|
294 | 294 | } |
|
295 | 295 | |
|
296 | 296 | rval = list; |
|
297 | 297 | Py_XINCREF(rval); |
|
298 | 298 | error: |
|
299 | 299 | Py_XDECREF(list); |
|
300 | 300 | error_list: |
|
301 | 301 | FindClose(fh); |
|
302 | 302 | error_file: |
|
303 | 303 | PyMem_Free(pattern); |
|
304 | 304 | error_nomem: |
|
305 | 305 | return rval; |
|
306 | 306 | } |
|
307 | 307 | |
|
308 | 308 | #else |
|
309 | 309 | |
|
310 | 310 | int entkind(struct dirent *ent) |
|
311 | 311 | { |
|
312 | 312 | #ifdef DT_REG |
|
313 | 313 | switch (ent->d_type) { |
|
314 | 314 | case DT_REG: return S_IFREG; |
|
315 | 315 | case DT_DIR: return S_IFDIR; |
|
316 | 316 | case DT_LNK: return S_IFLNK; |
|
317 | 317 | case DT_BLK: return S_IFBLK; |
|
318 | 318 | case DT_CHR: return S_IFCHR; |
|
319 | 319 | case DT_FIFO: return S_IFIFO; |
|
320 | 320 | case DT_SOCK: return S_IFSOCK; |
|
321 | 321 | } |
|
322 | 322 | #endif |
|
323 | 323 | return -1; |
|
324 | 324 | } |
|
325 | 325 | |
|
326 | 326 | static PyObject *makestat(const struct stat *st) |
|
327 | 327 | { |
|
328 | 328 | PyObject *stat; |
|
329 | 329 | |
|
330 | 330 | stat = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL); |
|
331 | 331 | if (stat) |
|
332 | 332 | memcpy(&((struct listdir_stat *)stat)->st, st, sizeof(*st)); |
|
333 | 333 | return stat; |
|
334 | 334 | } |
|
335 | 335 | |
|
336 | 336 | static PyObject *_listdir_stat(char *path, int pathlen, int keepstat, |
|
337 | 337 | char *skip) |
|
338 | 338 | { |
|
339 |
PyObject *list, *elem, * |
|
|
339 | PyObject *list, *elem, *ret = NULL; | |
|
340 | 340 | char fullpath[PATH_MAX + 10]; |
|
341 | 341 | int kind, err; |
|
342 | 342 | struct stat st; |
|
343 | 343 | struct dirent *ent; |
|
344 | 344 | DIR *dir; |
|
345 | 345 | #ifdef AT_SYMLINK_NOFOLLOW |
|
346 | 346 | int dfd = -1; |
|
347 | 347 | #endif |
|
348 | 348 | |
|
349 | 349 | if (pathlen >= PATH_MAX) { |
|
350 | 350 | errno = ENAMETOOLONG; |
|
351 | 351 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
|
352 | 352 | goto error_value; |
|
353 | 353 | } |
|
354 | 354 | strncpy(fullpath, path, PATH_MAX); |
|
355 | 355 | fullpath[pathlen] = '/'; |
|
356 | 356 | |
|
357 | 357 | #ifdef AT_SYMLINK_NOFOLLOW |
|
358 | 358 | dfd = open(path, O_RDONLY); |
|
359 | 359 | if (dfd == -1) { |
|
360 | 360 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
|
361 | 361 | goto error_value; |
|
362 | 362 | } |
|
363 | 363 | dir = fdopendir(dfd); |
|
364 | 364 | #else |
|
365 | 365 | dir = opendir(path); |
|
366 | 366 | #endif |
|
367 | 367 | if (!dir) { |
|
368 | 368 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
|
369 | 369 | goto error_dir; |
|
370 | 370 | } |
|
371 | 371 | |
|
372 | 372 | list = PyList_New(0); |
|
373 | 373 | if (!list) |
|
374 | 374 | goto error_list; |
|
375 | 375 | |
|
376 | 376 | while ((ent = readdir(dir))) { |
|
377 | 377 | if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) |
|
378 | 378 | continue; |
|
379 | 379 | |
|
380 | 380 | kind = entkind(ent); |
|
381 | 381 | if (kind == -1 || keepstat) { |
|
382 | 382 | #ifdef AT_SYMLINK_NOFOLLOW |
|
383 | 383 | err = fstatat(dfd, ent->d_name, &st, |
|
384 | 384 | AT_SYMLINK_NOFOLLOW); |
|
385 | 385 | #else |
|
386 | 386 | strncpy(fullpath + pathlen + 1, ent->d_name, |
|
387 | 387 | PATH_MAX - pathlen); |
|
388 | 388 | fullpath[PATH_MAX] = '\0'; |
|
389 | 389 | err = lstat(fullpath, &st); |
|
390 | 390 | #endif |
|
391 | 391 | if (err == -1) { |
|
392 | 392 | /* race with file deletion? */ |
|
393 | 393 | if (errno == ENOENT) |
|
394 | 394 | continue; |
|
395 | 395 | strncpy(fullpath + pathlen + 1, ent->d_name, |
|
396 | 396 | PATH_MAX - pathlen); |
|
397 | 397 | fullpath[PATH_MAX] = 0; |
|
398 | 398 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, |
|
399 | 399 | fullpath); |
|
400 | 400 | goto error; |
|
401 | 401 | } |
|
402 | 402 | kind = st.st_mode & S_IFMT; |
|
403 | 403 | } |
|
404 | 404 | |
|
405 | 405 | /* quit early? */ |
|
406 | 406 | if (skip && kind == S_IFDIR && !strcmp(ent->d_name, skip)) { |
|
407 | 407 | ret = PyList_New(0); |
|
408 | 408 | goto error; |
|
409 | 409 | } |
|
410 | 410 | |
|
411 | 411 | if (keepstat) { |
|
412 | stat = makestat(&st); | |
|
412 | PyObject *stat = makestat(&st); | |
|
413 | 413 | if (!stat) |
|
414 | 414 | goto error; |
|
415 | 415 | elem = Py_BuildValue(PY23("siN", "yiN"), ent->d_name, |
|
416 | 416 | kind, stat); |
|
417 | 417 | } else |
|
418 | 418 | elem = Py_BuildValue(PY23("si", "yi"), ent->d_name, |
|
419 | 419 | kind); |
|
420 | 420 | if (!elem) |
|
421 | 421 | goto error; |
|
422 | stat = NULL; | |
|
423 | 422 | |
|
424 | 423 | PyList_Append(list, elem); |
|
425 | 424 | Py_DECREF(elem); |
|
426 | 425 | } |
|
427 | 426 | |
|
428 | 427 | ret = list; |
|
429 | 428 | Py_INCREF(ret); |
|
430 | 429 | |
|
431 | 430 | error: |
|
432 | 431 | Py_DECREF(list); |
|
433 | Py_XDECREF(stat); | |
|
434 | 432 | error_list: |
|
435 | 433 | closedir(dir); |
|
436 | 434 | /* closedir also closes its dirfd */ |
|
437 | 435 | goto error_value; |
|
438 | 436 | error_dir: |
|
439 | 437 | #ifdef AT_SYMLINK_NOFOLLOW |
|
440 | 438 | close(dfd); |
|
441 | 439 | #endif |
|
442 | 440 | error_value: |
|
443 | 441 | return ret; |
|
444 | 442 | } |
|
445 | 443 | |
|
446 | 444 | #ifdef __APPLE__ |
|
447 | 445 | |
|
448 | 446 | typedef struct { |
|
449 | 447 | u_int32_t length; |
|
450 | 448 | attrreference_t name; |
|
451 | 449 | fsobj_type_t obj_type; |
|
452 | 450 | struct timespec mtime; |
|
453 | 451 | #if __LITTLE_ENDIAN__ |
|
454 | 452 | mode_t access_mask; |
|
455 | 453 | uint16_t padding; |
|
456 | 454 | #else |
|
457 | 455 | uint16_t padding; |
|
458 | 456 | mode_t access_mask; |
|
459 | 457 | #endif |
|
460 | 458 | off_t size; |
|
461 | 459 | } __attribute__((packed)) attrbuf_entry; |
|
462 | 460 | |
|
463 | 461 | int attrkind(attrbuf_entry *entry) |
|
464 | 462 | { |
|
465 | 463 | switch (entry->obj_type) { |
|
466 | 464 | case VREG: return S_IFREG; |
|
467 | 465 | case VDIR: return S_IFDIR; |
|
468 | 466 | case VLNK: return S_IFLNK; |
|
469 | 467 | case VBLK: return S_IFBLK; |
|
470 | 468 | case VCHR: return S_IFCHR; |
|
471 | 469 | case VFIFO: return S_IFIFO; |
|
472 | 470 | case VSOCK: return S_IFSOCK; |
|
473 | 471 | } |
|
474 | 472 | return -1; |
|
475 | 473 | } |
|
476 | 474 | |
|
477 | 475 | /* get these many entries at a time */ |
|
478 | 476 | #define LISTDIR_BATCH_SIZE 50 |
|
479 | 477 | |
|
480 | 478 | static PyObject *_listdir_batch(char *path, int pathlen, int keepstat, |
|
481 | 479 | char *skip, bool *fallback) |
|
482 | 480 | { |
|
483 |
PyObject *list, *elem, * |
|
|
481 | PyObject *list, *elem, *ret = NULL; | |
|
484 | 482 | int kind, err; |
|
485 | 483 | unsigned long index; |
|
486 | 484 | unsigned int count, old_state, new_state; |
|
487 | 485 | bool state_seen = false; |
|
488 | 486 | attrbuf_entry *entry; |
|
489 | 487 | /* from the getattrlist(2) man page: a path can be no longer than |
|
490 | 488 | (NAME_MAX * 3 + 1) bytes. Also, "The getattrlist() function will |
|
491 | 489 | silently truncate attribute data if attrBufSize is too small." So |
|
492 | 490 | pass in a buffer big enough for the worst case. */ |
|
493 | 491 | char attrbuf[LISTDIR_BATCH_SIZE * (sizeof(attrbuf_entry) + NAME_MAX * 3 + 1)]; |
|
494 | 492 | unsigned int basep_unused; |
|
495 | 493 | |
|
496 | 494 | struct stat st; |
|
497 | 495 | int dfd = -1; |
|
498 | 496 | |
|
499 | 497 | /* these must match the attrbuf_entry struct, otherwise you'll end up |
|
500 | 498 | with garbage */ |
|
501 | 499 | struct attrlist requested_attr = {0}; |
|
502 | 500 | requested_attr.bitmapcount = ATTR_BIT_MAP_COUNT; |
|
503 | 501 | requested_attr.commonattr = (ATTR_CMN_NAME | ATTR_CMN_OBJTYPE | |
|
504 | 502 | ATTR_CMN_MODTIME | ATTR_CMN_ACCESSMASK); |
|
505 | 503 | requested_attr.fileattr = ATTR_FILE_DATALENGTH; |
|
506 | 504 | |
|
507 | 505 | *fallback = false; |
|
508 | 506 | |
|
509 | 507 | if (pathlen >= PATH_MAX) { |
|
510 | 508 | errno = ENAMETOOLONG; |
|
511 | 509 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
|
512 | 510 | goto error_value; |
|
513 | 511 | } |
|
514 | 512 | |
|
515 | 513 | dfd = open(path, O_RDONLY); |
|
516 | 514 | if (dfd == -1) { |
|
517 | 515 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
|
518 | 516 | goto error_value; |
|
519 | 517 | } |
|
520 | 518 | |
|
521 | 519 | list = PyList_New(0); |
|
522 | 520 | if (!list) |
|
523 | 521 | goto error_dir; |
|
524 | 522 | |
|
525 | 523 | do { |
|
526 | 524 | count = LISTDIR_BATCH_SIZE; |
|
527 | 525 | err = getdirentriesattr(dfd, &requested_attr, &attrbuf, |
|
528 | 526 | sizeof(attrbuf), &count, &basep_unused, |
|
529 | 527 | &new_state, 0); |
|
530 | 528 | if (err < 0) { |
|
531 | 529 | if (errno == ENOTSUP) { |
|
532 | 530 | /* We're on a filesystem that doesn't support |
|
533 | 531 | getdirentriesattr. Fall back to the |
|
534 | 532 | stat-based implementation. */ |
|
535 | 533 | *fallback = true; |
|
536 | 534 | } else |
|
537 | 535 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
|
538 | 536 | goto error; |
|
539 | 537 | } |
|
540 | 538 | |
|
541 | 539 | if (!state_seen) { |
|
542 | 540 | old_state = new_state; |
|
543 | 541 | state_seen = true; |
|
544 | 542 | } else if (old_state != new_state) { |
|
545 | 543 | /* There's an edge case with getdirentriesattr. Consider |
|
546 | 544 | the following initial list of files: |
|
547 | 545 | |
|
548 | 546 | a |
|
549 | 547 | b |
|
550 | 548 | <-- |
|
551 | 549 | c |
|
552 | 550 | d |
|
553 | 551 | |
|
554 | 552 | If the iteration is paused at the arrow, and b is |
|
555 | 553 | deleted before it is resumed, getdirentriesattr will |
|
556 | 554 | not return d at all! Ordinarily we're expected to |
|
557 | 555 | restart the iteration from the beginning. To avoid |
|
558 | 556 | getting stuck in a retry loop here, fall back to |
|
559 | 557 | stat. */ |
|
560 | 558 | *fallback = true; |
|
561 | 559 | goto error; |
|
562 | 560 | } |
|
563 | 561 | |
|
564 | 562 | entry = (attrbuf_entry *)attrbuf; |
|
565 | 563 | |
|
566 | 564 | for (index = 0; index < count; index++) { |
|
567 | 565 | char *filename = ((char *)&entry->name) + |
|
568 | 566 | entry->name.attr_dataoffset; |
|
569 | 567 | |
|
570 | 568 | if (!strcmp(filename, ".") || !strcmp(filename, "..")) |
|
571 | 569 | continue; |
|
572 | 570 | |
|
573 | 571 | kind = attrkind(entry); |
|
574 | 572 | if (kind == -1) { |
|
575 | 573 | PyErr_Format(PyExc_OSError, |
|
576 | 574 | "unknown object type %u for file " |
|
577 | 575 | "%s%s!", |
|
578 | 576 | entry->obj_type, path, filename); |
|
579 | 577 | goto error; |
|
580 | 578 | } |
|
581 | 579 | |
|
582 | 580 | /* quit early? */ |
|
583 | 581 | if (skip && kind == S_IFDIR && !strcmp(filename, skip)) { |
|
584 | 582 | ret = PyList_New(0); |
|
585 | 583 | goto error; |
|
586 | 584 | } |
|
587 | 585 | |
|
588 | 586 | if (keepstat) { |
|
587 | PyObject *stat = NULL; | |
|
589 | 588 | /* from the getattrlist(2) man page: "Only the |
|
590 | 589 | permission bits ... are valid". */ |
|
591 | 590 | st.st_mode = (entry->access_mask & ~S_IFMT) | kind; |
|
592 | 591 | st.st_mtime = entry->mtime.tv_sec; |
|
593 | 592 | st.st_size = entry->size; |
|
594 | 593 | stat = makestat(&st); |
|
595 | 594 | if (!stat) |
|
596 | 595 | goto error; |
|
597 | 596 | elem = Py_BuildValue(PY23("siN", "yiN"), |
|
598 | 597 | filename, kind, stat); |
|
599 | 598 | } else |
|
600 | 599 | elem = Py_BuildValue(PY23("si", "yi"), |
|
601 | 600 | filename, kind); |
|
602 | 601 | if (!elem) |
|
603 | 602 | goto error; |
|
604 | stat = NULL; | |
|
605 | 603 | |
|
606 | 604 | PyList_Append(list, elem); |
|
607 | 605 | Py_DECREF(elem); |
|
608 | 606 | |
|
609 | 607 | entry = (attrbuf_entry *)((char *)entry + entry->length); |
|
610 | 608 | } |
|
611 | 609 | } while (err == 0); |
|
612 | 610 | |
|
613 | 611 | ret = list; |
|
614 | 612 | Py_INCREF(ret); |
|
615 | 613 | |
|
616 | 614 | error: |
|
617 | 615 | Py_DECREF(list); |
|
618 | Py_XDECREF(stat); | |
|
619 | 616 | error_dir: |
|
620 | 617 | close(dfd); |
|
621 | 618 | error_value: |
|
622 | 619 | return ret; |
|
623 | 620 | } |
|
624 | 621 | |
|
625 | 622 | #endif /* __APPLE__ */ |
|
626 | 623 | |
|
627 | 624 | static PyObject *_listdir(char *path, int pathlen, int keepstat, char *skip) |
|
628 | 625 | { |
|
629 | 626 | #ifdef __APPLE__ |
|
630 | 627 | PyObject *ret; |
|
631 | 628 | bool fallback = false; |
|
632 | 629 | |
|
633 | 630 | ret = _listdir_batch(path, pathlen, keepstat, skip, &fallback); |
|
634 | 631 | if (ret != NULL || !fallback) |
|
635 | 632 | return ret; |
|
636 | 633 | #endif |
|
637 | 634 | return _listdir_stat(path, pathlen, keepstat, skip); |
|
638 | 635 | } |
|
639 | 636 | |
|
640 | 637 | static PyObject *statfiles(PyObject *self, PyObject *args) |
|
641 | 638 | { |
|
642 | 639 | PyObject *names, *stats; |
|
643 | 640 | Py_ssize_t i, count; |
|
644 | 641 | |
|
645 | 642 | if (!PyArg_ParseTuple(args, "O:statfiles", &names)) |
|
646 | 643 | return NULL; |
|
647 | 644 | |
|
648 | 645 | count = PySequence_Length(names); |
|
649 | 646 | if (count == -1) { |
|
650 | 647 | PyErr_SetString(PyExc_TypeError, "not a sequence"); |
|
651 | 648 | return NULL; |
|
652 | 649 | } |
|
653 | 650 | |
|
654 | 651 | stats = PyList_New(count); |
|
655 | 652 | if (stats == NULL) |
|
656 | 653 | return NULL; |
|
657 | 654 | |
|
658 | 655 | for (i = 0; i < count; i++) { |
|
659 | 656 | PyObject *stat, *pypath; |
|
660 | 657 | struct stat st; |
|
661 | 658 | int ret, kind; |
|
662 | 659 | char *path; |
|
663 | 660 | |
|
664 | 661 | /* With a large file count or on a slow filesystem, |
|
665 | 662 | don't block signals for long (issue4878). */ |
|
666 | 663 | if ((i % 1000) == 999 && PyErr_CheckSignals() == -1) |
|
667 | 664 | goto bail; |
|
668 | 665 | |
|
669 | 666 | pypath = PySequence_GetItem(names, i); |
|
670 | 667 | if (!pypath) |
|
671 | 668 | goto bail; |
|
672 | 669 | path = PyBytes_AsString(pypath); |
|
673 | 670 | if (path == NULL) { |
|
674 | 671 | Py_DECREF(pypath); |
|
675 | 672 | PyErr_SetString(PyExc_TypeError, "not a string"); |
|
676 | 673 | goto bail; |
|
677 | 674 | } |
|
678 | 675 | ret = lstat(path, &st); |
|
679 | 676 | Py_DECREF(pypath); |
|
680 | 677 | kind = st.st_mode & S_IFMT; |
|
681 | 678 | if (ret != -1 && (kind == S_IFREG || kind == S_IFLNK)) { |
|
682 | 679 | stat = makestat(&st); |
|
683 | 680 | if (stat == NULL) |
|
684 | 681 | goto bail; |
|
685 | 682 | PyList_SET_ITEM(stats, i, stat); |
|
686 | 683 | } else { |
|
687 | 684 | Py_INCREF(Py_None); |
|
688 | 685 | PyList_SET_ITEM(stats, i, Py_None); |
|
689 | 686 | } |
|
690 | 687 | } |
|
691 | 688 | |
|
692 | 689 | return stats; |
|
693 | 690 | |
|
694 | 691 | bail: |
|
695 | 692 | Py_DECREF(stats); |
|
696 | 693 | return NULL; |
|
697 | 694 | } |
|
698 | 695 | |
|
699 | 696 | /* |
|
700 | 697 | * recvfds() simply does not release GIL during blocking io operation because |
|
701 | 698 | * command server is known to be single-threaded. |
|
702 | 699 | * |
|
703 | 700 | * Old systems such as Solaris don't provide CMSG_LEN, msg_control, etc. |
|
704 | 701 | * Currently, recvfds() is not supported on these platforms. |
|
705 | 702 | */ |
|
706 | 703 | #ifdef CMSG_LEN |
|
707 | 704 | |
|
708 | 705 | static ssize_t recvfdstobuf(int sockfd, int **rfds, void *cbuf, size_t cbufsize) |
|
709 | 706 | { |
|
710 | 707 | char dummy[1]; |
|
711 | 708 | struct iovec iov = {dummy, sizeof(dummy)}; |
|
712 | 709 | struct msghdr msgh = {0}; |
|
713 | 710 | struct cmsghdr *cmsg; |
|
714 | 711 | |
|
715 | 712 | msgh.msg_iov = &iov; |
|
716 | 713 | msgh.msg_iovlen = 1; |
|
717 | 714 | msgh.msg_control = cbuf; |
|
718 | 715 | msgh.msg_controllen = (socklen_t)cbufsize; |
|
719 | 716 | if (recvmsg(sockfd, &msgh, 0) < 0) |
|
720 | 717 | return -1; |
|
721 | 718 | |
|
722 | 719 | for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg; |
|
723 | 720 | cmsg = CMSG_NXTHDR(&msgh, cmsg)) { |
|
724 | 721 | if (cmsg->cmsg_level != SOL_SOCKET || |
|
725 | 722 | cmsg->cmsg_type != SCM_RIGHTS) |
|
726 | 723 | continue; |
|
727 | 724 | *rfds = (int *)CMSG_DATA(cmsg); |
|
728 | 725 | return (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); |
|
729 | 726 | } |
|
730 | 727 | |
|
731 | 728 | *rfds = cbuf; |
|
732 | 729 | return 0; |
|
733 | 730 | } |
|
734 | 731 | |
|
735 | 732 | static PyObject *recvfds(PyObject *self, PyObject *args) |
|
736 | 733 | { |
|
737 | 734 | int sockfd; |
|
738 | 735 | int *rfds = NULL; |
|
739 | 736 | ssize_t rfdscount, i; |
|
740 | 737 | char cbuf[256]; |
|
741 | 738 | PyObject *rfdslist = NULL; |
|
742 | 739 | |
|
743 | 740 | if (!PyArg_ParseTuple(args, "i", &sockfd)) |
|
744 | 741 | return NULL; |
|
745 | 742 | |
|
746 | 743 | rfdscount = recvfdstobuf(sockfd, &rfds, cbuf, sizeof(cbuf)); |
|
747 | 744 | if (rfdscount < 0) |
|
748 | 745 | return PyErr_SetFromErrno(PyExc_OSError); |
|
749 | 746 | |
|
750 | 747 | rfdslist = PyList_New(rfdscount); |
|
751 | 748 | if (!rfdslist) |
|
752 | 749 | goto bail; |
|
753 | 750 | for (i = 0; i < rfdscount; i++) { |
|
754 | 751 | PyObject *obj = PyLong_FromLong(rfds[i]); |
|
755 | 752 | if (!obj) |
|
756 | 753 | goto bail; |
|
757 | 754 | PyList_SET_ITEM(rfdslist, i, obj); |
|
758 | 755 | } |
|
759 | 756 | return rfdslist; |
|
760 | 757 | |
|
761 | 758 | bail: |
|
762 | 759 | Py_XDECREF(rfdslist); |
|
763 | 760 | return NULL; |
|
764 | 761 | } |
|
765 | 762 | |
|
766 | 763 | #endif /* CMSG_LEN */ |
|
767 | 764 | |
|
768 | 765 | /* allow disabling setprocname via compiler flags */ |
|
769 | 766 | #ifndef SETPROCNAME_USE_NONE |
|
770 | 767 | #if defined(HAVE_SETPROCTITLE) |
|
771 | 768 | /* setproctitle is the first choice - available in FreeBSD */ |
|
772 | 769 | #define SETPROCNAME_USE_SETPROCTITLE |
|
773 | 770 | #elif (defined(__linux__) || defined(__APPLE__)) && PY_MAJOR_VERSION == 2 |
|
774 | 771 | /* rewrite the argv buffer in place - works in Linux and OS X. Py_GetArgcArgv |
|
775 | 772 | * in Python 3 returns the copied wchar_t **argv, thus unsupported. */ |
|
776 | 773 | #define SETPROCNAME_USE_ARGVREWRITE |
|
777 | 774 | #else |
|
778 | 775 | #define SETPROCNAME_USE_NONE |
|
779 | 776 | #endif |
|
780 | 777 | #endif /* ndef SETPROCNAME_USE_NONE */ |
|
781 | 778 | |
|
782 | 779 | #ifndef SETPROCNAME_USE_NONE |
|
783 | 780 | static PyObject *setprocname(PyObject *self, PyObject *args) |
|
784 | 781 | { |
|
785 | 782 | const char *name = NULL; |
|
786 | 783 | if (!PyArg_ParseTuple(args, PY23("s", "y"), &name)) |
|
787 | 784 | return NULL; |
|
788 | 785 | |
|
789 | 786 | #if defined(SETPROCNAME_USE_SETPROCTITLE) |
|
790 | 787 | setproctitle("%s", name); |
|
791 | 788 | #elif defined(SETPROCNAME_USE_ARGVREWRITE) |
|
792 | 789 | { |
|
793 | 790 | static char *argvstart = NULL; |
|
794 | 791 | static size_t argvsize = 0; |
|
795 | 792 | if (argvstart == NULL) { |
|
796 | 793 | int argc = 0, i; |
|
797 | 794 | char **argv = NULL; |
|
798 | 795 | char *argvend; |
|
799 | 796 | extern void Py_GetArgcArgv(int *argc, char ***argv); |
|
800 | 797 | Py_GetArgcArgv(&argc, &argv); |
|
801 | 798 | /* Py_GetArgcArgv may not do much if a custom python |
|
802 | 799 | * launcher is used that doesn't record the information |
|
803 | 800 | * it needs. Let's handle this gracefully instead of |
|
804 | 801 | * segfaulting. */ |
|
805 | 802 | if (argv != NULL) |
|
806 | 803 | argvend = argvstart = argv[0]; |
|
807 | 804 | else |
|
808 | 805 | argvend = argvstart = NULL; |
|
809 | 806 | |
|
810 | 807 | /* Check the memory we can use. Typically, argv[i] and |
|
811 | 808 | * argv[i + 1] are continuous. */ |
|
812 | 809 | for (i = 0; i < argc; ++i) { |
|
813 | 810 | size_t len; |
|
814 | 811 | if (argv[i] > argvend || argv[i] < argvstart) |
|
815 | 812 | break; /* not continuous */ |
|
816 | 813 | len = strlen(argv[i]); |
|
817 | 814 | argvend = argv[i] + len + 1 /* '\0' */; |
|
818 | 815 | } |
|
819 | 816 | if (argvend > argvstart) /* sanity check */ |
|
820 | 817 | argvsize = argvend - argvstart; |
|
821 | 818 | } |
|
822 | 819 | |
|
823 | 820 | if (argvstart && argvsize > 1) { |
|
824 | 821 | int n = snprintf(argvstart, argvsize, "%s", name); |
|
825 | 822 | if (n >= 0 && (size_t)n < argvsize) |
|
826 | 823 | memset(argvstart + n, 0, argvsize - n); |
|
827 | 824 | } |
|
828 | 825 | } |
|
829 | 826 | #endif |
|
830 | 827 | |
|
831 | 828 | Py_RETURN_NONE; |
|
832 | 829 | } |
|
833 | 830 | #endif /* ndef SETPROCNAME_USE_NONE */ |
|
834 | 831 | |
|
835 | 832 | #if defined(HAVE_BSD_STATFS) |
|
836 | 833 | static const char *describefstype(const struct statfs *pbuf) |
|
837 | 834 | { |
|
838 | 835 | /* BSD or OSX provides a f_fstypename field */ |
|
839 | 836 | return pbuf->f_fstypename; |
|
840 | 837 | } |
|
841 | 838 | #elif defined(HAVE_LINUX_STATFS) |
|
842 | 839 | static const char *describefstype(const struct statfs *pbuf) |
|
843 | 840 | { |
|
844 | 841 | /* Begin of Linux filesystems */ |
|
845 | 842 | #ifdef ADFS_SUPER_MAGIC |
|
846 | 843 | if (pbuf->f_type == ADFS_SUPER_MAGIC) |
|
847 | 844 | return "adfs"; |
|
848 | 845 | #endif |
|
849 | 846 | #ifdef AFFS_SUPER_MAGIC |
|
850 | 847 | if (pbuf->f_type == AFFS_SUPER_MAGIC) |
|
851 | 848 | return "affs"; |
|
852 | 849 | #endif |
|
853 | 850 | #ifdef AUTOFS_SUPER_MAGIC |
|
854 | 851 | if (pbuf->f_type == AUTOFS_SUPER_MAGIC) |
|
855 | 852 | return "autofs"; |
|
856 | 853 | #endif |
|
857 | 854 | #ifdef BDEVFS_MAGIC |
|
858 | 855 | if (pbuf->f_type == BDEVFS_MAGIC) |
|
859 | 856 | return "bdevfs"; |
|
860 | 857 | #endif |
|
861 | 858 | #ifdef BEFS_SUPER_MAGIC |
|
862 | 859 | if (pbuf->f_type == BEFS_SUPER_MAGIC) |
|
863 | 860 | return "befs"; |
|
864 | 861 | #endif |
|
865 | 862 | #ifdef BFS_MAGIC |
|
866 | 863 | if (pbuf->f_type == BFS_MAGIC) |
|
867 | 864 | return "bfs"; |
|
868 | 865 | #endif |
|
869 | 866 | #ifdef BINFMTFS_MAGIC |
|
870 | 867 | if (pbuf->f_type == BINFMTFS_MAGIC) |
|
871 | 868 | return "binfmtfs"; |
|
872 | 869 | #endif |
|
873 | 870 | #ifdef BTRFS_SUPER_MAGIC |
|
874 | 871 | if (pbuf->f_type == BTRFS_SUPER_MAGIC) |
|
875 | 872 | return "btrfs"; |
|
876 | 873 | #endif |
|
877 | 874 | #ifdef CGROUP_SUPER_MAGIC |
|
878 | 875 | if (pbuf->f_type == CGROUP_SUPER_MAGIC) |
|
879 | 876 | return "cgroup"; |
|
880 | 877 | #endif |
|
881 | 878 | #ifdef CIFS_MAGIC_NUMBER |
|
882 | 879 | if (pbuf->f_type == CIFS_MAGIC_NUMBER) |
|
883 | 880 | return "cifs"; |
|
884 | 881 | #endif |
|
885 | 882 | #ifdef CODA_SUPER_MAGIC |
|
886 | 883 | if (pbuf->f_type == CODA_SUPER_MAGIC) |
|
887 | 884 | return "coda"; |
|
888 | 885 | #endif |
|
889 | 886 | #ifdef COH_SUPER_MAGIC |
|
890 | 887 | if (pbuf->f_type == COH_SUPER_MAGIC) |
|
891 | 888 | return "coh"; |
|
892 | 889 | #endif |
|
893 | 890 | #ifdef CRAMFS_MAGIC |
|
894 | 891 | if (pbuf->f_type == CRAMFS_MAGIC) |
|
895 | 892 | return "cramfs"; |
|
896 | 893 | #endif |
|
897 | 894 | #ifdef DEBUGFS_MAGIC |
|
898 | 895 | if (pbuf->f_type == DEBUGFS_MAGIC) |
|
899 | 896 | return "debugfs"; |
|
900 | 897 | #endif |
|
901 | 898 | #ifdef DEVFS_SUPER_MAGIC |
|
902 | 899 | if (pbuf->f_type == DEVFS_SUPER_MAGIC) |
|
903 | 900 | return "devfs"; |
|
904 | 901 | #endif |
|
905 | 902 | #ifdef DEVPTS_SUPER_MAGIC |
|
906 | 903 | if (pbuf->f_type == DEVPTS_SUPER_MAGIC) |
|
907 | 904 | return "devpts"; |
|
908 | 905 | #endif |
|
909 | 906 | #ifdef EFIVARFS_MAGIC |
|
910 | 907 | if (pbuf->f_type == EFIVARFS_MAGIC) |
|
911 | 908 | return "efivarfs"; |
|
912 | 909 | #endif |
|
913 | 910 | #ifdef EFS_SUPER_MAGIC |
|
914 | 911 | if (pbuf->f_type == EFS_SUPER_MAGIC) |
|
915 | 912 | return "efs"; |
|
916 | 913 | #endif |
|
917 | 914 | #ifdef EXT_SUPER_MAGIC |
|
918 | 915 | if (pbuf->f_type == EXT_SUPER_MAGIC) |
|
919 | 916 | return "ext"; |
|
920 | 917 | #endif |
|
921 | 918 | #ifdef EXT2_OLD_SUPER_MAGIC |
|
922 | 919 | if (pbuf->f_type == EXT2_OLD_SUPER_MAGIC) |
|
923 | 920 | return "ext2"; |
|
924 | 921 | #endif |
|
925 | 922 | #ifdef EXT2_SUPER_MAGIC |
|
926 | 923 | if (pbuf->f_type == EXT2_SUPER_MAGIC) |
|
927 | 924 | return "ext2"; |
|
928 | 925 | #endif |
|
929 | 926 | #ifdef EXT3_SUPER_MAGIC |
|
930 | 927 | if (pbuf->f_type == EXT3_SUPER_MAGIC) |
|
931 | 928 | return "ext3"; |
|
932 | 929 | #endif |
|
933 | 930 | #ifdef EXT4_SUPER_MAGIC |
|
934 | 931 | if (pbuf->f_type == EXT4_SUPER_MAGIC) |
|
935 | 932 | return "ext4"; |
|
936 | 933 | #endif |
|
937 | 934 | #ifdef F2FS_SUPER_MAGIC |
|
938 | 935 | if (pbuf->f_type == F2FS_SUPER_MAGIC) |
|
939 | 936 | return "f2fs"; |
|
940 | 937 | #endif |
|
941 | 938 | #ifdef FUSE_SUPER_MAGIC |
|
942 | 939 | if (pbuf->f_type == FUSE_SUPER_MAGIC) |
|
943 | 940 | return "fuse"; |
|
944 | 941 | #endif |
|
945 | 942 | #ifdef FUTEXFS_SUPER_MAGIC |
|
946 | 943 | if (pbuf->f_type == FUTEXFS_SUPER_MAGIC) |
|
947 | 944 | return "futexfs"; |
|
948 | 945 | #endif |
|
949 | 946 | #ifdef HFS_SUPER_MAGIC |
|
950 | 947 | if (pbuf->f_type == HFS_SUPER_MAGIC) |
|
951 | 948 | return "hfs"; |
|
952 | 949 | #endif |
|
953 | 950 | #ifdef HOSTFS_SUPER_MAGIC |
|
954 | 951 | if (pbuf->f_type == HOSTFS_SUPER_MAGIC) |
|
955 | 952 | return "hostfs"; |
|
956 | 953 | #endif |
|
957 | 954 | #ifdef HPFS_SUPER_MAGIC |
|
958 | 955 | if (pbuf->f_type == HPFS_SUPER_MAGIC) |
|
959 | 956 | return "hpfs"; |
|
960 | 957 | #endif |
|
961 | 958 | #ifdef HUGETLBFS_MAGIC |
|
962 | 959 | if (pbuf->f_type == HUGETLBFS_MAGIC) |
|
963 | 960 | return "hugetlbfs"; |
|
964 | 961 | #endif |
|
965 | 962 | #ifdef ISOFS_SUPER_MAGIC |
|
966 | 963 | if (pbuf->f_type == ISOFS_SUPER_MAGIC) |
|
967 | 964 | return "isofs"; |
|
968 | 965 | #endif |
|
969 | 966 | #ifdef JFFS2_SUPER_MAGIC |
|
970 | 967 | if (pbuf->f_type == JFFS2_SUPER_MAGIC) |
|
971 | 968 | return "jffs2"; |
|
972 | 969 | #endif |
|
973 | 970 | #ifdef JFS_SUPER_MAGIC |
|
974 | 971 | if (pbuf->f_type == JFS_SUPER_MAGIC) |
|
975 | 972 | return "jfs"; |
|
976 | 973 | #endif |
|
977 | 974 | #ifdef MINIX_SUPER_MAGIC |
|
978 | 975 | if (pbuf->f_type == MINIX_SUPER_MAGIC) |
|
979 | 976 | return "minix"; |
|
980 | 977 | #endif |
|
981 | 978 | #ifdef MINIX2_SUPER_MAGIC |
|
982 | 979 | if (pbuf->f_type == MINIX2_SUPER_MAGIC) |
|
983 | 980 | return "minix2"; |
|
984 | 981 | #endif |
|
985 | 982 | #ifdef MINIX3_SUPER_MAGIC |
|
986 | 983 | if (pbuf->f_type == MINIX3_SUPER_MAGIC) |
|
987 | 984 | return "minix3"; |
|
988 | 985 | #endif |
|
989 | 986 | #ifdef MQUEUE_MAGIC |
|
990 | 987 | if (pbuf->f_type == MQUEUE_MAGIC) |
|
991 | 988 | return "mqueue"; |
|
992 | 989 | #endif |
|
993 | 990 | #ifdef MSDOS_SUPER_MAGIC |
|
994 | 991 | if (pbuf->f_type == MSDOS_SUPER_MAGIC) |
|
995 | 992 | return "msdos"; |
|
996 | 993 | #endif |
|
997 | 994 | #ifdef NCP_SUPER_MAGIC |
|
998 | 995 | if (pbuf->f_type == NCP_SUPER_MAGIC) |
|
999 | 996 | return "ncp"; |
|
1000 | 997 | #endif |
|
1001 | 998 | #ifdef NFS_SUPER_MAGIC |
|
1002 | 999 | if (pbuf->f_type == NFS_SUPER_MAGIC) |
|
1003 | 1000 | return "nfs"; |
|
1004 | 1001 | #endif |
|
1005 | 1002 | #ifdef NILFS_SUPER_MAGIC |
|
1006 | 1003 | if (pbuf->f_type == NILFS_SUPER_MAGIC) |
|
1007 | 1004 | return "nilfs"; |
|
1008 | 1005 | #endif |
|
1009 | 1006 | #ifdef NTFS_SB_MAGIC |
|
1010 | 1007 | if (pbuf->f_type == NTFS_SB_MAGIC) |
|
1011 | 1008 | return "ntfs-sb"; |
|
1012 | 1009 | #endif |
|
1013 | 1010 | #ifdef OCFS2_SUPER_MAGIC |
|
1014 | 1011 | if (pbuf->f_type == OCFS2_SUPER_MAGIC) |
|
1015 | 1012 | return "ocfs2"; |
|
1016 | 1013 | #endif |
|
1017 | 1014 | #ifdef OPENPROM_SUPER_MAGIC |
|
1018 | 1015 | if (pbuf->f_type == OPENPROM_SUPER_MAGIC) |
|
1019 | 1016 | return "openprom"; |
|
1020 | 1017 | #endif |
|
1021 | 1018 | #ifdef OVERLAYFS_SUPER_MAGIC |
|
1022 | 1019 | if (pbuf->f_type == OVERLAYFS_SUPER_MAGIC) |
|
1023 | 1020 | return "overlay"; |
|
1024 | 1021 | #endif |
|
1025 | 1022 | #ifdef PIPEFS_MAGIC |
|
1026 | 1023 | if (pbuf->f_type == PIPEFS_MAGIC) |
|
1027 | 1024 | return "pipefs"; |
|
1028 | 1025 | #endif |
|
1029 | 1026 | #ifdef PROC_SUPER_MAGIC |
|
1030 | 1027 | if (pbuf->f_type == PROC_SUPER_MAGIC) |
|
1031 | 1028 | return "proc"; |
|
1032 | 1029 | #endif |
|
1033 | 1030 | #ifdef PSTOREFS_MAGIC |
|
1034 | 1031 | if (pbuf->f_type == PSTOREFS_MAGIC) |
|
1035 | 1032 | return "pstorefs"; |
|
1036 | 1033 | #endif |
|
1037 | 1034 | #ifdef QNX4_SUPER_MAGIC |
|
1038 | 1035 | if (pbuf->f_type == QNX4_SUPER_MAGIC) |
|
1039 | 1036 | return "qnx4"; |
|
1040 | 1037 | #endif |
|
1041 | 1038 | #ifdef QNX6_SUPER_MAGIC |
|
1042 | 1039 | if (pbuf->f_type == QNX6_SUPER_MAGIC) |
|
1043 | 1040 | return "qnx6"; |
|
1044 | 1041 | #endif |
|
1045 | 1042 | #ifdef RAMFS_MAGIC |
|
1046 | 1043 | if (pbuf->f_type == RAMFS_MAGIC) |
|
1047 | 1044 | return "ramfs"; |
|
1048 | 1045 | #endif |
|
1049 | 1046 | #ifdef REISERFS_SUPER_MAGIC |
|
1050 | 1047 | if (pbuf->f_type == REISERFS_SUPER_MAGIC) |
|
1051 | 1048 | return "reiserfs"; |
|
1052 | 1049 | #endif |
|
1053 | 1050 | #ifdef ROMFS_MAGIC |
|
1054 | 1051 | if (pbuf->f_type == ROMFS_MAGIC) |
|
1055 | 1052 | return "romfs"; |
|
1056 | 1053 | #endif |
|
1057 | 1054 | #ifdef SECURITYFS_MAGIC |
|
1058 | 1055 | if (pbuf->f_type == SECURITYFS_MAGIC) |
|
1059 | 1056 | return "securityfs"; |
|
1060 | 1057 | #endif |
|
1061 | 1058 | #ifdef SELINUX_MAGIC |
|
1062 | 1059 | if (pbuf->f_type == SELINUX_MAGIC) |
|
1063 | 1060 | return "selinux"; |
|
1064 | 1061 | #endif |
|
1065 | 1062 | #ifdef SMACK_MAGIC |
|
1066 | 1063 | if (pbuf->f_type == SMACK_MAGIC) |
|
1067 | 1064 | return "smack"; |
|
1068 | 1065 | #endif |
|
1069 | 1066 | #ifdef SMB_SUPER_MAGIC |
|
1070 | 1067 | if (pbuf->f_type == SMB_SUPER_MAGIC) |
|
1071 | 1068 | return "smb"; |
|
1072 | 1069 | #endif |
|
1073 | 1070 | #ifdef SOCKFS_MAGIC |
|
1074 | 1071 | if (pbuf->f_type == SOCKFS_MAGIC) |
|
1075 | 1072 | return "sockfs"; |
|
1076 | 1073 | #endif |
|
1077 | 1074 | #ifdef SQUASHFS_MAGIC |
|
1078 | 1075 | if (pbuf->f_type == SQUASHFS_MAGIC) |
|
1079 | 1076 | return "squashfs"; |
|
1080 | 1077 | #endif |
|
1081 | 1078 | #ifdef SYSFS_MAGIC |
|
1082 | 1079 | if (pbuf->f_type == SYSFS_MAGIC) |
|
1083 | 1080 | return "sysfs"; |
|
1084 | 1081 | #endif |
|
1085 | 1082 | #ifdef SYSV2_SUPER_MAGIC |
|
1086 | 1083 | if (pbuf->f_type == SYSV2_SUPER_MAGIC) |
|
1087 | 1084 | return "sysv2"; |
|
1088 | 1085 | #endif |
|
1089 | 1086 | #ifdef SYSV4_SUPER_MAGIC |
|
1090 | 1087 | if (pbuf->f_type == SYSV4_SUPER_MAGIC) |
|
1091 | 1088 | return "sysv4"; |
|
1092 | 1089 | #endif |
|
1093 | 1090 | #ifdef TMPFS_MAGIC |
|
1094 | 1091 | if (pbuf->f_type == TMPFS_MAGIC) |
|
1095 | 1092 | return "tmpfs"; |
|
1096 | 1093 | #endif |
|
1097 | 1094 | #ifdef UDF_SUPER_MAGIC |
|
1098 | 1095 | if (pbuf->f_type == UDF_SUPER_MAGIC) |
|
1099 | 1096 | return "udf"; |
|
1100 | 1097 | #endif |
|
1101 | 1098 | #ifdef UFS_MAGIC |
|
1102 | 1099 | if (pbuf->f_type == UFS_MAGIC) |
|
1103 | 1100 | return "ufs"; |
|
1104 | 1101 | #endif |
|
1105 | 1102 | #ifdef USBDEVICE_SUPER_MAGIC |
|
1106 | 1103 | if (pbuf->f_type == USBDEVICE_SUPER_MAGIC) |
|
1107 | 1104 | return "usbdevice"; |
|
1108 | 1105 | #endif |
|
1109 | 1106 | #ifdef V9FS_MAGIC |
|
1110 | 1107 | if (pbuf->f_type == V9FS_MAGIC) |
|
1111 | 1108 | return "v9fs"; |
|
1112 | 1109 | #endif |
|
1113 | 1110 | #ifdef VXFS_SUPER_MAGIC |
|
1114 | 1111 | if (pbuf->f_type == VXFS_SUPER_MAGIC) |
|
1115 | 1112 | return "vxfs"; |
|
1116 | 1113 | #endif |
|
1117 | 1114 | #ifdef XENFS_SUPER_MAGIC |
|
1118 | 1115 | if (pbuf->f_type == XENFS_SUPER_MAGIC) |
|
1119 | 1116 | return "xenfs"; |
|
1120 | 1117 | #endif |
|
1121 | 1118 | #ifdef XENIX_SUPER_MAGIC |
|
1122 | 1119 | if (pbuf->f_type == XENIX_SUPER_MAGIC) |
|
1123 | 1120 | return "xenix"; |
|
1124 | 1121 | #endif |
|
1125 | 1122 | #ifdef XFS_SUPER_MAGIC |
|
1126 | 1123 | if (pbuf->f_type == XFS_SUPER_MAGIC) |
|
1127 | 1124 | return "xfs"; |
|
1128 | 1125 | #endif |
|
1129 | 1126 | /* End of Linux filesystems */ |
|
1130 | 1127 | return NULL; |
|
1131 | 1128 | } |
|
1132 | 1129 | #endif /* def HAVE_LINUX_STATFS */ |
|
1133 | 1130 | |
|
1134 | 1131 | #if defined(HAVE_BSD_STATFS) || defined(HAVE_LINUX_STATFS) |
|
1135 | 1132 | /* given a directory path, return filesystem type name (best-effort) */ |
|
1136 | 1133 | static PyObject *getfstype(PyObject *self, PyObject *args) |
|
1137 | 1134 | { |
|
1138 | 1135 | const char *path = NULL; |
|
1139 | 1136 | struct statfs buf; |
|
1140 | 1137 | int r; |
|
1141 | 1138 | if (!PyArg_ParseTuple(args, PY23("s", "y"), &path)) |
|
1142 | 1139 | return NULL; |
|
1143 | 1140 | |
|
1144 | 1141 | memset(&buf, 0, sizeof(buf)); |
|
1145 | 1142 | r = statfs(path, &buf); |
|
1146 | 1143 | if (r != 0) |
|
1147 | 1144 | return PyErr_SetFromErrno(PyExc_OSError); |
|
1148 | 1145 | return Py_BuildValue(PY23("s", "y"), describefstype(&buf)); |
|
1149 | 1146 | } |
|
1150 | 1147 | #endif /* defined(HAVE_LINUX_STATFS) || defined(HAVE_BSD_STATFS) */ |
|
1151 | 1148 | |
|
1152 | 1149 | #if defined(HAVE_BSD_STATFS) |
|
1153 | 1150 | /* given a directory path, return filesystem mount point (best-effort) */ |
|
1154 | 1151 | static PyObject *getfsmountpoint(PyObject *self, PyObject *args) |
|
1155 | 1152 | { |
|
1156 | 1153 | const char *path = NULL; |
|
1157 | 1154 | struct statfs buf; |
|
1158 | 1155 | int r; |
|
1159 | 1156 | if (!PyArg_ParseTuple(args, PY23("s", "y"), &path)) |
|
1160 | 1157 | return NULL; |
|
1161 | 1158 | |
|
1162 | 1159 | memset(&buf, 0, sizeof(buf)); |
|
1163 | 1160 | r = statfs(path, &buf); |
|
1164 | 1161 | if (r != 0) |
|
1165 | 1162 | return PyErr_SetFromErrno(PyExc_OSError); |
|
1166 | 1163 | return Py_BuildValue(PY23("s", "y"), buf.f_mntonname); |
|
1167 | 1164 | } |
|
1168 | 1165 | #endif /* defined(HAVE_BSD_STATFS) */ |
|
1169 | 1166 | |
|
1170 | 1167 | static PyObject *unblocksignal(PyObject *self, PyObject *args) |
|
1171 | 1168 | { |
|
1172 | 1169 | int sig = 0; |
|
1173 | 1170 | sigset_t set; |
|
1174 | 1171 | int r; |
|
1175 | 1172 | if (!PyArg_ParseTuple(args, "i", &sig)) |
|
1176 | 1173 | return NULL; |
|
1177 | 1174 | r = sigemptyset(&set); |
|
1178 | 1175 | if (r != 0) |
|
1179 | 1176 | return PyErr_SetFromErrno(PyExc_OSError); |
|
1180 | 1177 | r = sigaddset(&set, sig); |
|
1181 | 1178 | if (r != 0) |
|
1182 | 1179 | return PyErr_SetFromErrno(PyExc_OSError); |
|
1183 | 1180 | r = sigprocmask(SIG_UNBLOCK, &set, NULL); |
|
1184 | 1181 | if (r != 0) |
|
1185 | 1182 | return PyErr_SetFromErrno(PyExc_OSError); |
|
1186 | 1183 | Py_RETURN_NONE; |
|
1187 | 1184 | } |
|
1188 | 1185 | |
|
1189 | 1186 | #endif /* ndef _WIN32 */ |
|
1190 | 1187 | |
|
1191 | 1188 | static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs) |
|
1192 | 1189 | { |
|
1193 | 1190 | PyObject *statobj = NULL; /* initialize - optional arg */ |
|
1194 | 1191 | PyObject *skipobj = NULL; /* initialize - optional arg */ |
|
1195 | 1192 | char *path, *skip = NULL; |
|
1196 | 1193 | Py_ssize_t plen; |
|
1197 | 1194 | int wantstat; |
|
1198 | 1195 | |
|
1199 | 1196 | static char *kwlist[] = {"path", "stat", "skip", NULL}; |
|
1200 | 1197 | |
|
1201 | 1198 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, PY23("s#|OO:listdir", |
|
1202 | 1199 | "y#|OO:listdir"), |
|
1203 | 1200 | kwlist, &path, &plen, &statobj, &skipobj)) |
|
1204 | 1201 | return NULL; |
|
1205 | 1202 | |
|
1206 | 1203 | wantstat = statobj && PyObject_IsTrue(statobj); |
|
1207 | 1204 | |
|
1208 | 1205 | if (skipobj && skipobj != Py_None) { |
|
1209 | 1206 | skip = PyBytes_AsString(skipobj); |
|
1210 | 1207 | if (!skip) |
|
1211 | 1208 | return NULL; |
|
1212 | 1209 | } |
|
1213 | 1210 | |
|
1214 | 1211 | return _listdir(path, plen, wantstat, skip); |
|
1215 | 1212 | } |
|
1216 | 1213 | |
|
1217 | 1214 | #ifdef _WIN32 |
|
1218 | 1215 | static PyObject *posixfile(PyObject *self, PyObject *args, PyObject *kwds) |
|
1219 | 1216 | { |
|
1220 | 1217 | static char *kwlist[] = {"name", "mode", "buffering", NULL}; |
|
1221 | 1218 | PyObject *file_obj = NULL; |
|
1222 | 1219 | char *name = NULL; |
|
1223 | 1220 | char *mode = "rb"; |
|
1224 | 1221 | DWORD access = 0; |
|
1225 | 1222 | DWORD creation; |
|
1226 | 1223 | HANDLE handle; |
|
1227 | 1224 | int fd, flags = 0; |
|
1228 | 1225 | int bufsize = -1; |
|
1229 | 1226 | char m0, m1, m2; |
|
1230 | 1227 | char fpmode[4]; |
|
1231 | 1228 | int fppos = 0; |
|
1232 | 1229 | int plus; |
|
1233 | 1230 | #ifndef IS_PY3K |
|
1234 | 1231 | FILE *fp; |
|
1235 | 1232 | #endif |
|
1236 | 1233 | |
|
1237 | 1234 | if (!PyArg_ParseTupleAndKeywords(args, kwds, PY23("et|si:posixfile", |
|
1238 | 1235 | "et|yi:posixfile"), |
|
1239 | 1236 | kwlist, |
|
1240 | 1237 | Py_FileSystemDefaultEncoding, |
|
1241 | 1238 | &name, &mode, &bufsize)) |
|
1242 | 1239 | return NULL; |
|
1243 | 1240 | |
|
1244 | 1241 | m0 = mode[0]; |
|
1245 | 1242 | m1 = m0 ? mode[1] : '\0'; |
|
1246 | 1243 | m2 = m1 ? mode[2] : '\0'; |
|
1247 | 1244 | plus = m1 == '+' || m2 == '+'; |
|
1248 | 1245 | |
|
1249 | 1246 | fpmode[fppos++] = m0; |
|
1250 | 1247 | if (m1 == 'b' || m2 == 'b') { |
|
1251 | 1248 | flags = _O_BINARY; |
|
1252 | 1249 | fpmode[fppos++] = 'b'; |
|
1253 | 1250 | } |
|
1254 | 1251 | else |
|
1255 | 1252 | flags = _O_TEXT; |
|
1256 | 1253 | if (m0 == 'r' && !plus) { |
|
1257 | 1254 | flags |= _O_RDONLY; |
|
1258 | 1255 | access = GENERIC_READ; |
|
1259 | 1256 | } else { |
|
1260 | 1257 | /* |
|
1261 | 1258 | work around http://support.microsoft.com/kb/899149 and |
|
1262 | 1259 | set _O_RDWR for 'w' and 'a', even if mode has no '+' |
|
1263 | 1260 | */ |
|
1264 | 1261 | flags |= _O_RDWR; |
|
1265 | 1262 | access = GENERIC_READ | GENERIC_WRITE; |
|
1266 | 1263 | fpmode[fppos++] = '+'; |
|
1267 | 1264 | } |
|
1268 | 1265 | fpmode[fppos++] = '\0'; |
|
1269 | 1266 | |
|
1270 | 1267 | switch (m0) { |
|
1271 | 1268 | case 'r': |
|
1272 | 1269 | creation = OPEN_EXISTING; |
|
1273 | 1270 | break; |
|
1274 | 1271 | case 'w': |
|
1275 | 1272 | creation = CREATE_ALWAYS; |
|
1276 | 1273 | break; |
|
1277 | 1274 | case 'a': |
|
1278 | 1275 | creation = OPEN_ALWAYS; |
|
1279 | 1276 | flags |= _O_APPEND; |
|
1280 | 1277 | break; |
|
1281 | 1278 | default: |
|
1282 | 1279 | PyErr_Format(PyExc_ValueError, |
|
1283 | 1280 | "mode string must begin with one of 'r', 'w', " |
|
1284 | 1281 | "or 'a', not '%c'", m0); |
|
1285 | 1282 | goto bail; |
|
1286 | 1283 | } |
|
1287 | 1284 | |
|
1288 | 1285 | handle = CreateFile(name, access, |
|
1289 | 1286 | FILE_SHARE_READ | FILE_SHARE_WRITE | |
|
1290 | 1287 | FILE_SHARE_DELETE, |
|
1291 | 1288 | NULL, |
|
1292 | 1289 | creation, |
|
1293 | 1290 | FILE_ATTRIBUTE_NORMAL, |
|
1294 | 1291 | 0); |
|
1295 | 1292 | |
|
1296 | 1293 | if (handle == INVALID_HANDLE_VALUE) { |
|
1297 | 1294 | PyErr_SetFromWindowsErrWithFilename(GetLastError(), name); |
|
1298 | 1295 | goto bail; |
|
1299 | 1296 | } |
|
1300 | 1297 | |
|
1301 | 1298 | fd = _open_osfhandle((intptr_t)handle, flags); |
|
1302 | 1299 | |
|
1303 | 1300 | if (fd == -1) { |
|
1304 | 1301 | CloseHandle(handle); |
|
1305 | 1302 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); |
|
1306 | 1303 | goto bail; |
|
1307 | 1304 | } |
|
1308 | 1305 | #ifndef IS_PY3K |
|
1309 | 1306 | fp = _fdopen(fd, fpmode); |
|
1310 | 1307 | if (fp == NULL) { |
|
1311 | 1308 | _close(fd); |
|
1312 | 1309 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); |
|
1313 | 1310 | goto bail; |
|
1314 | 1311 | } |
|
1315 | 1312 | |
|
1316 | 1313 | file_obj = PyFile_FromFile(fp, name, mode, fclose); |
|
1317 | 1314 | if (file_obj == NULL) { |
|
1318 | 1315 | fclose(fp); |
|
1319 | 1316 | goto bail; |
|
1320 | 1317 | } |
|
1321 | 1318 | |
|
1322 | 1319 | PyFile_SetBufSize(file_obj, bufsize); |
|
1323 | 1320 | #else |
|
1324 | 1321 | file_obj = PyFile_FromFd(fd, name, mode, bufsize, NULL, NULL, NULL, 1); |
|
1325 | 1322 | if (file_obj == NULL) |
|
1326 | 1323 | goto bail; |
|
1327 | 1324 | #endif |
|
1328 | 1325 | bail: |
|
1329 | 1326 | PyMem_Free(name); |
|
1330 | 1327 | return file_obj; |
|
1331 | 1328 | } |
|
1332 | 1329 | #endif |
|
1333 | 1330 | |
|
1334 | 1331 | #ifdef __APPLE__ |
|
1335 | 1332 | #include <ApplicationServices/ApplicationServices.h> |
|
1336 | 1333 | |
|
1337 | 1334 | static PyObject *isgui(PyObject *self) |
|
1338 | 1335 | { |
|
1339 | 1336 | CFDictionaryRef dict = CGSessionCopyCurrentDictionary(); |
|
1340 | 1337 | |
|
1341 | 1338 | if (dict != NULL) { |
|
1342 | 1339 | CFRelease(dict); |
|
1343 | 1340 | Py_RETURN_TRUE; |
|
1344 | 1341 | } else { |
|
1345 | 1342 | Py_RETURN_FALSE; |
|
1346 | 1343 | } |
|
1347 | 1344 | } |
|
1348 | 1345 | #endif |
|
1349 | 1346 | |
|
1350 | 1347 | static char osutil_doc[] = "Native operating system services."; |
|
1351 | 1348 | |
|
1352 | 1349 | static PyMethodDef methods[] = { |
|
1353 | 1350 | {"listdir", (PyCFunction)listdir, METH_VARARGS | METH_KEYWORDS, |
|
1354 | 1351 | "list a directory\n"}, |
|
1355 | 1352 | #ifdef _WIN32 |
|
1356 | 1353 | {"posixfile", (PyCFunction)posixfile, METH_VARARGS | METH_KEYWORDS, |
|
1357 | 1354 | "Open a file with POSIX-like semantics.\n" |
|
1358 | 1355 | "On error, this function may raise either a WindowsError or an IOError."}, |
|
1359 | 1356 | #else |
|
1360 | 1357 | {"statfiles", (PyCFunction)statfiles, METH_VARARGS | METH_KEYWORDS, |
|
1361 | 1358 | "stat a series of files or symlinks\n" |
|
1362 | 1359 | "Returns None for non-existent entries and entries of other types.\n"}, |
|
1363 | 1360 | #ifdef CMSG_LEN |
|
1364 | 1361 | {"recvfds", (PyCFunction)recvfds, METH_VARARGS, |
|
1365 | 1362 | "receive list of file descriptors via socket\n"}, |
|
1366 | 1363 | #endif |
|
1367 | 1364 | #ifndef SETPROCNAME_USE_NONE |
|
1368 | 1365 | {"setprocname", (PyCFunction)setprocname, METH_VARARGS, |
|
1369 | 1366 | "set process title (best-effort)\n"}, |
|
1370 | 1367 | #endif |
|
1371 | 1368 | #if defined(HAVE_BSD_STATFS) || defined(HAVE_LINUX_STATFS) |
|
1372 | 1369 | {"getfstype", (PyCFunction)getfstype, METH_VARARGS, |
|
1373 | 1370 | "get filesystem type (best-effort)\n"}, |
|
1374 | 1371 | #endif |
|
1375 | 1372 | #if defined(HAVE_BSD_STATFS) |
|
1376 | 1373 | {"getfsmountpoint", (PyCFunction)getfsmountpoint, METH_VARARGS, |
|
1377 | 1374 | "get filesystem mount point (best-effort)\n"}, |
|
1378 | 1375 | #endif |
|
1379 | 1376 | {"unblocksignal", (PyCFunction)unblocksignal, METH_VARARGS, |
|
1380 | 1377 | "change signal mask to unblock a given signal\n"}, |
|
1381 | 1378 | #endif /* ndef _WIN32 */ |
|
1382 | 1379 | #ifdef __APPLE__ |
|
1383 | 1380 | { |
|
1384 | 1381 | "isgui", (PyCFunction)isgui, METH_NOARGS, |
|
1385 | 1382 | "Is a CoreGraphics session available?" |
|
1386 | 1383 | }, |
|
1387 | 1384 | #endif |
|
1388 | 1385 | {NULL, NULL} |
|
1389 | 1386 | }; |
|
1390 | 1387 | |
|
1391 | 1388 | static const int version = 4; |
|
1392 | 1389 | |
|
1393 | 1390 | #ifdef IS_PY3K |
|
1394 | 1391 | static struct PyModuleDef osutil_module = { |
|
1395 | 1392 | PyModuleDef_HEAD_INIT, |
|
1396 | 1393 | "osutil", |
|
1397 | 1394 | osutil_doc, |
|
1398 | 1395 | -1, |
|
1399 | 1396 | methods |
|
1400 | 1397 | }; |
|
1401 | 1398 | |
|
1402 | 1399 | PyMODINIT_FUNC PyInit_osutil(void) |
|
1403 | 1400 | { |
|
1404 | 1401 | PyObject *m; |
|
1405 | 1402 | if (PyType_Ready(&listdir_stat_type) < 0) |
|
1406 | 1403 | return NULL; |
|
1407 | 1404 | |
|
1408 | 1405 | m = PyModule_Create(&osutil_module); |
|
1409 | 1406 | PyModule_AddIntConstant(m, "version", version); |
|
1410 | 1407 | return m; |
|
1411 | 1408 | } |
|
1412 | 1409 | #else |
|
1413 | 1410 | PyMODINIT_FUNC initosutil(void) |
|
1414 | 1411 | { |
|
1415 | 1412 | PyObject *m; |
|
1416 | 1413 | if (PyType_Ready(&listdir_stat_type) == -1) |
|
1417 | 1414 | return; |
|
1418 | 1415 | |
|
1419 | 1416 | m = Py_InitModule3("osutil", methods, osutil_doc); |
|
1420 | 1417 | PyModule_AddIntConstant(m, "version", version); |
|
1421 | 1418 | } |
|
1422 | 1419 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now