Author: Armin Rigo <[email protected]>
Branch: cffi-1.0
Changeset: r1749:d72e8ca1cb4e
Date: 2015-04-18 12:12 +0200
http://bitbucket.org/cffi/cffi/changeset/d72e8ca1cb4e/
Log: Add the readdir2 demo
diff --git a/new/readdir2.py b/new/readdir2.py
new file mode 100644
--- /dev/null
+++ b/new/readdir2.py
@@ -0,0 +1,33 @@
+# A Linux-only demo, using verify() instead of hard-coding the exact layouts
+#
+import sys
+from _readdir2 import ffi, lib
+
+if not sys.platform.startswith('linux'):
+ raise Exception("Linux-only demo")
+
+
+def walk(basefd, path):
+ print '{', path
+ dirfd = lib.openat(basefd, path, 0)
+ if dirfd < 0:
+ # error in openat()
+ return
+ dir = lib.fdopendir(dirfd)
+ dirent = ffi.new("struct dirent *")
+ result = ffi.new("struct dirent **")
+ while True:
+ if lib.readdir_r(dir, dirent, result):
+ # error in readdir_r()
+ break
+ if result[0] == ffi.NULL:
+ break
+ name = ffi.string(dirent.d_name)
+ print '%3d %s' % (dirent.d_type, name)
+ if dirent.d_type == lib.DT_DIR and name != '.' and name != '..':
+ walk(dirfd, name)
+ lib.closedir(dir)
+ print '}'
+
+
+walk(-1, "/tmp")
diff --git a/new/readdir2_build.py b/new/readdir2_build.py
new file mode 100644
--- /dev/null
+++ b/new/readdir2_build.py
@@ -0,0 +1,34 @@
+from cffi1 import FFI
+from recompiler import recompile
+
+ffi = FFI()
+ffi.cdef("""
+
+ typedef ... DIR;
+
+ struct dirent {
+ unsigned char d_type; /* type of file; not supported
+ by all file system types */
+ char d_name[...]; /* filename */
+ ...;
+ };
+
+ int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
+ int openat(int dirfd, const char *pathname, int flags);
+ DIR *fdopendir(int fd);
+ int closedir(DIR *dirp);
+
+ static const int DT_DIR;
+
+""")
+recompile(ffi, "_readdir2", """
+#ifndef _ATFILE_SOURCE
+# define _ATFILE_SOURCE
+#endif
+#ifndef _BSD_SOURCE
+# define _BSD_SOURCE
+#endif
+#include <fcntl.h>
+#include <sys/types.h>
+#include <dirent.h>
+""")
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit