##// END OF EJS Templates
osutil.c: use strncpy instead of strncat
Alexis S. L. Carvalho -
r5430:0bdea0ab default
parent child Browse files
Show More
@@ -1,307 +1,307 b''
1 /*
1 /*
2 osutil.c - native operating system services
2 osutil.c - native operating system services
3
3
4 Copyright 2007 Matt Mackall and others
4 Copyright 2007 Matt Mackall and others
5
5
6 This software may be used and distributed according to the terms of
6 This software may be used and distributed according to the terms of
7 the GNU General Public License, incorporated herein by reference.
7 the GNU General Public License, incorporated herein by reference.
8 */
8 */
9
9
10 #define _ATFILE_SOURCE
10 #define _ATFILE_SOURCE
11 #include <Python.h>
11 #include <Python.h>
12 #include <alloca.h>
12 #include <alloca.h>
13 #include <dirent.h>
13 #include <dirent.h>
14 #include <fcntl.h>
14 #include <fcntl.h>
15 #include <string.h>
15 #include <string.h>
16 #include <sys/stat.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
17 #include <sys/types.h>
18 #include <unistd.h>
18 #include <unistd.h>
19
19
20 struct listdir_stat {
20 struct listdir_stat {
21 PyObject_HEAD
21 PyObject_HEAD
22 struct stat st;
22 struct stat st;
23 };
23 };
24
24
25 #define listdir_slot(name) \
25 #define listdir_slot(name) \
26 static PyObject *listdir_stat_##name(PyObject *self, void *x) \
26 static PyObject *listdir_stat_##name(PyObject *self, void *x) \
27 { \
27 { \
28 return PyInt_FromLong(((struct listdir_stat *)self)->st.name); \
28 return PyInt_FromLong(((struct listdir_stat *)self)->st.name); \
29 }
29 }
30
30
31 listdir_slot(st_dev);
31 listdir_slot(st_dev);
32 listdir_slot(st_mode);
32 listdir_slot(st_mode);
33 listdir_slot(st_nlink);
33 listdir_slot(st_nlink);
34 listdir_slot(st_size);
34 listdir_slot(st_size);
35 listdir_slot(st_mtime);
35 listdir_slot(st_mtime);
36 listdir_slot(st_ctime);
36 listdir_slot(st_ctime);
37
37
38 static struct PyGetSetDef listdir_stat_getsets[] = {
38 static struct PyGetSetDef listdir_stat_getsets[] = {
39 {"st_dev", listdir_stat_st_dev, 0, 0, 0},
39 {"st_dev", listdir_stat_st_dev, 0, 0, 0},
40 {"st_mode", listdir_stat_st_mode, 0, 0, 0},
40 {"st_mode", listdir_stat_st_mode, 0, 0, 0},
41 {"st_nlink", listdir_stat_st_nlink, 0, 0, 0},
41 {"st_nlink", listdir_stat_st_nlink, 0, 0, 0},
42 {"st_size", listdir_stat_st_size, 0, 0, 0},
42 {"st_size", listdir_stat_st_size, 0, 0, 0},
43 {"st_mtime", listdir_stat_st_mtime, 0, 0, 0},
43 {"st_mtime", listdir_stat_st_mtime, 0, 0, 0},
44 {"st_ctime", listdir_stat_st_ctime, 0, 0, 0},
44 {"st_ctime", listdir_stat_st_ctime, 0, 0, 0},
45 {0, 0, 0, 0, 0}
45 {0, 0, 0, 0, 0}
46 };
46 };
47
47
48 static PyObject *listdir_stat_new(PyTypeObject *t, PyObject *a, PyObject *k)
48 static PyObject *listdir_stat_new(PyTypeObject *t, PyObject *a, PyObject *k)
49 {
49 {
50 return t->tp_alloc(t, 0);
50 return t->tp_alloc(t, 0);
51 }
51 }
52
52
53 static void listdir_stat_dealloc(PyObject *o)
53 static void listdir_stat_dealloc(PyObject *o)
54 {
54 {
55 o->ob_type->tp_free(o);
55 o->ob_type->tp_free(o);
56 }
56 }
57
57
58 static PyTypeObject listdir_stat_type = {
58 static PyTypeObject listdir_stat_type = {
59 PyObject_HEAD_INIT(NULL)
59 PyObject_HEAD_INIT(NULL)
60 0, /*ob_size*/
60 0, /*ob_size*/
61 "osutil.stat", /*tp_name*/
61 "osutil.stat", /*tp_name*/
62 sizeof(struct listdir_stat), /*tp_basicsize*/
62 sizeof(struct listdir_stat), /*tp_basicsize*/
63 0, /*tp_itemsize*/
63 0, /*tp_itemsize*/
64 (destructor)listdir_stat_dealloc, /*tp_dealloc*/
64 (destructor)listdir_stat_dealloc, /*tp_dealloc*/
65 0, /*tp_print*/
65 0, /*tp_print*/
66 0, /*tp_getattr*/
66 0, /*tp_getattr*/
67 0, /*tp_setattr*/
67 0, /*tp_setattr*/
68 0, /*tp_compare*/
68 0, /*tp_compare*/
69 0, /*tp_repr*/
69 0, /*tp_repr*/
70 0, /*tp_as_number*/
70 0, /*tp_as_number*/
71 0, /*tp_as_sequence*/
71 0, /*tp_as_sequence*/
72 0, /*tp_as_mapping*/
72 0, /*tp_as_mapping*/
73 0, /*tp_hash */
73 0, /*tp_hash */
74 0, /*tp_call*/
74 0, /*tp_call*/
75 0, /*tp_str*/
75 0, /*tp_str*/
76 0, /*tp_getattro*/
76 0, /*tp_getattro*/
77 0, /*tp_setattro*/
77 0, /*tp_setattro*/
78 0, /*tp_as_buffer*/
78 0, /*tp_as_buffer*/
79 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
79 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
80 "stat objects", /* tp_doc */
80 "stat objects", /* tp_doc */
81 0, /* tp_traverse */
81 0, /* tp_traverse */
82 0, /* tp_clear */
82 0, /* tp_clear */
83 0, /* tp_richcompare */
83 0, /* tp_richcompare */
84 0, /* tp_weaklistoffset */
84 0, /* tp_weaklistoffset */
85 0, /* tp_iter */
85 0, /* tp_iter */
86 0, /* tp_iternext */
86 0, /* tp_iternext */
87 0, /* tp_methods */
87 0, /* tp_methods */
88 0, /* tp_members */
88 0, /* tp_members */
89 listdir_stat_getsets, /* tp_getset */
89 listdir_stat_getsets, /* tp_getset */
90 0, /* tp_base */
90 0, /* tp_base */
91 0, /* tp_dict */
91 0, /* tp_dict */
92 0, /* tp_descr_get */
92 0, /* tp_descr_get */
93 0, /* tp_descr_set */
93 0, /* tp_descr_set */
94 0, /* tp_dictoffset */
94 0, /* tp_dictoffset */
95 0, /* tp_init */
95 0, /* tp_init */
96 0, /* tp_alloc */
96 0, /* tp_alloc */
97 listdir_stat_new, /* tp_new */
97 listdir_stat_new, /* tp_new */
98 };
98 };
99
99
100 static inline int mode_to_kind(int mode)
100 static inline int mode_to_kind(int mode)
101 {
101 {
102 if (S_ISREG(mode)) return S_IFREG;
102 if (S_ISREG(mode)) return S_IFREG;
103 if (S_ISDIR(mode)) return S_IFDIR;
103 if (S_ISDIR(mode)) return S_IFDIR;
104 if (S_ISLNK(mode)) return S_IFLNK;
104 if (S_ISLNK(mode)) return S_IFLNK;
105 if (S_ISBLK(mode)) return S_IFBLK;
105 if (S_ISBLK(mode)) return S_IFBLK;
106 if (S_ISCHR(mode)) return S_IFCHR;
106 if (S_ISCHR(mode)) return S_IFCHR;
107 if (S_ISFIFO(mode)) return S_IFIFO;
107 if (S_ISFIFO(mode)) return S_IFIFO;
108 if (S_ISSOCK(mode)) return S_IFSOCK;
108 if (S_ISSOCK(mode)) return S_IFSOCK;
109 return mode;
109 return mode;
110 }
110 }
111
111
112 static PyObject *listfiles(PyObject *list, DIR *dir,
112 static PyObject *listfiles(PyObject *list, DIR *dir,
113 int keep_stat, int *need_stat)
113 int keep_stat, int *need_stat)
114 {
114 {
115 struct dirent *ent;
115 struct dirent *ent;
116 PyObject *name, *py_kind, *val;
116 PyObject *name, *py_kind, *val;
117
117
118 #ifdef DT_REG
118 #ifdef DT_REG
119 *need_stat = 0;
119 *need_stat = 0;
120 #else
120 #else
121 *need_stat = 1;
121 *need_stat = 1;
122 #endif
122 #endif
123
123
124 for (ent = readdir(dir); ent; ent = readdir(dir)) {
124 for (ent = readdir(dir); ent; ent = readdir(dir)) {
125 int kind = -1;
125 int kind = -1;
126
126
127 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
127 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
128 continue;
128 continue;
129
129
130 #ifdef DT_REG
130 #ifdef DT_REG
131 if (!keep_stat)
131 if (!keep_stat)
132 switch (ent->d_type) {
132 switch (ent->d_type) {
133 case DT_REG: kind = S_IFREG; break;
133 case DT_REG: kind = S_IFREG; break;
134 case DT_DIR: kind = S_IFDIR; break;
134 case DT_DIR: kind = S_IFDIR; break;
135 case DT_LNK: kind = S_IFLNK; break;
135 case DT_LNK: kind = S_IFLNK; break;
136 case DT_BLK: kind = S_IFBLK; break;
136 case DT_BLK: kind = S_IFBLK; break;
137 case DT_CHR: kind = S_IFCHR; break;
137 case DT_CHR: kind = S_IFCHR; break;
138 case DT_FIFO: kind = S_IFIFO; break;
138 case DT_FIFO: kind = S_IFIFO; break;
139 case DT_SOCK: kind = S_IFSOCK; break;
139 case DT_SOCK: kind = S_IFSOCK; break;
140 default:
140 default:
141 *need_stat = 0;
141 *need_stat = 0;
142 break;
142 break;
143 }
143 }
144 #endif
144 #endif
145
145
146 if (kind != -1)
146 if (kind != -1)
147 py_kind = PyInt_FromLong(kind);
147 py_kind = PyInt_FromLong(kind);
148 else {
148 else {
149 py_kind = Py_None;
149 py_kind = Py_None;
150 Py_INCREF(Py_None);
150 Py_INCREF(Py_None);
151 }
151 }
152
152
153 val = PyTuple_New(keep_stat ? 3 : 2);
153 val = PyTuple_New(keep_stat ? 3 : 2);
154 name = PyString_FromString(ent->d_name);
154 name = PyString_FromString(ent->d_name);
155
155
156 if (!name || !py_kind || !val) {
156 if (!name || !py_kind || !val) {
157 Py_XDECREF(name);
157 Py_XDECREF(name);
158 Py_XDECREF(py_kind);
158 Py_XDECREF(py_kind);
159 Py_XDECREF(val);
159 Py_XDECREF(val);
160 return PyErr_NoMemory();
160 return PyErr_NoMemory();
161 }
161 }
162
162
163 PyTuple_SET_ITEM(val, 0, name);
163 PyTuple_SET_ITEM(val, 0, name);
164 PyTuple_SET_ITEM(val, 1, py_kind);
164 PyTuple_SET_ITEM(val, 1, py_kind);
165 if (keep_stat) {
165 if (keep_stat) {
166 PyTuple_SET_ITEM(val, 2, Py_None);
166 PyTuple_SET_ITEM(val, 2, Py_None);
167 Py_INCREF(Py_None);
167 Py_INCREF(Py_None);
168 }
168 }
169
169
170 PyList_Append(list, val);
170 PyList_Append(list, val);
171 Py_DECREF(val);
171 Py_DECREF(val);
172 }
172 }
173
173
174 return 0;
174 return 0;
175 }
175 }
176
176
177 static PyObject *statfiles(PyObject *list, PyObject *ctor_args, int keep,
177 static PyObject *statfiles(PyObject *list, PyObject *ctor_args, int keep,
178 char *path, int len, DIR *dir)
178 char *path, int len, DIR *dir)
179 {
179 {
180 struct stat buf;
180 struct stat buf;
181 struct stat *stp = &buf;
181 struct stat *stp = &buf;
182 int kind;
182 int kind;
183 int ret;
183 int ret;
184 ssize_t i;
184 ssize_t i;
185 ssize_t size = PyList_Size(list);
185 ssize_t size = PyList_Size(list);
186 #ifdef AT_SYMLINK_NOFOLLOW
186 #ifdef AT_SYMLINK_NOFOLLOW
187 int dfd = dirfd(dir);
187 int dfd = dirfd(dir);
188 #endif
188 #endif
189
189
190 for (i = 0; i < size; i++) {
190 for (i = 0; i < size; i++) {
191 PyObject *elt = PyList_GetItem(list, i);
191 PyObject *elt = PyList_GetItem(list, i);
192 char *name = PyString_AsString(PyTuple_GET_ITEM(elt, 0));
192 char *name = PyString_AsString(PyTuple_GET_ITEM(elt, 0));
193 PyObject *py_st = NULL;
193 PyObject *py_st = NULL;
194 PyObject *py_kind = PyTuple_GET_ITEM(elt, 1);
194 PyObject *py_kind = PyTuple_GET_ITEM(elt, 1);
195
195
196 kind = py_kind == Py_None ? -1 : PyInt_AsLong(py_kind);
196 kind = py_kind == Py_None ? -1 : PyInt_AsLong(py_kind);
197 if (kind != -1 && !keep)
197 if (kind != -1 && !keep)
198 continue;
198 continue;
199
199
200 strncat(path + len + 1, name, PATH_MAX - len);
200 strncpy(path + len + 1, name, PATH_MAX - len);
201 path[PATH_MAX] = 0;
201 path[PATH_MAX] = 0;
202
202
203 if (keep) {
203 if (keep) {
204 py_st = PyObject_CallObject(
204 py_st = PyObject_CallObject(
205 (PyObject *)&listdir_stat_type, ctor_args);
205 (PyObject *)&listdir_stat_type, ctor_args);
206 if (!py_st)
206 if (!py_st)
207 return PyErr_NoMemory();
207 return PyErr_NoMemory();
208 stp = &((struct listdir_stat *)py_st)->st;
208 stp = &((struct listdir_stat *)py_st)->st;
209 PyTuple_SET_ITEM(elt, 2, py_st);
209 PyTuple_SET_ITEM(elt, 2, py_st);
210 }
210 }
211
211
212 #ifdef AT_SYMLINK_NOFOLLOW
212 #ifdef AT_SYMLINK_NOFOLLOW
213 ret = fstatat(dfd, name, stp, AT_SYMLINK_NOFOLLOW);
213 ret = fstatat(dfd, name, stp, AT_SYMLINK_NOFOLLOW);
214 #else
214 #else
215 ret = lstat(path, stp);
215 ret = lstat(path, stp);
216 #endif
216 #endif
217 if (ret == -1)
217 if (ret == -1)
218 return PyErr_SetFromErrnoWithFilename(PyExc_OSError,
218 return PyErr_SetFromErrnoWithFilename(PyExc_OSError,
219 path);
219 path);
220
220
221 if (kind == -1)
221 if (kind == -1)
222 kind = mode_to_kind(stp->st_mode);
222 kind = mode_to_kind(stp->st_mode);
223
223
224 if (py_kind == Py_None && kind != -1) {
224 if (py_kind == Py_None && kind != -1) {
225 py_kind = PyInt_FromLong(kind);
225 py_kind = PyInt_FromLong(kind);
226 if (!py_kind)
226 if (!py_kind)
227 return PyErr_NoMemory();
227 return PyErr_NoMemory();
228 Py_XDECREF(Py_None);
228 Py_XDECREF(Py_None);
229 PyTuple_SET_ITEM(elt, 1, py_kind);
229 PyTuple_SET_ITEM(elt, 1, py_kind);
230 }
230 }
231 }
231 }
232
232
233 return 0;
233 return 0;
234 }
234 }
235
235
236 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs)
236 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs)
237 {
237 {
238 static char *kwlist[] = { "path", "stat", NULL };
238 static char *kwlist[] = { "path", "stat", NULL };
239 DIR *dir = NULL;
239 DIR *dir = NULL;
240 PyObject *statobj = NULL;
240 PyObject *statobj = NULL;
241 PyObject *list = NULL;
241 PyObject *list = NULL;
242 PyObject *err = NULL;
242 PyObject *err = NULL;
243 PyObject *ctor_args = NULL;
243 PyObject *ctor_args = NULL;
244 char *path;
244 char *path;
245 char full_path[PATH_MAX + 10];
245 char full_path[PATH_MAX + 10];
246 int path_len;
246 int path_len;
247 int need_stat, keep_stat;
247 int need_stat, keep_stat;
248
248
249 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:listdir", kwlist,
249 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:listdir", kwlist,
250 &path, &path_len, &statobj))
250 &path, &path_len, &statobj))
251 goto bail;
251 goto bail;
252
252
253 keep_stat = statobj && PyObject_IsTrue(statobj);
253 keep_stat = statobj && PyObject_IsTrue(statobj);
254
254
255 dir = opendir(path);
255 dir = opendir(path);
256 if (!dir) {
256 if (!dir) {
257 err = PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
257 err = PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
258 goto bail;
258 goto bail;
259 }
259 }
260
260
261 list = PyList_New(0);
261 list = PyList_New(0);
262 ctor_args = PyTuple_New(0);
262 ctor_args = PyTuple_New(0);
263 if (!list || !ctor_args)
263 if (!list || !ctor_args)
264 goto bail;
264 goto bail;
265
265
266 strncpy(full_path, path, PATH_MAX);
266 strncpy(full_path, path, PATH_MAX);
267 full_path[path_len] = '/';
267 full_path[path_len] = '/';
268
268
269 err = listfiles(list, dir, keep_stat, &need_stat);
269 err = listfiles(list, dir, keep_stat, &need_stat);
270 if (err)
270 if (err)
271 goto bail;
271 goto bail;
272
272
273 PyList_Sort(list);
273 PyList_Sort(list);
274
274
275 if (!keep_stat && !need_stat)
275 if (!keep_stat && !need_stat)
276 goto done;
276 goto done;
277
277
278 err = statfiles(list, ctor_args, keep_stat, full_path, path_len, dir);
278 err = statfiles(list, ctor_args, keep_stat, full_path, path_len, dir);
279 if (!err)
279 if (!err)
280 goto done;
280 goto done;
281
281
282 bail:
282 bail:
283 Py_XDECREF(list);
283 Py_XDECREF(list);
284
284
285 done:
285 done:
286 Py_XDECREF(ctor_args);
286 Py_XDECREF(ctor_args);
287 if (dir)
287 if (dir)
288 closedir(dir);
288 closedir(dir);
289 return err ? err : list;
289 return err ? err : list;
290 }
290 }
291
291
292
292
293 static char osutil_doc[] = "Native operating system services.";
293 static char osutil_doc[] = "Native operating system services.";
294
294
295 static PyMethodDef methods[] = {
295 static PyMethodDef methods[] = {
296 {"listdir", (PyCFunction)listdir, METH_VARARGS | METH_KEYWORDS,
296 {"listdir", (PyCFunction)listdir, METH_VARARGS | METH_KEYWORDS,
297 "list a directory\n"},
297 "list a directory\n"},
298 {NULL, NULL}
298 {NULL, NULL}
299 };
299 };
300
300
301 PyMODINIT_FUNC initosutil(void)
301 PyMODINIT_FUNC initosutil(void)
302 {
302 {
303 if (PyType_Ready(&listdir_stat_type) == -1)
303 if (PyType_Ready(&listdir_stat_type) == -1)
304 return;
304 return;
305
305
306 Py_InitModule3("osutil", methods, osutil_doc);
306 Py_InitModule3("osutil", methods, osutil_doc);
307 }
307 }
General Comments 0
You need to be logged in to leave comments. Login now