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
# the opaque C library DIR type.
25
cdef extern from 'errno.h':
30
char *strerror(int errno)
32
cdef extern from 'sys/types.h':
34
ctypedef unsigned long size_t
36
cdef extern from 'dirent.h':
44
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.
51
# should be DIR *, pyrex barfs.
52
DIR * opendir(char * name)
53
int closedir(DIR * dir)
54
dirent *readdir(DIR *dir)
56
_directory = 'directory'
67
# add a typedef struct dirent dirent to workaround pyrex
68
cdef extern from 'readdir.h':
72
"""Like os.listdir, this reads the contents of a directory.
74
:param path: the directory to list.
75
:return: a list of (sort_key, basename) tuples.
78
# currently this needs a fixup - the C code says 'dirent' but should say
83
the_dir = opendir(path)
85
raise OSError(errno, strerror(errno))
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))
112
if -1 == closedir(the_dir):
113
raise OSError(errno, strerror(errno))
117
# vim: tw=79 ai expandtab sw=4 sts=4