author | Benoit Boissinot <benoit.boissinot@ens-lyon.org> |
Wed, 10 Sep 2008 22:37:07 +0200 | |
changeset 7026 | 3e49127bcec3 |
parent 5463 | 3b204881f959 |
child 7031 | 19e8d034932e |
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> |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
12 |
#include <dirent.h> |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
13 |
#include <fcntl.h> |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
14 |
#include <string.h> |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
15 |
#include <sys/stat.h> |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
16 |
#include <sys/types.h> |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
17 |
#include <unistd.h> |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
18 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
19 |
struct listdir_stat { |
5421 | 20 |
PyObject_HEAD |
21 |
struct stat st; |
|
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
22 |
}; |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
23 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
24 |
#define listdir_slot(name) \ |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
25 |
static PyObject *listdir_stat_##name(PyObject *self, void *x) \ |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
26 |
{ \ |
5421 | 27 |
return PyInt_FromLong(((struct listdir_stat *)self)->st.name); \ |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
28 |
} |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
29 |
|
5431
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
30 |
listdir_slot(st_dev) |
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
31 |
listdir_slot(st_mode) |
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
32 |
listdir_slot(st_nlink) |
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
33 |
listdir_slot(st_size) |
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
34 |
listdir_slot(st_mtime) |
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
35 |
listdir_slot(st_ctime) |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
36 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
37 |
static struct PyGetSetDef listdir_stat_getsets[] = { |
5421 | 38 |
{"st_dev", listdir_stat_st_dev, 0, 0, 0}, |
39 |
{"st_mode", listdir_stat_st_mode, 0, 0, 0}, |
|
40 |
{"st_nlink", listdir_stat_st_nlink, 0, 0, 0}, |
|
41 |
{"st_size", listdir_stat_st_size, 0, 0, 0}, |
|
42 |
{"st_mtime", listdir_stat_st_mtime, 0, 0, 0}, |
|
43 |
{"st_ctime", listdir_stat_st_ctime, 0, 0, 0}, |
|
44 |
{0, 0, 0, 0, 0} |
|
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
45 |
}; |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
46 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
47 |
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
|
48 |
{ |
5421 | 49 |
return t->tp_alloc(t, 0); |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
50 |
} |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
51 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
52 |
static void listdir_stat_dealloc(PyObject *o) |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
53 |
{ |
5421 | 54 |
o->ob_type->tp_free(o); |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
55 |
} |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
56 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
57 |
static PyTypeObject listdir_stat_type = { |
5421 | 58 |
PyObject_HEAD_INIT(NULL) |
59 |
0, /*ob_size*/ |
|
60 |
"osutil.stat", /*tp_name*/ |
|
61 |
sizeof(struct listdir_stat), /*tp_basicsize*/ |
|
62 |
0, /*tp_itemsize*/ |
|
63 |
(destructor)listdir_stat_dealloc, /*tp_dealloc*/ |
|
64 |
0, /*tp_print*/ |
|
65 |
0, /*tp_getattr*/ |
|
66 |
0, /*tp_setattr*/ |
|
67 |
0, /*tp_compare*/ |
|
68 |
0, /*tp_repr*/ |
|
69 |
0, /*tp_as_number*/ |
|
70 |
0, /*tp_as_sequence*/ |
|
71 |
0, /*tp_as_mapping*/ |
|
72 |
0, /*tp_hash */ |
|
73 |
0, /*tp_call*/ |
|
74 |
0, /*tp_str*/ |
|
75 |
0, /*tp_getattro*/ |
|
76 |
0, /*tp_setattro*/ |
|
77 |
0, /*tp_as_buffer*/ |
|
78 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
|
79 |
"stat objects", /* tp_doc */ |
|
80 |
0, /* tp_traverse */ |
|
81 |
0, /* tp_clear */ |
|
82 |
0, /* tp_richcompare */ |
|
83 |
0, /* tp_weaklistoffset */ |
|
84 |
0, /* tp_iter */ |
|
85 |
0, /* tp_iternext */ |
|
86 |
0, /* tp_methods */ |
|
87 |
0, /* tp_members */ |
|
88 |
listdir_stat_getsets, /* tp_getset */ |
|
89 |
0, /* tp_base */ |
|
90 |
0, /* tp_dict */ |
|
91 |
0, /* tp_descr_get */ |
|
92 |
0, /* tp_descr_set */ |
|
93 |
0, /* tp_dictoffset */ |
|
94 |
0, /* tp_init */ |
|
95 |
0, /* tp_alloc */ |
|
96 |
listdir_stat_new, /* tp_new */ |
|
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
97 |
}; |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
98 |
|
5428 | 99 |
static PyObject *listfiles(PyObject *list, DIR *dir, |
100 |
int keep_stat, int *need_stat) |
|
5427
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
101 |
{ |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
102 |
struct dirent *ent; |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
103 |
PyObject *name, *py_kind, *val; |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
104 |
|
5428 | 105 |
#ifdef DT_REG |
106 |
*need_stat = 0; |
|
107 |
#else |
|
108 |
*need_stat = 1; |
|
109 |
#endif |
|
110 |
||
5427
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
111 |
for (ent = readdir(dir); ent; ent = readdir(dir)) { |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
112 |
int kind = -1; |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
113 |
|
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
114 |
if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
115 |
continue; |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
116 |
|
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
117 |
#ifdef DT_REG |
5428 | 118 |
if (!keep_stat) |
5427
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
119 |
switch (ent->d_type) { |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
120 |
case DT_REG: kind = S_IFREG; break; |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
121 |
case DT_DIR: kind = S_IFDIR; break; |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
122 |
case DT_LNK: kind = S_IFLNK; break; |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
123 |
case DT_BLK: kind = S_IFBLK; break; |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
124 |
case DT_CHR: kind = S_IFCHR; break; |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
125 |
case DT_FIFO: kind = S_IFIFO; break; |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
126 |
case DT_SOCK: kind = S_IFSOCK; break; |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
127 |
default: |
5428 | 128 |
*need_stat = 0; |
5427
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
129 |
break; |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
130 |
} |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
131 |
#endif |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
132 |
|
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
133 |
if (kind != -1) |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
134 |
py_kind = PyInt_FromLong(kind); |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
135 |
else { |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
136 |
py_kind = Py_None; |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
137 |
Py_INCREF(Py_None); |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
138 |
} |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
139 |
|
5428 | 140 |
val = PyTuple_New(keep_stat ? 3 : 2); |
5427
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
141 |
name = PyString_FromString(ent->d_name); |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
142 |
|
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
143 |
if (!name || !py_kind || !val) { |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
144 |
Py_XDECREF(name); |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
145 |
Py_XDECREF(py_kind); |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
146 |
Py_XDECREF(val); |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
147 |
return PyErr_NoMemory(); |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
148 |
} |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
149 |
|
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
150 |
PyTuple_SET_ITEM(val, 0, name); |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
151 |
PyTuple_SET_ITEM(val, 1, py_kind); |
5428 | 152 |
if (keep_stat) { |
5427
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
153 |
PyTuple_SET_ITEM(val, 2, Py_None); |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
154 |
Py_INCREF(Py_None); |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
155 |
} |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
156 |
|
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
157 |
PyList_Append(list, val); |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
158 |
Py_DECREF(val); |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
159 |
} |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
160 |
|
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
161 |
return 0; |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
162 |
} |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
163 |
|
5425
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
164 |
static PyObject *statfiles(PyObject *list, PyObject *ctor_args, int keep, |
5463
3b204881f959
osutil: use fdopendir instead of dirfd
Bryan O'Sullivan <bos@serpentine.com>
parents:
5457
diff
changeset
|
165 |
char *path, int len, int dfd) |
5425
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
166 |
{ |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
167 |
struct stat buf; |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
168 |
struct stat *stp = &buf; |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
169 |
int kind; |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
170 |
int ret; |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
171 |
ssize_t i; |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
172 |
ssize_t size = PyList_Size(list); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
173 |
|
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
174 |
for (i = 0; i < size; i++) { |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
175 |
PyObject *elt = PyList_GetItem(list, i); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
176 |
char *name = PyString_AsString(PyTuple_GET_ITEM(elt, 0)); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
177 |
PyObject *py_st = NULL; |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
178 |
PyObject *py_kind = PyTuple_GET_ITEM(elt, 1); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
179 |
|
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
180 |
kind = py_kind == Py_None ? -1 : PyInt_AsLong(py_kind); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
181 |
if (kind != -1 && !keep) |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
182 |
continue; |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
183 |
|
5430
0bdea0abe62e
osutil.c: use strncpy instead of strncat
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5428
diff
changeset
|
184 |
strncpy(path + len + 1, name, PATH_MAX - len); |
5425
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
185 |
path[PATH_MAX] = 0; |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
186 |
|
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
187 |
if (keep) { |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
188 |
py_st = PyObject_CallObject( |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
189 |
(PyObject *)&listdir_stat_type, ctor_args); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
190 |
if (!py_st) |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
191 |
return PyErr_NoMemory(); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
192 |
stp = &((struct listdir_stat *)py_st)->st; |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
193 |
PyTuple_SET_ITEM(elt, 2, py_st); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
194 |
} |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
195 |
|
5463
3b204881f959
osutil: use fdopendir instead of dirfd
Bryan O'Sullivan <bos@serpentine.com>
parents:
5457
diff
changeset
|
196 |
#ifdef AT_SYMLINK_NOFOLLOW |
3b204881f959
osutil: use fdopendir instead of dirfd
Bryan O'Sullivan <bos@serpentine.com>
parents:
5457
diff
changeset
|
197 |
ret = fstatat(dfd, name, stp, AT_SYMLINK_NOFOLLOW); |
3b204881f959
osutil: use fdopendir instead of dirfd
Bryan O'Sullivan <bos@serpentine.com>
parents:
5457
diff
changeset
|
198 |
#else |
5425
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
199 |
ret = lstat(path, stp); |
5463
3b204881f959
osutil: use fdopendir instead of dirfd
Bryan O'Sullivan <bos@serpentine.com>
parents:
5457
diff
changeset
|
200 |
#endif |
5425
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
201 |
if (ret == -1) |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
202 |
return PyErr_SetFromErrnoWithFilename(PyExc_OSError, |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
203 |
path); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
204 |
|
5457
7372b6bbc5e6
osutil: improve portability
Matt Mackall <mpm@selenic.com>
parents:
5435
diff
changeset
|
205 |
if (kind == -1) { |
7372b6bbc5e6
osutil: improve portability
Matt Mackall <mpm@selenic.com>
parents:
5435
diff
changeset
|
206 |
if (S_ISREG(stp->st_mode)) |
7372b6bbc5e6
osutil: improve portability
Matt Mackall <mpm@selenic.com>
parents:
5435
diff
changeset
|
207 |
kind = S_IFREG; |
7372b6bbc5e6
osutil: improve portability
Matt Mackall <mpm@selenic.com>
parents:
5435
diff
changeset
|
208 |
else if (S_ISDIR(stp->st_mode)) |
7372b6bbc5e6
osutil: improve portability
Matt Mackall <mpm@selenic.com>
parents:
5435
diff
changeset
|
209 |
kind = S_IFDIR; |
7372b6bbc5e6
osutil: improve portability
Matt Mackall <mpm@selenic.com>
parents:
5435
diff
changeset
|
210 |
else if (S_ISLNK(stp->st_mode)) |
7372b6bbc5e6
osutil: improve portability
Matt Mackall <mpm@selenic.com>
parents:
5435
diff
changeset
|
211 |
kind = S_IFLNK; |
7372b6bbc5e6
osutil: improve portability
Matt Mackall <mpm@selenic.com>
parents:
5435
diff
changeset
|
212 |
else if (S_ISBLK(stp->st_mode)) |
7372b6bbc5e6
osutil: improve portability
Matt Mackall <mpm@selenic.com>
parents:
5435
diff
changeset
|
213 |
kind = S_IFBLK; |
7372b6bbc5e6
osutil: improve portability
Matt Mackall <mpm@selenic.com>
parents:
5435
diff
changeset
|
214 |
else if (S_ISCHR(stp->st_mode)) |
7372b6bbc5e6
osutil: improve portability
Matt Mackall <mpm@selenic.com>
parents:
5435
diff
changeset
|
215 |
kind = S_IFCHR; |
7372b6bbc5e6
osutil: improve portability
Matt Mackall <mpm@selenic.com>
parents:
5435
diff
changeset
|
216 |
else if (S_ISFIFO(stp->st_mode)) |
7372b6bbc5e6
osutil: improve portability
Matt Mackall <mpm@selenic.com>
parents:
5435
diff
changeset
|
217 |
kind = S_IFIFO; |
7372b6bbc5e6
osutil: improve portability
Matt Mackall <mpm@selenic.com>
parents:
5435
diff
changeset
|
218 |
else if (S_ISSOCK(stp->st_mode)) |
7372b6bbc5e6
osutil: improve portability
Matt Mackall <mpm@selenic.com>
parents:
5435
diff
changeset
|
219 |
kind = S_IFSOCK; |
7372b6bbc5e6
osutil: improve portability
Matt Mackall <mpm@selenic.com>
parents:
5435
diff
changeset
|
220 |
else |
7372b6bbc5e6
osutil: improve portability
Matt Mackall <mpm@selenic.com>
parents:
5435
diff
changeset
|
221 |
kind = stp->st_mode; |
7372b6bbc5e6
osutil: improve portability
Matt Mackall <mpm@selenic.com>
parents:
5435
diff
changeset
|
222 |
} |
5425
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
223 |
|
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
224 |
if (py_kind == Py_None && kind != -1) { |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
225 |
py_kind = PyInt_FromLong(kind); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
226 |
if (!py_kind) |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
227 |
return PyErr_NoMemory(); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
228 |
Py_XDECREF(Py_None); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
229 |
PyTuple_SET_ITEM(elt, 1, py_kind); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
230 |
} |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
231 |
} |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
232 |
|
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
233 |
return 0; |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
234 |
} |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
235 |
|
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
236 |
static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs) |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
237 |
{ |
5416
ca890c0c3f1f
osutil.c: style fix - delete trailing end-of-line spaces
Giorgos Keramidas <keramida@ceid.upatras.gr>
parents:
5398
diff
changeset
|
238 |
static char *kwlist[] = { "path", "stat", NULL }; |
5428 | 239 |
DIR *dir = NULL; |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
240 |
PyObject *statobj = NULL; |
5421 | 241 |
PyObject *list = NULL; |
5425
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
242 |
PyObject *err = NULL; |
5421 | 243 |
PyObject *ctor_args = NULL; |
5428 | 244 |
char *path; |
5422
a3ba7ef98c94
osutil: eliminate alloca call
Matt Mackall <mpm@selenic.com>
parents:
5421
diff
changeset
|
245 |
char full_path[PATH_MAX + 10]; |
5421 | 246 |
int path_len; |
5428 | 247 |
int need_stat, keep_stat; |
5463
3b204881f959
osutil: use fdopendir instead of dirfd
Bryan O'Sullivan <bos@serpentine.com>
parents:
5457
diff
changeset
|
248 |
int dfd; |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
249 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
250 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:listdir", kwlist, |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
251 |
&path, &path_len, &statobj)) |
5421 | 252 |
goto bail; |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
253 |
|
5428 | 254 |
keep_stat = statobj && PyObject_IsTrue(statobj); |
5416
ca890c0c3f1f
osutil.c: style fix - delete trailing end-of-line spaces
Giorgos Keramidas <keramida@ceid.upatras.gr>
parents:
5398
diff
changeset
|
255 |
|
5463
3b204881f959
osutil: use fdopendir instead of dirfd
Bryan O'Sullivan <bos@serpentine.com>
parents:
5457
diff
changeset
|
256 |
#ifdef AT_SYMLINK_NOFOLLOW |
3b204881f959
osutil: use fdopendir instead of dirfd
Bryan O'Sullivan <bos@serpentine.com>
parents:
5457
diff
changeset
|
257 |
dfd = open(path, O_RDONLY); |
7026
3e49127bcec3
osutil: proper error checking and reporting
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
5463
diff
changeset
|
258 |
if (dfd != -1) |
3e49127bcec3
osutil: proper error checking and reporting
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
5463
diff
changeset
|
259 |
dir = fdopendir(dfd); |
5463
3b204881f959
osutil: use fdopendir instead of dirfd
Bryan O'Sullivan <bos@serpentine.com>
parents:
5457
diff
changeset
|
260 |
#else |
5421 | 261 |
dir = opendir(path); |
5463
3b204881f959
osutil: use fdopendir instead of dirfd
Bryan O'Sullivan <bos@serpentine.com>
parents:
5457
diff
changeset
|
262 |
dfd = -1; |
3b204881f959
osutil: use fdopendir instead of dirfd
Bryan O'Sullivan <bos@serpentine.com>
parents:
5457
diff
changeset
|
263 |
#endif |
5421 | 264 |
if (!dir) { |
5428 | 265 |
err = PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
5421 | 266 |
goto bail; |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
267 |
} |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
268 |
|
5421 | 269 |
list = PyList_New(0); |
5428 | 270 |
ctor_args = PyTuple_New(0); |
271 |
if (!list || !ctor_args) |
|
5421 | 272 |
goto bail; |
5416
ca890c0c3f1f
osutil.c: style fix - delete trailing end-of-line spaces
Giorgos Keramidas <keramida@ceid.upatras.gr>
parents:
5398
diff
changeset
|
273 |
|
5422
a3ba7ef98c94
osutil: eliminate alloca call
Matt Mackall <mpm@selenic.com>
parents:
5421
diff
changeset
|
274 |
strncpy(full_path, path, PATH_MAX); |
5421 | 275 |
full_path[path_len] = '/'; |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
276 |
|
5428 | 277 |
err = listfiles(list, dir, keep_stat, &need_stat); |
5427
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
278 |
if (err) |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
279 |
goto bail; |
5421 | 280 |
|
281 |
PyList_Sort(list); |
|
282 |
||
5428 | 283 |
if (!keep_stat && !need_stat) |
284 |
goto done; |
|
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
285 |
|
5463
3b204881f959
osutil: use fdopendir instead of dirfd
Bryan O'Sullivan <bos@serpentine.com>
parents:
5457
diff
changeset
|
286 |
err = statfiles(list, ctor_args, keep_stat, full_path, path_len, dfd); |
5428 | 287 |
if (!err) |
288 |
goto done; |
|
5421 | 289 |
|
5423 | 290 |
bail: |
291 |
Py_XDECREF(list); |
|
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
292 |
|
5423 | 293 |
done: |
294 |
Py_XDECREF(ctor_args); |
|
295 |
if (dir) |
|
296 |
closedir(dir); |
|
5425
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
297 |
return err ? err : list; |
5421 | 298 |
} |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
299 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
300 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
301 |
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
|
302 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
303 |
static PyMethodDef methods[] = { |
5421 | 304 |
{"listdir", (PyCFunction)listdir, METH_VARARGS | METH_KEYWORDS, |
305 |
"list a directory\n"}, |
|
306 |
{NULL, NULL} |
|
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
307 |
}; |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
308 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
309 |
PyMODINIT_FUNC initosutil(void) |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
310 |
{ |
5421 | 311 |
if (PyType_Ready(&listdir_stat_type) == -1) |
312 |
return; |
|
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
313 |
|
5421 | 314 |
Py_InitModule3("osutil", methods, osutil_doc); |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
315 |
} |