Mercurial > hg
annotate mercurial/osutil.c @ 7868:2b901f9b37aa
keepalive: fix reference to IncompleteRead
author | Peter Arrenbrecht <peter.arrenbrecht@gmail.com> |
---|---|
date | Mon, 23 Mar 2009 11:12:01 +0100 |
parents | aecea6934fdd |
children | 7de68012f86e 48da69ff79bd |
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> |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
12 #ifdef _WIN32 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
13 #include <windows.h> |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
14 #else |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
15 #include <dirent.h> |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
16 #include <fcntl.h> |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
17 #include <string.h> |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
18 #include <sys/stat.h> |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
19 #include <sys/types.h> |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
20 #include <unistd.h> |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
21 #endif |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
22 |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
23 #ifdef _WIN32 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
24 /* |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
25 stat struct compatible with hg expectations |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
26 Mercurial only uses st_mode, st_size and st_mtime |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
27 the rest is kept to minimize changes between implementations |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
28 */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
29 struct hg_stat { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
30 int st_dev; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
31 int st_mode; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
32 int st_nlink; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
33 __int64 st_size; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
34 int st_mtime; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
35 int st_ctime; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
36 }; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
37 struct listdir_stat { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
38 PyObject_HEAD |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
39 struct hg_stat st; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
40 }; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
41 #else |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
42 struct listdir_stat { |
5421 | 43 PyObject_HEAD |
44 struct stat st; | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
45 }; |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
46 #endif |
5396
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 #define listdir_slot(name) \ |
7190
aecea6934fdd
Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents:
7136
diff
changeset
|
49 static PyObject *listdir_stat_##name(PyObject *self, void *x) \ |
aecea6934fdd
Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents:
7136
diff
changeset
|
50 { \ |
aecea6934fdd
Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents:
7136
diff
changeset
|
51 return PyInt_FromLong(((struct listdir_stat *)self)->st.name); \ |
aecea6934fdd
Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents:
7136
diff
changeset
|
52 } |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
53 |
5431
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
54 listdir_slot(st_dev) |
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
55 listdir_slot(st_mode) |
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
56 listdir_slot(st_nlink) |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
57 #ifdef _WIN32 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
58 static PyObject *listdir_stat_st_size(PyObject *self, void *x) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
59 { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
60 return PyLong_FromLongLong( |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
61 (PY_LONG_LONG)((struct listdir_stat *)self)->st.st_size); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
62 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
63 #else |
5431
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
64 listdir_slot(st_size) |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
65 #endif |
5431
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
66 listdir_slot(st_mtime) |
a7c832abd29c
Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5430
diff
changeset
|
67 listdir_slot(st_ctime) |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
68 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
69 static struct PyGetSetDef listdir_stat_getsets[] = { |
5421 | 70 {"st_dev", listdir_stat_st_dev, 0, 0, 0}, |
71 {"st_mode", listdir_stat_st_mode, 0, 0, 0}, | |
72 {"st_nlink", listdir_stat_st_nlink, 0, 0, 0}, | |
73 {"st_size", listdir_stat_st_size, 0, 0, 0}, | |
74 {"st_mtime", listdir_stat_st_mtime, 0, 0, 0}, | |
75 {"st_ctime", listdir_stat_st_ctime, 0, 0, 0}, | |
76 {0, 0, 0, 0, 0} | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
77 }; |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
78 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
79 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
|
80 { |
5421 | 81 return t->tp_alloc(t, 0); |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
82 } |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
83 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
84 static void listdir_stat_dealloc(PyObject *o) |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
85 { |
5421 | 86 o->ob_type->tp_free(o); |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
87 } |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
88 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
89 static PyTypeObject listdir_stat_type = { |
5421 | 90 PyObject_HEAD_INIT(NULL) |
91 0, /*ob_size*/ | |
92 "osutil.stat", /*tp_name*/ | |
93 sizeof(struct listdir_stat), /*tp_basicsize*/ | |
94 0, /*tp_itemsize*/ | |
95 (destructor)listdir_stat_dealloc, /*tp_dealloc*/ | |
96 0, /*tp_print*/ | |
97 0, /*tp_getattr*/ | |
98 0, /*tp_setattr*/ | |
99 0, /*tp_compare*/ | |
100 0, /*tp_repr*/ | |
101 0, /*tp_as_number*/ | |
102 0, /*tp_as_sequence*/ | |
103 0, /*tp_as_mapping*/ | |
104 0, /*tp_hash */ | |
105 0, /*tp_call*/ | |
106 0, /*tp_str*/ | |
107 0, /*tp_getattro*/ | |
108 0, /*tp_setattro*/ | |
109 0, /*tp_as_buffer*/ | |
110 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ | |
111 "stat objects", /* tp_doc */ | |
112 0, /* tp_traverse */ | |
113 0, /* tp_clear */ | |
114 0, /* tp_richcompare */ | |
115 0, /* tp_weaklistoffset */ | |
116 0, /* tp_iter */ | |
117 0, /* tp_iternext */ | |
118 0, /* tp_methods */ | |
119 0, /* tp_members */ | |
120 listdir_stat_getsets, /* tp_getset */ | |
121 0, /* tp_base */ | |
122 0, /* tp_dict */ | |
123 0, /* tp_descr_get */ | |
124 0, /* tp_descr_set */ | |
125 0, /* tp_dictoffset */ | |
126 0, /* tp_init */ | |
127 0, /* tp_alloc */ | |
128 listdir_stat_new, /* tp_new */ | |
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 |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
131 #ifdef _WIN32 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
132 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
133 static int to_python_time(const FILETIME *tm) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
134 { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
135 /* number of seconds between epoch and January 1 1601 */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
136 const __int64 a0 = (__int64)134774L * (__int64)24L * (__int64)3600L; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
137 /* conversion factor from 100ns to 1s */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
138 const __int64 a1 = 10000000; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
139 /* explicit (int) cast to suspend compiler warnings */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
140 return (int)((((__int64)tm->dwHighDateTime << 32) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
141 + tm->dwLowDateTime) / a1 - a0); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
142 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
143 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
144 static PyObject *make_item(const WIN32_FIND_DATAA *fd, int wantstat) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
145 { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
146 PyObject *py_st; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
147 struct hg_stat *stp; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
148 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
149 int kind = (fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
150 ? _S_IFDIR : _S_IFREG; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
151 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
152 if (!wantstat) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
153 return Py_BuildValue("si", fd->cFileName, kind); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
154 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
155 py_st = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
156 if (!py_st) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
157 return NULL; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
158 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
159 stp = &((struct listdir_stat *)py_st)->st; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
160 /* |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
161 use kind as st_mode |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
162 rwx bits on Win32 are meaningless |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
163 and Hg does not use them anyway |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
164 */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
165 stp->st_mode = kind; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
166 stp->st_mtime = to_python_time(&fd->ftLastWriteTime); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
167 stp->st_ctime = to_python_time(&fd->ftCreationTime); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
168 if (kind == _S_IFREG) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
169 stp->st_size = ((__int64)fd->nFileSizeHigh << 32) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
170 + fd->nFileSizeLow; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
171 return Py_BuildValue("siN", fd->cFileName, |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
172 kind, py_st); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
173 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
174 |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
175 static PyObject *_listdir(char *path, int plen, int wantstat, char *skip) |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
176 { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
177 PyObject *rval = NULL; /* initialize - return value */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
178 PyObject *list; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
179 HANDLE fh; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
180 WIN32_FIND_DATAA fd; |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
181 char *pattern; |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
182 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
183 /* build the path + \* pattern string */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
184 pattern = malloc(plen+3); /* path + \* + \0 */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
185 if (!pattern) { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
186 PyErr_NoMemory(); |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
187 goto error_nomem; |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
188 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
189 strcpy(pattern, path); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
190 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
191 if (plen > 0) { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
192 char c = path[plen-1]; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
193 if (c != ':' && c != '/' && c != '\\') |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
194 pattern[plen++] = '\\'; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
195 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
196 strcpy(pattern + plen, "*"); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
197 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
198 fh = FindFirstFileA(pattern, &fd); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
199 if (fh == INVALID_HANDLE_VALUE) { |
7059
6a76cf980999
Improve error handling in osutil.c
Petr Kodl <petrkodl@gmail.com>
parents:
7056
diff
changeset
|
200 PyErr_SetFromWindowsErrWithFilename(GetLastError(), path); |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
201 goto error_file; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
202 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
203 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
204 list = PyList_New(0); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
205 if (!list) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
206 goto error_list; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
207 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
208 do { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
209 PyObject *item; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
210 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
211 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
212 if (!strcmp(fd.cFileName, ".") |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
213 || !strcmp(fd.cFileName, "..")) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
214 continue; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
215 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
216 if (skip && !strcmp(fd.cFileName, skip)) { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
217 rval = PyList_New(0); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
218 goto error; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
219 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
220 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
221 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
222 item = make_item(&fd, wantstat); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
223 if (!item) |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
224 goto error; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
225 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
226 if (PyList_Append(list, item)) { |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
227 Py_XDECREF(item); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
228 goto error; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
229 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
230 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
231 Py_XDECREF(item); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
232 } while (FindNextFileA(fh, &fd)); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
233 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
234 if (GetLastError() != ERROR_NO_MORE_FILES) { |
7059
6a76cf980999
Improve error handling in osutil.c
Petr Kodl <petrkodl@gmail.com>
parents:
7056
diff
changeset
|
235 PyErr_SetFromWindowsErrWithFilename(GetLastError(), path); |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
236 goto error; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
237 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
238 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
239 rval = list; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
240 Py_XINCREF(rval); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
241 error: |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
242 Py_XDECREF(list); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
243 error_list: |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
244 FindClose(fh); |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
245 error_file: |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
246 free(pattern); |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
247 error_nomem: |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
248 return rval; |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
249 } |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
250 |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
251 #else |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
252 |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
253 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
|
254 { |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
255 #ifdef DT_REG |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
256 switch (ent->d_type) { |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
257 case DT_REG: return S_IFREG; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
258 case DT_DIR: return S_IFDIR; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
259 case DT_LNK: return S_IFLNK; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
260 case DT_BLK: return S_IFBLK; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
261 case DT_CHR: return S_IFCHR; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
262 case DT_FIFO: return S_IFIFO; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
263 case DT_SOCK: return S_IFSOCK; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
264 } |
5463
3b204881f959
osutil: use fdopendir instead of dirfd
Bryan O'Sullivan <bos@serpentine.com>
parents:
5457
diff
changeset
|
265 #endif |
7033
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
266 return -1; |
5425
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
267 } |
830f6e280c90
osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents:
5424
diff
changeset
|
268 |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
269 static PyObject *_listdir(char *path, int pathlen, int keepstat, char *skip) |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
270 { |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
271 PyObject *list, *elem, *stat, *ret = NULL; |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
272 char fullpath[PATH_MAX + 10]; |
7136
d834ed27199f
_listdir only uses dfd if AT_SYMLINK_NOFOLLOW is defined
Brendan Cully <brendan@kublai.com>
parents:
7098
diff
changeset
|
273 int kind, err; |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
274 struct stat st; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
275 struct dirent *ent; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
276 DIR *dir; |
7136
d834ed27199f
_listdir only uses dfd if AT_SYMLINK_NOFOLLOW is defined
Brendan Cully <brendan@kublai.com>
parents:
7098
diff
changeset
|
277 #ifdef AT_SYMLINK_NOFOLLOW |
d834ed27199f
_listdir only uses dfd if AT_SYMLINK_NOFOLLOW is defined
Brendan Cully <brendan@kublai.com>
parents:
7098
diff
changeset
|
278 int dfd = -1; |
d834ed27199f
_listdir only uses dfd if AT_SYMLINK_NOFOLLOW is defined
Brendan Cully <brendan@kublai.com>
parents:
7098
diff
changeset
|
279 #endif |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
280 |
7059
6a76cf980999
Improve error handling in osutil.c
Petr Kodl <petrkodl@gmail.com>
parents:
7056
diff
changeset
|
281 if (pathlen >= PATH_MAX) { |
6a76cf980999
Improve error handling in osutil.c
Petr Kodl <petrkodl@gmail.com>
parents:
7056
diff
changeset
|
282 PyErr_SetString(PyExc_ValueError, "path too long"); |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
283 goto error_value; |
7059
6a76cf980999
Improve error handling in osutil.c
Petr Kodl <petrkodl@gmail.com>
parents:
7056
diff
changeset
|
284 } |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
285 strncpy(fullpath, path, PATH_MAX); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
286 fullpath[pathlen] = '/'; |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
287 |
7033
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
288 #ifdef AT_SYMLINK_NOFOLLOW |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
289 dfd = open(path, O_RDONLY); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
290 if (dfd == -1) { |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
291 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
292 goto error_value; |
7033
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
293 } |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
294 dir = fdopendir(dfd); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
295 #else |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
296 dir = opendir(path); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
297 #endif |
5421 | 298 if (!dir) { |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
299 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
300 goto error_dir; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
301 } |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
302 |
5421 | 303 list = PyList_New(0); |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
304 if (!list) |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
305 goto error_list; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
306 |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
307 while ((ent = readdir(dir))) { |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
308 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
309 continue; |
5416
ca890c0c3f1f
osutil.c: style fix - delete trailing end-of-line spaces
Giorgos Keramidas <keramida@ceid.upatras.gr>
parents:
5398
diff
changeset
|
310 |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
311 kind = entkind(ent); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
312 if (kind == -1 || keepstat) { |
7033
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
313 #ifdef AT_SYMLINK_NOFOLLOW |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
314 err = fstatat(dfd, ent->d_name, &st, |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
315 AT_SYMLINK_NOFOLLOW); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
316 #else |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
317 strncpy(fullpath + pathlen + 1, ent->d_name, |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
318 PATH_MAX - pathlen); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
319 fullpath[PATH_MAX] = 0; |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
320 err = lstat(fullpath, &st); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
321 #endif |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
322 if (err == -1) { |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
323 strncpy(fullpath + pathlen + 1, ent->d_name, |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
324 PATH_MAX - pathlen); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
325 fullpath[PATH_MAX] = 0; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
326 PyErr_SetFromErrnoWithFilename(PyExc_OSError, |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
327 fullpath); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
328 goto error; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
329 } |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
330 kind = st.st_mode & S_IFMT; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
331 } |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
332 |
7034
0d513661d6c2
listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents:
7033
diff
changeset
|
333 /* quit early? */ |
0d513661d6c2
listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents:
7033
diff
changeset
|
334 if (skip && kind == S_IFDIR && !strcmp(ent->d_name, skip)) { |
0d513661d6c2
listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents:
7033
diff
changeset
|
335 ret = PyList_New(0); |
0d513661d6c2
listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents:
7033
diff
changeset
|
336 goto error; |
0d513661d6c2
listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents:
7033
diff
changeset
|
337 } |
0d513661d6c2
listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents:
7033
diff
changeset
|
338 |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
339 if (keepstat) { |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
340 stat = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
341 if (!stat) |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
342 goto error; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
343 memcpy(&((struct listdir_stat *)stat)->st, &st, sizeof(st)); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
344 elem = Py_BuildValue("siN", ent->d_name, kind, stat); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
345 } else |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
346 elem = Py_BuildValue("si", ent->d_name, kind); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
347 if (!elem) |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
348 goto error; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
349 |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
350 PyList_Append(list, elem); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
351 Py_DECREF(elem); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
352 } |
5421 | 353 |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
354 ret = list; |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
355 Py_INCREF(ret); |
5421 | 356 |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
357 error: |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
358 Py_DECREF(list); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
359 error_list: |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
360 closedir(dir); |
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
361 error_dir: |
7033
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
362 #ifdef AT_SYMLINK_NOFOLLOW |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
363 close(dfd); |
892d27fb04a5
osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents:
7031
diff
changeset
|
364 #endif |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
365 error_value: |
7031
19e8d034932e
osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents:
7022
diff
changeset
|
366 return ret; |
5421 | 367 } |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
368 |
7056
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
369 #endif /* ndef _WIN32 */ |
2c1f18b88b6a
osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents:
7034
diff
changeset
|
370 |
7098
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
371 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs) |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
372 { |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
373 PyObject *statobj = NULL; /* initialize - optional arg */ |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
374 PyObject *skipobj = NULL; /* initialize - optional arg */ |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
375 char *path, *skip = NULL; |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
376 int wantstat, plen; |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
377 |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
378 static char *kwlist[] = {"path", "stat", "skip", NULL}; |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
379 |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
380 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|OO:listdir", |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
381 kwlist, &path, &plen, &statobj, &skipobj)) |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
382 return NULL; |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
383 |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
384 wantstat = statobj && PyObject_IsTrue(statobj); |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
385 |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
386 if (skipobj && skipobj != Py_None) { |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
387 skip = PyString_AsString(skipobj); |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
388 if (!skip) |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
389 return NULL; |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
390 } |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
391 |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
392 return _listdir(path, plen, wantstat, skip); |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
393 } |
8a5c88c7e97b
osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7059
diff
changeset
|
394 |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
395 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
|
396 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
397 static PyMethodDef methods[] = { |
5421 | 398 {"listdir", (PyCFunction)listdir, METH_VARARGS | METH_KEYWORDS, |
399 "list a directory\n"}, | |
400 {NULL, NULL} | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
401 }; |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
402 |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
403 PyMODINIT_FUNC initosutil(void) |
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
404 { |
5421 | 405 if (PyType_Ready(&listdir_stat_type) == -1) |
406 return; | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
407 |
5421 | 408 Py_InitModule3("osutil", methods, osutil_doc); |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
409 } |