author | Pierre-Yves David <pierre-yves.david@ens-lyon.org> |
Wed, 16 Jan 2013 14:22:43 +0100 | |
changeset 18428 | 8c10f760ca34 |
parent 18027 | 4ca434500dbf |
child 23961 | bc851e2851b1 |
permissions | -rw-r--r-- |
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> |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
14 |
#include <string.h> |
14873
f79d47813b8b
osutil: emulate os.listdir's OSError for long names (issue2898)
Matt Mackall <mpm@selenic.com>
parents:
13748
diff
changeset
|
15 |
#include <errno.h> |
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
16 |
|
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
17 |
#ifdef _WIN32 |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
9353
diff
changeset
|
18 |
#include <windows.h> |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
9353
diff
changeset
|
19 |
#include <io.h> |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
20 |
#else |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
9353
diff
changeset
|
21 |
#include <dirent.h> |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
9353
diff
changeset
|
22 |
#include <sys/stat.h> |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
9353
diff
changeset
|
23 |
#include <sys/types.h> |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
9353
diff
changeset
|
24 |
#include <unistd.h> |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
25 |
#endif |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
26 |
|
11359
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
27 |
#include "util.h" |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
28 |
|
9353
3ac42ca1f3e6
osutil: fix compilation with -ansi
Sebastien Binet <binet@cern.ch>
parents:
8723
diff
changeset
|
29 |
/* 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
|
30 |
#ifndef PATH_MAX |
48da69ff79bd
Some platforms lack the PATH_MAX definition (eg. GNU/Hurd).
Arne Babenhauserheide <bab@draketo.de>
parents:
7190
diff
changeset
|
31 |
#define PATH_MAX 4096 |
48da69ff79bd
Some platforms lack the PATH_MAX definition (eg. GNU/Hurd).
Arne Babenhauserheide <bab@draketo.de>
parents:
7190
diff
changeset
|
32 |
#endif |
48da69ff79bd
Some platforms lack the PATH_MAX definition (eg. GNU/Hurd).
Arne Babenhauserheide <bab@draketo.de>
parents:
7190
diff
changeset
|
33 |
|
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
34 |
#ifdef _WIN32 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
35 |
/* |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
36 |
stat struct compatible with hg expectations |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
37 |
Mercurial only uses st_mode, st_size and st_mtime |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
38 |
the rest is kept to minimize changes between implementations |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
39 |
*/ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
40 |
struct hg_stat { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
41 |
int st_dev; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
42 |
int st_mode; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
43 |
int st_nlink; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
44 |
__int64 st_size; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
45 |
int st_mtime; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
46 |
int st_ctime; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
47 |
}; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
48 |
struct listdir_stat { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
49 |
PyObject_HEAD |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
50 |
struct hg_stat st; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
51 |
}; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
52 |
#else |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
53 |
struct listdir_stat { |
5421 | 54 |
PyObject_HEAD |
55 |
struct stat st; |
|
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
56 |
}; |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
57 |
#endif |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
58 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
59 |
#define listdir_slot(name) \ |
7190
aecea6934fdd
Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents:
7136
diff
changeset
|
60 |
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
|
61 |
{ \ |
aecea6934fdd
Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents:
7136
diff
changeset
|
62 |
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
|
63 |
} |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
64 |
|
5431
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
65 |
listdir_slot(st_dev) |
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
66 |
listdir_slot(st_mode) |
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
67 |
listdir_slot(st_nlink) |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
68 |
#ifdef _WIN32 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
69 |
static PyObject *listdir_stat_st_size(PyObject *self, void *x) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
70 |
{ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
71 |
return PyLong_FromLongLong( |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
72 |
(PY_LONG_LONG)((struct listdir_stat *)self)->st.st_size); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
73 |
} |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
74 |
#else |
5431
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
75 |
listdir_slot(st_size) |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
76 |
#endif |
5431
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
77 |
listdir_slot(st_mtime) |
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
78 |
listdir_slot(st_ctime) |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
79 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
80 |
static struct PyGetSetDef listdir_stat_getsets[] = { |
5421 | 81 |
{"st_dev", listdir_stat_st_dev, 0, 0, 0}, |
82 |
{"st_mode", listdir_stat_st_mode, 0, 0, 0}, |
|
83 |
{"st_nlink", listdir_stat_st_nlink, 0, 0, 0}, |
|
84 |
{"st_size", listdir_stat_st_size, 0, 0, 0}, |
|
85 |
{"st_mtime", listdir_stat_st_mtime, 0, 0, 0}, |
|
86 |
{"st_ctime", listdir_stat_st_ctime, 0, 0, 0}, |
|
87 |
{0, 0, 0, 0, 0} |
|
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
88 |
}; |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
89 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
90 |
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
|
91 |
{ |
5421 | 92 |
return t->tp_alloc(t, 0); |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
93 |
} |
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 void listdir_stat_dealloc(PyObject *o) |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
96 |
{ |
5421 | 97 |
o->ob_type->tp_free(o); |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
98 |
} |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
99 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
100 |
static PyTypeObject listdir_stat_type = { |
11359
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
101 |
PyVarObject_HEAD_INIT(NULL, 0) |
5421 | 102 |
"osutil.stat", /*tp_name*/ |
103 |
sizeof(struct listdir_stat), /*tp_basicsize*/ |
|
104 |
0, /*tp_itemsize*/ |
|
105 |
(destructor)listdir_stat_dealloc, /*tp_dealloc*/ |
|
106 |
0, /*tp_print*/ |
|
107 |
0, /*tp_getattr*/ |
|
108 |
0, /*tp_setattr*/ |
|
109 |
0, /*tp_compare*/ |
|
110 |
0, /*tp_repr*/ |
|
111 |
0, /*tp_as_number*/ |
|
112 |
0, /*tp_as_sequence*/ |
|
113 |
0, /*tp_as_mapping*/ |
|
114 |
0, /*tp_hash */ |
|
115 |
0, /*tp_call*/ |
|
116 |
0, /*tp_str*/ |
|
117 |
0, /*tp_getattro*/ |
|
118 |
0, /*tp_setattro*/ |
|
119 |
0, /*tp_as_buffer*/ |
|
120 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
|
121 |
"stat objects", /* tp_doc */ |
|
122 |
0, /* tp_traverse */ |
|
123 |
0, /* tp_clear */ |
|
124 |
0, /* tp_richcompare */ |
|
125 |
0, /* tp_weaklistoffset */ |
|
126 |
0, /* tp_iter */ |
|
127 |
0, /* tp_iternext */ |
|
128 |
0, /* tp_methods */ |
|
129 |
0, /* tp_members */ |
|
130 |
listdir_stat_getsets, /* tp_getset */ |
|
131 |
0, /* tp_base */ |
|
132 |
0, /* tp_dict */ |
|
133 |
0, /* tp_descr_get */ |
|
134 |
0, /* tp_descr_set */ |
|
135 |
0, /* tp_dictoffset */ |
|
136 |
0, /* tp_init */ |
|
137 |
0, /* tp_alloc */ |
|
138 |
listdir_stat_new, /* tp_new */ |
|
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
139 |
}; |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
140 |
|
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
141 |
#ifdef _WIN32 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
142 |
|
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
143 |
static int to_python_time(const FILETIME *tm) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
144 |
{ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
145 |
/* number of seconds between epoch and January 1 1601 */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
146 |
const __int64 a0 = (__int64)134774L * (__int64)24L * (__int64)3600L; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
147 |
/* conversion factor from 100ns to 1s */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
148 |
const __int64 a1 = 10000000; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
149 |
/* explicit (int) cast to suspend compiler warnings */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
150 |
return (int)((((__int64)tm->dwHighDateTime << 32) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
151 |
+ tm->dwLowDateTime) / a1 - a0); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
152 |
} |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
153 |
|
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
154 |
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
|
155 |
{ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
156 |
PyObject *py_st; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
157 |
struct hg_stat *stp; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
158 |
|
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
159 |
int kind = (fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
160 |
? _S_IFDIR : _S_IFREG; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
161 |
|
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
162 |
if (!wantstat) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
163 |
return Py_BuildValue("si", fd->cFileName, kind); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
164 |
|
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
165 |
py_st = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
166 |
if (!py_st) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
167 |
return NULL; |
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 |
stp = &((struct listdir_stat *)py_st)->st; |
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 |
use kind as st_mode |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
172 |
rwx bits on Win32 are meaningless |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
173 |
and Hg does not use them anyway |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
174 |
*/ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
175 |
stp->st_mode = kind; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
176 |
stp->st_mtime = to_python_time(&fd->ftLastWriteTime); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
177 |
stp->st_ctime = to_python_time(&fd->ftCreationTime); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
178 |
if (kind == _S_IFREG) |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
9353
diff
changeset
|
179 |
stp->st_size = ((__int64)fd->nFileSizeHigh << 32) |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
180 |
+ fd->nFileSizeLow; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
181 |
return Py_BuildValue("siN", fd->cFileName, |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
182 |
kind, py_st); |
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 |
|
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
185 |
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
|
186 |
{ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
187 |
PyObject *rval = NULL; /* initialize - return value */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
188 |
PyObject *list; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
189 |
HANDLE fh; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
190 |
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
|
191 |
char *pattern; |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
192 |
|
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
193 |
/* build the path + \* pattern string */ |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
9353
diff
changeset
|
194 |
pattern = malloc(plen + 3); /* path + \* + \0 */ |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
195 |
if (!pattern) { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
196 |
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
|
197 |
goto error_nomem; |
7056
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 |
strcpy(pattern, path); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
200 |
|
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
201 |
if (plen > 0) { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
202 |
char c = path[plen-1]; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
203 |
if (c != ':' && c != '/' && c != '\\') |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
204 |
pattern[plen++] = '\\'; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
205 |
} |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
206 |
strcpy(pattern + plen, "*"); |
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 |
fh = FindFirstFileA(pattern, &fd); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
209 |
if (fh == INVALID_HANDLE_VALUE) { |
7059
6a76cf980999
Improve error handling in osutil.c
Petr Kodl <petrkodl@gmail.com>
parents:
7056
diff
changeset
|
210 |
PyErr_SetFromWindowsErrWithFilename(GetLastError(), path); |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
211 |
goto error_file; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
212 |
} |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
213 |
|
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
214 |
list = PyList_New(0); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
215 |
if (!list) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
216 |
goto error_list; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
217 |
|
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
218 |
do { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
219 |
PyObject *item; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
220 |
|
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
221 |
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
222 |
if (!strcmp(fd.cFileName, ".") |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
223 |
|| !strcmp(fd.cFileName, "..")) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
224 |
continue; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
225 |
|
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
226 |
if (skip && !strcmp(fd.cFileName, skip)) { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
227 |
rval = PyList_New(0); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
228 |
goto error; |
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 |
} |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
231 |
|
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
232 |
item = make_item(&fd, wantstat); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
233 |
if (!item) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
234 |
goto error; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
235 |
|
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
236 |
if (PyList_Append(list, item)) { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
237 |
Py_XDECREF(item); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
238 |
goto error; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
239 |
} |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
240 |
|
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
241 |
Py_XDECREF(item); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
242 |
} while (FindNextFileA(fh, &fd)); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
243 |
|
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
244 |
if (GetLastError() != ERROR_NO_MORE_FILES) { |
7059
6a76cf980999
Improve error handling in osutil.c
Petr Kodl <petrkodl@gmail.com>
parents:
7056
diff
changeset
|
245 |
PyErr_SetFromWindowsErrWithFilename(GetLastError(), path); |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
246 |
goto error; |
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 |
|
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
249 |
rval = list; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
250 |
Py_XINCREF(rval); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
251 |
error: |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
252 |
Py_XDECREF(list); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
253 |
error_list: |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
254 |
FindClose(fh); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
255 |
error_file: |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
256 |
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
|
257 |
error_nomem: |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
258 |
return rval; |
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 |
|
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
261 |
#else |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
262 |
|
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
263 |
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
|
264 |
{ |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
265 |
#ifdef DT_REG |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
266 |
switch (ent->d_type) { |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
267 |
case DT_REG: return S_IFREG; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
268 |
case DT_DIR: return S_IFDIR; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
269 |
case DT_LNK: return S_IFLNK; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
270 |
case DT_BLK: return S_IFBLK; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
271 |
case DT_CHR: return S_IFCHR; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
272 |
case DT_FIFO: return S_IFIFO; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
273 |
case DT_SOCK: return S_IFSOCK; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
274 |
} |
5463
3b204881f959
osutil: use fdopendir instead of dirfd
Bryan O'Sullivan <bos@serpentine.com>
parents:
5457
diff
changeset
|
275 |
#endif |
7033
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
276 |
return -1; |
5425
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
277 |
} |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
278 |
|
18019
e248bff2d8dd
osutil: factor out creation and init of listdir_stat
Bryan O'Sullivan <bryano@fb.com>
parents:
16747
diff
changeset
|
279 |
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
|
280 |
{ |
18021 | 281 |
PyObject *stat; |
18019
e248bff2d8dd
osutil: factor out creation and init of listdir_stat
Bryan O'Sullivan <bryano@fb.com>
parents:
16747
diff
changeset
|
282 |
|
18021 | 283 |
stat = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL); |
284 |
if (stat) |
|
285 |
memcpy(&((struct listdir_stat *)stat)->st, st, sizeof(*st)); |
|
286 |
return stat; |
|
18019
e248bff2d8dd
osutil: factor out creation and init of listdir_stat
Bryan O'Sullivan <bryano@fb.com>
parents:
16747
diff
changeset
|
287 |
} |
e248bff2d8dd
osutil: factor out creation and init of listdir_stat
Bryan O'Sullivan <bryano@fb.com>
parents:
16747
diff
changeset
|
288 |
|
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
289 |
static PyObject *_listdir(char *path, int pathlen, int keepstat, char *skip) |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
290 |
{ |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
291 |
PyObject *list, *elem, *stat, *ret = NULL; |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
292 |
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
|
293 |
int kind, err; |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
294 |
struct stat st; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
295 |
struct dirent *ent; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
296 |
DIR *dir; |
7136
d834ed27199f
_listdir only uses dfd if AT_SYMLINK_NOFOLLOW is defined
Brendan Cully <brendan@kublai.com>
parents:
7098
diff
changeset
|
297 |
#ifdef AT_SYMLINK_NOFOLLOW |
d834ed27199f
_listdir only uses dfd if AT_SYMLINK_NOFOLLOW is defined
Brendan Cully <brendan@kublai.com>
parents:
7098
diff
changeset
|
298 |
int dfd = -1; |
d834ed27199f
_listdir only uses dfd if AT_SYMLINK_NOFOLLOW is defined
Brendan Cully <brendan@kublai.com>
parents:
7098
diff
changeset
|
299 |
#endif |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
300 |
|
7059
6a76cf980999
Improve error handling in osutil.c
Petr Kodl <petrkodl@gmail.com>
parents:
7056
diff
changeset
|
301 |
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
|
302 |
errno = ENAMETOOLONG; |
f79d47813b8b
osutil: emulate os.listdir's OSError for long names (issue2898)
Matt Mackall <mpm@selenic.com>
parents:
13748
diff
changeset
|
303 |
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
|
304 |
goto error_value; |
7059
6a76cf980999
Improve error handling in osutil.c
Petr Kodl <petrkodl@gmail.com>
parents:
7056
diff
changeset
|
305 |
} |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
306 |
strncpy(fullpath, path, PATH_MAX); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
307 |
fullpath[pathlen] = '/'; |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
308 |
|
7033
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
309 |
#ifdef AT_SYMLINK_NOFOLLOW |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
310 |
dfd = open(path, O_RDONLY); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
311 |
if (dfd == -1) { |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
312 |
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
|
313 |
goto error_value; |
7033
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
314 |
} |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
315 |
dir = fdopendir(dfd); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
316 |
#else |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
317 |
dir = opendir(path); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
318 |
#endif |
5421 | 319 |
if (!dir) { |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
320 |
PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
321 |
goto error_dir; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
322 |
} |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
323 |
|
5421 | 324 |
list = PyList_New(0); |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
325 |
if (!list) |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
326 |
goto error_list; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
327 |
|
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
328 |
while ((ent = readdir(dir))) { |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
329 |
if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
330 |
continue; |
5416
ca890c0c3f1f
osutil.c: style fix - delete trailing end-of-line spaces
Giorgos Keramidas <keramida@ceid.upatras.gr>
parents:
5398
diff
changeset
|
331 |
|
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
332 |
kind = entkind(ent); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
333 |
if (kind == -1 || keepstat) { |
7033
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
334 |
#ifdef AT_SYMLINK_NOFOLLOW |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
335 |
err = fstatat(dfd, ent->d_name, &st, |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
336 |
AT_SYMLINK_NOFOLLOW); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
337 |
#else |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
338 |
strncpy(fullpath + pathlen + 1, ent->d_name, |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
339 |
PATH_MAX - pathlen); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
340 |
fullpath[PATH_MAX] = 0; |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
341 |
err = lstat(fullpath, &st); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
342 |
#endif |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
343 |
if (err == -1) { |
16747
6476a21337a6
osutil: handle deletion race with readdir/stat (issue3463)
Matt Mackall <mpm@selenic.com>
parents:
15098
diff
changeset
|
344 |
/* race with file deletion? */ |
6476a21337a6
osutil: handle deletion race with readdir/stat (issue3463)
Matt Mackall <mpm@selenic.com>
parents:
15098
diff
changeset
|
345 |
if (errno == ENOENT) |
6476a21337a6
osutil: handle deletion race with readdir/stat (issue3463)
Matt Mackall <mpm@selenic.com>
parents:
15098
diff
changeset
|
346 |
continue; |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
347 |
strncpy(fullpath + pathlen + 1, ent->d_name, |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
348 |
PATH_MAX - pathlen); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
349 |
fullpath[PATH_MAX] = 0; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
350 |
PyErr_SetFromErrnoWithFilename(PyExc_OSError, |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
351 |
fullpath); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
352 |
goto error; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
353 |
} |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
354 |
kind = st.st_mode & S_IFMT; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
355 |
} |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
356 |
|
7034
0d513661d6c2
listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents:
7033
diff
changeset
|
357 |
/* quit early? */ |
0d513661d6c2
listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents:
7033
diff
changeset
|
358 |
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
|
359 |
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
|
360 |
goto error; |
0d513661d6c2
listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents:
7033
diff
changeset
|
361 |
} |
0d513661d6c2
listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents:
7033
diff
changeset
|
362 |
|
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
363 |
if (keepstat) { |
18019
e248bff2d8dd
osutil: factor out creation and init of listdir_stat
Bryan O'Sullivan <bryano@fb.com>
parents:
16747
diff
changeset
|
364 |
stat = makestat(&st); |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
365 |
if (!stat) |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
366 |
goto error; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
367 |
elem = Py_BuildValue("siN", ent->d_name, kind, stat); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
368 |
} else |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
369 |
elem = Py_BuildValue("si", ent->d_name, kind); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
370 |
if (!elem) |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
371 |
goto error; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
372 |
|
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
373 |
PyList_Append(list, elem); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
374 |
Py_DECREF(elem); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
375 |
} |
5421 | 376 |
|
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
377 |
ret = list; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
378 |
Py_INCREF(ret); |
5421 | 379 |
|
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
380 |
error: |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
381 |
Py_DECREF(list); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
382 |
error_list: |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
383 |
closedir(dir); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
384 |
error_dir: |
7033
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
385 |
#ifdef AT_SYMLINK_NOFOLLOW |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
386 |
close(dfd); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
387 |
#endif |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
388 |
error_value: |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
389 |
return ret; |
5421 | 390 |
} |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
391 |
|
18026
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
392 |
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
|
393 |
{ |
18027
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
394 |
PyObject *names, *stats; |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
395 |
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
|
396 |
|
18027
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
397 |
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
|
398 |
return NULL; |
18026
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
399 |
|
18027
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
400 |
count = PySequence_Length(names); |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
401 |
if (count == -1) { |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
402 |
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
|
403 |
return NULL; |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
404 |
} |
18026
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
405 |
|
18027
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
406 |
stats = PyList_New(count); |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
407 |
if (stats == NULL) |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
408 |
return NULL; |
18026
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
409 |
|
18027
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
410 |
for (i = 0; i < count; i++) { |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
411 |
PyObject *stat; |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
412 |
struct stat st; |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
413 |
int ret, kind; |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
414 |
char *path; |
18026
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
415 |
|
18027
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
416 |
path = PyString_AsString(PySequence_GetItem(names, i)); |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
417 |
if (path == NULL) { |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
418 |
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
|
419 |
goto bail; |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
420 |
} |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
421 |
ret = lstat(path, &st); |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
422 |
kind = st.st_mode & S_IFMT; |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
423 |
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
|
424 |
stat = makestat(&st); |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
425 |
if (stat == NULL) |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
426 |
goto bail; |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
427 |
PyList_SET_ITEM(stats, i, stat); |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
428 |
} else { |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
429 |
Py_INCREF(Py_None); |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
430 |
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
|
431 |
} |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
432 |
} |
18026
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
433 |
|
18027
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
434 |
return stats; |
18026
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
435 |
|
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
436 |
bail: |
18027
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
437 |
Py_DECREF(stats); |
4ca434500dbf
osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents:
18026
diff
changeset
|
438 |
return NULL; |
18026
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
439 |
} |
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
440 |
|
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
441 |
#endif /* ndef _WIN32 */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
442 |
|
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
443 |
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
|
444 |
{ |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
445 |
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
|
446 |
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
|
447 |
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
|
448 |
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
|
449 |
|
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
450 |
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
|
451 |
|
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
452 |
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
|
453 |
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
|
454 |
return NULL; |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
455 |
|
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
456 |
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
|
457 |
|
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
458 |
if (skipobj && skipobj != Py_None) { |
11359
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
459 |
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
|
460 |
if (!skip) |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
461 |
return NULL; |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
462 |
} |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
463 |
|
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
464 |
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
|
465 |
} |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
466 |
|
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
467 |
#ifdef _WIN32 |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
468 |
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
|
469 |
{ |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
470 |
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
|
471 |
PyObject *file_obj = NULL; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
472 |
char *name = NULL; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
473 |
char *mode = "rb"; |
8597
dc1b9e22f288
osutil: silence uninitialized variable warning
Patrick Mezard <pmezard@gmail.com>
parents:
8330
diff
changeset
|
474 |
DWORD access = 0; |
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
475 |
DWORD creation; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
476 |
HANDLE handle; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
477 |
int fd, flags = 0; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
478 |
int bufsize = -1; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
479 |
char m0, m1, m2; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
480 |
char fpmode[4]; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
481 |
int fppos = 0; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
482 |
int plus; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
483 |
FILE *fp; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
484 |
|
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
485 |
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
|
486 |
Py_FileSystemDefaultEncoding, |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
487 |
&name, &mode, &bufsize)) |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
488 |
return NULL; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
489 |
|
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
490 |
m0 = mode[0]; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
491 |
m1 = m0 ? mode[1] : '\0'; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
492 |
m2 = m1 ? mode[2] : '\0'; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
493 |
plus = m1 == '+' || m2 == '+'; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
494 |
|
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
495 |
fpmode[fppos++] = m0; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
496 |
if (m1 == 'b' || m2 == 'b') { |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
497 |
flags = _O_BINARY; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
498 |
fpmode[fppos++] = 'b'; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
499 |
} |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
500 |
else |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
501 |
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
|
502 |
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
|
503 |
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
|
504 |
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
|
505 |
} else { |
764441ecbf2e
osutil: treat open modes 'w' and 'a' as 'w+' and 'a+' in posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents:
11359
diff
changeset
|
506 |
/* |
764441ecbf2e
osutil: treat open modes 'w' and 'a' as 'w+' and 'a+' in posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents:
11359
diff
changeset
|
507 |
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
|
508 |
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
|
509 |
*/ |
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
510 |
flags |= _O_RDWR; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
511 |
access = GENERIC_READ | GENERIC_WRITE; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
512 |
fpmode[fppos++] = '+'; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
513 |
} |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
514 |
fpmode[fppos++] = '\0'; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
515 |
|
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
516 |
switch (m0) { |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
517 |
case 'r': |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
518 |
creation = OPEN_EXISTING; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
519 |
break; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
520 |
case 'w': |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
521 |
creation = CREATE_ALWAYS; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
522 |
break; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
523 |
case 'a': |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
524 |
creation = OPEN_ALWAYS; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
525 |
flags |= _O_APPEND; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
526 |
break; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
527 |
default: |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
528 |
PyErr_Format(PyExc_ValueError, |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
529 |
"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
|
530 |
"or 'a', not '%c'", m0); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
531 |
goto bail; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
532 |
} |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
533 |
|
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
534 |
handle = CreateFile(name, access, |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
535 |
FILE_SHARE_READ | FILE_SHARE_WRITE | |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
536 |
FILE_SHARE_DELETE, |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
537 |
NULL, |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
538 |
creation, |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
539 |
FILE_ATTRIBUTE_NORMAL, |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
540 |
0); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
541 |
|
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
542 |
if (handle == INVALID_HANDLE_VALUE) { |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
543 |
PyErr_SetFromWindowsErrWithFilename(GetLastError(), name); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
544 |
goto bail; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
545 |
} |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
546 |
|
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
9353
diff
changeset
|
547 |
fd = _open_osfhandle((intptr_t)handle, flags); |
11359
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
548 |
|
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
549 |
if (fd == -1) { |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
550 |
CloseHandle(handle); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
551 |
PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
552 |
goto bail; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
553 |
} |
11359
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
554 |
#ifndef IS_PY3K |
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
555 |
fp = _fdopen(fd, fpmode); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
556 |
if (fp == NULL) { |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
557 |
_close(fd); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
558 |
PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
559 |
goto bail; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
560 |
} |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
561 |
|
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
562 |
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
|
563 |
if (file_obj == NULL) { |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
564 |
fclose(fp); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
565 |
goto bail; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
566 |
} |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
567 |
|
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
568 |
PyFile_SetBufSize(file_obj, bufsize); |
11359
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
569 |
#else |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
570 |
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
|
571 |
if (file_obj == NULL) |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
572 |
goto bail; |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
573 |
#endif |
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
574 |
bail: |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
575 |
PyMem_Free(name); |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
576 |
return file_obj; |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
577 |
} |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
578 |
#endif |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
579 |
|
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
|
580 |
#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
|
581 |
#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
|
582 |
|
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
|
583 |
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
|
584 |
{ |
13736
f3c4421e121c
osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents:
13734
diff
changeset
|
585 |
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
|
586 |
|
13736
f3c4421e121c
osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents:
13734
diff
changeset
|
587 |
if (dict != NULL) { |
f3c4421e121c
osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents:
13734
diff
changeset
|
588 |
CFRelease(dict); |
15094
258eee414ab7
osutil: avoid accidentally destroying the True object in isgui (issue2937)
Steve Streeting <steve@stevestreeting.com>
parents:
13748
diff
changeset
|
589 |
Py_RETURN_TRUE; |
13736
f3c4421e121c
osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents:
13734
diff
changeset
|
590 |
} else { |
15094
258eee414ab7
osutil: avoid accidentally destroying the True object in isgui (issue2937)
Steve Streeting <steve@stevestreeting.com>
parents:
13748
diff
changeset
|
591 |
Py_RETURN_FALSE; |
13736
f3c4421e121c
osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents:
13734
diff
changeset
|
592 |
} |
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
|
593 |
} |
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
|
594 |
#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
|
595 |
|
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
596 |
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
|
597 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
598 |
static PyMethodDef methods[] = { |
5421 | 599 |
{"listdir", (PyCFunction)listdir, METH_VARARGS | METH_KEYWORDS, |
600 |
"list a directory\n"}, |
|
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
601 |
#ifdef _WIN32 |
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
602 |
{"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
|
603 |
"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
|
604 |
"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
|
605 |
#else |
ddc0323db78b
osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents:
18021
diff
changeset
|
606 |
{"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
|
607 |
"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
|
608 |
"Returns None for non-existent entries and entries of other types.\n"}, |
8330
7de68012f86e
Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents:
7190
diff
changeset
|
609 |
#endif |
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
|
610 |
#ifdef __APPLE__ |
13736
f3c4421e121c
osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents:
13734
diff
changeset
|
611 |
{ |
f3c4421e121c
osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents:
13734
diff
changeset
|
612 |
"isgui", (PyCFunction)isgui, METH_NOARGS, |
f3c4421e121c
osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents:
13734
diff
changeset
|
613 |
"Is a CoreGraphics session available?" |
f3c4421e121c
osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents:
13734
diff
changeset
|
614 |
}, |
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
|
615 |
#endif |
5421 | 616 |
{NULL, NULL} |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
617 |
}; |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
618 |
|
11359
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
619 |
#ifdef IS_PY3K |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
620 |
static struct PyModuleDef osutil_module = { |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
621 |
PyModuleDef_HEAD_INIT, |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
622 |
"osutil", |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
623 |
osutil_doc, |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
624 |
-1, |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
625 |
methods |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
626 |
}; |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
627 |
|
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
628 |
PyMODINIT_FUNC PyInit_osutil(void) |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
629 |
{ |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
630 |
if (PyType_Ready(&listdir_stat_type) < 0) |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
631 |
return NULL; |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
632 |
|
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
633 |
return PyModule_Create(&osutil_module); |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
634 |
} |
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
635 |
#else |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
636 |
PyMODINIT_FUNC initosutil(void) |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
637 |
{ |
5421 | 638 |
if (PyType_Ready(&listdir_stat_type) == -1) |
639 |
return; |
|
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
640 |
|
5421 | 641 |
Py_InitModule3("osutil", methods, osutil_doc); |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
642 |
} |
11359
4eaacccbb2ca
osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
643 |
#endif |