1
# A Linux-only demo, using verify() instead of hard-coding the exact layouts
6
if not sys.platform.startswith('linux'):
7
raise Exception("Linux-only demo")
16
unsigned char d_type; /* type of file; not supported
17
by all file system types */
18
char d_name[...]; /* filename */
22
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
23
int openat(int dirfd, const char *pathname, int flags);
24
DIR *fdopendir(int fd);
25
int closedir(DIR *dirp);
27
static const int DT_DIR;
30
ffi.C = ffi.verify("""
31
#ifndef _ATFILE_SOURCE
32
# define _ATFILE_SOURCE
38
#include <sys/types.h>
43
def walk(basefd, path):
45
dirfd = ffi.C.openat(basefd, path, 0)
49
dir = ffi.C.fdopendir(dirfd)
50
dirent = ffi.new("struct dirent *")
51
result = ffi.new("struct dirent **")
53
if ffi.C.readdir_r(dir, dirent, result):
54
# error in readdir_r()
56
if result[0] == ffi.NULL:
58
name = ffi.string(dirent.d_name)
59
print '%3d %s' % (dirent.d_type, name)
60
if dirent.d_type == ffi.C.DT_DIR and name != '.' and name != '..':