1
# Bazaar-NG -- distributed version control
3
# Copyright (C) 2006 by Canonical Ltd
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
"""Wrapper for readdir which grabs file type from d_type."""
26
# the opaque C library DIR type.
27
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.
50
# should be DIR *, pyrex barfs.
51
DIR * opendir(char * name) except NULL
52
int closedir(DIR * dir) except -1
53
dirent *readdir(DIR *dir)
55
_directory = 'directory'
66
# add a typedef struct dirent dirent to workaround pyrex
67
cdef extern from 'readdir.h':
71
"""Like os.listdir, this reads a directories contents.
73
:param path: the directory to list.
74
:return: a list of (basename, kind) tuples.
77
# currently this needs a fixup - the C code says 'dirent' but should say
81
the_dir = opendir(path)
84
entry = readdir(the_dir)
87
if not (name[0] == dot and (
89
(name[1] == dot and name [2] == 0))
91
if entry.d_type == DT_UNKNOWN:
93
elif entry.d_type == DT_REG:
95
elif entry.d_type == DT_DIR:
97
elif entry.d_type == DT_FIFO:
99
elif entry.d_type == DT_SOCK:
101
elif entry.d_type == DT_CHR:
103
elif entry.d_type == DT_BLK:
107
result.append((entry.d_name, type))
108
entry = readdir(the_dir)
109
if entry == NULL and errno != ENOENT:
110
raise OSError(errno, strerror(errno))