comparison mercurial/osutil.c @ 5428:eb1b6aaeb32e

osutil: more tidying - do_stat -> keep_stat - all_kinds -> !need_stat - simplify main error logic - reorder declarations
author Matt Mackall <mpm@selenic.com>
date Mon, 08 Oct 2007 18:47:21 -0500
parents dae6188e8c9e
children 0bdea0abe62e
comparison
equal deleted inserted replaced
5427:dae6188e8c9e 5428:eb1b6aaeb32e
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, int stat, int *all) 112 static PyObject *listfiles(PyObject *list, DIR *dir,
113 int keep_stat, int *need_stat)
113 { 114 {
114 struct dirent *ent; 115 struct dirent *ent;
115 PyObject *name, *py_kind, *val; 116 PyObject *name, *py_kind, *val;
116 117
118 #ifdef DT_REG
119 *need_stat = 0;
120 #else
121 *need_stat = 1;
122 #endif
123
117 for (ent = readdir(dir); ent; ent = readdir(dir)) { 124 for (ent = readdir(dir); ent; ent = readdir(dir)) {
118 int kind = -1; 125 int kind = -1;
119 126
120 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) 127 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
121 continue; 128 continue;
122 129
123 #ifdef DT_REG 130 #ifdef DT_REG
124 if (!stat) 131 if (!keep_stat)
125 switch (ent->d_type) { 132 switch (ent->d_type) {
126 case DT_REG: kind = S_IFREG; break; 133 case DT_REG: kind = S_IFREG; break;
127 case DT_DIR: kind = S_IFDIR; break; 134 case DT_DIR: kind = S_IFDIR; break;
128 case DT_LNK: kind = S_IFLNK; break; 135 case DT_LNK: kind = S_IFLNK; break;
129 case DT_BLK: kind = S_IFBLK; break; 136 case DT_BLK: kind = S_IFBLK; break;
130 case DT_CHR: kind = S_IFCHR; break; 137 case DT_CHR: kind = S_IFCHR; break;
131 case DT_FIFO: kind = S_IFIFO; break; 138 case DT_FIFO: kind = S_IFIFO; break;
132 case DT_SOCK: kind = S_IFSOCK; break; 139 case DT_SOCK: kind = S_IFSOCK; break;
133 default: 140 default:
134 *all = 0; 141 *need_stat = 0;
135 break; 142 break;
136 } 143 }
137 #else
138 *all = 0;
139 #endif 144 #endif
140 145
141 if (kind != -1) 146 if (kind != -1)
142 py_kind = PyInt_FromLong(kind); 147 py_kind = PyInt_FromLong(kind);
143 else { 148 else {
144 py_kind = Py_None; 149 py_kind = Py_None;
145 Py_INCREF(Py_None); 150 Py_INCREF(Py_None);
146 } 151 }
147 152
148 val = PyTuple_New(stat ? 3 : 2); 153 val = PyTuple_New(keep_stat ? 3 : 2);
149 name = PyString_FromString(ent->d_name); 154 name = PyString_FromString(ent->d_name);
150 155
151 if (!name || !py_kind || !val) { 156 if (!name || !py_kind || !val) {
152 Py_XDECREF(name); 157 Py_XDECREF(name);
153 Py_XDECREF(py_kind); 158 Py_XDECREF(py_kind);
155 return PyErr_NoMemory(); 160 return PyErr_NoMemory();
156 } 161 }
157 162
158 PyTuple_SET_ITEM(val, 0, name); 163 PyTuple_SET_ITEM(val, 0, name);
159 PyTuple_SET_ITEM(val, 1, py_kind); 164 PyTuple_SET_ITEM(val, 1, py_kind);
160 if (stat) { 165 if (keep_stat) {
161 PyTuple_SET_ITEM(val, 2, Py_None); 166 PyTuple_SET_ITEM(val, 2, Py_None);
162 Py_INCREF(Py_None); 167 Py_INCREF(Py_None);
163 } 168 }
164 169
165 PyList_Append(list, val); 170 PyList_Append(list, val);
229 } 234 }
230 235
231 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs) 236 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs)
232 { 237 {
233 static char *kwlist[] = { "path", "stat", NULL }; 238 static char *kwlist[] = { "path", "stat", NULL };
239 DIR *dir = NULL;
234 PyObject *statobj = NULL; 240 PyObject *statobj = NULL;
235 DIR *dir = NULL;
236 PyObject *list = NULL; 241 PyObject *list = NULL;
237 PyObject *err = NULL; 242 PyObject *err = NULL;
238 PyObject *ctor_args = NULL; 243 PyObject *ctor_args = NULL;
239 int all_kinds = 1; 244 char *path;
240 char full_path[PATH_MAX + 10]; 245 char full_path[PATH_MAX + 10];
241 int path_len; 246 int path_len;
242 int do_stat; 247 int need_stat, keep_stat;
243 char *path;
244 248
245 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:listdir", kwlist, 249 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:listdir", kwlist,
246 &path, &path_len, &statobj)) 250 &path, &path_len, &statobj))
247 goto bail; 251 goto bail;
248 252
249 do_stat = statobj && PyObject_IsTrue(statobj); 253 keep_stat = statobj && PyObject_IsTrue(statobj);
250 254
251 dir = opendir(path); 255 dir = opendir(path);
252 if (!dir) { 256 if (!dir) {
253 list = PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); 257 err = PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
254 goto bail; 258 goto bail;
255 } 259 }
256 260
257 list = PyList_New(0); 261 list = PyList_New(0);
258 if (!list) 262 ctor_args = PyTuple_New(0);
263 if (!list || !ctor_args)
259 goto bail; 264 goto bail;
260 265
261 strncpy(full_path, path, PATH_MAX); 266 strncpy(full_path, path, PATH_MAX);
262 full_path[path_len] = '/'; 267 full_path[path_len] = '/';
263 268
264 err = listfiles(list, dir, do_stat, &all_kinds); 269 err = listfiles(list, dir, keep_stat, &need_stat);
265 if (err) 270 if (err)
266 goto bail; 271 goto bail;
267 272
268 PyList_Sort(list); 273 PyList_Sort(list);
269 274
270 if (do_stat) { 275 if (!keep_stat && !need_stat)
271 ctor_args = PyTuple_New(0); 276 goto done;
272 if (!ctor_args) 277
273 goto bail; 278 err = statfiles(list, ctor_args, keep_stat, full_path, path_len, dir);
274 } 279 if (!err)
275 280 goto done;
276 if (do_stat || !all_kinds)
277 if (statfiles(list, ctor_args, do_stat, full_path, path_len,
278 dir))
279 goto bail;
280 goto done;
281 281
282 bail: 282 bail:
283 Py_XDECREF(list); 283 Py_XDECREF(list);
284 284
285 done: 285 done: