30
34
char *strerror(int errno)
36
cdef extern from 'unistd.h':
38
char *getcwd(char *, int size)
40
cdef extern from 'stdlib.h':
32
45
cdef extern from 'sys/types.h':
33
46
ctypedef long ssize_t
34
47
ctypedef unsigned long size_t
49
ctypedef unsigned long ino_t
50
ctypedef unsigned long long off_t
53
cdef extern from 'sys/stat.h':
61
int lstat(char *path, stat *buf)
66
int S_ISFIFO(int mode)
68
int S_ISSOCK(int mode)
71
cdef extern from 'Python.h':
72
char * PyString_AS_STRING(object)
73
ctypedef int Py_ssize_t # Required for older pyrex versions
74
ctypedef struct PyObject:
76
Py_ssize_t PyString_Size(object s)
77
object PyList_GetItem(object lst, Py_ssize_t index)
78
void *PyList_GetItem_object_void "PyList_GET_ITEM" (object lst, int index)
79
int PyList_Append(object lst, object item) except -1
80
void *PyTuple_GetItem_void_void "PyTuple_GET_ITEM" (void* tpl, int index)
81
int PyTuple_SetItem(void *, Py_ssize_t pos, object item) except -1
82
int PyTuple_SetItem_obj "PyTuple_SetItem" (void *, Py_ssize_t pos, PyObject * item) except -1
83
void Py_INCREF(object o)
84
void Py_DECREF(object o)
85
void PyString_Concat(PyObject **string, object newpart)
36
88
cdef extern from 'dirent.h':
44
89
ctypedef struct dirent:
46
# this will fail to compile if d_type is not defined.
47
# if this module fails to compile, use the .py version.
50
92
ctypedef struct DIR
51
93
# should be DIR *, pyrex barfs.
52
94
DIR * opendir(char * name)
61
103
_symlink = 'symlink'
62
104
_socket = 'socket'
63
105
_unknown = 'unknown'
67
108
# add a typedef struct dirent dirent to workaround pyrex
68
109
cdef extern from 'readdir.h':
114
"""Represent a 'stat' result."""
120
return self._st.st_dev
124
return self._st.st_ino
128
return self._st.st_mode
132
return self._st.st_ctime
136
return self._st.st_mtime
140
return self._st.st_size
143
"""Repr is the same as a Stat object.
145
(mode, ino, dev, nlink, uid, gid, size, None(atime), mtime, ctime)
147
return repr((self.st_mode, 0, 0, 0, 0, 0, self.st_size, None,
148
self._mtime, self._ctime))
151
from bzrlib import osutils
154
cdef class UTF8DirReader:
155
"""A dir reader for utf8 file systems."""
157
cdef readonly object _safe_utf8
158
cdef _directory, _chardev, _block, _file, _fifo, _symlink
159
cdef _socket, _unknown
162
self._safe_utf8 = osutils.safe_utf8
163
self._directory = _directory
164
self._chardev = _chardev
168
self._symlink = _symlink
169
self._socket = _socket
170
self._unknown = _unknown
172
def kind_from_mode(self, int mode):
173
"""Get the kind of a path from a mode status."""
174
return self._kind_from_mode(mode)
176
cdef _kind_from_mode(self, int mode):
177
# Files and directories are the most common - check them first.
181
return self._directory
194
def top_prefix_to_starting_dir(self, top, prefix=""):
195
"""See DirReader.top_prefix_to_starting_dir."""
196
return (self._safe_utf8(prefix), None, None, None,
197
self._safe_utf8(top))
199
def read_dir(self, prefix, top):
200
"""Read a single directory from a utf8 file system.
202
All paths in and out are utf8.
204
This sub-function is called when we know the filesystem is already in utf8
205
encoding. So we don't need to transcode filenames.
207
See DirReader.read_dir for details.
209
#cdef char *_prefix = prefix
210
#cdef char *_top = top
211
# Use C accelerated directory listing.
217
cdef PyObject * new_val_obj
219
if PyString_Size(prefix):
220
relprefix = prefix + '/'
223
top_slash = top + '/'
225
# read_dir supplies in should-stat order.
226
# for _, name in sorted(_listdir(top)):
227
result = _read_dir(top)
230
for index from 0 <= index < length:
231
atuple = PyList_GetItem_object_void(result, index)
232
name = <object>PyTuple_GetItem_void_void(atuple, 1)
233
# We have a tuple with (inode, name, None, statvalue, None)
235
# inode -> path_from_top
236
# direct concat - faster than operator +.
237
new_val_obj = <PyObject *>relprefix
239
PyString_Concat(&new_val_obj, name)
240
if NULL == new_val_obj:
241
# PyString_Concat will have setup an exception, but how to get
243
raise Exception("failed to strcat")
244
PyTuple_SetItem_obj(atuple, 0, new_val_obj)
246
newval = self._kind_from_mode(
247
(<_Stat>PyTuple_GetItem_void_void(atuple, 3)).st_mode)
249
PyTuple_SetItem(atuple, 2, newval)
250
# 2nd None -> abspath # for all - the caller may need to stat files
252
# direct concat - faster than operator +.
253
new_val_obj = <PyObject *>top_slash
255
PyString_Concat(&new_val_obj, name)
256
if NULL == new_val_obj:
257
# PyString_Concat will have setup an exception, but how to get
259
raise Exception("failed to strcat")
260
PyTuple_SetItem_obj(atuple, 4, new_val_obj)
264
cdef _read_dir(path):
72
265
"""Like os.listdir, this reads the contents of a directory.
74
267
:param path: the directory to list.
75
:return: a list of (sort_key, basename) tuples.
268
:return: a list of single-owner (the list) tuples ready for editing into
269
the result tuples walkdirs needs to yield. They contain (inode, name,
270
None, statvalue, None).
78
273
# currently this needs a fixup - the C code says 'dirent' but should say
105
307
name = entry.d_name
106
if not (name[0] == dot and (
308
if not (name[0] == c"." and (
107
309
(name[1] == 0) or
108
(name[1] == dot and name[2] == 0))
310
(name[1] == c"." and name[2] == 0))
110
result.append((entry.d_ino, entry.d_name))
313
stat_result = lstat(entry.d_name, &statvalue._st)
316
raise OSError(errno, strerror(errno))
320
# We append a 5-tuple that can be modified in-place by the C
322
# inode to sort on (to replace with top_path)
324
# kind (None, to set)
325
# statvalue (to keep)
326
# abspath (None, to set)
327
PyList_Append(result, (entry.d_ino, entry.d_name, None,
332
raise OSError(errno, strerror(errno))
112
334
if -1 == closedir(the_dir):
113
335
raise OSError(errno, strerror(errno))