~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_readdir_pyx.pyx

  • Committer: Robert Collins
  • Date: 2008-09-23 23:28:27 UTC
  • mto: (3696.4.15 process-entry-optimised)
  • mto: This revision was merged to the branch mainline in revision 3731.
  • Revision ID: robertc@robertcollins.net-20080923232827-vpd4xsif8x8op6i3
Review feedback.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2008, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Wrapper for readdir which returns files ordered by inode."""
18
18
 
20
20
import os
21
21
import sys
22
22
 
23
 
#python2.4 support
24
 
cdef extern from "python-compat.h":
25
 
    pass
26
 
 
27
 
 
 
23
 
 
24
# the opaque C library DIR type.
28
25
cdef extern from 'errno.h':
29
26
    int ENOENT
30
27
    int ENOTDIR
31
28
    int EAGAIN
32
 
    int EINTR
 
29
    int errno
33
30
    char *strerror(int errno)
34
 
    # not necessarily a real variable, but this should be close enough
35
 
    int errno
36
31
 
37
32
cdef extern from 'unistd.h':
38
33
    int chdir(char *path)
39
 
    int close(int fd)
40
 
    int fchdir(int fd)
41
34
    char *getcwd(char *, int size)
42
35
 
43
36
cdef extern from 'stdlib.h':
51
44
    ctypedef long time_t
52
45
    ctypedef unsigned long ino_t
53
46
    ctypedef unsigned long long off_t
54
 
    ctypedef int mode_t
55
47
 
56
48
 
57
49
cdef extern from 'sys/stat.h':
72
64
    int S_ISSOCK(int mode)
73
65
 
74
66
 
75
 
cdef extern from 'fcntl.h':
76
 
    int O_RDONLY
77
 
    int open(char *pathname, int flags, mode_t mode)
78
 
 
79
 
 
80
67
cdef extern from 'Python.h':
81
 
    int PyErr_CheckSignals() except -1
82
68
    char * PyString_AS_STRING(object)
83
69
    ctypedef int Py_ssize_t # Required for older pyrex versions
84
70
    ctypedef struct PyObject:
99
85
    ctypedef struct dirent:
100
86
        char d_name[256]
101
87
        ino_t d_ino
102
 
    # the opaque C library DIR type.
103
88
    ctypedef struct DIR
104
89
    # should be DIR *, pyrex barfs.
105
90
    DIR * opendir(char * name)
156
141
        (mode, ino, dev, nlink, uid, gid, size, None(atime), mtime, ctime)
157
142
        """
158
143
        return repr((self.st_mode, 0, 0, 0, 0, 0, self.st_size, None,
159
 
                     self.st_mtime, self.st_ctime))
 
144
                     self._mtime, self._ctime))
160
145
 
161
146
 
162
147
from bzrlib import osutils
272
257
        return result
273
258
 
274
259
 
275
 
cdef raise_os_error(int errnum, char *msg_prefix, path):
276
 
    if errnum == EINTR:
277
 
        PyErr_CheckSignals()
278
 
    raise OSError(errnum, msg_prefix + strerror(errnum), path)
