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