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