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