1
# Copyright (C) 2006, 2008 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Wrapper for readdir which returns files ordered by inode."""
24
cdef extern from "python-compat.h":
28
# the opaque C library DIR type.
29
cdef extern from 'errno.h':
34
char *strerror(int errno)
36
cdef extern from 'unistd.h':
38
char *getcwd(char *, int size)
40
cdef extern from 'stdlib.h':
45
cdef extern from 'sys/types.h':
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)
88
cdef extern from 'dirent.h':
89
ctypedef struct dirent:
93
# should be DIR *, pyrex barfs.
94
DIR * opendir(char * name)
95
int closedir(DIR * dir)
96
dirent *readdir(DIR *dir)
98
_directory = 'directory'
108
# add a typedef struct dirent dirent to workaround pyrex
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):
265
"""Like os.listdir, this reads the contents of a directory.
267
:param path: the directory to list.
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).
273
# currently this needs a fixup - the C code says 'dirent' but should say
282
cwd = getcwd(NULL, 0)
283
if -1 == chdir(path):
284
raise OSError(errno, strerror(errno))
285
the_dir = opendir(".")
287
raise OSError(errno, strerror(errno))
292
entry = readdir(the_dir)
297
elif errno != ENOTDIR and errno != ENOENT and errno != 0:
298
# We see ENOTDIR at the end of a normal directory.
299
# As ENOTDIR for read_dir(file) is triggered on opendir,
300
# we consider ENOTDIR to be 'no error'.
301
# ENOENT is listed as 'invalid position in the dir stream' for
302
# readdir. We swallow this for now and just keep reading.
303
raise OSError(errno, strerror(errno))
308
if not (name[0] == c"." and (
310
(name[1] == c"." and name[2] == 0))
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))
334
if -1 == closedir(the_dir):
335
raise OSError(errno, strerror(errno))
339
# vim: tw=79 ai expandtab sw=4 sts=4