30
30
char *strerror(int errno)
32
cdef extern from 'unistd.h':
34
char *getcwd(char *, int size)
36
cdef extern from 'stdlib.h':
32
41
cdef extern from 'sys/types.h':
33
42
ctypedef long ssize_t
34
43
ctypedef unsigned long size_t
45
ctypedef unsigned long ino_t
46
ctypedef unsigned long long off_t
49
cdef extern from 'sys/stat.h':
57
int lstat(char *path, stat *buf)
62
int S_ISFIFO(int mode)
64
int S_ISSOCK(int mode)
67
cdef extern from 'Python.h':
68
char * PyString_AS_STRING(object)
69
ctypedef int Py_ssize_t # Required for older pyrex versions
70
ctypedef struct PyObject:
72
Py_ssize_t PyString_Size(object s)
73
object PyList_GetItem(object lst, Py_ssize_t index)
74
void *PyList_GetItem_object_void "PyList_GET_ITEM" (object lst, int index)
75
int PyList_Append(object lst, object item) except -1
76
void *PyTuple_GetItem_void_void "PyTuple_GET_ITEM" (void* tpl, int index)
77
int PyTuple_SetItem(void *, Py_ssize_t pos, object item) except -1
78
int PyTuple_SetItem_obj "PyTuple_SetItem" (void *, Py_ssize_t pos, PyObject * item) except -1
79
void Py_INCREF(object o)
80
void Py_DECREF(object o)
81
void PyString_Concat(PyObject **string, object newpart)
36
84
cdef extern from 'dirent.h':
44
85
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
88
ctypedef struct DIR
51
89
# should be DIR *, pyrex barfs.
52
90
DIR * opendir(char * name)
61
99
_symlink = 'symlink'
62
100
_socket = 'socket'
63
101
_unknown = 'unknown'
67
104
# add a typedef struct dirent dirent to workaround pyrex
68
105
cdef extern from 'readdir.h':
110
"""Represent a 'stat' result."""
116
return self._st.st_dev
120
return self._st.st_ino
124
return self._st.st_mode
128
return self._st.st_ctime
132
return self._st.st_mtime
136
return self._st.st_size
139
"""Repr is the same as a Stat object.
141
(mode, ino, dev, nlink, uid, gid, size, None(atime), mtime, ctime)
143
return repr((self.st_mode, 0, 0, 0, 0, 0, self.st_size, None,
144
self._mtime, self._ctime))
147
from bzrlib import osutils
150
cdef class UTF8DirReader:
151
"""A dir reader for utf8 file systems."""
153
cdef readonly object _safe_utf8
154
cdef _directory, _chardev, _block, _file, _fifo, _symlink
155
cdef _socket, _unknown
158
self._safe_utf8 = osutils.safe_utf8
159
self._directory = _directory
160
self._chardev = _chardev
164
self._symlink = _symlink
165
self._socket = _socket
166
self._unknown = _unknown
168
def kind_from_mode(self, int mode):
169
"""Get the kind of a path from a mode status."""
170
return self._kind_from_mode(mode)
172
cdef _kind_from_mode(self, int mode):
173
# Files and directories are the most common - check them first.
177
return self._directory
190
def top_prefix_to_starting_dir(self, top, prefix=""):
191
"""See DirReader.top_prefix_to_starting_dir."""
192
return (self._safe_utf8(prefix), None, None, None,
193
self._safe_utf8(top))
195
def read_dir(self, prefix, top):
196
"""Read a single directory from a utf8 file system.
198
All paths in and out are utf8.
200
This sub-function is called when we know the filesystem is already in utf8
201
encoding. So we don't need to transcode filenames.
203
See DirReader.read_dir for details.
205
#cdef char *_prefix = prefix
206
#cdef char *_top = top
207
# Use C accelerated directory listing.
213
cdef PyObject * new_val_obj
215
if PyString_Size(prefix):
216
relprefix = prefix + '/'
219
top_slash = top + '/'
221
# read_dir supplies in should-stat order.
222
# for _, name in sorted(_listdir(top)):
223
result = _read_dir(top)
226
for index from 0 <= index < length:
227
atuple = PyList_GetItem_object_void(result, index)
228
name = <object>PyTuple_GetItem_void_void(atuple, 1)
229
# We have a tuple with (inode, name, None, statvalue, None)
231
# inode -> path_from_top
232
# direct concat - faster than operator +.
233
new_val_obj = <PyObject *>relprefix
235
PyString_Concat(&new_val_obj, name)
236
if NULL == new_val_obj:
237
# PyString_Concat will have setup an exception, but how to get
239
raise Exception("failed to strcat")
240
PyTuple_SetItem_obj(atuple, 0, new_val_obj)
242
newval = self._kind_from_mode(
243
(<_Stat>PyTuple_GetItem_void_void(atuple, 3)).st_mode)
245
PyTuple_SetItem(atuple, 2, newval)
246
# 2nd None -> abspath # for all - the caller may need to stat files
248
# direct concat - faster than operator +.
249
new_val_obj = <PyObject *>top_slash
251
PyString_Concat(&new_val_obj, name)
252
if NULL == new_val_obj:
253
# PyString_Concat will have setup an exception, but how to get
255
raise Exception("failed to strcat")
256
PyTuple_SetItem_obj(atuple, 4, new_val_obj)
260
cdef _read_dir(path):
72
261
"""Like os.listdir, this reads the contents of a directory.
74
263
:param path: the directory to list.
75
:return: a list of (sort_key, basename) tuples.
264
:return: a list of single-owner (the list) tuples ready for editing into
265
the result tuples walkdirs needs to yield. They contain (inode, name,
266
None, statvalue, None).
78
269
# currently this needs a fixup - the C code says 'dirent' but should say
105
303
name = entry.d_name
106
if not (name[0] == dot and (
304
if not (name[0] == c"." and (
107
305
(name[1] == 0) or
108
(name[1] == dot and name[2] == 0))
306
(name[1] == c"." and name[2] == 0))
110
result.append((entry.d_ino, entry.d_name))
309
stat_result = lstat(entry.d_name, &statvalue._st)
312
raise OSError(errno, strerror(errno))
316
# We append a 5-tuple that can be modified in-place by the C
318
# inode to sort on (to replace with top_path)
320
# kind (None, to set)
321
# statvalue (to keep)
322
# abspath (None, to set)
323
PyList_Append(result, (entry.d_ino, entry.d_name, None,
328
raise OSError(errno, strerror(errno))
112
330
if -1 == closedir(the_dir):
113
331
raise OSError(errno, strerror(errno))