##// END OF EJS Templates
osutil: use PyLongObject on Python 3 for listdir_slot...
Gregory Szorc -
r30111:a989fa78 default
parent child Browse files
Show More
@@ -1,928 +1,936
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 #endif
28 28
29 29 #ifdef __APPLE__
30 30 #include <sys/attr.h>
31 31 #include <sys/vnode.h>
32 32 #endif
33 33
34 34 #include "util.h"
35 35
36 36 /* some platforms lack the PATH_MAX definition (eg. GNU/Hurd) */
37 37 #ifndef PATH_MAX
38 38 #define PATH_MAX 4096
39 39 #endif
40 40
41 41 #ifdef _WIN32
42 42 /*
43 43 stat struct compatible with hg expectations
44 44 Mercurial only uses st_mode, st_size and st_mtime
45 45 the rest is kept to minimize changes between implementations
46 46 */
47 47 struct hg_stat {
48 48 int st_dev;
49 49 int st_mode;
50 50 int st_nlink;
51 51 __int64 st_size;
52 52 int st_mtime;
53 53 int st_ctime;
54 54 };
55 55 struct listdir_stat {
56 56 PyObject_HEAD
57 57 struct hg_stat st;
58 58 };
59 59 #else
60 60 struct listdir_stat {
61 61 PyObject_HEAD
62 62 struct stat st;
63 63 };
64 64 #endif
65 65
66 #ifdef IS_PY3K
67 #define listdir_slot(name) \
68 static PyObject *listdir_stat_##name(PyObject *self, void *x) \
69 { \
70 return PyLong_FromLong(((struct listdir_stat *)self)->st.name); \
71 }
72 #else
66 73 #define listdir_slot(name) \
67 74 static PyObject *listdir_stat_##name(PyObject *self, void *x) \
68 75 { \
69 76 return PyInt_FromLong(((struct listdir_stat *)self)->st.name); \
70 77 }
78 #endif
71 79
72 80 listdir_slot(st_dev)
73 81 listdir_slot(st_mode)
74 82 listdir_slot(st_nlink)
75 83 #ifdef _WIN32
76 84 static PyObject *listdir_stat_st_size(PyObject *self, void *x)
77 85 {
78 86 return PyLong_FromLongLong(
79 87 (PY_LONG_LONG)((struct listdir_stat *)self)->st.st_size);
80 88 }
81 89 #else
82 90 listdir_slot(st_size)
83 91 #endif
84 92 listdir_slot(st_mtime)
85 93 listdir_slot(st_ctime)
86 94
87 95 static struct PyGetSetDef listdir_stat_getsets[] = {
88 96 {"st_dev", listdir_stat_st_dev, 0, 0, 0},
89 97 {"st_mode", listdir_stat_st_mode, 0, 0, 0},
90 98 {"st_nlink", listdir_stat_st_nlink, 0, 0, 0},
91 99 {"st_size", listdir_stat_st_size, 0, 0, 0},
92 100 {"st_mtime", listdir_stat_st_mtime, 0, 0, 0},
93 101 {"st_ctime", listdir_stat_st_ctime, 0, 0, 0},
94 102 {0, 0, 0, 0, 0}
95 103 };
96 104
97 105 static PyObject *listdir_stat_new(PyTypeObject *t, PyObject *a, PyObject *k)
98 106 {
99 107 return t->tp_alloc(t, 0);
100 108 }
101 109
102 110 static void listdir_stat_dealloc(PyObject *o)
103 111 {
104 112 o->ob_type->tp_free(o);
105 113 }
106 114
107 115 static PyTypeObject listdir_stat_type = {
108 116 PyVarObject_HEAD_INIT(NULL, 0)
109 117 "osutil.stat", /*tp_name*/
110 118 sizeof(struct listdir_stat), /*tp_basicsize*/
111 119 0, /*tp_itemsize*/
112 120 (destructor)listdir_stat_dealloc, /*tp_dealloc*/
113 121 0, /*tp_print*/
114 122 0, /*tp_getattr*/
115 123 0, /*tp_setattr*/
116 124 0, /*tp_compare*/
117 125 0, /*tp_repr*/
118 126 0, /*tp_as_number*/
119 127 0, /*tp_as_sequence*/
120 128 0, /*tp_as_mapping*/
121 129 0, /*tp_hash */
122 130 0, /*tp_call*/
123 131 0, /*tp_str*/
124 132 0, /*tp_getattro*/
125 133 0, /*tp_setattro*/
126 134 0, /*tp_as_buffer*/
127 135 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
128 136 "stat objects", /* tp_doc */
129 137 0, /* tp_traverse */
130 138 0, /* tp_clear */
131 139 0, /* tp_richcompare */
132 140 0, /* tp_weaklistoffset */
133 141 0, /* tp_iter */
134 142 0, /* tp_iternext */
135 143 0, /* tp_methods */
136 144 0, /* tp_members */
137 145 listdir_stat_getsets, /* tp_getset */
138 146 0, /* tp_base */
139 147 0, /* tp_dict */
140 148 0, /* tp_descr_get */
141 149 0, /* tp_descr_set */
142 150 0, /* tp_dictoffset */
143 151 0, /* tp_init */
144 152 0, /* tp_alloc */
145 153 listdir_stat_new, /* tp_new */
146 154 };
147 155
148 156 #ifdef _WIN32
149 157
150 158 static int to_python_time(const FILETIME *tm)
151 159 {
152 160 /* number of seconds between epoch and January 1 1601 */
153 161 const __int64 a0 = (__int64)134774L * (__int64)24L * (__int64)3600L;
154 162 /* conversion factor from 100ns to 1s */
155 163 const __int64 a1 = 10000000;
156 164 /* explicit (int) cast to suspend compiler warnings */
157 165 return (int)((((__int64)tm->dwHighDateTime << 32)
158 166 + tm->dwLowDateTime) / a1 - a0);
159 167 }
160 168
161 169 static PyObject *make_item(const WIN32_FIND_DATAA *fd, int wantstat)
162 170 {
163 171 PyObject *py_st;
164 172 struct hg_stat *stp;
165 173
166 174 int kind = (fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
167 175 ? _S_IFDIR : _S_IFREG;
168 176
169 177 if (!wantstat)
170 178 return Py_BuildValue("si", fd->cFileName, kind);
171 179
172 180 py_st = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL);
173 181 if (!py_st)
174 182 return NULL;
175 183
176 184 stp = &((struct listdir_stat *)py_st)->st;
177 185 /*
178 186 use kind as st_mode
179 187 rwx bits on Win32 are meaningless
180 188 and Hg does not use them anyway
181 189 */
182 190 stp->st_mode = kind;
183 191 stp->st_mtime = to_python_time(&fd->ftLastWriteTime);
184 192 stp->st_ctime = to_python_time(&fd->ftCreationTime);
185 193 if (kind == _S_IFREG)
186 194 stp->st_size = ((__int64)fd->nFileSizeHigh << 32)
187 195 + fd->nFileSizeLow;
188 196 return Py_BuildValue("siN", fd->cFileName,
189 197 kind, py_st);
190 198 }
191 199
192 200 static PyObject *_listdir(char *path, int plen, int wantstat, char *skip)
193 201 {
194 202 PyObject *rval = NULL; /* initialize - return value */
195 203 PyObject *list;
196 204 HANDLE fh;
197 205 WIN32_FIND_DATAA fd;
198 206 char *pattern;
199 207
200 208 /* build the path + \* pattern string */
201 209 pattern = malloc(plen + 3); /* path + \* + \0 */
202 210 if (!pattern) {
203 211 PyErr_NoMemory();
204 212 goto error_nomem;
205 213 }
206 214 memcpy(pattern, path, plen);
207 215
208 216 if (plen > 0) {
209 217 char c = path[plen-1];
210 218 if (c != ':' && c != '/' && c != '\\')
211 219 pattern[plen++] = '\\';
212 220 }
213 221 pattern[plen++] = '*';
214 222 pattern[plen] = '\0';
215 223
216 224 fh = FindFirstFileA(pattern, &fd);
217 225 if (fh == INVALID_HANDLE_VALUE) {
218 226 PyErr_SetFromWindowsErrWithFilename(GetLastError(), path);
219 227 goto error_file;
220 228 }
221 229
222 230 list = PyList_New(0);
223 231 if (!list)
224 232 goto error_list;
225 233
226 234 do {
227 235 PyObject *item;
228 236
229 237 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
230 238 if (!strcmp(fd.cFileName, ".")
231 239 || !strcmp(fd.cFileName, ".."))
232 240 continue;
233 241
234 242 if (skip && !strcmp(fd.cFileName, skip)) {
235 243 rval = PyList_New(0);
236 244 goto error;
237 245 }
238 246 }
239 247
240 248 item = make_item(&fd, wantstat);
241 249 if (!item)
242 250 goto error;
243 251
244 252 if (PyList_Append(list, item)) {
245 253 Py_XDECREF(item);
246 254 goto error;
247 255 }
248 256
249 257 Py_XDECREF(item);
250 258 } while (FindNextFileA(fh, &fd));
251 259
252 260 if (GetLastError() != ERROR_NO_MORE_FILES) {
253 261 PyErr_SetFromWindowsErrWithFilename(GetLastError(), path);
254 262 goto error;
255 263 }
256 264
257 265 rval = list;
258 266 Py_XINCREF(rval);
259 267 error:
260 268 Py_XDECREF(list);
261 269 error_list:
262 270 FindClose(fh);
263 271 error_file:
264 272 free(pattern);
265 273 error_nomem:
266 274 return rval;
267 275 }
268 276
269 277 #else
270 278
271 279 int entkind(struct dirent *ent)
272 280 {
273 281 #ifdef DT_REG
274 282 switch (ent->d_type) {
275 283 case DT_REG: return S_IFREG;
276 284 case DT_DIR: return S_IFDIR;
277 285 case DT_LNK: return S_IFLNK;
278 286 case DT_BLK: return S_IFBLK;
279 287 case DT_CHR: return S_IFCHR;
280 288 case DT_FIFO: return S_IFIFO;
281 289 case DT_SOCK: return S_IFSOCK;
282 290 }
283 291 #endif
284 292 return -1;
285 293 }
286 294
287 295 static PyObject *makestat(const struct stat *st)
288 296 {
289 297 PyObject *stat;
290 298
291 299 stat = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL);
292 300 if (stat)
293 301 memcpy(&((struct listdir_stat *)stat)->st, st, sizeof(*st));
294 302 return stat;
295 303 }
296 304
297 305 static PyObject *_listdir_stat(char *path, int pathlen, int keepstat,
298 306 char *skip)
299 307 {
300 308 PyObject *list, *elem, *stat = NULL, *ret = NULL;
301 309 char fullpath[PATH_MAX + 10];
302 310 int kind, err;
303 311 struct stat st;
304 312 struct dirent *ent;
305 313 DIR *dir;
306 314 #ifdef AT_SYMLINK_NOFOLLOW
307 315 int dfd = -1;
308 316 #endif
309 317
310 318 if (pathlen >= PATH_MAX) {
311 319 errno = ENAMETOOLONG;
312 320 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
313 321 goto error_value;
314 322 }
315 323 strncpy(fullpath, path, PATH_MAX);
316 324 fullpath[pathlen] = '/';
317 325
318 326 #ifdef AT_SYMLINK_NOFOLLOW
319 327 dfd = open(path, O_RDONLY);
320 328 if (dfd == -1) {
321 329 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
322 330 goto error_value;
323 331 }
324 332 dir = fdopendir(dfd);
325 333 #else
326 334 dir = opendir(path);
327 335 #endif
328 336 if (!dir) {
329 337 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
330 338 goto error_dir;
331 339 }
332 340
333 341 list = PyList_New(0);
334 342 if (!list)
335 343 goto error_list;
336 344
337 345 while ((ent = readdir(dir))) {
338 346 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
339 347 continue;
340 348
341 349 kind = entkind(ent);
342 350 if (kind == -1 || keepstat) {
343 351 #ifdef AT_SYMLINK_NOFOLLOW
344 352 err = fstatat(dfd, ent->d_name, &st,
345 353 AT_SYMLINK_NOFOLLOW);
346 354 #else
347 355 strncpy(fullpath + pathlen + 1, ent->d_name,
348 356 PATH_MAX - pathlen);
349 357 fullpath[PATH_MAX] = '\0';
350 358 err = lstat(fullpath, &st);
351 359 #endif
352 360 if (err == -1) {
353 361 /* race with file deletion? */
354 362 if (errno == ENOENT)
355 363 continue;
356 364 strncpy(fullpath + pathlen + 1, ent->d_name,
357 365 PATH_MAX - pathlen);
358 366 fullpath[PATH_MAX] = 0;
359 367 PyErr_SetFromErrnoWithFilename(PyExc_OSError,
360 368 fullpath);
361 369 goto error;
362 370 }
363 371 kind = st.st_mode & S_IFMT;
364 372 }
365 373
366 374 /* quit early? */
367 375 if (skip && kind == S_IFDIR && !strcmp(ent->d_name, skip)) {
368 376 ret = PyList_New(0);
369 377 goto error;
370 378 }
371 379
372 380 if (keepstat) {
373 381 stat = makestat(&st);
374 382 if (!stat)
375 383 goto error;
376 384 elem = Py_BuildValue("siN", ent->d_name, kind, stat);
377 385 } else
378 386 elem = Py_BuildValue("si", ent->d_name, kind);
379 387 if (!elem)
380 388 goto error;
381 389 stat = NULL;
382 390
383 391 PyList_Append(list, elem);
384 392 Py_DECREF(elem);
385 393 }
386 394
387 395 ret = list;
388 396 Py_INCREF(ret);
389 397
390 398 error:
391 399 Py_DECREF(list);
392 400 Py_XDECREF(stat);
393 401 error_list:
394 402 closedir(dir);
395 403 error_dir:
396 404 #ifdef AT_SYMLINK_NOFOLLOW
397 405 close(dfd);
398 406 #endif
399 407 error_value:
400 408 return ret;
401 409 }
402 410
403 411 #ifdef __APPLE__
404 412
405 413 typedef struct {
406 414 u_int32_t length;
407 415 attrreference_t name;
408 416 fsobj_type_t obj_type;
409 417 struct timespec mtime;
410 418 #if __LITTLE_ENDIAN__
411 419 mode_t access_mask;
412 420 uint16_t padding;
413 421 #else
414 422 uint16_t padding;
415 423 mode_t access_mask;
416 424 #endif
417 425 off_t size;
418 426 } __attribute__((packed)) attrbuf_entry;
419 427
420 428 int attrkind(attrbuf_entry *entry)
421 429 {
422 430 switch (entry->obj_type) {
423 431 case VREG: return S_IFREG;
424 432 case VDIR: return S_IFDIR;
425 433 case VLNK: return S_IFLNK;
426 434 case VBLK: return S_IFBLK;
427 435 case VCHR: return S_IFCHR;
428 436 case VFIFO: return S_IFIFO;
429 437 case VSOCK: return S_IFSOCK;
430 438 }
431 439 return -1;
432 440 }
433 441
434 442 /* get these many entries at a time */
435 443 #define LISTDIR_BATCH_SIZE 50
436 444
437 445 static PyObject *_listdir_batch(char *path, int pathlen, int keepstat,
438 446 char *skip, bool *fallback)
439 447 {
440 448 PyObject *list, *elem, *stat = NULL, *ret = NULL;
441 449 int kind, err;
442 450 unsigned long index;
443 451 unsigned int count, old_state, new_state;
444 452 bool state_seen = false;
445 453 attrbuf_entry *entry;
446 454 /* from the getattrlist(2) man page: a path can be no longer than
447 455 (NAME_MAX * 3 + 1) bytes. Also, "The getattrlist() function will
448 456 silently truncate attribute data if attrBufSize is too small." So
449 457 pass in a buffer big enough for the worst case. */
450 458 char attrbuf[LISTDIR_BATCH_SIZE * (sizeof(attrbuf_entry) + NAME_MAX * 3 + 1)];
451 459 unsigned int basep_unused;
452 460
453 461 struct stat st;
454 462 int dfd = -1;
455 463
456 464 /* these must match the attrbuf_entry struct, otherwise you'll end up
457 465 with garbage */
458 466 struct attrlist requested_attr = {0};
459 467 requested_attr.bitmapcount = ATTR_BIT_MAP_COUNT;
460 468 requested_attr.commonattr = (ATTR_CMN_NAME | ATTR_CMN_OBJTYPE |
461 469 ATTR_CMN_MODTIME | ATTR_CMN_ACCESSMASK);
462 470 requested_attr.fileattr = ATTR_FILE_DATALENGTH;
463 471
464 472 *fallback = false;
465 473
466 474 if (pathlen >= PATH_MAX) {
467 475 errno = ENAMETOOLONG;
468 476 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
469 477 goto error_value;
470 478 }
471 479
472 480 dfd = open(path, O_RDONLY);
473 481 if (dfd == -1) {
474 482 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
475 483 goto error_value;
476 484 }
477 485
478 486 list = PyList_New(0);
479 487 if (!list)
480 488 goto error_dir;
481 489
482 490 do {
483 491 count = LISTDIR_BATCH_SIZE;
484 492 err = getdirentriesattr(dfd, &requested_attr, &attrbuf,
485 493 sizeof(attrbuf), &count, &basep_unused,
486 494 &new_state, 0);
487 495 if (err < 0) {
488 496 if (errno == ENOTSUP) {
489 497 /* We're on a filesystem that doesn't support
490 498 getdirentriesattr. Fall back to the
491 499 stat-based implementation. */
492 500 *fallback = true;
493 501 } else
494 502 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
495 503 goto error;
496 504 }
497 505
498 506 if (!state_seen) {
499 507 old_state = new_state;
500 508 state_seen = true;
501 509 } else if (old_state != new_state) {
502 510 /* There's an edge case with getdirentriesattr. Consider
503 511 the following initial list of files:
504 512
505 513 a
506 514 b
507 515 <--
508 516 c
509 517 d
510 518
511 519 If the iteration is paused at the arrow, and b is
512 520 deleted before it is resumed, getdirentriesattr will
513 521 not return d at all! Ordinarily we're expected to
514 522 restart the iteration from the beginning. To avoid
515 523 getting stuck in a retry loop here, fall back to
516 524 stat. */
517 525 *fallback = true;
518 526 goto error;
519 527 }
520 528
521 529 entry = (attrbuf_entry *)attrbuf;
522 530
523 531 for (index = 0; index < count; index++) {
524 532 char *filename = ((char *)&entry->name) +
525 533 entry->name.attr_dataoffset;
526 534
527 535 if (!strcmp(filename, ".") || !strcmp(filename, ".."))
528 536 continue;
529 537
530 538 kind = attrkind(entry);
531 539 if (kind == -1) {
532 540 PyErr_Format(PyExc_OSError,
533 541 "unknown object type %u for file "
534 542 "%s%s!",
535 543 entry->obj_type, path, filename);
536 544 goto error;
537 545 }
538 546
539 547 /* quit early? */
540 548 if (skip && kind == S_IFDIR && !strcmp(filename, skip)) {
541 549 ret = PyList_New(0);
542 550 goto error;
543 551 }
544 552
545 553 if (keepstat) {
546 554 /* from the getattrlist(2) man page: "Only the
547 555 permission bits ... are valid". */
548 556 st.st_mode = (entry->access_mask & ~S_IFMT) | kind;
549 557 st.st_mtime = entry->mtime.tv_sec;
550 558 st.st_size = entry->size;
551 559 stat = makestat(&st);
552 560 if (!stat)
553 561 goto error;
554 562 elem = Py_BuildValue("siN", filename, kind, stat);
555 563 } else
556 564 elem = Py_BuildValue("si", filename, kind);
557 565 if (!elem)
558 566 goto error;
559 567 stat = NULL;
560 568
561 569 PyList_Append(list, elem);
562 570 Py_DECREF(elem);
563 571
564 572 entry = (attrbuf_entry *)((char *)entry + entry->length);
565 573 }
566 574 } while (err == 0);
567 575
568 576 ret = list;
569 577 Py_INCREF(ret);
570 578
571 579 error:
572 580 Py_DECREF(list);
573 581 Py_XDECREF(stat);
574 582 error_dir:
575 583 close(dfd);
576 584 error_value:
577 585 return ret;
578 586 }
579 587
580 588 #endif /* __APPLE__ */
581 589
582 590 static PyObject *_listdir(char *path, int pathlen, int keepstat, char *skip)
583 591 {
584 592 #ifdef __APPLE__
585 593 PyObject *ret;
586 594 bool fallback = false;
587 595
588 596 ret = _listdir_batch(path, pathlen, keepstat, skip, &fallback);
589 597 if (ret != NULL || !fallback)
590 598 return ret;
591 599 #endif
592 600 return _listdir_stat(path, pathlen, keepstat, skip);
593 601 }
594 602
595 603 static PyObject *statfiles(PyObject *self, PyObject *args)
596 604 {
597 605 PyObject *names, *stats;
598 606 Py_ssize_t i, count;
599 607
600 608 if (!PyArg_ParseTuple(args, "O:statfiles", &names))
601 609 return NULL;
602 610
603 611 count = PySequence_Length(names);
604 612 if (count == -1) {
605 613 PyErr_SetString(PyExc_TypeError, "not a sequence");
606 614 return NULL;
607 615 }
608 616
609 617 stats = PyList_New(count);
610 618 if (stats == NULL)
611 619 return NULL;
612 620
613 621 for (i = 0; i < count; i++) {
614 622 PyObject *stat, *pypath;
615 623 struct stat st;
616 624 int ret, kind;
617 625 char *path;
618 626
619 627 /* With a large file count or on a slow filesystem,
620 628 don't block signals for long (issue4878). */
621 629 if ((i % 1000) == 999 && PyErr_CheckSignals() == -1)
622 630 goto bail;
623 631
624 632 pypath = PySequence_GetItem(names, i);
625 633 if (!pypath)
626 634 goto bail;
627 635 path = PyBytes_AsString(pypath);
628 636 if (path == NULL) {
629 637 Py_DECREF(pypath);
630 638 PyErr_SetString(PyExc_TypeError, "not a string");
631 639 goto bail;
632 640 }
633 641 ret = lstat(path, &st);
634 642 Py_DECREF(pypath);
635 643 kind = st.st_mode & S_IFMT;
636 644 if (ret != -1 && (kind == S_IFREG || kind == S_IFLNK)) {
637 645 stat = makestat(&st);
638 646 if (stat == NULL)
639 647 goto bail;
640 648 PyList_SET_ITEM(stats, i, stat);
641 649 } else {
642 650 Py_INCREF(Py_None);
643 651 PyList_SET_ITEM(stats, i, Py_None);
644 652 }
645 653 }
646 654
647 655 return stats;
648 656
649 657 bail:
650 658 Py_DECREF(stats);
651 659 return NULL;
652 660 }
653 661
654 662 /*
655 663 * recvfds() simply does not release GIL during blocking io operation because
656 664 * command server is known to be single-threaded.
657 665 *
658 666 * Old systems such as Solaris don't provide CMSG_LEN, msg_control, etc.
659 667 * Currently, recvfds() is not supported on these platforms.
660 668 */
661 669 #ifdef CMSG_LEN
662 670
663 671 static ssize_t recvfdstobuf(int sockfd, int **rfds, void *cbuf, size_t cbufsize)
664 672 {
665 673 char dummy[1];
666 674 struct iovec iov = {dummy, sizeof(dummy)};
667 675 struct msghdr msgh = {0};
668 676 struct cmsghdr *cmsg;
669 677
670 678 msgh.msg_iov = &iov;
671 679 msgh.msg_iovlen = 1;
672 680 msgh.msg_control = cbuf;
673 681 msgh.msg_controllen = (socklen_t)cbufsize;
674 682 if (recvmsg(sockfd, &msgh, 0) < 0)
675 683 return -1;
676 684
677 685 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg;
678 686 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
679 687 if (cmsg->cmsg_level != SOL_SOCKET ||
680 688 cmsg->cmsg_type != SCM_RIGHTS)
681 689 continue;
682 690 *rfds = (int *)CMSG_DATA(cmsg);
683 691 return (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
684 692 }
685 693
686 694 *rfds = cbuf;
687 695 return 0;
688 696 }
689 697
690 698 static PyObject *recvfds(PyObject *self, PyObject *args)
691 699 {
692 700 int sockfd;
693 701 int *rfds = NULL;
694 702 ssize_t rfdscount, i;
695 703 char cbuf[256];
696 704 PyObject *rfdslist = NULL;
697 705
698 706 if (!PyArg_ParseTuple(args, "i", &sockfd))
699 707 return NULL;
700 708
701 709 rfdscount = recvfdstobuf(sockfd, &rfds, cbuf, sizeof(cbuf));
702 710 if (rfdscount < 0)
703 711 return PyErr_SetFromErrno(PyExc_OSError);
704 712
705 713 rfdslist = PyList_New(rfdscount);
706 714 if (!rfdslist)
707 715 goto bail;
708 716 for (i = 0; i < rfdscount; i++) {
709 717 PyObject *obj = PyLong_FromLong(rfds[i]);
710 718 if (!obj)
711 719 goto bail;
712 720 PyList_SET_ITEM(rfdslist, i, obj);
713 721 }
714 722 return rfdslist;
715 723
716 724 bail:
717 725 Py_XDECREF(rfdslist);
718 726 return NULL;
719 727 }
720 728
721 729 #endif /* CMSG_LEN */
722 730 #endif /* ndef _WIN32 */
723 731
724 732 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs)
725 733 {
726 734 PyObject *statobj = NULL; /* initialize - optional arg */
727 735 PyObject *skipobj = NULL; /* initialize - optional arg */
728 736 char *path, *skip = NULL;
729 737 int wantstat, plen;
730 738
731 739 static char *kwlist[] = {"path", "stat", "skip", NULL};
732 740
733 741 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|OO:listdir",
734 742 kwlist, &path, &plen, &statobj, &skipobj))
735 743 return NULL;
736 744
737 745 wantstat = statobj && PyObject_IsTrue(statobj);
738 746
739 747 if (skipobj && skipobj != Py_None) {
740 748 skip = PyBytes_AsString(skipobj);
741 749 if (!skip)
742 750 return NULL;
743 751 }
744 752
745 753 return _listdir(path, plen, wantstat, skip);
746 754 }
747 755
748 756 #ifdef _WIN32
749 757 static PyObject *posixfile(PyObject *self, PyObject *args, PyObject *kwds)
750 758 {
751 759 static char *kwlist[] = {"name", "mode", "buffering", NULL};
752 760 PyObject *file_obj = NULL;
753 761 char *name = NULL;
754 762 char *mode = "rb";
755 763 DWORD access = 0;
756 764 DWORD creation;
757 765 HANDLE handle;
758 766 int fd, flags = 0;
759 767 int bufsize = -1;
760 768 char m0, m1, m2;
761 769 char fpmode[4];
762 770 int fppos = 0;
763 771 int plus;
764 772 FILE *fp;
765 773
766 774 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:posixfile", kwlist,
767 775 Py_FileSystemDefaultEncoding,
768 776 &name, &mode, &bufsize))
769 777 return NULL;
770 778
771 779 m0 = mode[0];
772 780 m1 = m0 ? mode[1] : '\0';
773 781 m2 = m1 ? mode[2] : '\0';
774 782 plus = m1 == '+' || m2 == '+';
775 783
776 784 fpmode[fppos++] = m0;
777 785 if (m1 == 'b' || m2 == 'b') {
778 786 flags = _O_BINARY;
779 787 fpmode[fppos++] = 'b';
780 788 }
781 789 else
782 790 flags = _O_TEXT;
783 791 if (m0 == 'r' && !plus) {
784 792 flags |= _O_RDONLY;
785 793 access = GENERIC_READ;
786 794 } else {
787 795 /*
788 796 work around http://support.microsoft.com/kb/899149 and
789 797 set _O_RDWR for 'w' and 'a', even if mode has no '+'
790 798 */
791 799 flags |= _O_RDWR;
792 800 access = GENERIC_READ | GENERIC_WRITE;
793 801 fpmode[fppos++] = '+';
794 802 }
795 803 fpmode[fppos++] = '\0';
796 804
797 805 switch (m0) {
798 806 case 'r':
799 807 creation = OPEN_EXISTING;
800 808 break;
801 809 case 'w':
802 810 creation = CREATE_ALWAYS;
803 811 break;
804 812 case 'a':
805 813 creation = OPEN_ALWAYS;
806 814 flags |= _O_APPEND;
807 815 break;
808 816 default:
809 817 PyErr_Format(PyExc_ValueError,
810 818 "mode string must begin with one of 'r', 'w', "
811 819 "or 'a', not '%c'", m0);
812 820 goto bail;
813 821 }
814 822
815 823 handle = CreateFile(name, access,
816 824 FILE_SHARE_READ | FILE_SHARE_WRITE |
817 825 FILE_SHARE_DELETE,
818 826 NULL,
819 827 creation,
820 828 FILE_ATTRIBUTE_NORMAL,
821 829 0);
822 830
823 831 if (handle == INVALID_HANDLE_VALUE) {
824 832 PyErr_SetFromWindowsErrWithFilename(GetLastError(), name);
825 833 goto bail;
826 834 }
827 835
828 836 fd = _open_osfhandle((intptr_t)handle, flags);
829 837
830 838 if (fd == -1) {
831 839 CloseHandle(handle);
832 840 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
833 841 goto bail;
834 842 }
835 843 #ifndef IS_PY3K
836 844 fp = _fdopen(fd, fpmode);
837 845 if (fp == NULL) {
838 846 _close(fd);
839 847 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
840 848 goto bail;
841 849 }
842 850
843 851 file_obj = PyFile_FromFile(fp, name, mode, fclose);
844 852 if (file_obj == NULL) {
845 853 fclose(fp);
846 854 goto bail;
847 855 }
848 856
849 857 PyFile_SetBufSize(file_obj, bufsize);
850 858 #else
851 859 file_obj = PyFile_FromFd(fd, name, mode, bufsize, NULL, NULL, NULL, 1);
852 860 if (file_obj == NULL)
853 861 goto bail;
854 862 #endif
855 863 bail:
856 864 PyMem_Free(name);
857 865 return file_obj;
858 866 }
859 867 #endif
860 868
861 869 #ifdef __APPLE__
862 870 #include <ApplicationServices/ApplicationServices.h>
863 871
864 872 static PyObject *isgui(PyObject *self)
865 873 {
866 874 CFDictionaryRef dict = CGSessionCopyCurrentDictionary();
867 875
868 876 if (dict != NULL) {
869 877 CFRelease(dict);
870 878 Py_RETURN_TRUE;
871 879 } else {
872 880 Py_RETURN_FALSE;
873 881 }
874 882 }
875 883 #endif
876 884
877 885 static char osutil_doc[] = "Native operating system services.";
878 886
879 887 static PyMethodDef methods[] = {
880 888 {"listdir", (PyCFunction)listdir, METH_VARARGS | METH_KEYWORDS,
881 889 "list a directory\n"},
882 890 #ifdef _WIN32
883 891 {"posixfile", (PyCFunction)posixfile, METH_VARARGS | METH_KEYWORDS,
884 892 "Open a file with POSIX-like semantics.\n"
885 893 "On error, this function may raise either a WindowsError or an IOError."},
886 894 #else
887 895 {"statfiles", (PyCFunction)statfiles, METH_VARARGS | METH_KEYWORDS,
888 896 "stat a series of files or symlinks\n"
889 897 "Returns None for non-existent entries and entries of other types.\n"},
890 898 #ifdef CMSG_LEN
891 899 {"recvfds", (PyCFunction)recvfds, METH_VARARGS,
892 900 "receive list of file descriptors via socket\n"},
893 901 #endif
894 902 #endif
895 903 #ifdef __APPLE__
896 904 {
897 905 "isgui", (PyCFunction)isgui, METH_NOARGS,
898 906 "Is a CoreGraphics session available?"
899 907 },
900 908 #endif
901 909 {NULL, NULL}
902 910 };
903 911
904 912 #ifdef IS_PY3K
905 913 static struct PyModuleDef osutil_module = {
906 914 PyModuleDef_HEAD_INIT,
907 915 "osutil",
908 916 osutil_doc,
909 917 -1,
910 918 methods
911 919 };
912 920
913 921 PyMODINIT_FUNC PyInit_osutil(void)
914 922 {
915 923 if (PyType_Ready(&listdir_stat_type) < 0)
916 924 return NULL;
917 925
918 926 return PyModule_Create(&osutil_module);
919 927 }
920 928 #else
921 929 PyMODINIT_FUNC initosutil(void)
922 930 {
923 931 if (PyType_Ready(&listdir_stat_type) == -1)
924 932 return;
925 933
926 934 Py_InitModule3("osutil", methods, osutil_doc);
927 935 }
928 936 #endif
General Comments 0
You need to be logged in to leave comments. Login now