279
 
 
280
 
 
281
260
cdef _read_dir(path):
282
261
    """Like os.listdir, this reads the contents of a directory.
283
262
 
294
273
    cdef char *name
295
274
    cdef int stat_result
296
275
    cdef _Stat statvalue
297
 
    global errno
298
 
    cdef int orig_dir_fd
299
 
 
300
 
    # Avoid chdir('') because it causes problems on Sun OS, and avoid this if
301
 
    # staying in .
302
 
    if path != "" and path != '.':
303
 
        # we change into the requested directory before reading, and back at the
304
 
        # end, because that turns out to make the stat calls measurably faster than
305
 
        # passing full paths every time.
306
 
        orig_dir_fd = open(".", O_RDONLY, 0)
307
 
        if orig_dir_fd == -1:
308
 
            raise_os_error(errno, "open: ", ".")
309
 
        if -1 == chdir(path):
310
 
            raise_os_error(errno, "chdir: ", path)
311
 
    else:
312
 
        orig_dir_fd = -1
313
 
 
 
276
    cdef char *cwd
 
277
 
 
278
    cwd = getcwd(NULL, 0)
 
279
    if -1 == chdir(path):
 
280
        raise OSError(errno, strerror(errno))
 
281
    the_dir = opendir(".")
 
282
    if NULL == the_dir:
 
283
        raise OSError(errno, strerror(errno))
 
284
    result = []
314
285
    try:
315
 
        the_dir = opendir(".")
316
 
        if NULL == the_dir:
317
 
            raise_os_error(errno, "opendir: ", path)
318
 
        try:
319
 
            result = []
320
 
            entry = &sentinel
321
 
            while entry != NULL:
322
 
                # Unlike most libc functions, readdir needs errno set to 0
323
 
                # beforehand so that eof can be distinguished from errors.  See
324
 
                # <https://bugs.launchpad.net/bzr/+bug/279381>
325
 
                while True:
326
 
                    errno = 0
327
 
                    entry = readdir(the_dir)
328
 
                    if entry == NULL and (errno == EAGAIN or errno == EINTR):
329
 
                        if errno == EINTR:
330
 
                            PyErr_CheckSignals()
331
 
                        # try again
332
 
                        continue
333
 
                    else:
334
 
                        break
335
 
                if entry == NULL:
336
 
                    if errno == ENOTDIR or errno == 0:
337
 
                        # We see ENOTDIR at the end of a normal directory.
338
 
                        # As ENOTDIR for read_dir(file) is triggered on opendir,
339
 
                        # we consider ENOTDIR to be 'no error'.
340
 
                        continue
341
 
                    else:
342
 
                        raise_os_error(errno, "readdir: ", path)
343
 
                name = entry.d_name
344
 
                if not (name[0] == c"." and (
345
 
                    (name[1] == 0) or 
346
 
                    (name[1] == c"." and name[2] == 0))
347
 
                    ):
348
 
                    statvalue = _Stat()
349
 
                    stat_result = lstat(entry.d_name, &statvalue._st)
350
 
                    if stat_result != 0:
351
 
                        if errno != ENOENT:
352
 
                            raise_os_error(errno, "lstat: ",
353
 
                                path + "/" + entry.d_name)
354
 
                        else:
355
 
                            # the file seems to have disappeared after being
356
 
                            # seen by readdir - perhaps a transient temporary
357
 
                            # file.  there's no point returning it.
358
 
                            continue
359
 
                    # We append a 5-tuple that can be modified in-place by the C
360
 
                    # api:
361
 
                    # inode to sort on (to replace with top_path)
362
 
                    # name (to keep)
363
 
                    # kind (None, to set)
364
 
                    # statvalue (to keep)
365
 
                    # abspath (None, to set)
366
 
                    PyList_Append(result, (entry.d_ino, entry.d_name, None,
367
 
                        statvalue, None))
368
 
        finally:
369
 
            if -1 == closedir(the_dir):
370
 
                raise_os_error(errno, "closedir: ", path)
 
286
        entry = &sentinel
 
287
        while entry != NULL:
 
288
            entry = readdir(the_dir)
 
289
            if entry == NULL:
 
290
                if errno == EAGAIN:
 
291
                    # try again
 
292
                    continue
 
293
                elif errno != ENOTDIR and errno != ENOENT and errno != 0:
 
294
                    # We see ENOTDIR at the end of a normal directory.
 
295
                    # As ENOTDIR for read_dir(file) is triggered on opendir,
 
296
                    # we consider ENOTDIR to be 'no error'.
 
297
                    # ENOENT is listed as 'invalid position in the dir stream' for
 
298
                    # readdir. We swallow this for now and just keep reading.
 
299
                    raise OSError(errno, strerror(errno))
 
300
                else:
 
301
                    # done
 
302
                    continue
 
303
            name = entry.d_name
 
304
            if not (name[0] == c"." and (
 
305
                (name[1] == 0) or 
 
306
                (name[1] == c"." and name[2] == 0))
 
307
                ):
 
308
                statvalue = _Stat()
 
309
                stat_result = lstat(entry.d_name, &statvalue._st)
 
310
                if stat_result != 0:
 
311
                    if errno != ENOENT:
 
312
                        raise OSError(errno, strerror(errno))
 
313
                    else:
 
314
                        kind = _missing
 
315
                        statvalue = None
 
316
                # We append a 5-tuple that can be modified in-place by the C
 
317
                # api:
 
318
                # inode to sort on (to replace with top_path)
 
319
                # name (to keep)
 
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,
 
324
                    statvalue, None))
371
325
    finally:
372
 
        if -1 != orig_dir_fd:
373
 
            failed = False
374
 
            if -1 == fchdir(orig_dir_fd):
375
 
                # try to close the original directory anyhow
376
 
                failed = True
377
 
            if -1 == close(orig_dir_fd) or failed:
378
 
                raise_os_error(errno, "return to orig_dir: ", "")
379
 
 
 
326
        if -1 == chdir(cwd):
 
327
            free(cwd)
 
328
            raise OSError(errno, strerror(errno))
 
329
        free(cwd)
 
330
        if -1 == closedir(the_dir):
 
331
            raise OSError(errno, strerror(errno))
380
332
    return result
381
333
 
382
334