Show More
@@ -1,645 +1,650 b'' | |||
|
1 | 1 | /* |
|
2 | 2 | osutil.c - native operating system services |
|
3 | 3 | |
|
4 | 4 | Copyright 2007 Matt Mackall and others |
|
5 | 5 | |
|
6 | 6 | This software may be used and distributed according to the terms of |
|
7 | 7 | the GNU General Public License, incorporated herein by reference. |
|
8 | 8 | */ |
|
9 | 9 | |
|
10 | 10 | #define _ATFILE_SOURCE |
|
11 | 11 | #include <Python.h> |
|
12 | 12 | #include <fcntl.h> |
|
13 | 13 | #include <stdio.h> |
|
14 | 14 | #include <string.h> |
|
15 | 15 | #include <errno.h> |
|
16 | 16 | |
|
17 | 17 | #ifdef _WIN32 |
|
18 | 18 | #include <windows.h> |
|
19 | 19 | #include <io.h> |
|
20 | 20 | #else |
|
21 | 21 | #include <dirent.h> |
|
22 | 22 | #include <sys/stat.h> |
|
23 | 23 | #include <sys/types.h> |
|
24 | 24 | #include <unistd.h> |
|
25 | 25 | #endif |
|
26 | 26 | |
|
27 | 27 | #include "util.h" |
|
28 | 28 | |
|
29 | 29 | /* some platforms lack the PATH_MAX definition (eg. GNU/Hurd) */ |
|
30 | 30 | #ifndef PATH_MAX |
|
31 | 31 | #define PATH_MAX 4096 |
|
32 | 32 | #endif |
|
33 | 33 | |
|
34 | 34 | #ifdef _WIN32 |
|
35 | 35 | /* |
|
36 | 36 | stat struct compatible with hg expectations |
|
37 | 37 | Mercurial only uses st_mode, st_size and st_mtime |
|
38 | 38 | the rest is kept to minimize changes between implementations |
|
39 | 39 | */ |
|
40 | 40 | struct hg_stat { |
|
41 | 41 | int st_dev; |
|
42 | 42 | int st_mode; |
|
43 | 43 | int st_nlink; |
|
44 | 44 | __int64 st_size; |
|
45 | 45 | int st_mtime; |
|
46 | 46 | int st_ctime; |
|
47 | 47 | }; |
|
48 | 48 | struct listdir_stat { |
|
49 | 49 | PyObject_HEAD |
|
50 | 50 | struct hg_stat st; |
|
51 | 51 | }; |
|
52 | 52 | #else |
|
53 | 53 | struct listdir_stat { |
|
54 | 54 | PyObject_HEAD |
|
55 | 55 | struct stat st; |
|
56 | 56 | }; |
|
57 | 57 | #endif |
|
58 | 58 | |
|
59 | 59 | #define listdir_slot(name) \ |
|
60 | 60 | static PyObject *listdir_stat_##name(PyObject *self, void *x) \ |
|
61 | 61 | { \ |
|
62 | 62 | return PyInt_FromLong(((struct listdir_stat *)self)->st.name); \ |
|
63 | 63 | } |
|
64 | 64 | |
|
65 | 65 | listdir_slot(st_dev) |
|
66 | 66 | listdir_slot(st_mode) |
|
67 | 67 | listdir_slot(st_nlink) |
|
68 | 68 | #ifdef _WIN32 |
|
69 | 69 | static PyObject *listdir_stat_st_size(PyObject *self, void *x) |
|
70 | 70 | { |
|
71 | 71 | return PyLong_FromLongLong( |
|
72 | 72 | (PY_LONG_LONG)((struct listdir_stat *)self)->st.st_size); |
|
73 | 73 | } |
|
74 | 74 | #else |
|
75 | 75 | listdir_slot(st_size) |
|
76 | 76 | #endif |
|
77 | 77 | listdir_slot(st_mtime) |
|
78 | 78 | listdir_slot(st_ctime) |
|
79 | 79 | |
|
80 | 80 | static struct PyGetSetDef listdir_stat_getsets[] = { |
|
81 | 81 | {"st_dev", listdir_stat_st_dev, 0, 0, 0}, |
|
82 | 82 | {"st_mode", listdir_stat_st_mode, 0, 0, 0}, |
|
83 | 83 | {"st_nlink", listdir_stat_st_nlink, 0, 0, 0}, |
|
84 | 84 | {"st_size", listdir_stat_st_size, 0, 0, 0}, |
|
85 | 85 | {"st_mtime", listdir_stat_st_mtime, 0, 0, 0}, |
|
86 | 86 | {"st_ctime", listdir_stat_st_ctime, 0, 0, 0}, |
|
87 | 87 | {0, 0, 0, 0, 0} |
|
88 | 88 | }; |
|
89 | 89 | |
|
90 | 90 | static PyObject *listdir_stat_new(PyTypeObject *t, PyObject *a, PyObject *k) |
|
91 | 91 | { |
|
92 | 92 | return t->tp_alloc(t, 0); |
|
93 | 93 | } |
|
94 | 94 | |
|
95 | 95 | static void listdir_stat_dealloc(PyObject *o) |
|
96 | 96 | { |
|
97 | 97 | o->ob_type->tp_free(o); |
|
98 | 98 | } |
|
99 | 99 | |
|
100 | 100 | static PyTypeObject listdir_stat_type = { |
|
101 | 101 | PyVarObject_HEAD_INIT(NULL, 0) |
|
102 | 102 | "osutil.stat", /*tp_name*/ |
|
103 | 103 | sizeof(struct listdir_stat), /*tp_basicsize*/ |
|
104 | 104 | 0, /*tp_itemsize*/ |
|
105 | 105 | (destructor)listdir_stat_dealloc, /*tp_dealloc*/ |
|
106 | 106 | 0, /*tp_print*/ |
|
107 | 107 | 0, /*tp_getattr*/ |
|
108 | 108 | 0, /*tp_setattr*/ |
|
109 | 109 | 0, /*tp_compare*/ |
|
110 | 110 | 0, /*tp_repr*/ |
|
111 | 111 | 0, /*tp_as_number*/ |
|
112 | 112 | 0, /*tp_as_sequence*/ |
|
113 | 113 | 0, /*tp_as_mapping*/ |
|
114 | 114 | 0, /*tp_hash */ |
|
115 | 115 | 0, /*tp_call*/ |
|
116 | 116 | 0, /*tp_str*/ |
|
117 | 117 | 0, /*tp_getattro*/ |
|
118 | 118 | 0, /*tp_setattro*/ |
|
119 | 119 | 0, /*tp_as_buffer*/ |
|
120 | 120 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
|
121 | 121 | "stat objects", /* tp_doc */ |
|
122 | 122 | 0, /* tp_traverse */ |
|
123 | 123 | 0, /* tp_clear */ |
|
124 | 124 | 0, /* tp_richcompare */ |
|
125 | 125 | 0, /* tp_weaklistoffset */ |
|
126 | 126 | 0, /* tp_iter */ |
|
127 | 127 | 0, /* tp_iternext */ |
|
128 | 128 | 0, /* tp_methods */ |
|
129 | 129 | 0, /* tp_members */ |
|
130 | 130 | listdir_stat_getsets, /* tp_getset */ |
|
131 | 131 | 0, /* tp_base */ |
|
132 | 132 | 0, /* tp_dict */ |
|
133 | 133 | 0, /* tp_descr_get */ |
|
134 | 134 | 0, /* tp_descr_set */ |
|
135 | 135 | 0, /* tp_dictoffset */ |
|
136 | 136 | 0, /* tp_init */ |
|
137 | 137 | 0, /* tp_alloc */ |
|
138 | 138 | listdir_stat_new, /* tp_new */ |
|
139 | 139 | }; |
|
140 | 140 | |
|
141 | 141 | #ifdef _WIN32 |
|
142 | 142 | |
|
143 | 143 | static int to_python_time(const FILETIME *tm) |
|
144 | 144 | { |
|
145 | 145 | /* number of seconds between epoch and January 1 1601 */ |
|
146 | 146 | const __int64 a0 = (__int64)134774L * (__int64)24L * (__int64)3600L; |
|
147 | 147 | /* conversion factor from 100ns to 1s */ |
|
148 | 148 | const __int64 a1 = 10000000; |
|
149 | 149 | /* explicit (int) cast to suspend compiler warnings */ |
|
150 | 150 | return (int)((((__int64)tm->dwHighDateTime << 32) |
|
151 | 151 | + tm->dwLowDateTime) / a1 - a0); |
|
152 | 152 | } |
|
153 | 153 | |
|
154 | 154 | static PyObject *make_item(const WIN32_FIND_DATAA *fd, int wantstat) |
|
155 | 155 | { |
|
156 | 156 | PyObject *py_st; |
|
157 | 157 | struct hg_stat *stp; |
|
158 | 158 | |
|
159 | 159 | int kind = (fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
|
160 | 160 | ? _S_IFDIR : _S_IFREG; |
|
161 | 161 | |
|
162 | 162 | if (!wantstat) |
|
163 | 163 | return Py_BuildValue("si", fd->cFileName, kind); |
|
164 | 164 | |
|
165 | 165 | py_st = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL); |
|
166 | 166 | if (!py_st) |
|
167 | 167 | return NULL; |
|
168 | 168 | |
|
169 | 169 | stp = &((struct listdir_stat *)py_st)->st; |
|
170 | 170 | /* |
|
171 | 171 | use kind as st_mode |
|
172 | 172 | rwx bits on Win32 are meaningless |
|
173 | 173 | and Hg does not use them anyway |
|
174 | 174 | */ |
|
175 | 175 | stp->st_mode = kind; |
|
176 | 176 | stp->st_mtime = to_python_time(&fd->ftLastWriteTime); |
|
177 | 177 | stp->st_ctime = to_python_time(&fd->ftCreationTime); |
|
178 | 178 | if (kind == _S_IFREG) |
|
179 | 179 | stp->st_size = ((__int64)fd->nFileSizeHigh << 32) |
|
180 | 180 | + fd->nFileSizeLow; |
|
181 | 181 | return Py_BuildValue("siN", fd->cFileName, |
|
182 | 182 | kind, py_st); |
|
183 | 183 | } |
|
184 | 184 | |
|
185 | 185 | static PyObject *_listdir(char *path, int plen, int wantstat, char *skip) |
|
186 | 186 | { |
|
187 | 187 | PyObject *rval = NULL; /* initialize - return value */ |
|
188 | 188 | PyObject *list; |
|
189 | 189 | HANDLE fh; |
|
190 | 190 | WIN32_FIND_DATAA fd; |
|
191 | 191 | char *pattern; |
|
192 | 192 | |
|
193 | 193 | /* build the path + \* pattern string */ |
|
194 | 194 | pattern = malloc(plen + 3); /* path + \* + \0 */ |
|
195 | 195 | if (!pattern) { |
|
196 | 196 | PyErr_NoMemory(); |
|
197 | 197 | goto error_nomem; |
|
198 | 198 | } |
|
199 | 199 | strcpy(pattern, path); |
|
200 | 200 | |
|
201 | 201 | if (plen > 0) { |
|
202 | 202 | char c = path[plen-1]; |
|
203 | 203 | if (c != ':' && c != '/' && c != '\\') |
|
204 | 204 | pattern[plen++] = '\\'; |
|
205 | 205 | } |
|
206 | 206 | strcpy(pattern + plen, "*"); |
|
207 | 207 | |
|
208 | 208 | fh = FindFirstFileA(pattern, &fd); |
|
209 | 209 | if (fh == INVALID_HANDLE_VALUE) { |
|
210 | 210 | PyErr_SetFromWindowsErrWithFilename(GetLastError(), path); |
|
211 | 211 | goto error_file; |
|
212 | 212 | } |
|
213 | 213 | |
|
214 | 214 | list = PyList_New(0); |
|
215 | 215 | if (!list) |
|
216 | 216 | goto error_list; |
|
217 | 217 | |
|
218 | 218 | do { |
|
219 | 219 | PyObject *item; |
|
220 | 220 | |
|
221 | 221 | if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
|
222 | 222 | if (!strcmp(fd.cFileName, ".") |
|
223 | 223 | || !strcmp(fd.cFileName, "..")) |
|
224 | 224 | continue; |
|
225 | 225 | |
|
226 | 226 | if (skip && !strcmp(fd.cFileName, skip)) { |
|
227 | 227 | rval = PyList_New(0); |
|
228 | 228 | goto error; |
|
229 | 229 | } |
|
230 | 230 | } |
|
231 | 231 | |
|
232 | 232 | item = make_item(&fd, wantstat); |
|
233 | 233 | if (!item) |
|
234 | 234 | goto error; |
|
235 | 235 | |
|
236 | 236 | if (PyList_Append(list, item)) { |
|
237 | 237 | Py_XDECREF(item); |
|
238 | 238 | goto error; |
|
239 | 239 | } |
|
240 | 240 | |
|
241 | 241 | Py_XDECREF(item); |
|
242 | 242 | } while (FindNextFileA(fh, &fd)); |
|
243 | 243 | |
|
244 | 244 | if (GetLastError() != ERROR_NO_MORE_FILES) { |
|
245 | 245 | PyErr_SetFromWindowsErrWithFilename(GetLastError(), path); |
|
246 | 246 | goto error; |
|
247 | 247 | } |
|
248 | 248 | |
|
249 | 249 | rval = list; |
|
250 | 250 | Py_XINCREF(rval); |
|
251 | 251 | error: |
|
252 | 252 | Py_XDECREF(list); |
|
253 | 253 | error_list: |
|
254 | 254 | FindClose(fh); |
|
255 | 255 | error_file: |
|
256 | 256 | free(pattern); |
|
257 | 257 | error_nomem: |
|
258 | 258 | return rval; |
|
259 | 259 | } |
|
260 | 260 | |
|
261 | 261 | #else |
|
262 | 262 | |
|
263 | 263 | int entkind(struct dirent *ent) |
|
264 | 264 | { |
|
265 | 265 | #ifdef DT_REG |
|
266 | 266 | switch (ent->d_type) { |
|
267 | 267 | case DT_REG: return S_IFREG; |
|
268 | 268 | case DT_DIR: return S_IFDIR; |
|
269 | 269 | case DT_LNK: return S_IFLNK; |
|
270 | 270 | case DT_BLK: return S_IFBLK; |
|
271 | 271 | case DT_CHR: return S_IFCHR; |
|
272 | 272 | case DT_FIFO: return S_IFIFO; |
|
273 | 273 | case DT_SOCK: return S_IFSOCK; |
|
274 | 274 | } |
|
275 | 275 | #endif |
|
276 | 276 | return -1; |
|
277 | 277 | } |
|
278 | 278 | |
|
279 | 279 | static PyObject *makestat(const struct stat *st) |
|
280 | 280 | { |
|
281 | 281 | PyObject *stat; |
|
282 | 282 | |
|
283 | 283 | stat = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL); |
|
284 | 284 | if (stat) |
|
285 | 285 | memcpy(&((struct listdir_stat *)stat)->st, st, sizeof(*st)); |
|
286 | 286 | return stat; |
|
287 | 287 | } |
|
288 | 288 | |
|
289 | 289 | static PyObject *_listdir(char *path, int pathlen, int keepstat, char *skip) |
|
290 | 290 | { |
|
291 | 291 | PyObject *list, *elem, *stat = NULL, *ret = NULL; |
|
292 | 292 | char fullpath[PATH_MAX + 10]; |
|
293 | 293 | int kind, err; |
|
294 | 294 | struct stat st; |
|
295 | 295 | struct dirent *ent; |
|
296 | 296 | DIR *dir; |
|
297 | 297 | #ifdef AT_SYMLINK_NOFOLLOW |
|
298 | 298 | int dfd = -1; |
|
299 | 299 | #endif |
|
300 | 300 | |
|
301 | 301 | if (pathlen >= PATH_MAX) { |
|
302 | 302 | errno = ENAMETOOLONG; |
|
303 | 303 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
|
304 | 304 | goto error_value; |
|
305 | 305 | } |
|
306 | 306 | strncpy(fullpath, path, PATH_MAX); |
|
307 | 307 | fullpath[pathlen] = '/'; |
|
308 | 308 | |
|
309 | 309 | #ifdef AT_SYMLINK_NOFOLLOW |
|
310 | 310 | dfd = open(path, O_RDONLY); |
|
311 | 311 | if (dfd == -1) { |
|
312 | 312 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
|
313 | 313 | goto error_value; |
|
314 | 314 | } |
|
315 | 315 | dir = fdopendir(dfd); |
|
316 | 316 | #else |
|
317 | 317 | dir = opendir(path); |
|
318 | 318 | #endif |
|
319 | 319 | if (!dir) { |
|
320 | 320 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
|
321 | 321 | goto error_dir; |
|
322 | 322 | } |
|
323 | 323 | |
|
324 | 324 | list = PyList_New(0); |
|
325 | 325 | if (!list) |
|
326 | 326 | goto error_list; |
|
327 | 327 | |
|
328 | 328 | while ((ent = readdir(dir))) { |
|
329 | 329 | if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) |
|
330 | 330 | continue; |
|
331 | 331 | |
|
332 | 332 | kind = entkind(ent); |
|
333 | 333 | if (kind == -1 || keepstat) { |
|
334 | 334 | #ifdef AT_SYMLINK_NOFOLLOW |
|
335 | 335 | err = fstatat(dfd, ent->d_name, &st, |
|
336 | 336 | AT_SYMLINK_NOFOLLOW); |
|
337 | 337 | #else |
|
338 | 338 | strncpy(fullpath + pathlen + 1, ent->d_name, |
|
339 | 339 | PATH_MAX - pathlen); |
|
340 | 340 | fullpath[PATH_MAX] = 0; |
|
341 | 341 | err = lstat(fullpath, &st); |
|
342 | 342 | #endif |
|
343 | 343 | if (err == -1) { |
|
344 | 344 | /* race with file deletion? */ |
|
345 | 345 | if (errno == ENOENT) |
|
346 | 346 | continue; |
|
347 | 347 | strncpy(fullpath + pathlen + 1, ent->d_name, |
|
348 | 348 | PATH_MAX - pathlen); |
|
349 | 349 | fullpath[PATH_MAX] = 0; |
|
350 | 350 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, |
|
351 | 351 | fullpath); |
|
352 | 352 | goto error; |
|
353 | 353 | } |
|
354 | 354 | kind = st.st_mode & S_IFMT; |
|
355 | 355 | } |
|
356 | 356 | |
|
357 | 357 | /* quit early? */ |
|
358 | 358 | if (skip && kind == S_IFDIR && !strcmp(ent->d_name, skip)) { |
|
359 | 359 | ret = PyList_New(0); |
|
360 | 360 | goto error; |
|
361 | 361 | } |
|
362 | 362 | |
|
363 | 363 | if (keepstat) { |
|
364 | 364 | stat = makestat(&st); |
|
365 | 365 | if (!stat) |
|
366 | 366 | goto error; |
|
367 | 367 | elem = Py_BuildValue("siN", ent->d_name, kind, stat); |
|
368 | 368 | } else |
|
369 | 369 | elem = Py_BuildValue("si", ent->d_name, kind); |
|
370 | 370 | if (!elem) |
|
371 | 371 | goto error; |
|
372 | 372 | stat = NULL; |
|
373 | 373 | |
|
374 | 374 | PyList_Append(list, elem); |
|
375 | 375 | Py_DECREF(elem); |
|
376 | 376 | } |
|
377 | 377 | |
|
378 | 378 | ret = list; |
|
379 | 379 | Py_INCREF(ret); |
|
380 | 380 | |
|
381 | 381 | error: |
|
382 | 382 | Py_DECREF(list); |
|
383 | 383 | Py_XDECREF(stat); |
|
384 | 384 | error_list: |
|
385 | 385 | closedir(dir); |
|
386 | 386 | error_dir: |
|
387 | 387 | #ifdef AT_SYMLINK_NOFOLLOW |
|
388 | 388 | close(dfd); |
|
389 | 389 | #endif |
|
390 | 390 | error_value: |
|
391 | 391 | return ret; |
|
392 | 392 | } |
|
393 | 393 | |
|
394 | 394 | static PyObject *statfiles(PyObject *self, PyObject *args) |
|
395 | 395 | { |
|
396 | 396 | PyObject *names, *stats; |
|
397 | 397 | Py_ssize_t i, count; |
|
398 | 398 | |
|
399 | 399 | if (!PyArg_ParseTuple(args, "O:statfiles", &names)) |
|
400 | 400 | return NULL; |
|
401 | 401 | |
|
402 | 402 | count = PySequence_Length(names); |
|
403 | 403 | if (count == -1) { |
|
404 | 404 | PyErr_SetString(PyExc_TypeError, "not a sequence"); |
|
405 | 405 | return NULL; |
|
406 | 406 | } |
|
407 | 407 | |
|
408 | 408 | stats = PyList_New(count); |
|
409 | 409 | if (stats == NULL) |
|
410 | 410 | return NULL; |
|
411 | 411 | |
|
412 | 412 | for (i = 0; i < count; i++) { |
|
413 | PyObject *stat; | |
|
413 | PyObject *stat, *pypath; | |
|
414 | 414 | struct stat st; |
|
415 | 415 | int ret, kind; |
|
416 | 416 | char *path; |
|
417 | 417 | |
|
418 |
path = |
|
|
418 | pypath = PySequence_GetItem(names, i); | |
|
419 | if (!pypath) | |
|
420 | return NULL; | |
|
421 | path = PyString_AsString(pypath); | |
|
419 | 422 | if (path == NULL) { |
|
423 | Py_DECREF(pypath); | |
|
420 | 424 | PyErr_SetString(PyExc_TypeError, "not a string"); |
|
421 | 425 | goto bail; |
|
422 | 426 | } |
|
423 | 427 | ret = lstat(path, &st); |
|
428 | Py_DECREF(pypath); | |
|
424 | 429 | kind = st.st_mode & S_IFMT; |
|
425 | 430 | if (ret != -1 && (kind == S_IFREG || kind == S_IFLNK)) { |
|
426 | 431 | stat = makestat(&st); |
|
427 | 432 | if (stat == NULL) |
|
428 | 433 | goto bail; |
|
429 | 434 | PyList_SET_ITEM(stats, i, stat); |
|
430 | 435 | } else { |
|
431 | 436 | Py_INCREF(Py_None); |
|
432 | 437 | PyList_SET_ITEM(stats, i, Py_None); |
|
433 | 438 | } |
|
434 | 439 | } |
|
435 | 440 | |
|
436 | 441 | return stats; |
|
437 | 442 | |
|
438 | 443 | bail: |
|
439 | 444 | Py_DECREF(stats); |
|
440 | 445 | return NULL; |
|
441 | 446 | } |
|
442 | 447 | |
|
443 | 448 | #endif /* ndef _WIN32 */ |
|
444 | 449 | |
|
445 | 450 | static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs) |
|
446 | 451 | { |
|
447 | 452 | PyObject *statobj = NULL; /* initialize - optional arg */ |
|
448 | 453 | PyObject *skipobj = NULL; /* initialize - optional arg */ |
|
449 | 454 | char *path, *skip = NULL; |
|
450 | 455 | int wantstat, plen; |
|
451 | 456 | |
|
452 | 457 | static char *kwlist[] = {"path", "stat", "skip", NULL}; |
|
453 | 458 | |
|
454 | 459 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|OO:listdir", |
|
455 | 460 | kwlist, &path, &plen, &statobj, &skipobj)) |
|
456 | 461 | return NULL; |
|
457 | 462 | |
|
458 | 463 | wantstat = statobj && PyObject_IsTrue(statobj); |
|
459 | 464 | |
|
460 | 465 | if (skipobj && skipobj != Py_None) { |
|
461 | 466 | skip = PyBytes_AsString(skipobj); |
|
462 | 467 | if (!skip) |
|
463 | 468 | return NULL; |
|
464 | 469 | } |
|
465 | 470 | |
|
466 | 471 | return _listdir(path, plen, wantstat, skip); |
|
467 | 472 | } |
|
468 | 473 | |
|
469 | 474 | #ifdef _WIN32 |
|
470 | 475 | static PyObject *posixfile(PyObject *self, PyObject *args, PyObject *kwds) |
|
471 | 476 | { |
|
472 | 477 | static char *kwlist[] = {"name", "mode", "buffering", NULL}; |
|
473 | 478 | PyObject *file_obj = NULL; |
|
474 | 479 | char *name = NULL; |
|
475 | 480 | char *mode = "rb"; |
|
476 | 481 | DWORD access = 0; |
|
477 | 482 | DWORD creation; |
|
478 | 483 | HANDLE handle; |
|
479 | 484 | int fd, flags = 0; |
|
480 | 485 | int bufsize = -1; |
|
481 | 486 | char m0, m1, m2; |
|
482 | 487 | char fpmode[4]; |
|
483 | 488 | int fppos = 0; |
|
484 | 489 | int plus; |
|
485 | 490 | FILE *fp; |
|
486 | 491 | |
|
487 | 492 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:posixfile", kwlist, |
|
488 | 493 | Py_FileSystemDefaultEncoding, |
|
489 | 494 | &name, &mode, &bufsize)) |
|
490 | 495 | return NULL; |
|
491 | 496 | |
|
492 | 497 | m0 = mode[0]; |
|
493 | 498 | m1 = m0 ? mode[1] : '\0'; |
|
494 | 499 | m2 = m1 ? mode[2] : '\0'; |
|
495 | 500 | plus = m1 == '+' || m2 == '+'; |
|
496 | 501 | |
|
497 | 502 | fpmode[fppos++] = m0; |
|
498 | 503 | if (m1 == 'b' || m2 == 'b') { |
|
499 | 504 | flags = _O_BINARY; |
|
500 | 505 | fpmode[fppos++] = 'b'; |
|
501 | 506 | } |
|
502 | 507 | else |
|
503 | 508 | flags = _O_TEXT; |
|
504 | 509 | if (m0 == 'r' && !plus) { |
|
505 | 510 | flags |= _O_RDONLY; |
|
506 | 511 | access = GENERIC_READ; |
|
507 | 512 | } else { |
|
508 | 513 | /* |
|
509 | 514 | work around http://support.microsoft.com/kb/899149 and |
|
510 | 515 | set _O_RDWR for 'w' and 'a', even if mode has no '+' |
|
511 | 516 | */ |
|
512 | 517 | flags |= _O_RDWR; |
|
513 | 518 | access = GENERIC_READ | GENERIC_WRITE; |
|
514 | 519 | fpmode[fppos++] = '+'; |
|
515 | 520 | } |
|
516 | 521 | fpmode[fppos++] = '\0'; |
|
517 | 522 | |
|
518 | 523 | switch (m0) { |
|
519 | 524 | case 'r': |
|
520 | 525 | creation = OPEN_EXISTING; |
|
521 | 526 | break; |
|
522 | 527 | case 'w': |
|
523 | 528 | creation = CREATE_ALWAYS; |
|
524 | 529 | break; |
|
525 | 530 | case 'a': |
|
526 | 531 | creation = OPEN_ALWAYS; |
|
527 | 532 | flags |= _O_APPEND; |
|
528 | 533 | break; |
|
529 | 534 | default: |
|
530 | 535 | PyErr_Format(PyExc_ValueError, |
|
531 | 536 | "mode string must begin with one of 'r', 'w', " |
|
532 | 537 | "or 'a', not '%c'", m0); |
|
533 | 538 | goto bail; |
|
534 | 539 | } |
|
535 | 540 | |
|
536 | 541 | handle = CreateFile(name, access, |
|
537 | 542 | FILE_SHARE_READ | FILE_SHARE_WRITE | |
|
538 | 543 | FILE_SHARE_DELETE, |
|
539 | 544 | NULL, |
|
540 | 545 | creation, |
|
541 | 546 | FILE_ATTRIBUTE_NORMAL, |
|
542 | 547 | 0); |
|
543 | 548 | |
|
544 | 549 | if (handle == INVALID_HANDLE_VALUE) { |
|
545 | 550 | PyErr_SetFromWindowsErrWithFilename(GetLastError(), name); |
|
546 | 551 | goto bail; |
|
547 | 552 | } |
|
548 | 553 | |
|
549 | 554 | fd = _open_osfhandle((intptr_t)handle, flags); |
|
550 | 555 | |
|
551 | 556 | if (fd == -1) { |
|
552 | 557 | CloseHandle(handle); |
|
553 | 558 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); |
|
554 | 559 | goto bail; |
|
555 | 560 | } |
|
556 | 561 | #ifndef IS_PY3K |
|
557 | 562 | fp = _fdopen(fd, fpmode); |
|
558 | 563 | if (fp == NULL) { |
|
559 | 564 | _close(fd); |
|
560 | 565 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); |
|
561 | 566 | goto bail; |
|
562 | 567 | } |
|
563 | 568 | |
|
564 | 569 | file_obj = PyFile_FromFile(fp, name, mode, fclose); |
|
565 | 570 | if (file_obj == NULL) { |
|
566 | 571 | fclose(fp); |
|
567 | 572 | goto bail; |
|
568 | 573 | } |
|
569 | 574 | |
|
570 | 575 | PyFile_SetBufSize(file_obj, bufsize); |
|
571 | 576 | #else |
|
572 | 577 | file_obj = PyFile_FromFd(fd, name, mode, bufsize, NULL, NULL, NULL, 1); |
|
573 | 578 | if (file_obj == NULL) |
|
574 | 579 | goto bail; |
|
575 | 580 | #endif |
|
576 | 581 | bail: |
|
577 | 582 | PyMem_Free(name); |
|
578 | 583 | return file_obj; |
|
579 | 584 | } |
|
580 | 585 | #endif |
|
581 | 586 | |
|
582 | 587 | #ifdef __APPLE__ |
|
583 | 588 | #include <ApplicationServices/ApplicationServices.h> |
|
584 | 589 | |
|
585 | 590 | static PyObject *isgui(PyObject *self) |
|
586 | 591 | { |
|
587 | 592 | CFDictionaryRef dict = CGSessionCopyCurrentDictionary(); |
|
588 | 593 | |
|
589 | 594 | if (dict != NULL) { |
|
590 | 595 | CFRelease(dict); |
|
591 | 596 | Py_RETURN_TRUE; |
|
592 | 597 | } else { |
|
593 | 598 | Py_RETURN_FALSE; |
|
594 | 599 | } |
|
595 | 600 | } |
|
596 | 601 | #endif |
|
597 | 602 | |
|
598 | 603 | static char osutil_doc[] = "Native operating system services."; |
|
599 | 604 | |
|
600 | 605 | static PyMethodDef methods[] = { |
|
601 | 606 | {"listdir", (PyCFunction)listdir, METH_VARARGS | METH_KEYWORDS, |
|
602 | 607 | "list a directory\n"}, |
|
603 | 608 | #ifdef _WIN32 |
|
604 | 609 | {"posixfile", (PyCFunction)posixfile, METH_VARARGS | METH_KEYWORDS, |
|
605 | 610 | "Open a file with POSIX-like semantics.\n" |
|
606 | 611 | "On error, this function may raise either a WindowsError or an IOError."}, |
|
607 | 612 | #else |
|
608 | 613 | {"statfiles", (PyCFunction)statfiles, METH_VARARGS | METH_KEYWORDS, |
|
609 | 614 | "stat a series of files or symlinks\n" |
|
610 | 615 | "Returns None for non-existent entries and entries of other types.\n"}, |
|
611 | 616 | #endif |
|
612 | 617 | #ifdef __APPLE__ |
|
613 | 618 | { |
|
614 | 619 | "isgui", (PyCFunction)isgui, METH_NOARGS, |
|
615 | 620 | "Is a CoreGraphics session available?" |
|
616 | 621 | }, |
|
617 | 622 | #endif |
|
618 | 623 | {NULL, NULL} |
|
619 | 624 | }; |
|
620 | 625 | |
|
621 | 626 | #ifdef IS_PY3K |
|
622 | 627 | static struct PyModuleDef osutil_module = { |
|
623 | 628 | PyModuleDef_HEAD_INIT, |
|
624 | 629 | "osutil", |
|
625 | 630 | osutil_doc, |
|
626 | 631 | -1, |
|
627 | 632 | methods |
|
628 | 633 | }; |
|
629 | 634 | |
|
630 | 635 | PyMODINIT_FUNC PyInit_osutil(void) |
|
631 | 636 | { |
|
632 | 637 | if (PyType_Ready(&listdir_stat_type) < 0) |
|
633 | 638 | return NULL; |
|
634 | 639 | |
|
635 | 640 | return PyModule_Create(&osutil_module); |
|
636 | 641 | } |
|
637 | 642 | #else |
|
638 | 643 | PyMODINIT_FUNC initosutil(void) |
|
639 | 644 | { |
|
640 | 645 | if (PyType_Ready(&listdir_stat_type) == -1) |
|
641 | 646 | return; |
|
642 | 647 | |
|
643 | 648 | Py_InitModule3("osutil", methods, osutil_doc); |
|
644 | 649 | } |
|
645 | 650 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now