author | Matt Mackall <mpm@selenic.com> |
Fri, 12 Sep 2008 15:11:02 -0500 | |
changeset 7031 | 19e8d034932e |
parent 7022 | bdc8d00c4ffa |
child 7033 | 892d27fb04a5 |
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 |
|
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
99 |
#ifdef AT_SYMLINK_NOFOLLOW |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
100 |
#define USEFDOPEN 1 |
5428 | 101 |
#else |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
102 |
#define USEFDOPEN 0 |
5427
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
103 |
#endif |
dae6188e8c9e
osutil: move file list loop to its own function
Matt Mackall <mpm@selenic.com>
parents:
5426
diff
changeset
|
104 |
|
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
105 |
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
|
106 |
{ |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
107 |
#ifdef DT_REG |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
108 |
switch (ent->d_type) { |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
109 |
case DT_REG: return S_IFREG; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
110 |
case DT_DIR: return S_IFDIR; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
111 |
case DT_LNK: return S_IFLNK; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
112 |
case DT_BLK: return S_IFBLK; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
113 |
case DT_CHR: return S_IFCHR; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
114 |
case DT_FIFO: return S_IFIFO; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
115 |
case DT_SOCK: return S_IFSOCK; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
116 |
} |
5463
3b204881f959
osutil: use fdopendir instead of dirfd
Bryan O'Sullivan <bos@serpentine.com>
parents:
5457
diff
changeset
|
117 |
#endif |
5425
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
118 |
} |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
119 |
|
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
120 |
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
|
121 |
{ |
5416
ca890c0c3f1f
osutil.c: style fix - delete trailing end-of-line spaces
Giorgos Keramidas <keramida@ceid.upatras.gr>
parents:
5398
diff
changeset
|
122 |
static char *kwlist[] = { "path", "stat", NULL }; |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
123 |
PyObject *statflag = NULL, *list, *elem, *stat, *ret = NULL; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
124 |
char fullpath[PATH_MAX + 10], *path; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
125 |
int pathlen, keepstat, kind, dfd, err; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
126 |
struct stat st; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
127 |
struct dirent *ent; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
128 |
DIR *dir; |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
129 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
130 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:listdir", kwlist, |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
131 |
&path, &pathlen, &statflag)) |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
132 |
goto error_parse; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
133 |
|
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
134 |
strncpy(fullpath, path, PATH_MAX); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
135 |
fullpath[pathlen] = '/'; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
136 |
keepstat = statflag && PyObject_IsTrue(statflag); |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
137 |
|
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
138 |
if (USEFDOPEN) { |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
139 |
dfd = open(path, O_RDONLY); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
140 |
if (dfd == -1) { |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
141 |
PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
142 |
goto error_parse; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
143 |
} |
7022
bdc8d00c4ffa
osutil: proper error checking and reporting
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
5463
diff
changeset
|
144 |
dir = fdopendir(dfd); |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
145 |
} else |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
146 |
dir = opendir(path); |
5421 | 147 |
if (!dir) { |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
148 |
PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
149 |
goto error_dir; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
150 |
} |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
151 |
|
5421 | 152 |
list = PyList_New(0); |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
153 |
if (!list) |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
154 |
goto error_list; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
155 |
|
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
156 |
while ((ent = readdir(dir))) { |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
157 |
if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
158 |
continue; |
5416
ca890c0c3f1f
osutil.c: style fix - delete trailing end-of-line spaces
Giorgos Keramidas <keramida@ceid.upatras.gr>
parents:
5398
diff
changeset
|
159 |
|
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
160 |
kind = entkind(ent); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
161 |
if (kind == -1 || keepstat) { |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
162 |
if (USEFDOPEN) |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
163 |
err = fstatat(dfd, ent->d_name, &st, |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
164 |
AT_SYMLINK_NOFOLLOW); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
165 |
else { |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
166 |
strncpy(fullpath + pathlen + 1, ent->d_name, |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
167 |
PATH_MAX - pathlen); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
168 |
fullpath[PATH_MAX] = 0; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
169 |
err = lstat(fullpath, &st); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
170 |
} |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
171 |
if (err == -1) { |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
172 |
strncpy(fullpath + pathlen + 1, ent->d_name, |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
173 |
PATH_MAX - pathlen); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
174 |
fullpath[PATH_MAX] = 0; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
175 |
PyErr_SetFromErrnoWithFilename(PyExc_OSError, |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
176 |
fullpath); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
177 |
goto error; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
178 |
} |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
179 |
kind = st.st_mode & S_IFMT; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
180 |
} |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
181 |
|
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
182 |
if (keepstat) { |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
183 |
stat = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
184 |
if (!stat) |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
185 |
goto error; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
186 |
memcpy(&((struct listdir_stat *)stat)->st, &st, sizeof(st)); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
187 |
elem = Py_BuildValue("siN", ent->d_name, kind, stat); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
188 |
} else |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
189 |
elem = Py_BuildValue("si", ent->d_name, kind); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
190 |
if (!elem) |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
191 |
goto error; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
192 |
|
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
193 |
PyList_Append(list, elem); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
194 |
Py_DECREF(elem); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
195 |
} |
5421 | 196 |
|
197 |
PyList_Sort(list); |
|
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
198 |
ret = list; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
199 |
Py_INCREF(ret); |
5421 | 200 |
|
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
201 |
error: |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
202 |
Py_DECREF(list); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
203 |
error_list: |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
204 |
closedir(dir); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
205 |
error_dir: |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
206 |
if (USEFDOPEN) |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
207 |
close(dfd); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
208 |
error_parse: |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
209 |
return ret; |
5421 | 210 |
} |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
211 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
212 |
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
|
213 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
214 |
static PyMethodDef methods[] = { |
5421 | 215 |
{"listdir", (PyCFunction)listdir, METH_VARARGS | METH_KEYWORDS, |
216 |
"list a directory\n"}, |
|
217 |
{NULL, NULL} |
|
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
218 |
}; |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
219 |
|
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
220 |
PyMODINIT_FUNC initosutil(void) |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
221 |
{ |
5421 | 222 |
if (PyType_Ready(&listdir_stat_type) == -1) |
223 |
return; |
|
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
224 |
|
5421 | 225 |
Py_InitModule3("osutil", methods, osutil_doc); |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
226 |
} |