Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/osutil.c @ 5425:830f6e280c90
osutils: pull file stat loop into its own function
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Mon, 08 Oct 2007 18:47:16 -0500 |
parents | 005638db9d0c |
children | e760cb72c02e |
rev | line source |
---|---|
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
1 /* |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
2 osutil.c - native operating system services |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
3 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
4 Copyright 2007 Matt Mackall and others |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
5 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
6 This software may be used and distributed according to the terms of |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
7 the GNU General Public License, incorporated herein by reference. |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
8 */ |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
9 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
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 <alloca.h> |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
13 #include <dirent.h> |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
14 #include <fcntl.h> |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
15 #include <string.h> |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
16 #include <sys/stat.h> |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
17 #include <sys/types.h> |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
18 #include <unistd.h> |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
19 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
20 struct listdir_stat { |
5421 | 21 PyObject_HEAD |
22 struct stat st; | |
5396
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 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
25 #define listdir_slot(name) \ |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
26 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
|
27 { \ |
5421 | 28 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
|
29 } |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
30 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
31 listdir_slot(st_dev); |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
32 listdir_slot(st_mode); |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
33 listdir_slot(st_nlink); |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
34 listdir_slot(st_size); |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
35 listdir_slot(st_mtime); |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
36 listdir_slot(st_ctime); |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
37 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
38 static struct PyGetSetDef listdir_stat_getsets[] = { |
5421 | 39 {"st_dev", listdir_stat_st_dev, 0, 0, 0}, |
40 {"st_mode", listdir_stat_st_mode, 0, 0, 0}, | |
41 {"st_nlink", listdir_stat_st_nlink, 0, 0, 0}, | |
42 {"st_size", listdir_stat_st_size, 0, 0, 0}, | |
43 {"st_mtime", listdir_stat_st_mtime, 0, 0, 0}, | |
44 {"st_ctime", listdir_stat_st_ctime, 0, 0, 0}, | |
45 {0, 0, 0, 0, 0} | |
5396
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 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
48 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
|
49 { |
5421 | 50 return t->tp_alloc(t, 0); |
5396
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 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
53 static void listdir_stat_dealloc(PyObject *o) |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
54 { |
5421 | 55 o->ob_type->tp_free(o); |
5396
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 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
58 static PyTypeObject listdir_stat_type = { |
5421 | 59 PyObject_HEAD_INIT(NULL) |
60 0, /*ob_size*/ | |
61 "osutil.stat", /*tp_name*/ | |
62 sizeof(struct listdir_stat), /*tp_basicsize*/ | |
63 0, /*tp_itemsize*/ | |
64 (destructor)listdir_stat_dealloc, /*tp_dealloc*/ | |
65 0, /*tp_print*/ | |
66 0, /*tp_getattr*/ | |
67 0, /*tp_setattr*/ | |
68 0, /*tp_compare*/ | |
69 0, /*tp_repr*/ | |
70 0, /*tp_as_number*/ | |
71 0, /*tp_as_sequence*/ | |
72 0, /*tp_as_mapping*/ | |
73 0, /*tp_hash */ | |
74 0, /*tp_call*/ | |
75 0, /*tp_str*/ | |
76 0, /*tp_getattro*/ | |
77 0, /*tp_setattro*/ | |
78 0, /*tp_as_buffer*/ | |
79 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ | |
80 "stat objects", /* tp_doc */ | |
81 0, /* tp_traverse */ | |
82 0, /* tp_clear */ | |
83 0, /* tp_richcompare */ | |
84 0, /* tp_weaklistoffset */ | |
85 0, /* tp_iter */ | |
86 0, /* tp_iternext */ | |
87 0, /* tp_methods */ | |
88 0, /* tp_members */ | |
89 listdir_stat_getsets, /* tp_getset */ | |
90 0, /* tp_base */ | |
91 0, /* tp_dict */ | |
92 0, /* tp_descr_get */ | |
93 0, /* tp_descr_set */ | |
94 0, /* tp_dictoffset */ | |
95 0, /* tp_init */ | |
96 0, /* tp_alloc */ | |
97 listdir_stat_new, /* tp_new */ | |
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 inline int mode_to_kind(int mode) |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
101 { |
5421 | 102 if (S_ISREG(mode)) return S_IFREG; |
103 if (S_ISDIR(mode)) return S_IFDIR; | |
104 if (S_ISLNK(mode)) return S_IFLNK; | |
105 if (S_ISBLK(mode)) return S_IFBLK; | |
106 if (S_ISCHR(mode)) return S_IFCHR; | |
107 if (S_ISFIFO(mode)) return S_IFIFO; | |
108 if (S_ISSOCK(mode)) return S_IFSOCK; | |
109 return mode; | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
110 } |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
111 |
5425
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
112 static PyObject *statfiles(PyObject *list, PyObject *ctor_args, int keep, |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
113 char *path, int len, DIR *dir) |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
114 { |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
115 struct stat buf; |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
116 struct stat *stp = &buf; |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
117 int kind; |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
118 int ret; |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
119 ssize_t i; |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
120 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
|
121 #ifdef AT_SYMLINK_NOFOLLOW |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
122 int dfd = dirfd(dir); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
123 #endif |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
124 |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
125 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
|
126 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
|
127 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
|
128 PyObject *py_st = NULL; |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
129 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
|
130 |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
131 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
|
132 if (kind != -1 && !keep) |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
133 continue; |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
134 |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
135 strncat(path + len + 1, name, PATH_MAX - len); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
136 path[PATH_MAX] = 0; |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
137 |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
138 if (keep) { |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
139 py_st = PyObject_CallObject( |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
140 (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
|
141 if (!py_st) |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
142 return PyErr_NoMemory(); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
143 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
|
144 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
|
145 } |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
146 |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
147 #ifdef AT_SYMLINK_NOFOLLOW |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
148 ret = fstatat(dfd, name, stp, AT_SYMLINK_NOFOLLOW); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
149 #else |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
150 ret = lstat(path, stp); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
151 #endif |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
152 if (ret == -1) |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
153 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
154 path); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
155 |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
156 if (kind == -1) |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
157 kind = mode_to_kind(stp->st_mode); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
158 |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
159 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
|
160 py_kind = PyInt_FromLong(kind); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
161 if (!py_kind) |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
162 return PyErr_NoMemory(); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
163 Py_XDECREF(Py_None); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
164 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
|
165 } |
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 |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
168 return 0; |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
169 } |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
170 |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
171 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
|
172 { |
5416
ca890c0c3f1f
osutil.c: style fix - delete trailing end-of-line spaces
Giorgos Keramidas <keramida@ceid.upatras.gr>
parents:
5398
diff
changeset
|
173 static char *kwlist[] = { "path", "stat", NULL }; |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
174 PyObject *statobj = NULL; |
5421 | 175 DIR *dir = NULL; |
176 struct dirent *ent; | |
177 PyObject *list = NULL; | |
5425
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
178 PyObject *err = NULL; |
5421 | 179 PyObject *ctor_args = NULL; |
180 int all_kinds = 1; | |
5422
a3ba7ef98c94
osutil: eliminate alloca call
Matt Mackall <mpm@selenic.com>
parents:
5421
diff
changeset
|
181 char full_path[PATH_MAX + 10]; |
5421 | 182 int path_len; |
183 int do_stat; | |
184 char *path; | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
185 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
186 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
|
187 &path, &path_len, &statobj)) |
5421 | 188 goto bail; |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
189 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
190 do_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
|
191 |
5421 | 192 dir = opendir(path); |
193 if (!dir) { | |
194 list = PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); | |
195 goto bail; | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
196 } |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
197 |
5421 | 198 list = PyList_New(0); |
199 if (!list) | |
200 goto bail; | |
5416
ca890c0c3f1f
osutil.c: style fix - delete trailing end-of-line spaces
Giorgos Keramidas <keramida@ceid.upatras.gr>
parents:
5398
diff
changeset
|
201 |
5422
a3ba7ef98c94
osutil: eliminate alloca call
Matt Mackall <mpm@selenic.com>
parents:
5421
diff
changeset
|
202 strncpy(full_path, path, PATH_MAX); |
5421 | 203 full_path[path_len] = '/'; |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
204 |
5421 | 205 for (ent = readdir(dir); ent; ent = readdir(dir)) { |
206 PyObject *name = NULL; | |
207 PyObject *py_kind = NULL; | |
208 PyObject *val = NULL; | |
209 unsigned char d_type; | |
210 int kind = -1; | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
211 |
5421 | 212 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) |
213 continue; | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
214 |
5421 | 215 #ifdef DT_REG |
216 if (do_stat) | |
217 d_type = 0; | |
218 else | |
219 d_type = ent->d_type; | |
220 #else | |
221 d_type = 0; | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
222 #endif |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
223 |
5421 | 224 switch (d_type) { |
225 #ifdef DT_REG | |
226 case DT_REG: kind = S_IFREG; break; | |
227 case DT_DIR: kind = S_IFDIR; break; | |
228 case DT_LNK: kind = S_IFLNK; break; | |
229 case DT_BLK: kind = S_IFBLK; break; | |
230 case DT_CHR: kind = S_IFCHR; break; | |
231 case DT_FIFO: kind = S_IFIFO; break; | |
232 case DT_SOCK: kind = S_IFSOCK; break; | |
233 #endif | |
234 default: | |
235 if (all_kinds) | |
236 all_kinds = 0; | |
237 break; | |
238 } | |
5416
ca890c0c3f1f
osutil.c: style fix - delete trailing end-of-line spaces
Giorgos Keramidas <keramida@ceid.upatras.gr>
parents:
5398
diff
changeset
|
239 |
5421 | 240 name = PyString_FromString(ent->d_name); |
241 if (kind != -1) | |
242 py_kind = PyInt_FromLong(kind); | |
243 else { | |
244 py_kind = Py_None; | |
245 Py_INCREF(Py_None); | |
246 } | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
247 |
5421 | 248 val = PyTuple_New(do_stat ? 3 : 2); |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
249 |
5421 | 250 if (!name || !py_kind || !val) { |
251 Py_XDECREF(name); | |
252 Py_XDECREF(py_kind); | |
253 Py_XDECREF(val); | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
254 goto bail; |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
255 } |
5416
ca890c0c3f1f
osutil.c: style fix - delete trailing end-of-line spaces
Giorgos Keramidas <keramida@ceid.upatras.gr>
parents:
5398
diff
changeset
|
256 |
5421 | 257 PyTuple_SET_ITEM(val, 0, name); |
258 PyTuple_SET_ITEM(val, 1, py_kind); | |
259 if (do_stat) { | |
260 PyTuple_SET_ITEM(val, 2, Py_None); | |
261 Py_INCREF(Py_None); | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
262 } |
5421 | 263 |
264 PyList_Append(list, val); | |
265 Py_DECREF(val); | |
266 } | |
267 | |
268 PyList_Sort(list); | |
269 | |
5425
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
270 if (do_stat) { |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
271 ctor_args = PyTuple_New(0); |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
272 if (!ctor_args) |
5424
005638db9d0c
osutil: fold stat paths together
Matt Mackall <mpm@selenic.com>
parents:
5423
diff
changeset
|
273 goto bail; |
5421 | 274 } |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
275 |
5425
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
276 if (do_stat || !all_kinds) |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
277 if (statfiles(list, ctor_args, do_stat, full_path, path_len, |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
278 dir)) |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
279 goto bail; |
5421 | 280 goto done; |
281 | |
5423 | 282 bail: |
283 Py_XDECREF(list); | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
284 |
5423 | 285 done: |
286 Py_XDECREF(ctor_args); | |
287 if (dir) | |
288 closedir(dir); | |
5425
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
289 return err ? err : list; |
5421 | 290 } |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
291 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
292 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
293 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
|
294 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
295 static PyMethodDef methods[] = { |
5421 | 296 {"listdir", (PyCFunction)listdir, METH_VARARGS | METH_KEYWORDS, |
297 "list a directory\n"}, | |
298 {NULL, NULL} | |
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 PyMODINIT_FUNC initosutil(void) |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
302 { |
5421 | 303 if (PyType_Ready(&listdir_stat_type) == -1) |
304 return; | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
305 |
5421 | 306 Py_InitModule3("osutil", methods, osutil_doc); |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
307 } |