• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# A Linux-only demo, using set_source() instead of hard-coding the exact layouts
2#
3import sys
4
5if not sys.platform.startswith('linux'):
6    raise Exception("Linux-only demo")
7
8# run readdir2_build first, then make sure the shared object is on sys.path
9from _readdir2_cffi import ffi, lib
10
11
12def walk(basefd, path):
13    print '{', path
14    dirfd = lib.openat(basefd, path, 0)
15    if dirfd < 0:
16        # error in openat()
17        return
18    dir = lib.fdopendir(dirfd)
19    dirent = ffi.new("struct dirent *")
20    result = ffi.new("struct dirent **")
21    while True:
22        if lib.readdir_r(dir, dirent, result):
23            # error in readdir_r()
24            break
25        if result[0] == ffi.NULL:
26            break
27        name = ffi.string(dirent.d_name)
28        print '%3d %s' % (dirent.d_type, name)
29        if dirent.d_type == lib.DT_DIR and name != '.' and name != '..':
30            walk(dirfd, name)
31    lib.closedir(dir)
32    print '}'
33
34
35walk(-1, "/tmp")
36