##// END OF EJS Templates
cext: remove inline rewriting of argv...
Gregory Szorc -
r49677:3aa1b7de default
parent child Browse files
Show More
@@ -1,1389 +1,1347 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 #elif (defined(__linux__) || defined(__APPLE__)) && PY_MAJOR_VERSION == 2
763 /* rewrite the argv buffer in place - works in Linux and OS X. Py_GetArgcArgv
764 * in Python 3 returns the copied wchar_t **argv, thus unsupported. */
765 #define SETPROCNAME_USE_ARGVREWRITE
766 762 #else
767 763 #define SETPROCNAME_USE_NONE
768 764 #endif
769 765 #endif /* ndef SETPROCNAME_USE_NONE */
770 766
771 767 #ifndef SETPROCNAME_USE_NONE
772 768 static PyObject *setprocname(PyObject *self, PyObject *args)
773 769 {
774 770 const char *name = NULL;
775 771 if (!PyArg_ParseTuple(args, "y", &name))
776 772 return NULL;
777 773
778 774 #if defined(SETPROCNAME_USE_SETPROCTITLE)
779 775 setproctitle("%s", name);
780 #elif defined(SETPROCNAME_USE_ARGVREWRITE)
781 {
782 static char *argvstart = NULL;
783 static size_t argvsize = 0;
784 if (argvstart == NULL) {
785 int argc = 0, i;
786 char **argv = NULL;
787 char *argvend;
788 extern void Py_GetArgcArgv(int *argc, char ***argv);
789 Py_GetArgcArgv(&argc, &argv);
790 /* Py_GetArgcArgv may not do much if a custom python
791 * launcher is used that doesn't record the information
792 * it needs. Let's handle this gracefully instead of
793 * segfaulting. */
794 if (argv != NULL)
795 argvend = argvstart = argv[0];
796 else
797 argvend = argvstart = NULL;
798
799 /* Check the memory we can use. Typically, argv[i] and
800 * argv[i + 1] are continuous. */
801 for (i = 0; i < argc; ++i) {
802 size_t len;
803 if (argv[i] > argvend || argv[i] < argvstart)
804 break; /* not continuous */
805 len = strlen(argv[i]);
806 argvend = argv[i] + len + 1 /* '\0' */;
807 }
808 if (argvend > argvstart) /* sanity check */
809 argvsize = argvend - argvstart;
810 }
811
812 if (argvstart && argvsize > 1) {
813 int n = snprintf(argvstart, argvsize, "%s", name);
814 if (n >= 0 && (size_t)n < argvsize)
815 memset(argvstart + n, 0, argvsize - n);
816 }
817 }
818 776 #endif
819 777
820 778 Py_RETURN_NONE;
821 779 }
822 780 #endif /* ndef SETPROCNAME_USE_NONE */
823 781
824 782 #if defined(HAVE_BSD_STATFS)
825 783 static const char *describefstype(const struct statfs *pbuf)
826 784 {
827 785 /* BSD or OSX provides a f_fstypename field */
828 786 return pbuf->f_fstypename;
829 787 }
830 788 #elif defined(HAVE_LINUX_STATFS)
831 789 static const char *describefstype(const struct statfs *pbuf)
832 790 {
833 791 /* Begin of Linux filesystems */
834 792 #ifdef ADFS_SUPER_MAGIC
835 793 if (pbuf->f_type == ADFS_SUPER_MAGIC)
836 794 return "adfs";
837 795 #endif
838 796 #ifdef AFFS_SUPER_MAGIC
839 797 if (pbuf->f_type == AFFS_SUPER_MAGIC)
840 798 return "affs";
841 799 #endif
842 800 #ifdef AUTOFS_SUPER_MAGIC
843 801 if (pbuf->f_type == AUTOFS_SUPER_MAGIC)
844 802 return "autofs";
845 803 #endif
846 804 #ifdef BDEVFS_MAGIC
847 805 if (pbuf->f_type == BDEVFS_MAGIC)
848 806 return "bdevfs";
849 807 #endif
850 808 #ifdef BEFS_SUPER_MAGIC
851 809 if (pbuf->f_type == BEFS_SUPER_MAGIC)
852 810 return "befs";
853 811 #endif
854 812 #ifdef BFS_MAGIC
855 813 if (pbuf->f_type == BFS_MAGIC)
856 814 return "bfs";
857 815 #endif
858 816 #ifdef BINFMTFS_MAGIC
859 817 if (pbuf->f_type == BINFMTFS_MAGIC)
860 818 return "binfmtfs";
861 819 #endif
862 820 #ifdef BTRFS_SUPER_MAGIC
863 821 if (pbuf->f_type == BTRFS_SUPER_MAGIC)
864 822 return "btrfs";
865 823 #endif
866 824 #ifdef CGROUP_SUPER_MAGIC
867 825 if (pbuf->f_type == CGROUP_SUPER_MAGIC)
868 826 return "cgroup";
869 827 #endif
870 828 #ifdef CIFS_MAGIC_NUMBER
871 829 if (pbuf->f_type == CIFS_MAGIC_NUMBER)
872 830 return "cifs";
873 831 #endif
874 832 #ifdef CODA_SUPER_MAGIC
875 833 if (pbuf->f_type == CODA_SUPER_MAGIC)
876 834 return "coda";
877 835 #endif
878 836 #ifdef COH_SUPER_MAGIC
879 837 if (pbuf->f_type == COH_SUPER_MAGIC)
880 838 return "coh";
881 839 #endif
882 840 #ifdef CRAMFS_MAGIC
883 841 if (pbuf->f_type == CRAMFS_MAGIC)
884 842 return "cramfs";
885 843 #endif
886 844 #ifdef DEBUGFS_MAGIC
887 845 if (pbuf->f_type == DEBUGFS_MAGIC)
888 846 return "debugfs";
889 847 #endif
890 848 #ifdef DEVFS_SUPER_MAGIC
891 849 if (pbuf->f_type == DEVFS_SUPER_MAGIC)
892 850 return "devfs";
893 851 #endif
894 852 #ifdef DEVPTS_SUPER_MAGIC
895 853 if (pbuf->f_type == DEVPTS_SUPER_MAGIC)
896 854 return "devpts";
897 855 #endif
898 856 #ifdef EFIVARFS_MAGIC
899 857 if (pbuf->f_type == EFIVARFS_MAGIC)
900 858 return "efivarfs";
901 859 #endif
902 860 #ifdef EFS_SUPER_MAGIC
903 861 if (pbuf->f_type == EFS_SUPER_MAGIC)
904 862 return "efs";
905 863 #endif
906 864 #ifdef EXT_SUPER_MAGIC
907 865 if (pbuf->f_type == EXT_SUPER_MAGIC)
908 866 return "ext";
909 867 #endif
910 868 #ifdef EXT2_OLD_SUPER_MAGIC
911 869 if (pbuf->f_type == EXT2_OLD_SUPER_MAGIC)
912 870 return "ext2";
913 871 #endif
914 872 #ifdef EXT2_SUPER_MAGIC
915 873 if (pbuf->f_type == EXT2_SUPER_MAGIC)
916 874 return "ext2";
917 875 #endif
918 876 #ifdef EXT3_SUPER_MAGIC
919 877 if (pbuf->f_type == EXT3_SUPER_MAGIC)
920 878 return "ext3";
921 879 #endif
922 880 #ifdef EXT4_SUPER_MAGIC
923 881 if (pbuf->f_type == EXT4_SUPER_MAGIC)
924 882 return "ext4";
925 883 #endif
926 884 #ifdef F2FS_SUPER_MAGIC
927 885 if (pbuf->f_type == F2FS_SUPER_MAGIC)
928 886 return "f2fs";
929 887 #endif
930 888 #ifdef FUSE_SUPER_MAGIC
931 889 if (pbuf->f_type == FUSE_SUPER_MAGIC)
932 890 return "fuse";
933 891 #endif
934 892 #ifdef FUTEXFS_SUPER_MAGIC
935 893 if (pbuf->f_type == FUTEXFS_SUPER_MAGIC)
936 894 return "futexfs";
937 895 #endif
938 896 #ifdef HFS_SUPER_MAGIC
939 897 if (pbuf->f_type == HFS_SUPER_MAGIC)
940 898 return "hfs";
941 899 #endif
942 900 #ifdef HOSTFS_SUPER_MAGIC
943 901 if (pbuf->f_type == HOSTFS_SUPER_MAGIC)
944 902 return "hostfs";
945 903 #endif
946 904 #ifdef HPFS_SUPER_MAGIC
947 905 if (pbuf->f_type == HPFS_SUPER_MAGIC)
948 906 return "hpfs";
949 907 #endif
950 908 #ifdef HUGETLBFS_MAGIC
951 909 if (pbuf->f_type == HUGETLBFS_MAGIC)
952 910 return "hugetlbfs";
953 911 #endif
954 912 #ifdef ISOFS_SUPER_MAGIC
955 913 if (pbuf->f_type == ISOFS_SUPER_MAGIC)
956 914 return "isofs";
957 915 #endif
958 916 #ifdef JFFS2_SUPER_MAGIC
959 917 if (pbuf->f_type == JFFS2_SUPER_MAGIC)
960 918 return "jffs2";
961 919 #endif
962 920 #ifdef JFS_SUPER_MAGIC
963 921 if (pbuf->f_type == JFS_SUPER_MAGIC)
964 922 return "jfs";
965 923 #endif
966 924 #ifdef MINIX_SUPER_MAGIC
967 925 if (pbuf->f_type == MINIX_SUPER_MAGIC)
968 926 return "minix";
969 927 #endif
970 928 #ifdef MINIX2_SUPER_MAGIC
971 929 if (pbuf->f_type == MINIX2_SUPER_MAGIC)
972 930 return "minix2";
973 931 #endif
974 932 #ifdef MINIX3_SUPER_MAGIC
975 933 if (pbuf->f_type == MINIX3_SUPER_MAGIC)
976 934 return "minix3";
977 935 #endif
978 936 #ifdef MQUEUE_MAGIC
979 937 if (pbuf->f_type == MQUEUE_MAGIC)
980 938 return "mqueue";
981 939 #endif
982 940 #ifdef MSDOS_SUPER_MAGIC
983 941 if (pbuf->f_type == MSDOS_SUPER_MAGIC)
984 942 return "msdos";
985 943 #endif
986 944 #ifdef NCP_SUPER_MAGIC
987 945 if (pbuf->f_type == NCP_SUPER_MAGIC)
988 946 return "ncp";
989 947 #endif
990 948 #ifdef NFS_SUPER_MAGIC
991 949 if (pbuf->f_type == NFS_SUPER_MAGIC)
992 950 return "nfs";
993 951 #endif
994 952 #ifdef NILFS_SUPER_MAGIC
995 953 if (pbuf->f_type == NILFS_SUPER_MAGIC)
996 954 return "nilfs";
997 955 #endif
998 956 #ifdef NTFS_SB_MAGIC
999 957 if (pbuf->f_type == NTFS_SB_MAGIC)
1000 958 return "ntfs-sb";
1001 959 #endif
1002 960 #ifdef OCFS2_SUPER_MAGIC
1003 961 if (pbuf->f_type == OCFS2_SUPER_MAGIC)
1004 962 return "ocfs2";
1005 963 #endif
1006 964 #ifdef OPENPROM_SUPER_MAGIC
1007 965 if (pbuf->f_type == OPENPROM_SUPER_MAGIC)
1008 966 return "openprom";
1009 967 #endif
1010 968 #ifdef OVERLAYFS_SUPER_MAGIC
1011 969 if (pbuf->f_type == OVERLAYFS_SUPER_MAGIC)
1012 970 return "overlay";
1013 971 #endif
1014 972 #ifdef PIPEFS_MAGIC
1015 973 if (pbuf->f_type == PIPEFS_MAGIC)
1016 974 return "pipefs";
1017 975 #endif
1018 976 #ifdef PROC_SUPER_MAGIC
1019 977 if (pbuf->f_type == PROC_SUPER_MAGIC)
1020 978 return "proc";
1021 979 #endif
1022 980 #ifdef PSTOREFS_MAGIC
1023 981 if (pbuf->f_type == PSTOREFS_MAGIC)
1024 982 return "pstorefs";
1025 983 #endif
1026 984 #ifdef QNX4_SUPER_MAGIC
1027 985 if (pbuf->f_type == QNX4_SUPER_MAGIC)
1028 986 return "qnx4";
1029 987 #endif
1030 988 #ifdef QNX6_SUPER_MAGIC
1031 989 if (pbuf->f_type == QNX6_SUPER_MAGIC)
1032 990 return "qnx6";
1033 991 #endif
1034 992 #ifdef RAMFS_MAGIC
1035 993 if (pbuf->f_type == RAMFS_MAGIC)
1036 994 return "ramfs";
1037 995 #endif
1038 996 #ifdef REISERFS_SUPER_MAGIC
1039 997 if (pbuf->f_type == REISERFS_SUPER_MAGIC)
1040 998 return "reiserfs";
1041 999 #endif
1042 1000 #ifdef ROMFS_MAGIC
1043 1001 if (pbuf->f_type == ROMFS_MAGIC)
1044 1002 return "romfs";
1045 1003 #endif
1046 1004 #ifdef SECURITYFS_MAGIC
1047 1005 if (pbuf->f_type == SECURITYFS_MAGIC)
1048 1006 return "securityfs";
1049 1007 #endif
1050 1008 #ifdef SELINUX_MAGIC
1051 1009 if (pbuf->f_type == SELINUX_MAGIC)
1052 1010 return "selinux";
1053 1011 #endif
1054 1012 #ifdef SMACK_MAGIC
1055 1013 if (pbuf->f_type == SMACK_MAGIC)
1056 1014 return "smack";
1057 1015 #endif
1058 1016 #ifdef SMB_SUPER_MAGIC
1059 1017 if (pbuf->f_type == SMB_SUPER_MAGIC)
1060 1018 return "smb";
1061 1019 #endif
1062 1020 #ifdef SOCKFS_MAGIC
1063 1021 if (pbuf->f_type == SOCKFS_MAGIC)
1064 1022 return "sockfs";
1065 1023 #endif
1066 1024 #ifdef SQUASHFS_MAGIC
1067 1025 if (pbuf->f_type == SQUASHFS_MAGIC)
1068 1026 return "squashfs";
1069 1027 #endif
1070 1028 #ifdef SYSFS_MAGIC
1071 1029 if (pbuf->f_type == SYSFS_MAGIC)
1072 1030 return "sysfs";
1073 1031 #endif
1074 1032 #ifdef SYSV2_SUPER_MAGIC
1075 1033 if (pbuf->f_type == SYSV2_SUPER_MAGIC)
1076 1034 return "sysv2";
1077 1035 #endif
1078 1036 #ifdef SYSV4_SUPER_MAGIC
1079 1037 if (pbuf->f_type == SYSV4_SUPER_MAGIC)
1080 1038 return "sysv4";
1081 1039 #endif
1082 1040 #ifdef TMPFS_MAGIC
1083 1041 if (pbuf->f_type == TMPFS_MAGIC)
1084 1042 return "tmpfs";
1085 1043 #endif
1086 1044 #ifdef UDF_SUPER_MAGIC
1087 1045 if (pbuf->f_type == UDF_SUPER_MAGIC)
1088 1046 return "udf";
1089 1047 #endif
1090 1048 #ifdef UFS_MAGIC
1091 1049 if (pbuf->f_type == UFS_MAGIC)
1092 1050 return "ufs";
1093 1051 #endif
1094 1052 #ifdef USBDEVICE_SUPER_MAGIC
1095 1053 if (pbuf->f_type == USBDEVICE_SUPER_MAGIC)
1096 1054 return "usbdevice";
1097 1055 #endif
1098 1056 #ifdef V9FS_MAGIC
1099 1057 if (pbuf->f_type == V9FS_MAGIC)
1100 1058 return "v9fs";
1101 1059 #endif
1102 1060 #ifdef VXFS_SUPER_MAGIC
1103 1061 if (pbuf->f_type == VXFS_SUPER_MAGIC)
1104 1062 return "vxfs";
1105 1063 #endif
1106 1064 #ifdef XENFS_SUPER_MAGIC
1107 1065 if (pbuf->f_type == XENFS_SUPER_MAGIC)
1108 1066 return "xenfs";
1109 1067 #endif
1110 1068 #ifdef XENIX_SUPER_MAGIC
1111 1069 if (pbuf->f_type == XENIX_SUPER_MAGIC)
1112 1070 return "xenix";
1113 1071 #endif
1114 1072 #ifdef XFS_SUPER_MAGIC
1115 1073 if (pbuf->f_type == XFS_SUPER_MAGIC)
1116 1074 return "xfs";
1117 1075 #endif
1118 1076 /* End of Linux filesystems */
1119 1077 return NULL;
1120 1078 }
1121 1079 #endif /* def HAVE_LINUX_STATFS */
1122 1080
1123 1081 #if defined(HAVE_BSD_STATFS) || defined(HAVE_LINUX_STATFS)
1124 1082 /* given a directory path, return filesystem type name (best-effort) */
1125 1083 static PyObject *getfstype(PyObject *self, PyObject *args)
1126 1084 {
1127 1085 const char *path = NULL;
1128 1086 struct statfs buf;
1129 1087 int r;
1130 1088 if (!PyArg_ParseTuple(args, "y", &path))
1131 1089 return NULL;
1132 1090
1133 1091 memset(&buf, 0, sizeof(buf));
1134 1092 r = statfs(path, &buf);
1135 1093 if (r != 0)
1136 1094 return PyErr_SetFromErrno(PyExc_OSError);
1137 1095 return Py_BuildValue("y", describefstype(&buf));
1138 1096 }
1139 1097 #endif /* defined(HAVE_LINUX_STATFS) || defined(HAVE_BSD_STATFS) */
1140 1098
1141 1099 #if defined(HAVE_BSD_STATFS)
1142 1100 /* given a directory path, return filesystem mount point (best-effort) */
1143 1101 static PyObject *getfsmountpoint(PyObject *self, PyObject *args)
1144 1102 {
1145 1103 const char *path = NULL;
1146 1104 struct statfs buf;
1147 1105 int r;
1148 1106 if (!PyArg_ParseTuple(args, "y", &path))
1149 1107 return NULL;
1150 1108
1151 1109 memset(&buf, 0, sizeof(buf));
1152 1110 r = statfs(path, &buf);
1153 1111 if (r != 0)
1154 1112 return PyErr_SetFromErrno(PyExc_OSError);
1155 1113 return Py_BuildValue("y", buf.f_mntonname);
1156 1114 }
1157 1115 #endif /* defined(HAVE_BSD_STATFS) */
1158 1116
1159 1117 static PyObject *unblocksignal(PyObject *self, PyObject *args)
1160 1118 {
1161 1119 int sig = 0;
1162 1120 sigset_t set;
1163 1121 int r;
1164 1122 if (!PyArg_ParseTuple(args, "i", &sig))
1165 1123 return NULL;
1166 1124 r = sigemptyset(&set);
1167 1125 if (r != 0)
1168 1126 return PyErr_SetFromErrno(PyExc_OSError);
1169 1127 r = sigaddset(&set, sig);
1170 1128 if (r != 0)
1171 1129 return PyErr_SetFromErrno(PyExc_OSError);
1172 1130 r = sigprocmask(SIG_UNBLOCK, &set, NULL);
1173 1131 if (r != 0)
1174 1132 return PyErr_SetFromErrno(PyExc_OSError);
1175 1133 Py_RETURN_NONE;
1176 1134 }
1177 1135
1178 1136 #endif /* ndef _WIN32 */
1179 1137
1180 1138 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs)
1181 1139 {
1182 1140 PyObject *statobj = NULL; /* initialize - optional arg */
1183 1141 PyObject *skipobj = NULL; /* initialize - optional arg */
1184 1142 char *path, *skip = NULL;
1185 1143 Py_ssize_t plen;
1186 1144 int wantstat;
1187 1145
1188 1146 static char *kwlist[] = {"path", "stat", "skip", NULL};
1189 1147
1190 1148 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y#|OO:listdir",
1191 1149 kwlist, &path, &plen, &statobj, &skipobj))
1192 1150 return NULL;
1193 1151
1194 1152 wantstat = statobj && PyObject_IsTrue(statobj);
1195 1153
1196 1154 if (skipobj && skipobj != Py_None) {
1197 1155 skip = PyBytes_AsString(skipobj);
1198 1156 if (!skip)
1199 1157 return NULL;
1200 1158 }
1201 1159
1202 1160 return _listdir(path, plen, wantstat, skip);
1203 1161 }
1204 1162
1205 1163 #ifdef _WIN32
1206 1164 static PyObject *posixfile(PyObject *self, PyObject *args, PyObject *kwds)
1207 1165 {
1208 1166 static char *kwlist[] = {"name", "mode", "buffering", NULL};
1209 1167 PyObject *file_obj = NULL;
1210 1168 char *name = NULL;
1211 1169 char *mode = "rb";
1212 1170 DWORD access = 0;
1213 1171 DWORD creation;
1214 1172 HANDLE handle;
1215 1173 int fd, flags = 0;
1216 1174 int bufsize = -1;
1217 1175 char m0, m1, m2;
1218 1176 char fpmode[4];
1219 1177 int fppos = 0;
1220 1178 int plus;
1221 1179 FILE *fp;
1222 1180
1223 1181 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|yi:posixfile",
1224 1182 kwlist,
1225 1183 Py_FileSystemDefaultEncoding,
1226 1184 &name, &mode, &bufsize))
1227 1185 return NULL;
1228 1186
1229 1187 m0 = mode[0];
1230 1188 m1 = m0 ? mode[1] : '\0';
1231 1189 m2 = m1 ? mode[2] : '\0';
1232 1190 plus = m1 == '+' || m2 == '+';
1233 1191
1234 1192 fpmode[fppos++] = m0;
1235 1193 if (m1 == 'b' || m2 == 'b') {
1236 1194 flags = _O_BINARY;
1237 1195 fpmode[fppos++] = 'b';
1238 1196 }
1239 1197 else
1240 1198 flags = _O_TEXT;
1241 1199 if (m0 == 'r' && !plus) {
1242 1200 flags |= _O_RDONLY;
1243 1201 access = GENERIC_READ;
1244 1202 } else {
1245 1203 /*
1246 1204 work around http://support.microsoft.com/kb/899149 and
1247 1205 set _O_RDWR for 'w' and 'a', even if mode has no '+'
1248 1206 */
1249 1207 flags |= _O_RDWR;
1250 1208 access = GENERIC_READ | GENERIC_WRITE;
1251 1209 fpmode[fppos++] = '+';
1252 1210 }
1253 1211 fpmode[fppos++] = '\0';
1254 1212
1255 1213 switch (m0) {
1256 1214 case 'r':
1257 1215 creation = OPEN_EXISTING;
1258 1216 break;
1259 1217 case 'w':
1260 1218 creation = CREATE_ALWAYS;
1261 1219 break;
1262 1220 case 'a':
1263 1221 creation = OPEN_ALWAYS;
1264 1222 flags |= _O_APPEND;
1265 1223 break;
1266 1224 default:
1267 1225 PyErr_Format(PyExc_ValueError,
1268 1226 "mode string must begin with one of 'r', 'w', "
1269 1227 "or 'a', not '%c'", m0);
1270 1228 goto bail;
1271 1229 }
1272 1230
1273 1231 handle = CreateFile(name, access,
1274 1232 FILE_SHARE_READ | FILE_SHARE_WRITE |
1275 1233 FILE_SHARE_DELETE,
1276 1234 NULL,
1277 1235 creation,
1278 1236 FILE_ATTRIBUTE_NORMAL,
1279 1237 0);
1280 1238
1281 1239 if (handle == INVALID_HANDLE_VALUE) {
1282 1240 PyErr_SetFromWindowsErrWithFilename(GetLastError(), name);
1283 1241 goto bail;
1284 1242 }
1285 1243
1286 1244 fd = _open_osfhandle((intptr_t)handle, flags);
1287 1245
1288 1246 if (fd == -1) {
1289 1247 CloseHandle(handle);
1290 1248 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
1291 1249 goto bail;
1292 1250 }
1293 1251 fp = _fdopen(fd, fpmode);
1294 1252 if (fp == NULL) {
1295 1253 _close(fd);
1296 1254 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
1297 1255 goto bail;
1298 1256 }
1299 1257
1300 1258 file_obj = PyFile_FromFile(fp, name, mode, fclose);
1301 1259 if (file_obj == NULL) {
1302 1260 fclose(fp);
1303 1261 goto bail;
1304 1262 }
1305 1263
1306 1264 PyFile_SetBufSize(file_obj, bufsize);
1307 1265 bail:
1308 1266 PyMem_Free(name);
1309 1267 return file_obj;
1310 1268 }
1311 1269 #endif
1312 1270
1313 1271 #ifdef __APPLE__
1314 1272 #include <ApplicationServices/ApplicationServices.h>
1315 1273
1316 1274 static PyObject *isgui(PyObject *self)
1317 1275 {
1318 1276 CFDictionaryRef dict = CGSessionCopyCurrentDictionary();
1319 1277
1320 1278 if (dict != NULL) {
1321 1279 CFRelease(dict);
1322 1280 Py_RETURN_TRUE;
1323 1281 } else {
1324 1282 Py_RETURN_FALSE;
1325 1283 }
1326 1284 }
1327 1285 #endif
1328 1286
1329 1287 static char osutil_doc[] = "Native operating system services.";
1330 1288
1331 1289 static PyMethodDef methods[] = {
1332 1290 {"listdir", (PyCFunction)listdir, METH_VARARGS | METH_KEYWORDS,
1333 1291 "list a directory\n"},
1334 1292 #ifdef _WIN32
1335 1293 {"posixfile", (PyCFunction)posixfile, METH_VARARGS | METH_KEYWORDS,
1336 1294 "Open a file with POSIX-like semantics.\n"
1337 1295 "On error, this function may raise either a WindowsError or an IOError."},
1338 1296 #else
1339 1297 {"statfiles", (PyCFunction)statfiles, METH_VARARGS | METH_KEYWORDS,
1340 1298 "stat a series of files or symlinks\n"
1341 1299 "Returns None for non-existent entries and entries of other types.\n"},
1342 1300 #ifdef CMSG_LEN
1343 1301 {"recvfds", (PyCFunction)recvfds, METH_VARARGS,
1344 1302 "receive list of file descriptors via socket\n"},
1345 1303 #endif
1346 1304 #ifndef SETPROCNAME_USE_NONE
1347 1305 {"setprocname", (PyCFunction)setprocname, METH_VARARGS,
1348 1306 "set process title (best-effort)\n"},
1349 1307 #endif
1350 1308 #if defined(HAVE_BSD_STATFS) || defined(HAVE_LINUX_STATFS)
1351 1309 {"getfstype", (PyCFunction)getfstype, METH_VARARGS,
1352 1310 "get filesystem type (best-effort)\n"},
1353 1311 #endif
1354 1312 #if defined(HAVE_BSD_STATFS)
1355 1313 {"getfsmountpoint", (PyCFunction)getfsmountpoint, METH_VARARGS,
1356 1314 "get filesystem mount point (best-effort)\n"},
1357 1315 #endif
1358 1316 {"unblocksignal", (PyCFunction)unblocksignal, METH_VARARGS,
1359 1317 "change signal mask to unblock a given signal\n"},
1360 1318 #endif /* ndef _WIN32 */
1361 1319 #ifdef __APPLE__
1362 1320 {
1363 1321 "isgui", (PyCFunction)isgui, METH_NOARGS,
1364 1322 "Is a CoreGraphics session available?"
1365 1323 },
1366 1324 #endif
1367 1325 {NULL, NULL}
1368 1326 };
1369 1327
1370 1328 static const int version = 4;
1371 1329
1372 1330 static struct PyModuleDef osutil_module = {
1373 1331 PyModuleDef_HEAD_INIT,
1374 1332 "osutil",
1375 1333 osutil_doc,
1376 1334 -1,
1377 1335 methods
1378 1336 };
1379 1337
1380 1338 PyMODINIT_FUNC PyInit_osutil(void)
1381 1339 {
1382 1340 PyObject *m;
1383 1341 if (PyType_Ready(&listdir_stat_type) < 0)
1384 1342 return NULL;
1385 1343
1386 1344 m = PyModule_Create(&osutil_module);
1387 1345 PyModule_AddIntConstant(m, "version", version);
1388 1346 return m;
1389 1347 }
@@ -1,85 +1,81 b''
1 1 /*
2 2 util.h - utility functions for interfacing with the various python APIs.
3 3
4 4 This software may be used and distributed according to the terms of
5 5 the GNU General Public License, incorporated herein by reference.
6 6 */
7 7
8 8 #ifndef _HG_UTIL_H_
9 9 #define _HG_UTIL_H_
10 10
11 11 #include "compat.h"
12 12
13 #if PY_MAJOR_VERSION >= 3
14 #define IS_PY3K
15 #endif
16
17 13 /* clang-format off */
18 14 typedef struct {
19 15 PyObject_HEAD
20 16 int flags;
21 17 int mode;
22 18 int size;
23 19 int mtime_s;
24 20 int mtime_ns;
25 21 } dirstateItemObject;
26 22 /* clang-format on */
27 23
28 24 static const int dirstate_flag_wc_tracked = 1 << 0;
29 25 static const int dirstate_flag_p1_tracked = 1 << 1;
30 26 static const int dirstate_flag_p2_info = 1 << 2;
31 27 static const int dirstate_flag_mode_exec_perm = 1 << 3;
32 28 static const int dirstate_flag_mode_is_symlink = 1 << 4;
33 29 static const int dirstate_flag_has_fallback_exec = 1 << 5;
34 30 static const int dirstate_flag_fallback_exec = 1 << 6;
35 31 static const int dirstate_flag_has_fallback_symlink = 1 << 7;
36 32 static const int dirstate_flag_fallback_symlink = 1 << 8;
37 33 static const int dirstate_flag_expected_state_is_modified = 1 << 9;
38 34 static const int dirstate_flag_has_meaningful_data = 1 << 10;
39 35 static const int dirstate_flag_has_mtime = 1 << 11;
40 36 static const int dirstate_flag_mtime_second_ambiguous = 1 << 12;
41 37 static const int dirstate_flag_directory = 1 << 13;
42 38 static const int dirstate_flag_all_unknown_recorded = 1 << 14;
43 39 static const int dirstate_flag_all_ignored_recorded = 1 << 15;
44 40
45 41 extern PyTypeObject dirstateItemType;
46 42 #define dirstate_tuple_check(op) (Py_TYPE(op) == &dirstateItemType)
47 43
48 44 #ifndef MIN
49 45 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
50 46 #endif
51 47 /* VC9 doesn't include bool and lacks stdbool.h based on my searching */
52 48 #if defined(_MSC_VER) || __STDC_VERSION__ < 199901L
53 49 #define true 1
54 50 #define false 0
55 51 typedef unsigned char bool;
56 52 #else
57 53 #include <stdbool.h>
58 54 #endif
59 55
60 56 static inline PyObject *_dict_new_presized(Py_ssize_t expected_size)
61 57 {
62 58 /* _PyDict_NewPresized expects a minused parameter, but it actually
63 59 creates a dictionary that's the nearest power of two bigger than the
64 60 parameter. For example, with the initial minused = 1000, the
65 61 dictionary created has size 1024. Of course in a lot of cases that
66 62 can be greater than the maximum load factor Python's dict object
67 63 expects (= 2/3), so as soon as we cross the threshold we'll resize
68 64 anyway. So create a dictionary that's at least 3/2 the size. */
69 65 return _PyDict_NewPresized(((1 + expected_size) / 2) * 3);
70 66 }
71 67
72 68 /* Convert a PyInt or PyLong to a long. Returns false if there is an
73 69 error, in which case an exception will already have been set. */
74 70 static inline bool pylong_to_long(PyObject *pylong, long *out)
75 71 {
76 72 *out = PyLong_AsLong(pylong);
77 73 /* Fast path to avoid hitting PyErr_Occurred if the value was obviously
78 74 * not an error. */
79 75 if (*out != -1) {
80 76 return true;
81 77 }
82 78 return PyErr_Occurred() == NULL;
83 79 }
84 80
85 81 #endif /* _HG_UTIL_H_ */
General Comments 0
You need to be logged in to leave comments. Login now