Mercurial > hg
annotate mercurial/osutil.c @ 31497:a369482e9649
branchmap: be more careful about using %d on ints
Not doing so breaks Python 3.
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 19 Mar 2017 01:01:25 -0400 |
parents | 95be8b7181d3 |
children | cc3ec3027695 |
rev | line source |
---|---|
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
1 /* |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
2 osutil.c - native operating system services |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
3 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
4 Copyright 2007 Matt Mackall and others |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
5 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
6 This software may be used and distributed according to the terms of |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
7 the GNU General Public License, incorporated herein by reference. |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
8 */ |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
9 |
5463
3b204881f959
osutil: use fdopendir instead of dirfd
Bryan O'Sullivan <bos@serpentine.com>
parents:
5457
diff
changeset
|
10 #define _ATFILE_SOURCE |
5397
11caa374f497
osutil.c: include Python.h before the other headers
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5396
diff
changeset
|
11 #include <Python.h> |
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
12 #include <fcntl.h> |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
13 #include <stdio.h> |
27473
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
14 #include <stdlib.h> |
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
15 #include <string.h> |
14873
f79d47813b8b
osutil: emulate os.listdir's OSError for long names (issue2898)
Matt Mackall <mpm@selenic.com>
parents:
13748
diff
changeset
|
16 #include <errno.h> |
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
17 |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
18 #ifdef _WIN32 |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
9353
diff
changeset
|
19 #include <windows.h> |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
9353
diff
changeset
|
20 #include <io.h> |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
21 #else |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
9353
diff
changeset
|
22 #include <dirent.h> |
27473
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
23 #include <sys/socket.h> |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
9353
diff
changeset
|
24 #include <sys/stat.h> |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
9353
diff
changeset
|
25 #include <sys/types.h> |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
9353
diff
changeset
|
26 #include <unistd.h> |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
27 #endif |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
28 |
24461
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
29 #ifdef __APPLE__ |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
30 #include <sys/attr.h> |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
31 #include <sys/vnode.h> |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
32 #endif |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
33 |
11359
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
34 #include "util.h" |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
35 |
9353
3ac42ca1f3e6
osutil: fix compilation with -ansi
Sebastien Binet <binet@cern.ch>
parents:
8723
diff
changeset
|
36 /* some platforms lack the PATH_MAX definition (eg. GNU/Hurd) */ |
8722
48da69ff79bd
Some platforms lack the PATH_MAX definition (eg. GNU/Hurd).
Arne Babenhauserheide <bab@draketo.de>
parents:
7190
diff
changeset
|
37 #ifndef PATH_MAX |
48da69ff79bd
Some platforms lack the PATH_MAX definition (eg. GNU/Hurd).
Arne Babenhauserheide <bab@draketo.de>
parents:
7190
diff
changeset
|
38 #define PATH_MAX 4096 |
48da69ff79bd
Some platforms lack the PATH_MAX definition (eg. GNU/Hurd).
Arne Babenhauserheide <bab@draketo.de>
parents:
7190
diff
changeset
|
39 #endif |
48da69ff79bd
Some platforms lack the PATH_MAX definition (eg. GNU/Hurd).
Arne Babenhauserheide <bab@draketo.de>
parents:
7190
diff
changeset
|
40 |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
41 #ifdef _WIN32 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
42 /* |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
43 stat struct compatible with hg expectations |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
44 Mercurial only uses st_mode, st_size and st_mtime |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
45 the rest is kept to minimize changes between implementations |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
46 */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
47 struct hg_stat { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
48 int st_dev; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
49 int st_mode; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
50 int st_nlink; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
51 __int64 st_size; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
52 int st_mtime; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
53 int st_ctime; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
54 }; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
55 struct listdir_stat { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
56 PyObject_HEAD |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
57 struct hg_stat st; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
58 }; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
59 #else |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
60 struct listdir_stat { |
5421 | 61 PyObject_HEAD |
62 struct stat st; | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
63 }; |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
64 #endif |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
65 |
30111
a989fa78dafa
osutil: use PyLongObject on Python 3 for listdir_slot
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30110
diff
changeset
|
66 #ifdef IS_PY3K |
a989fa78dafa
osutil: use PyLongObject on Python 3 for listdir_slot
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30110
diff
changeset
|
67 #define listdir_slot(name) \ |
a989fa78dafa
osutil: use PyLongObject on Python 3 for listdir_slot
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30110
diff
changeset
|
68 static PyObject *listdir_stat_##name(PyObject *self, void *x) \ |
a989fa78dafa
osutil: use PyLongObject on Python 3 for listdir_slot
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30110
diff
changeset
|
69 { \ |
a989fa78dafa
osutil: use PyLongObject on Python 3 for listdir_slot
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30110
diff
changeset
|
70 return PyLong_FromLong(((struct listdir_stat *)self)->st.name); \ |
a989fa78dafa
osutil: use PyLongObject on Python 3 for listdir_slot
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30110
diff
changeset
|
71 } |
a989fa78dafa
osutil: use PyLongObject on Python 3 for listdir_slot
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30110
diff
changeset
|
72 #else |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
73 #define listdir_slot(name) \ |
7190
aecea6934fdd
Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents:
7136
diff
changeset
|
74 static PyObject *listdir_stat_##name(PyObject *self, void *x) \ |
aecea6934fdd
Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents:
7136
diff
changeset
|
75 { \ |
aecea6934fdd
Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents:
7136
diff
changeset
|
76 return PyInt_FromLong(((struct listdir_stat *)self)->st.name); \ |
aecea6934fdd
Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents:
7136
diff
changeset
|
77 } |
30111
a989fa78dafa
osutil: use PyLongObject on Python 3 for listdir_slot
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30110
diff
changeset
|
78 #endif |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
79 |
5431
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
80 listdir_slot(st_dev) |
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
81 listdir_slot(st_mode) |
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
82 listdir_slot(st_nlink) |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
83 #ifdef _WIN32 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
84 static PyObject *listdir_stat_st_size(PyObject *self, void *x) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
85 { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
86 return PyLong_FromLongLong( |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
87 (PY_LONG_LONG)((struct listdir_stat *)self)->st.st_size); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
88 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
89 #else |
5431
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
90 listdir_slot(st_size) |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
91 #endif |
5431
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
92 listdir_slot(st_mtime) |
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
93 listdir_slot(st_ctime) |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
94 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
95 static struct PyGetSetDef listdir_stat_getsets[] = { |
5421 | 96 {"st_dev", listdir_stat_st_dev, 0, 0, 0}, |
97 {"st_mode", listdir_stat_st_mode, 0, 0, 0}, | |
98 {"st_nlink", listdir_stat_st_nlink, 0, 0, 0}, | |
99 {"st_size", listdir_stat_st_size, 0, 0, 0}, | |
100 {"st_mtime", listdir_stat_st_mtime, 0, 0, 0}, | |
101 {"st_ctime", listdir_stat_st_ctime, 0, 0, 0}, | |
102 {0, 0, 0, 0, 0} | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
103 }; |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
104 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
105 static PyObject *listdir_stat_new(PyTypeObject *t, PyObject *a, PyObject *k) |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
106 { |
5421 | 107 return t->tp_alloc(t, 0); |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
108 } |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
109 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
110 static void listdir_stat_dealloc(PyObject *o) |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
111 { |
5421 | 112 o->ob_type->tp_free(o); |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
113 } |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
114 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
115 static PyTypeObject listdir_stat_type = { |
11359
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
116 PyVarObject_HEAD_INIT(NULL, 0) |
5421 | 117 "osutil.stat", /*tp_name*/ |
118 sizeof(struct listdir_stat), /*tp_basicsize*/ | |
119 0, /*tp_itemsize*/ | |
120 (destructor)listdir_stat_dealloc, /*tp_dealloc*/ | |
121 0, /*tp_print*/ | |
122 0, /*tp_getattr*/ | |
123 0, /*tp_setattr*/ | |
124 0, /*tp_compare*/ | |
125 0, /*tp_repr*/ | |
126 0, /*tp_as_number*/ | |
127 0, /*tp_as_sequence*/ | |
128 0, /*tp_as_mapping*/ | |
129 0, /*tp_hash */ | |
130 0, /*tp_call*/ | |
131 0, /*tp_str*/ | |
132 0, /*tp_getattro*/ | |
133 0, /*tp_setattro*/ | |
134 0, /*tp_as_buffer*/ | |
135 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ | |
136 "stat objects", /* tp_doc */ | |
137 0, /* tp_traverse */ | |
138 0, /* tp_clear */ | |
139 0, /* tp_richcompare */ | |
140 0, /* tp_weaklistoffset */ | |
141 0, /* tp_iter */ | |
142 0, /* tp_iternext */ | |
143 0, /* tp_methods */ | |
144 0, /* tp_members */ | |
145 listdir_stat_getsets, /* tp_getset */ | |
146 0, /* tp_base */ | |
147 0, /* tp_dict */ | |
148 0, /* tp_descr_get */ | |
149 0, /* tp_descr_set */ | |
150 0, /* tp_dictoffset */ | |
151 0, /* tp_init */ | |
152 0, /* tp_alloc */ | |
153 listdir_stat_new, /* tp_new */ | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
154 }; |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
155 |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
156 #ifdef _WIN32 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
157 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
158 static int to_python_time(const FILETIME *tm) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
159 { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
160 /* number of seconds between epoch and January 1 1601 */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
161 const __int64 a0 = (__int64)134774L * (__int64)24L * (__int64)3600L; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
162 /* conversion factor from 100ns to 1s */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
163 const __int64 a1 = 10000000; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
164 /* explicit (int) cast to suspend compiler warnings */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
165 return (int)((((__int64)tm->dwHighDateTime << 32) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
166 + tm->dwLowDateTime) / a1 - a0); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
167 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
168 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
169 static PyObject *make_item(const WIN32_FIND_DATAA *fd, int wantstat) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
170 { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
171 PyObject *py_st; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
172 struct hg_stat *stp; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
173 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
174 int kind = (fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
175 ? _S_IFDIR : _S_IFREG; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
176 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
177 if (!wantstat) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
178 return Py_BuildValue("si", fd->cFileName, kind); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
179 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
180 py_st = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
181 if (!py_st) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
182 return NULL; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
183 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
184 stp = &((struct listdir_stat *)py_st)->st; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
185 /* |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
186 use kind as st_mode |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
187 rwx bits on Win32 are meaningless |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
188 and Hg does not use them anyway |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
189 */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
190 stp->st_mode = kind; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
191 stp->st_mtime = to_python_time(&fd->ftLastWriteTime); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
192 stp->st_ctime = to_python_time(&fd->ftCreationTime); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
193 if (kind == _S_IFREG) |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
9353
diff
changeset
|
194 stp->st_size = ((__int64)fd->nFileSizeHigh << 32) |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
195 + fd->nFileSizeLow; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
196 return Py_BuildValue("siN", fd->cFileName, |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
197 kind, py_st); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
198 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
199 |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
200 static PyObject *_listdir(char *path, int plen, int wantstat, char *skip) |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
201 { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
202 PyObject *rval = NULL; /* initialize - return value */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
203 PyObject *list; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
204 HANDLE fh; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
205 WIN32_FIND_DATAA fd; |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
206 char *pattern; |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
207 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
208 /* build the path + \* pattern string */ |
31468
b9dd03ed564f
osutil: use Python memory allocator in _listdir
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30409
diff
changeset
|
209 pattern = PyMem_Malloc(plen + 3); /* path + \* + \0 */ |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
210 if (!pattern) { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
211 PyErr_NoMemory(); |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
212 goto error_nomem; |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
213 } |
28593
e60c492a0d9b
osutil: stop using strcpy
Augie Fackler <augie@google.com>
parents:
27970
diff
changeset
|
214 memcpy(pattern, path, plen); |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
215 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
216 if (plen > 0) { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
217 char c = path[plen-1]; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
218 if (c != ':' && c != '/' && c != '\\') |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
219 pattern[plen++] = '\\'; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
220 } |
28593
e60c492a0d9b
osutil: stop using strcpy
Augie Fackler <augie@google.com>
parents:
27970
diff
changeset
|
221 pattern[plen++] = '*'; |
e60c492a0d9b
osutil: stop using strcpy
Augie Fackler <augie@google.com>
parents:
27970
diff
changeset
|
222 pattern[plen] = '\0'; |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
223 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
224 fh = FindFirstFileA(pattern, &fd); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
225 if (fh == INVALID_HANDLE_VALUE) { |
7059
6a76cf980999
Improve error handling in osutil.c
Petr Kodl <petrkodl@gmail.com>
parents:
7056
diff
changeset
|
226 PyErr_SetFromWindowsErrWithFilename(GetLastError(), path); |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
227 goto error_file; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
228 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
229 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
230 list = PyList_New(0); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
231 if (!list) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
232 goto error_list; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
233 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
234 do { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
235 PyObject *item; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
236 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
237 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
238 if (!strcmp(fd.cFileName, ".") |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
239 || !strcmp(fd.cFileName, "..")) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
240 continue; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
241 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
242 if (skip && !strcmp(fd.cFileName, skip)) { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
243 rval = PyList_New(0); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
244 goto error; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
245 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
246 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
247 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
248 item = make_item(&fd, wantstat); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
249 if (!item) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
250 goto error; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
251 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
252 if (PyList_Append(list, item)) { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
253 Py_XDECREF(item); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
254 goto error; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
255 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
256 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
257 Py_XDECREF(item); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
258 } while (FindNextFileA(fh, &fd)); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
259 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
260 if (GetLastError() != ERROR_NO_MORE_FILES) { |
7059
6a76cf980999
Improve error handling in osutil.c
Petr Kodl <petrkodl@gmail.com>
parents:
7056
diff
changeset
|
261 PyErr_SetFromWindowsErrWithFilename(GetLastError(), path); |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
262 goto error; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
263 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
264 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
265 rval = list; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
266 Py_XINCREF(rval); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
267 error: |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
268 Py_XDECREF(list); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
269 error_list: |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
270 FindClose(fh); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
271 error_file: |
31468
b9dd03ed564f
osutil: use Python memory allocator in _listdir
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30409
diff
changeset
|
272 PyMem_Free(pattern); |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
273 error_nomem: |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
274 return rval; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
275 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
276 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
277 #else |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
278 |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
279 int entkind(struct dirent *ent) |
5425
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
280 { |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
281 #ifdef DT_REG |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
282 switch (ent->d_type) { |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
283 case DT_REG: return S_IFREG; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
284 case DT_DIR: return S_IFDIR; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
285 case DT_LNK: return S_IFLNK; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
286 case DT_BLK: return S_IFBLK; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
287 case DT_CHR: return S_IFCHR; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
288 case DT_FIFO: return S_IFIFO; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
289 case DT_SOCK: return S_IFSOCK; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
290 } |
5463
3b204881f959
osutil: use fdopendir instead of dirfd
Bryan O'Sullivan <bos@serpentine.com>
parents:
5457
diff
changeset
|
291 #endif |
7033
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
292 return -1; |
5425
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
293 } |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
294 |
18019
e248bff2d8dd
osutil: factor out creation and init of listdir_stat
Bryan O'Sullivan <bryano@fb.com>
parents:
16747
diff
changeset
|
295 static PyObject *makestat(const struct stat *st) |
e248bff2d8dd
osutil: factor out creation and init of listdir_stat
Bryan O'Sullivan <bryano@fb.com>
parents:
16747
diff
changeset
|
296 { |
18021 | 297 PyObject *stat; |
18019
e248bff2d8dd
osutil: factor out creation and init of listdir_stat
Bryan O'Sullivan <bryano@fb.com>
parents:
16747
diff
changeset
|
298 |
18021 | 299 stat = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL); |
300 if (stat) | |
301 memcpy(&((struct listdir_stat *)stat)->st, st, sizeof(*st)); | |
302 return stat; | |
18019
e248bff2d8dd
osutil: factor out creation and init of listdir_stat
Bryan O'Sullivan <bryano@fb.com>
parents:
16747
diff
changeset
|
303 } |
e248bff2d8dd
osutil: factor out creation and init of listdir_stat
Bryan O'Sullivan <bryano@fb.com>
parents:
16747
diff
changeset
|
304 |
24460
73477e755cd2
osutil._listdir: rename to _listdir_stat
Siddharth Agarwal <sid0@fb.com>
parents:
23966
diff
changeset
|
305 static PyObject *_listdir_stat(char *path, int pathlen, int keepstat, |
73477e755cd2
osutil._listdir: rename to _listdir_stat
Siddharth Agarwal <sid0@fb.com>
parents:
23966
diff
changeset
|
306 char *skip) |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
307 { |
23962
1f3b94e8dc40
osutil: fix leak of stat in makestat when Py_BuildValue fails
Augie Fackler <augie@google.com>
parents:
23961
diff
changeset
|
308 PyObject *list, *elem, *stat = NULL, *ret = NULL; |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
309 char fullpath[PATH_MAX + 10]; |
7136
d834ed27199f
_listdir only uses dfd if AT_SYMLINK_NOFOLLOW is defined
Brendan Cully <brendan@kublai.com>
parents:
7098
diff
changeset
|
310 int kind, err; |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
311 struct stat st; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
312 struct dirent *ent; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
313 DIR *dir; |
7136
d834ed27199f
_listdir only uses dfd if AT_SYMLINK_NOFOLLOW is defined
Brendan Cully <brendan@kublai.com>
parents:
7098
diff
changeset
|
314 #ifdef AT_SYMLINK_NOFOLLOW |
d834ed27199f
_listdir only uses dfd if AT_SYMLINK_NOFOLLOW is defined
Brendan Cully <brendan@kublai.com>
parents:
7098
diff
changeset
|
315 int dfd = -1; |
d834ed27199f
_listdir only uses dfd if AT_SYMLINK_NOFOLLOW is defined
Brendan Cully <brendan@kublai.com>
parents:
7098
diff
changeset
|
316 #endif |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
317 |
7059
6a76cf980999
Improve error handling in osutil.c
Petr Kodl <petrkodl@gmail.com>
parents:
7056
diff
changeset
|
318 if (pathlen >= PATH_MAX) { |
14873
f79d47813b8b
osutil: emulate os.listdir's OSError for long names (issue2898)
Matt Mackall <mpm@selenic.com>
parents:
13748
diff
changeset
|
319 errno = ENAMETOOLONG; |
f79d47813b8b
osutil: emulate os.listdir's OSError for long names (issue2898)
Matt Mackall <mpm@selenic.com>
parents:
13748
diff
changeset
|
320 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
321 goto error_value; |
7059
6a76cf980999
Improve error handling in osutil.c
Petr Kodl <petrkodl@gmail.com>
parents:
7056
diff
changeset
|
322 } |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
323 strncpy(fullpath, path, PATH_MAX); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
324 fullpath[pathlen] = '/'; |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
325 |
7033
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
326 #ifdef AT_SYMLINK_NOFOLLOW |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
327 dfd = open(path, O_RDONLY); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
328 if (dfd == -1) { |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
329 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
330 goto error_value; |
7033
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
331 } |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
332 dir = fdopendir(dfd); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
333 #else |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
334 dir = opendir(path); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
335 #endif |
5421 | 336 if (!dir) { |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
337 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
338 goto error_dir; |
23961
bc851e2851b1
osutil.c: clean up space before a tab
Augie Fackler <augie@google.com>
parents:
18027
diff
changeset
|
339 } |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
340 |
5421 | 341 list = PyList_New(0); |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
342 if (!list) |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
343 goto error_list; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
344 |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
345 while ((ent = readdir(dir))) { |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
346 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
347 continue; |
5416
ca890c0c3f1f
osutil.c: style fix - delete trailing end-of-line spaces
Giorgos Keramidas <keramida@ceid.upatras.gr>
parents:
5398
diff
changeset
|
348 |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
349 kind = entkind(ent); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
350 if (kind == -1 || keepstat) { |
7033
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
351 #ifdef AT_SYMLINK_NOFOLLOW |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
352 err = fstatat(dfd, ent->d_name, &st, |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
353 AT_SYMLINK_NOFOLLOW); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
354 #else |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
355 strncpy(fullpath + pathlen + 1, ent->d_name, |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
356 PATH_MAX - pathlen); |
24462
40b05303ac32
osutil: mark end of string with null char, not 0
Siddharth Agarwal <sid0@fb.com>
parents:
24461
diff
changeset
|
357 fullpath[PATH_MAX] = '\0'; |
7033
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
358 err = lstat(fullpath, &st); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
359 #endif |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
360 if (err == -1) { |
16747
6476a21337a6
osutil: handle deletion race with readdir/stat (issue3463)
Matt Mackall <mpm@selenic.com>
parents:
15098
diff
changeset
|
361 /* race with file deletion? */ |
6476a21337a6
osutil: handle deletion race with readdir/stat (issue3463)
Matt Mackall <mpm@selenic.com>
parents:
15098
diff
changeset
|
362 if (errno == ENOENT) |
6476a21337a6
osutil: handle deletion race with readdir/stat (issue3463)
Matt Mackall <mpm@selenic.com>
parents:
15098
diff
changeset
|
363 continue; |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
364 strncpy(fullpath + pathlen + 1, ent->d_name, |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
365 PATH_MAX - pathlen); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
366 fullpath[PATH_MAX] = 0; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
367 PyErr_SetFromErrnoWithFilename(PyExc_OSError, |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
368 fullpath); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
369 goto error; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
370 } |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
371 kind = st.st_mode & S_IFMT; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
372 } |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
373 |
7034
0d513661d6c2
listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents:
7033
diff
changeset
|
374 /* quit early? */ |
0d513661d6c2
listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents:
7033
diff
changeset
|
375 if (skip && kind == S_IFDIR && !strcmp(ent->d_name, skip)) { |
0d513661d6c2
listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents:
7033
diff
changeset
|
376 ret = PyList_New(0); |
0d513661d6c2
listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents:
7033
diff
changeset
|
377 goto error; |
0d513661d6c2
listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents:
7033
diff
changeset
|
378 } |
0d513661d6c2
listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents:
7033
diff
changeset
|
379 |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
380 if (keepstat) { |
18019
e248bff2d8dd
osutil: factor out creation and init of listdir_stat
Bryan O'Sullivan <bryano@fb.com>
parents:
16747
diff
changeset
|
381 stat = makestat(&st); |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
382 if (!stat) |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
383 goto error; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
384 elem = Py_BuildValue("siN", ent->d_name, kind, stat); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
385 } else |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
386 elem = Py_BuildValue("si", ent->d_name, kind); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
387 if (!elem) |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
388 goto error; |
23962
1f3b94e8dc40
osutil: fix leak of stat in makestat when Py_BuildValue fails
Augie Fackler <augie@google.com>
parents:
23961
diff
changeset
|
389 stat = NULL; |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
390 |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
391 PyList_Append(list, elem); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
392 Py_DECREF(elem); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
393 } |
5421 | 394 |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
395 ret = list; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
396 Py_INCREF(ret); |
5421 | 397 |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
398 error: |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
399 Py_DECREF(list); |
23962
1f3b94e8dc40
osutil: fix leak of stat in makestat when Py_BuildValue fails
Augie Fackler <augie@google.com>
parents:
23961
diff
changeset
|
400 Py_XDECREF(stat); |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
401 error_list: |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
402 closedir(dir); |
31471
95be8b7181d3
osutil: fix potential wrong fd close
Jun Wu <quark@fb.com>
parents:
31468
diff
changeset
|
403 /* closedir also closes its dirfd */ |
95be8b7181d3
osutil: fix potential wrong fd close
Jun Wu <quark@fb.com>
parents:
31468
diff
changeset
|
404 goto error_value; |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
405 error_dir: |
7033
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
406 #ifdef AT_SYMLINK_NOFOLLOW |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
407 close(dfd); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
408 #endif |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
409 error_value: |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
410 return ret; |
5421 | 411 } |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
412 |
24461
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
413 #ifdef __APPLE__ |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
414 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
415 typedef struct { |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
416 u_int32_t length; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
417 attrreference_t name; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
418 fsobj_type_t obj_type; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
419 struct timespec mtime; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
420 #if __LITTLE_ENDIAN__ |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
421 mode_t access_mask; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
422 uint16_t padding; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
423 #else |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
424 uint16_t padding; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
425 mode_t access_mask; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
426 #endif |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
427 off_t size; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
428 } __attribute__((packed)) attrbuf_entry; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
429 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
430 int attrkind(attrbuf_entry *entry) |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
431 { |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
432 switch (entry->obj_type) { |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
433 case VREG: return S_IFREG; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
434 case VDIR: return S_IFDIR; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
435 case VLNK: return S_IFLNK; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
436 case VBLK: return S_IFBLK; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
437 case VCHR: return S_IFCHR; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
438 case VFIFO: return S_IFIFO; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
439 case VSOCK: return S_IFSOCK; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
440 } |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
441 return -1; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
442 } |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
443 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
444 /* get these many entries at a time */ |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
445 #define LISTDIR_BATCH_SIZE 50 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
446 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
447 static PyObject *_listdir_batch(char *path, int pathlen, int keepstat, |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
448 char *skip, bool *fallback) |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
449 { |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
450 PyObject *list, *elem, *stat = NULL, *ret = NULL; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
451 int kind, err; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
452 unsigned long index; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
453 unsigned int count, old_state, new_state; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
454 bool state_seen = false; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
455 attrbuf_entry *entry; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
456 /* from the getattrlist(2) man page: a path can be no longer than |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
457 (NAME_MAX * 3 + 1) bytes. Also, "The getattrlist() function will |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
458 silently truncate attribute data if attrBufSize is too small." So |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
459 pass in a buffer big enough for the worst case. */ |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
460 char attrbuf[LISTDIR_BATCH_SIZE * (sizeof(attrbuf_entry) + NAME_MAX * 3 + 1)]; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
461 unsigned int basep_unused; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
462 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
463 struct stat st; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
464 int dfd = -1; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
465 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
466 /* these must match the attrbuf_entry struct, otherwise you'll end up |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
467 with garbage */ |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
468 struct attrlist requested_attr = {0}; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
469 requested_attr.bitmapcount = ATTR_BIT_MAP_COUNT; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
470 requested_attr.commonattr = (ATTR_CMN_NAME | ATTR_CMN_OBJTYPE | |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
471 ATTR_CMN_MODTIME | ATTR_CMN_ACCESSMASK); |
27877
f6d1e92fdf8c
mac: ignore resource fork when checking file sizes
Matt Mackall <mpm@selenic.com>
parents:
27473
diff
changeset
|
472 requested_attr.fileattr = ATTR_FILE_DATALENGTH; |
24461
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
473 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
474 *fallback = false; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
475 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
476 if (pathlen >= PATH_MAX) { |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
477 errno = ENAMETOOLONG; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
478 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
479 goto error_value; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
480 } |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
481 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
482 dfd = open(path, O_RDONLY); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
483 if (dfd == -1) { |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
484 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
485 goto error_value; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
486 } |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
487 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
488 list = PyList_New(0); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
489 if (!list) |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
490 goto error_dir; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
491 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
492 do { |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
493 count = LISTDIR_BATCH_SIZE; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
494 err = getdirentriesattr(dfd, &requested_attr, &attrbuf, |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
495 sizeof(attrbuf), &count, &basep_unused, |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
496 &new_state, 0); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
497 if (err < 0) { |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
498 if (errno == ENOTSUP) { |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
499 /* We're on a filesystem that doesn't support |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
500 getdirentriesattr. Fall back to the |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
501 stat-based implementation. */ |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
502 *fallback = true; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
503 } else |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
504 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
505 goto error; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
506 } |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
507 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
508 if (!state_seen) { |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
509 old_state = new_state; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
510 state_seen = true; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
511 } else if (old_state != new_state) { |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
512 /* There's an edge case with getdirentriesattr. Consider |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
513 the following initial list of files: |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
514 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
515 a |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
516 b |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
517 <-- |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
518 c |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
519 d |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
520 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
521 If the iteration is paused at the arrow, and b is |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
522 deleted before it is resumed, getdirentriesattr will |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
523 not return d at all! Ordinarily we're expected to |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
524 restart the iteration from the beginning. To avoid |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
525 getting stuck in a retry loop here, fall back to |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
526 stat. */ |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
527 *fallback = true; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
528 goto error; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
529 } |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
530 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
531 entry = (attrbuf_entry *)attrbuf; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
532 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
533 for (index = 0; index < count; index++) { |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
534 char *filename = ((char *)&entry->name) + |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
535 entry->name.attr_dataoffset; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
536 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
537 if (!strcmp(filename, ".") || !strcmp(filename, "..")) |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
538 continue; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
539 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
540 kind = attrkind(entry); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
541 if (kind == -1) { |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
542 PyErr_Format(PyExc_OSError, |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
543 "unknown object type %u for file " |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
544 "%s%s!", |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
545 entry->obj_type, path, filename); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
546 goto error; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
547 } |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
548 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
549 /* quit early? */ |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
550 if (skip && kind == S_IFDIR && !strcmp(filename, skip)) { |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
551 ret = PyList_New(0); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
552 goto error; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
553 } |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
554 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
555 if (keepstat) { |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
556 /* from the getattrlist(2) man page: "Only the |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
557 permission bits ... are valid". */ |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
558 st.st_mode = (entry->access_mask & ~S_IFMT) | kind; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
559 st.st_mtime = entry->mtime.tv_sec; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
560 st.st_size = entry->size; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
561 stat = makestat(&st); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
562 if (!stat) |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
563 goto error; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
564 elem = Py_BuildValue("siN", filename, kind, stat); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
565 } else |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
566 elem = Py_BuildValue("si", filename, kind); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
567 if (!elem) |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
568 goto error; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
569 stat = NULL; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
570 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
571 PyList_Append(list, elem); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
572 Py_DECREF(elem); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
573 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
574 entry = (attrbuf_entry *)((char *)entry + entry->length); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
575 } |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
576 } while (err == 0); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
577 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
578 ret = list; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
579 Py_INCREF(ret); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
580 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
581 error: |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
582 Py_DECREF(list); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
583 Py_XDECREF(stat); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
584 error_dir: |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
585 close(dfd); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
586 error_value: |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
587 return ret; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
588 } |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
589 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
590 #endif /* __APPLE__ */ |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
591 |
24460
73477e755cd2
osutil._listdir: rename to _listdir_stat
Siddharth Agarwal <sid0@fb.com>
parents:
23966
diff
changeset
|
592 static PyObject *_listdir(char *path, int pathlen, int keepstat, char *skip) |
73477e755cd2
osutil._listdir: rename to _listdir_stat
Siddharth Agarwal <sid0@fb.com>
parents:
23966
diff
changeset
|
593 { |
24461
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
594 #ifdef __APPLE__ |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
595 PyObject *ret; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
596 bool fallback = false; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
597 |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
598 ret = _listdir_batch(path, pathlen, keepstat, skip, &fallback); |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
599 if (ret != NULL || !fallback) |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
600 return ret; |
05ccfe6763f1
osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents:
24460
diff
changeset
|
601 #endif |
24460
73477e755cd2
osutil._listdir: rename to _listdir_stat
Siddharth Agarwal <sid0@fb.com>
parents:
23966
diff
changeset
|
602 return _listdir_stat(path, pathlen, keepstat, skip); |
73477e755cd2
osutil._listdir: rename to _listdir_stat
Siddharth Agarwal <sid0@fb.com>
parents:
23966
diff
changeset
|
603 } |
73477e755cd2
osutil._listdir: rename to _listdir_stat
Siddharth Agarwal <sid0@fb.com>
parents:
23966
diff
changeset
|
604 |
18026
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
605 static PyObject *statfiles(PyObject *self, PyObject *args) |
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
606 { |
18027
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
607 PyObject *names, *stats; |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
608 Py_ssize_t i, count; |
18026
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
609 |
18027
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
610 if (!PyArg_ParseTuple(args, "O:statfiles", &names)) |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
611 return NULL; |
18026
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
612 |
18027
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
613 count = PySequence_Length(names); |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
614 if (count == -1) { |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
615 PyErr_SetString(PyExc_TypeError, "not a sequence"); |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
616 return NULL; |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
617 } |
18026
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
618 |
18027
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
619 stats = PyList_New(count); |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
620 if (stats == NULL) |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
621 return NULL; |
18026
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
622 |
18027
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
623 for (i = 0; i < count; i++) { |
23966
2d2c0a8eeeb8
osutil: fix memory leak of PyBytes of path in statfiles
Augie Fackler <augie@google.com>
parents:
23962
diff
changeset
|
624 PyObject *stat, *pypath; |
18027
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
625 struct stat st; |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
626 int ret, kind; |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
627 char *path; |
18026
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
628 |
26983
f9f2f29ce023
osutil: make statfiles check for interrupts periodically
Bryan O'Sullivan <bos@serpentine.com>
parents:
26982
diff
changeset
|
629 /* With a large file count or on a slow filesystem, |
f9f2f29ce023
osutil: make statfiles check for interrupts periodically
Bryan O'Sullivan <bos@serpentine.com>
parents:
26982
diff
changeset
|
630 don't block signals for long (issue4878). */ |
f9f2f29ce023
osutil: make statfiles check for interrupts periodically
Bryan O'Sullivan <bos@serpentine.com>
parents:
26982
diff
changeset
|
631 if ((i % 1000) == 999 && PyErr_CheckSignals() == -1) |
f9f2f29ce023
osutil: make statfiles check for interrupts periodically
Bryan O'Sullivan <bos@serpentine.com>
parents:
26982
diff
changeset
|
632 goto bail; |
f9f2f29ce023
osutil: make statfiles check for interrupts periodically
Bryan O'Sullivan <bos@serpentine.com>
parents:
26982
diff
changeset
|
633 |
23966
2d2c0a8eeeb8
osutil: fix memory leak of PyBytes of path in statfiles
Augie Fackler <augie@google.com>
parents:
23962
diff
changeset
|
634 pypath = PySequence_GetItem(names, i); |
2d2c0a8eeeb8
osutil: fix memory leak of PyBytes of path in statfiles
Augie Fackler <augie@google.com>
parents:
23962
diff
changeset
|
635 if (!pypath) |
26982
fa6685ea7ad8
osutil: don't leak on statfiles error
Bryan O'Sullivan <bos@serpentine.com>
parents:
24462
diff
changeset
|
636 goto bail; |
30098
301ef65e8ebb
osutil: convert PyString* to PyBytes*
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28593
diff
changeset
|
637 path = PyBytes_AsString(pypath); |
18027
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
638 if (path == NULL) { |
23966
2d2c0a8eeeb8
osutil: fix memory leak of PyBytes of path in statfiles
Augie Fackler <augie@google.com>
parents:
23962
diff
changeset
|
639 Py_DECREF(pypath); |
18027
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
640 PyErr_SetString(PyExc_TypeError, "not a string"); |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
641 goto bail; |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
642 } |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
643 ret = lstat(path, &st); |
23966
2d2c0a8eeeb8
osutil: fix memory leak of PyBytes of path in statfiles
Augie Fackler <augie@google.com>
parents:
23962
diff
changeset
|
644 Py_DECREF(pypath); |
18027
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
645 kind = st.st_mode & S_IFMT; |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
646 if (ret != -1 && (kind == S_IFREG || kind == S_IFLNK)) { |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
647 stat = makestat(&st); |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
648 if (stat == NULL) |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
649 goto bail; |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
650 PyList_SET_ITEM(stats, i, stat); |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
651 } else { |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
652 Py_INCREF(Py_None); |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
653 PyList_SET_ITEM(stats, i, Py_None); |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
654 } |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
655 } |
18026
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
656 |
18027
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
657 return stats; |
18026
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
658 |
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
659 bail: |
18027
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
660 Py_DECREF(stats); |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
661 return NULL; |
18026
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
662 } |
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
663 |
27473
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
664 /* |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
665 * recvfds() simply does not release GIL during blocking io operation because |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
666 * command server is known to be single-threaded. |
27970
3d23700cf4a9
osutil: disable compilation of recvfds() on unsupported platforms
Yuya Nishihara <yuya@tcha.org>
parents:
27877
diff
changeset
|
667 * |
3d23700cf4a9
osutil: disable compilation of recvfds() on unsupported platforms
Yuya Nishihara <yuya@tcha.org>
parents:
27877
diff
changeset
|
668 * Old systems such as Solaris don't provide CMSG_LEN, msg_control, etc. |
3d23700cf4a9
osutil: disable compilation of recvfds() on unsupported platforms
Yuya Nishihara <yuya@tcha.org>
parents:
27877
diff
changeset
|
669 * Currently, recvfds() is not supported on these platforms. |
27473
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
670 */ |
27970
3d23700cf4a9
osutil: disable compilation of recvfds() on unsupported platforms
Yuya Nishihara <yuya@tcha.org>
parents:
27877
diff
changeset
|
671 #ifdef CMSG_LEN |
27473
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
672 |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
673 static ssize_t recvfdstobuf(int sockfd, int **rfds, void *cbuf, size_t cbufsize) |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
674 { |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
675 char dummy[1]; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
676 struct iovec iov = {dummy, sizeof(dummy)}; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
677 struct msghdr msgh = {0}; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
678 struct cmsghdr *cmsg; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
679 |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
680 msgh.msg_iov = &iov; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
681 msgh.msg_iovlen = 1; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
682 msgh.msg_control = cbuf; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
683 msgh.msg_controllen = (socklen_t)cbufsize; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
684 if (recvmsg(sockfd, &msgh, 0) < 0) |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
685 return -1; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
686 |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
687 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
688 cmsg = CMSG_NXTHDR(&msgh, cmsg)) { |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
689 if (cmsg->cmsg_level != SOL_SOCKET || |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
690 cmsg->cmsg_type != SCM_RIGHTS) |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
691 continue; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
692 *rfds = (int *)CMSG_DATA(cmsg); |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
693 return (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
694 } |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
695 |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
696 *rfds = cbuf; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
697 return 0; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
698 } |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
699 |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
700 static PyObject *recvfds(PyObject *self, PyObject *args) |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
701 { |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
702 int sockfd; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
703 int *rfds = NULL; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
704 ssize_t rfdscount, i; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
705 char cbuf[256]; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
706 PyObject *rfdslist = NULL; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
707 |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
708 if (!PyArg_ParseTuple(args, "i", &sockfd)) |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
709 return NULL; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
710 |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
711 rfdscount = recvfdstobuf(sockfd, &rfds, cbuf, sizeof(cbuf)); |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
712 if (rfdscount < 0) |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
713 return PyErr_SetFromErrno(PyExc_OSError); |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
714 |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
715 rfdslist = PyList_New(rfdscount); |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
716 if (!rfdslist) |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
717 goto bail; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
718 for (i = 0; i < rfdscount; i++) { |
30110
79f438f5dd02
osutil: use PyLongObject in recvfds
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30098
diff
changeset
|
719 PyObject *obj = PyLong_FromLong(rfds[i]); |
27473
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
720 if (!obj) |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
721 goto bail; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
722 PyList_SET_ITEM(rfdslist, i, obj); |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
723 } |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
724 return rfdslist; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
725 |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
726 bail: |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
727 Py_XDECREF(rfdslist); |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
728 return NULL; |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
729 } |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
730 |
27970
3d23700cf4a9
osutil: disable compilation of recvfds() on unsupported platforms
Yuya Nishihara <yuya@tcha.org>
parents:
27877
diff
changeset
|
731 #endif /* CMSG_LEN */ |
30409
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
732 |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
733 #if defined(HAVE_SETPROCTITLE) |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
734 /* setproctitle is the first choice - available in FreeBSD */ |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
735 #define SETPROCNAME_USE_SETPROCTITLE |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
736 #elif (defined(__linux__) || defined(__APPLE__)) && PY_MAJOR_VERSION == 2 |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
737 /* rewrite the argv buffer in place - works in Linux and OS X. Py_GetArgcArgv |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
738 * in Python 3 returns the copied wchar_t **argv, thus unsupported. */ |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
739 #define SETPROCNAME_USE_ARGVREWRITE |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
740 #else |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
741 #define SETPROCNAME_USE_NONE |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
742 #endif |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
743 |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
744 #ifndef SETPROCNAME_USE_NONE |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
745 static PyObject *setprocname(PyObject *self, PyObject *args) |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
746 { |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
747 const char *name = NULL; |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
748 if (!PyArg_ParseTuple(args, "s", &name)) |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
749 return NULL; |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
750 |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
751 #if defined(SETPROCNAME_USE_SETPROCTITLE) |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
752 setproctitle("%s", name); |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
753 #elif defined(SETPROCNAME_USE_ARGVREWRITE) |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
754 { |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
755 static char *argvstart = NULL; |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
756 static size_t argvsize = 0; |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
757 if (argvstart == NULL) { |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
758 int argc = 0, i; |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
759 char **argv = NULL; |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
760 char *argvend; |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
761 extern void Py_GetArgcArgv(int *argc, char ***argv); |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
762 Py_GetArgcArgv(&argc, &argv); |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
763 |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
764 /* Check the memory we can use. Typically, argv[i] and |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
765 * argv[i + 1] are continuous. */ |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
766 argvend = argvstart = argv[0]; |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
767 for (i = 0; i < argc; ++i) { |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
768 if (argv[i] > argvend || argv[i] < argvstart) |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
769 break; /* not continuous */ |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
770 size_t len = strlen(argv[i]); |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
771 argvend = argv[i] + len + 1 /* '\0' */; |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
772 } |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
773 if (argvend > argvstart) /* sanity check */ |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
774 argvsize = argvend - argvstart; |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
775 } |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
776 |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
777 if (argvstart && argvsize > 1) { |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
778 int n = snprintf(argvstart, argvsize, "%s", name); |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
779 if (n >= 0 && (size_t)n < argvsize) |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
780 memset(argvstart + n, 0, argvsize - n); |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
781 } |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
782 } |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
783 #endif |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
784 |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
785 Py_RETURN_NONE; |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
786 } |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
787 #endif /* ndef SETPROCNAME_USE_NONE */ |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
788 |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
789 #endif /* ndef _WIN32 */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
790 |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
791 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs) |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
792 { |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
793 PyObject *statobj = NULL; /* initialize - optional arg */ |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
794 PyObject *skipobj = NULL; /* initialize - optional arg */ |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
795 char *path, *skip = NULL; |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
796 int wantstat, plen; |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
797 |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
798 static char *kwlist[] = {"path", "stat", "skip", NULL}; |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
799 |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
800 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|OO:listdir", |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
801 kwlist, &path, &plen, &statobj, &skipobj)) |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
802 return NULL; |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
803 |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
804 wantstat = statobj && PyObject_IsTrue(statobj); |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
805 |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
806 if (skipobj && skipobj != Py_None) { |
11359
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
807 skip = PyBytes_AsString(skipobj); |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
808 if (!skip) |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
809 return NULL; |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
810 } |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
811 |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
812 return _listdir(path, plen, wantstat, skip); |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
813 } |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
814 |
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
815 #ifdef _WIN32 |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
816 static PyObject *posixfile(PyObject *self, PyObject *args, PyObject *kwds) |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
817 { |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
818 static char *kwlist[] = {"name", "mode", "buffering", NULL}; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
819 PyObject *file_obj = NULL; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
820 char *name = NULL; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
821 char *mode = "rb"; |
8597
dc1b9e22f288
osutil: silence uninitialized variable warning
Patrick Mezard <pmezard@gmail.com>
parents:
8330
diff
changeset
|
822 DWORD access = 0; |
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
823 DWORD creation; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
824 HANDLE handle; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
825 int fd, flags = 0; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
826 int bufsize = -1; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
827 char m0, m1, m2; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
828 char fpmode[4]; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
829 int fppos = 0; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
830 int plus; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
831 FILE *fp; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
832 |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
833 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:posixfile", kwlist, |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
834 Py_FileSystemDefaultEncoding, |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
835 &name, &mode, &bufsize)) |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
836 return NULL; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
837 |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
838 m0 = mode[0]; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
839 m1 = m0 ? mode[1] : '\0'; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
840 m2 = m1 ? mode[2] : '\0'; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
841 plus = m1 == '+' || m2 == '+'; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
842 |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
843 fpmode[fppos++] = m0; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
844 if (m1 == 'b' || m2 == 'b') { |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
845 flags = _O_BINARY; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
846 fpmode[fppos++] = 'b'; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
847 } |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
848 else |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
849 flags = _O_TEXT; |
13273
764441ecbf2e
osutil: treat open modes 'w' and 'a' as 'w+' and 'a+' in posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents:
11359
diff
changeset
|
850 if (m0 == 'r' && !plus) { |
764441ecbf2e
osutil: treat open modes 'w' and 'a' as 'w+' and 'a+' in posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents:
11359
diff
changeset
|
851 flags |= _O_RDONLY; |
764441ecbf2e
osutil: treat open modes 'w' and 'a' as 'w+' and 'a+' in posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents:
11359
diff
changeset
|
852 access = GENERIC_READ; |
764441ecbf2e
osutil: treat open modes 'w' and 'a' as 'w+' and 'a+' in posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents:
11359
diff
changeset
|
853 } else { |
764441ecbf2e
osutil: treat open modes 'w' and 'a' as 'w+' and 'a+' in posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents:
11359
diff
changeset
|
854 /* |
764441ecbf2e
osutil: treat open modes 'w' and 'a' as 'w+' and 'a+' in posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents:
11359
diff
changeset
|
855 work around http://support.microsoft.com/kb/899149 and |
764441ecbf2e
osutil: treat open modes 'w' and 'a' as 'w+' and 'a+' in posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents:
11359
diff
changeset
|
856 set _O_RDWR for 'w' and 'a', even if mode has no '+' |
764441ecbf2e
osutil: treat open modes 'w' and 'a' as 'w+' and 'a+' in posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents:
11359
diff
changeset
|
857 */ |
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
858 flags |= _O_RDWR; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
859 access = GENERIC_READ | GENERIC_WRITE; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
860 fpmode[fppos++] = '+'; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
861 } |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
862 fpmode[fppos++] = '\0'; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
863 |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
864 switch (m0) { |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
865 case 'r': |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
866 creation = OPEN_EXISTING; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
867 break; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
868 case 'w': |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
869 creation = CREATE_ALWAYS; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
870 break; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
871 case 'a': |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
872 creation = OPEN_ALWAYS; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
873 flags |= _O_APPEND; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
874 break; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
875 default: |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
876 PyErr_Format(PyExc_ValueError, |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
877 "mode string must begin with one of 'r', 'w', " |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
878 "or 'a', not '%c'", m0); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
879 goto bail; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
880 } |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
881 |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
882 handle = CreateFile(name, access, |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
883 FILE_SHARE_READ | FILE_SHARE_WRITE | |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
884 FILE_SHARE_DELETE, |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
885 NULL, |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
886 creation, |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
887 FILE_ATTRIBUTE_NORMAL, |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
888 0); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
889 |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
890 if (handle == INVALID_HANDLE_VALUE) { |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
891 PyErr_SetFromWindowsErrWithFilename(GetLastError(), name); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
892 goto bail; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
893 } |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
894 |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
9353
diff
changeset
|
895 fd = _open_osfhandle((intptr_t)handle, flags); |
11359
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
896 |
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
897 if (fd == -1) { |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
898 CloseHandle(handle); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
899 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
900 goto bail; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
901 } |
11359
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
902 #ifndef IS_PY3K |
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
903 fp = _fdopen(fd, fpmode); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
904 if (fp == NULL) { |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
905 _close(fd); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
906 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
907 goto bail; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
908 } |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
909 |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
910 file_obj = PyFile_FromFile(fp, name, mode, fclose); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
911 if (file_obj == NULL) { |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
912 fclose(fp); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
913 goto bail; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
914 } |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
915 |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
916 PyFile_SetBufSize(file_obj, bufsize); |
11359
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
917 #else |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
918 file_obj = PyFile_FromFd(fd, name, mode, bufsize, NULL, NULL, NULL, 1); |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
919 if (file_obj == NULL) |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
920 goto bail; |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
921 #endif |
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
922 bail: |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
923 PyMem_Free(name); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
924 return file_obj; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
925 } |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
926 #endif |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
927 |
13734
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13302
diff
changeset
|
928 #ifdef __APPLE__ |
13748
26f8844d1757
osutil: replace #import with #include, and add a check for it
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13736
diff
changeset
|
929 #include <ApplicationServices/ApplicationServices.h> |
13734
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13302
diff
changeset
|
930 |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13302
diff
changeset
|
931 static PyObject *isgui(PyObject *self) |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13302
diff
changeset
|
932 { |
13736
f3c4421e121c
osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents:
13734
diff
changeset
|
933 CFDictionaryRef dict = CGSessionCopyCurrentDictionary(); |
13734
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13302
diff
changeset
|
934 |
13736
f3c4421e121c
osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents:
13734
diff
changeset
|
935 if (dict != NULL) { |
f3c4421e121c
osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents:
13734
diff
changeset
|
936 CFRelease(dict); |
15094
258eee414ab7
osutil: avoid accidentally destroying the True object in isgui (issue2937)
Steve Streeting <steve@stevestreeting.com>
parents:
13748
diff
changeset
|
937 Py_RETURN_TRUE; |
13736
f3c4421e121c
osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents:
13734
diff
changeset
|
938 } else { |
15094
258eee414ab7
osutil: avoid accidentally destroying the True object in isgui (issue2937)
Steve Streeting <steve@stevestreeting.com>
parents:
13748
diff
changeset
|
939 Py_RETURN_FALSE; |
13736
f3c4421e121c
osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents:
13734
diff
changeset
|
940 } |
13734
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13302
diff
changeset
|
941 } |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13302
diff
changeset
|
942 #endif |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13302
diff
changeset
|
943 |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
944 static char osutil_doc[] = "Native operating system services."; |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
945 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
946 static PyMethodDef methods[] = { |
5421 | 947 {"listdir", (PyCFunction)listdir, METH_VARARGS | METH_KEYWORDS, |
948 "list a directory\n"}, | |
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
949 #ifdef _WIN32 |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
950 {"posixfile", (PyCFunction)posixfile, METH_VARARGS | METH_KEYWORDS, |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
951 "Open a file with POSIX-like semantics.\n" |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
952 "On error, this function may raise either a WindowsError or an IOError."}, |
18026
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
953 #else |
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
954 {"statfiles", (PyCFunction)statfiles, METH_VARARGS | METH_KEYWORDS, |
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
955 "stat a series of files or symlinks\n" |
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
956 "Returns None for non-existent entries and entries of other types.\n"}, |
27970
3d23700cf4a9
osutil: disable compilation of recvfds() on unsupported platforms
Yuya Nishihara <yuya@tcha.org>
parents:
27877
diff
changeset
|
957 #ifdef CMSG_LEN |
27473
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
958 {"recvfds", (PyCFunction)recvfds, METH_VARARGS, |
34a37a2e03e6
osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents:
26983
diff
changeset
|
959 "receive list of file descriptors via socket\n"}, |
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
960 #endif |
30409
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
961 #ifndef SETPROCNAME_USE_NONE |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
962 {"setprocname", (PyCFunction)setprocname, METH_VARARGS, |
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
963 "set process title (best-effort)\n"}, |
27970
3d23700cf4a9
osutil: disable compilation of recvfds() on unsupported platforms
Yuya Nishihara <yuya@tcha.org>
parents:
27877
diff
changeset
|
964 #endif |
30409
0852161588c6
osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents:
30111
diff
changeset
|
965 #endif /* ndef _WIN32 */ |
13734
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13302
diff
changeset
|
966 #ifdef __APPLE__ |
13736
f3c4421e121c
osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents:
13734
diff
changeset
|
967 { |
f3c4421e121c
osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents:
13734
diff
changeset
|
968 "isgui", (PyCFunction)isgui, METH_NOARGS, |
f3c4421e121c
osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents:
13734
diff
changeset
|
969 "Is a CoreGraphics session available?" |
f3c4421e121c
osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents:
13734
diff
changeset
|
970 }, |
13734
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13302
diff
changeset
|
971 #endif |
5421 | 972 {NULL, NULL} |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
973 }; |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
974 |
11359
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
975 #ifdef IS_PY3K |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
976 static struct PyModuleDef osutil_module = { |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
977 PyModuleDef_HEAD_INIT, |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
978 "osutil", |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
979 osutil_doc, |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
980 -1, |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
981 methods |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
982 }; |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
983 |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
984 PyMODINIT_FUNC PyInit_osutil(void) |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
985 { |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
986 if (PyType_Ready(&listdir_stat_type) < 0) |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
987 return NULL; |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
988 |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
989 return PyModule_Create(&osutil_module); |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
990 } |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
991 #else |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
992 PyMODINIT_FUNC initosutil(void) |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
993 { |
5421 | 994 if (PyType_Ready(&listdir_stat_type) == -1) |
995 return; | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
996 |
5421 | 997 Py_InitModule3("osutil", methods, osutil_doc); |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
998 } |
11359
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
999 #endif |