30
30
char *strerror(int errno)
32
cdef extern from 'unistd.h':
34
char *getcwd(char *, int size)
36
cdef extern from 'stdlib.h':
40
cdef extern from 'sys/stat.h':
48
int lstat(char *path, stat *buf)
53
int S_ISFIFO(int mode)
55
int S_ISSOCK(int mode)
32
58
cdef extern from 'sys/types.h':
33
59
ctypedef long ssize_t
34
60
ctypedef unsigned long size_t
64
cdef extern from 'Python.h':
65
char * PyString_AS_STRING(object)
66
int PyOS_snprintf(char *str, size_t size, char *format, ...)
67
ctypedef int Py_ssize_t # Required for older pyrex versions
68
Py_ssize_t PyString_Size(object s)
69
object PyList_GetItem(object lst, Py_ssize_t index)
70
void *PyList_GetItem_object_void "PyList_GET_ITEM" (object lst, int index)
71
int PyList_Append(object lst, object item) except -1
72
void *PyTuple_GetItem_void_void "PyTuple_GET_ITEM" (void* tpl, int index)
73
int PyTuple_SetItem(void *, Py_ssize_t pos, object item) except -1
74
void Py_INCREF(object o)
75
void Py_DECREF(object o)
36
78
cdef extern from 'dirent.h':
68
111
cdef extern from 'readdir.h':
116
"""Represent a 'stat' result."""
118
cdef readonly int st_mode
119
# nanosecond time definitions use MACROS, fucking up te ability to have a
120
# variable called st_mtime. Yay Ulrich, thanks a lot.
121
cdef readonly time_t _ctime
122
cdef readonly time_t _mtime
123
# cdef readonly double st_atime
124
cdef readonly int st_size
126
cdef readonly int st_dev
127
cdef readonly int st_ino
138
"""Repr is the same as a Stat object.
140
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)
142
#return repr((self.st_mode, 0, 0, 0, 0, 0, self.st_size, self.st_atime,
143
# self.st_mtime, self.st_ctime))
144
return repr((self.st_mode, 0, 0, 0, 0, 0, self.st_size, None,
145
self._mtime, self._ctime))
148
from bzrlib import osutils
151
cdef class UTF8DirReader:
152
"""A dir reader for utf8 file systems."""
154
cdef readonly object _safe_utf8
155
cdef _directory, _chardev, _block, _file, _fifo, _symlink
156
cdef _socket, _unknown
159
self._safe_utf8 = osutils.safe_utf8
160
self._directory = _directory
161
self._chardev = _chardev
165
self._symlink = _symlink
166
self._socket = _socket
167
self._unknown = _unknown
169
def kind_from_mode(self, int mode):
170
"""Get the kind of a path from a mode status."""
171
return self._kind_from_mode(mode)
173
cdef _kind_from_mode(self, int mode):
174
# in order of frequency:
178
return self._directory
191
def top_prefix_to_starting_dir(self, top, prefix=""):
192
"""See DirReader.top_prefix_to_starting_dir."""
193
return (self._safe_utf8(prefix), None, None, None,
194
self._safe_utf8(top))
196
def read_dir(self, prefix, top):
197
"""Read a single directory from a utf8 file system.
199
All paths in and out are utf8.
201
This sub-function is called when we know the filesystem is already in utf8
202
encoding. So we don't need to transcode filenames.
204
See DirReader.read_dir for details.
206
#cdef char *_prefix = prefix
207
#cdef char *_top = top
208
# Use C accelerated directory listing.
216
if PyString_Size(prefix):
217
relprefix = prefix + '/'
220
top_slash = top + '/'
222
# read_dir supplies in should-stat order.
223
# for _, name in sorted(_listdir(top)):
224
result = _read_dir(top)
227
for index from 0 <= index < length:
228
atuple = PyList_GetItem_object_void(result, index)
229
name = <object>PyTuple_GetItem_void_void(atuple, 1)
230
# We have inode, name, None, statvalue, None
231
# inode -> path_from_top
232
newval = relprefix + name
234
PyTuple_SetItem(atuple, 0, newval)
236
newval = self._kind_from_mode(
237
(<_Stat>PyTuple_GetItem_void_void(atuple, 3)).st_mode)
239
PyTuple_SetItem(atuple, 2, newval)
240
# none -> abspath # perhaps only do if its a dir?
241
newval = top_slash + name
243
PyTuple_SetItem(atuple, 4, newval)
247
cdef _read_dir(path):
72
248
"""Like os.listdir, this reads the contents of a directory.
74
250
:param path: the directory to list.
107
302
(name[1] == 0) or
108
303
(name[1] == dot and name[2] == 0))
110
result.append((entry.d_ino, entry.d_name))
305
snprintf_result = PyOS_snprintf(pathstart, 1025, "%s", name)
306
if snprintf_result < 0:
307
raise OSError(errno, strerror(errno))
308
stat_result = lstat(entry.d_name, &st)
309
# stat_result = lstat(abspath, &st)
312
raise OSError(errno, strerror(errno))
318
statvalue.st_mode = st.st_mode
319
statvalue._ctime = st.st_ctime
320
statvalue._mtime = st.st_mtime
321
# statvalue.st_atime = st.st_atime
322
statvalue.st_size = st.st_size
323
statvalue.st_ino = st.st_ino
324
statvalue.st_dev = st.st_dev
325
# We append a 5-tuple that can be modified in-place by the C
327
# inode to sort on and replace with top_path
332
PyList_Append(result, (entry.d_ino, entry.d_name, None,
338
raise OSError(errno, strerror(errno))
112
340
if -1 == closedir(the_dir):
113
341
raise OSError(errno, strerror(errno))