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 grabs file type from d_type."""
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.
50
# should be DIR *, pyrex barfs.
51
DIR * opendir(char * name)
52
int closedir(DIR * dir)
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
82
the_dir = opendir(path)
84
raise OSError(errno, strerror(errno))
89
entry = readdir(the_dir)
94
elif errno != ENOTDIR and errno != ENOENT and errno != 0:
95
# We see ENOTDIR at the end of a normal directory.
96
# As ENOTDIR for read_dir(file) is triggered on opendir,
97
# we consider ENOTDIR to be 'no error'.
98
# ENOENT is listed as 'invalid position in the dir stream' for
99
# readdir. We swallow this for now and just keep reading.
100
raise OSError(errno, strerror(errno))
105
if not (name[0] == dot and (
107
(name[1] == dot and name [2] == 0))
109
if entry.d_type == DT_UNKNOWN:
111
elif entry.d_type == DT_REG:
113
elif entry.d_type == DT_DIR:
115
elif entry.d_type == DT_FIFO:
117
elif entry.d_type == DT_SOCK:
119
elif entry.d_type == DT_CHR:
121
elif entry.d_type == DT_BLK:
125
# result.append((entry.d_name, type))
126
result.append((entry.d_name, 'unknown'))
128
if -1 == closedir(the_dir):
129
raise OSError(errno, strerror(errno))
133
# vim: tw=79 ai expandtab sw=4 sts=4