24
# the opaque C library DIR type.
24
cdef extern from "python-compat.h":
25
28
cdef extern from 'errno.h':
33
char *strerror(int errno)
34
# not necessarily a real variable, but this should be close enough
30
char *strerror(int errno)
37
cdef extern from 'unistd.h':
41
char *getcwd(char *, int size)
43
cdef extern from 'stdlib.h':
32
48
cdef extern from 'sys/types.h':
33
49
ctypedef long ssize_t
34
50
ctypedef unsigned long size_t
52
ctypedef unsigned long ino_t
53
ctypedef unsigned long long off_t
57
cdef extern from 'sys/stat.h':
65
int lstat(char *path, stat *buf)
70
int S_ISFIFO(int mode)
72
int S_ISSOCK(int mode)
75
cdef extern from 'fcntl.h':
77
int open(char *pathname, int flags, mode_t mode)
80
cdef extern from 'Python.h':
81
int PyErr_CheckSignals() except -1
82
char * PyString_AS_STRING(object)
83
ctypedef int Py_ssize_t # Required for older pyrex versions
84
ctypedef struct PyObject:
86
Py_ssize_t PyString_Size(object s)
87
object PyList_GetItem(object lst, Py_ssize_t index)
88
void *PyList_GetItem_object_void "PyList_GET_ITEM" (object lst, int index)
89
int PyList_Append(object lst, object item) except -1
90
void *PyTuple_GetItem_void_void "PyTuple_GET_ITEM" (void* tpl, int index)
91
int PyTuple_SetItem(void *, Py_ssize_t pos, object item) except -1
92
int PyTuple_SetItem_obj "PyTuple_SetItem" (void *, Py_ssize_t pos, PyObject * item) except -1
93
void Py_INCREF(object o)
94
void Py_DECREF(object o)
95
void PyString_Concat(PyObject **string, object newpart)
36
98
cdef extern from 'dirent.h':
44
99
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.
102
# the opaque C library DIR type.
50
103
ctypedef struct DIR
51
104
# should be DIR *, pyrex barfs.
52
105
DIR * opendir(char * name)
53
106
int closedir(DIR * dir)
54
107
dirent *readdir(DIR *dir)
109
cdef object _directory
56
110
_directory = 'directory'
57
112
_chardev = 'chardev'
61
120
_symlink = 'symlink'
62
122
_socket = 'socket'
63
124
_unknown = 'unknown'
67
126
# add a typedef struct dirent dirent to workaround pyrex
68
127
cdef extern from 'readdir.h':
132
"""Represent a 'stat' result."""
138
return self._st.st_dev
142
return self._st.st_ino
146
return self._st.st_mode
150
return self._st.st_ctime
154
return self._st.st_mtime
158
return self._st.st_size
161
"""Repr is the same as a Stat object.
163
(mode, ino, dev, nlink, uid, gid, size, None(atime), mtime, ctime)
165
return repr((self.st_mode, 0, 0, 0, 0, 0, self.st_size, None,
166
self.st_mtime, self.st_ctime))
169
from bzrlib import osutils
171
cdef object _safe_utf8
172
_safe_utf8 = osutils.safe_utf8
174
cdef class UTF8DirReader:
175
"""A dir reader for utf8 file systems."""
177
def kind_from_mode(self, int mode):
178
"""Get the kind of a path from a mode status."""
179
return self._kind_from_mode(mode)
181
cdef _kind_from_mode(self, int mode):
182
# Files and directories are the most common - check them first.
199
def top_prefix_to_starting_dir(self, top, prefix=""):
200
"""See DirReader.top_prefix_to_starting_dir."""
201
return (_safe_utf8(prefix), None, None, None, _safe_utf8(top))
203
def read_dir(self, prefix, top):
204
"""Read a single directory from a utf8 file system.
206
All paths in and out are utf8.
208
This sub-function is called when we know the filesystem is already in utf8
209
encoding. So we don't need to transcode filenames.
211
See DirReader.read_dir for details.
213
#cdef char *_prefix = prefix
214
#cdef char *_top = top
215
# Use C accelerated directory listing.
221
cdef PyObject * new_val_obj
223
if PyString_Size(prefix):
224
relprefix = prefix + '/'
227
top_slash = top + '/'
229
# read_dir supplies in should-stat order.
230
# for _, name in sorted(_listdir(top)):
231
result = _read_dir(top)
234
for index from 0 <= index < length:
235
atuple = PyList_GetItem_object_void(result, index)
236
name = <object>PyTuple_GetItem_void_void(atuple, 1)
237
# We have a tuple with (inode, name, None, statvalue, None)
239
# inode -> path_from_top
240
# direct concat - faster than operator +.
241
new_val_obj = <PyObject *>relprefix
243
PyString_Concat(&new_val_obj, name)
244
if NULL == new_val_obj:
245
# PyString_Concat will have setup an exception, but how to get
247
raise Exception("failed to strcat")
248
PyTuple_SetItem_obj(atuple, 0, new_val_obj)
250
newval = self._kind_from_mode(
251
(<_Stat>PyTuple_GetItem_void_void(atuple, 3)).st_mode)
253
PyTuple_SetItem(atuple, 2, newval)
254
# 2nd None -> abspath # for all - the caller may need to stat files
256
# direct concat - faster than operator +.
257
new_val_obj = <PyObject *>top_slash
259
PyString_Concat(&new_val_obj, name)
260
if NULL == new_val_obj:
261
# PyString_Concat will have setup an exception, but how to get
263
raise Exception("failed to strcat")
264
PyTuple_SetItem_obj(atuple, 4, new_val_obj)
268
cdef raise_os_error(int errnum, char *msg_prefix, path):
271
raise OSError(errnum, msg_prefix + strerror(errnum), path)
274
cdef _read_dir(path):
72
275
"""Like os.listdir, this reads the contents of a directory.
74
277
:param path: the directory to list.
75
:return: a list of (sort_key, basename) tuples.
278
:return: a list of single-owner (the list) tuples ready for editing into
279
the result tuples walkdirs needs to yield. They contain (inode, name,
280
None, statvalue, None).
78
283
# currently this needs a fixup - the C code says 'dirent' but should say
80
285
cdef dirent * entry
81
286
cdef dirent sentinel
83
the_dir = opendir(path)
85
raise OSError(errno, strerror(errno))
293
# Avoid chdir('') because it causes problems on Sun OS, and avoid this if
295
if path != "" and path != '.':
296
# we change into the requested directory before reading, and back at the
297
# end, because that turns out to make the stat calls measurably faster than
298
# passing full paths every time.
299
orig_dir_fd = open(".", O_RDONLY, 0)
300
if orig_dir_fd == -1:
301
raise_os_error(errno, "open: ", ".")
302
if -1 == chdir(path):
303
# Ignore the return value, because we are already raising an
306
raise_os_error(errno, "chdir: ", path)
90
entry = readdir(the_dir)
95
elif errno != ENOTDIR and errno != ENOENT and errno != 0:
96
# We see ENOTDIR at the end of a normal directory.
97
# As ENOTDIR for read_dir(file) is triggered on opendir,
98
# we consider ENOTDIR to be 'no error'.
99
# ENOENT is listed as 'invalid position in the dir stream' for
100
# readdir. We swallow this for now and just keep reading.
101
raise OSError(errno, strerror(errno))
106
if not (name[0] == dot and (
108
(name[1] == dot and name[2] == 0))
110
result.append((entry.d_ino, entry.d_name))
311
the_dir = opendir(".")
313
raise_os_error(errno, "opendir: ", path)
318
# Unlike most libc functions, readdir needs errno set to 0
319
# beforehand so that eof can be distinguished from errors. See
320
# <https://bugs.launchpad.net/bzr/+bug/279381>
323
entry = readdir(the_dir)
324
if entry == NULL and (errno == EAGAIN or errno == EINTR):
332
if errno == ENOTDIR or errno == 0:
333
# We see ENOTDIR at the end of a normal directory.
334
# As ENOTDIR for read_dir(file) is triggered on opendir,
335
# we consider ENOTDIR to be 'no error'.
338
raise_os_error(errno, "readdir: ", path)
340
if not (name[0] == c"." and (
342
(name[1] == c"." and name[2] == 0))
345
stat_result = lstat(entry.d_name, &statvalue._st)
348
raise_os_error(errno, "lstat: ",
349
path + "/" + entry.d_name)
351
# the file seems to have disappeared after being
352
# seen by readdir - perhaps a transient temporary
353
# file. there's no point returning it.
355
# We append a 5-tuple that can be modified in-place by the C
357
# inode to sort on (to replace with top_path)
359
# kind (None, to set)
360
# statvalue (to keep)
361
# abspath (None, to set)
362
PyList_Append(result, (entry.d_ino, entry.d_name, None,
365
if -1 == closedir(the_dir):
366
raise_os_error(errno, "closedir: ", path)
112
if -1 == closedir(the_dir):
113
raise OSError(errno, strerror(errno))
368
if -1 != orig_dir_fd:
370
if -1 == fchdir(orig_dir_fd):
371
# try to close the original directory anyhow
373
if -1 == close(orig_dir_fd) or failed:
374
raise_os_error(errno, "return to orig_dir: ", "")