740027
From eafaa2e88f7af16756142a31ab63d032b31395e3 Mon Sep 17 00:00:00 2001
740027
From: Pádraig Brady <P@draigBrady.com>
740027
Date: Fri, 06 Nov 2015 16:31:22 +0000
740027
Subject: tests: fix dirent d_type support verification
740027
740027
* tests/d_type-check: Check also the d_type of files,
740027
which excludes XFS appropriately.  Specify all argument
740027
and return types to avoid truncated pointers being passed,
740027
which skipped the test due to crashes on x86_64 at least.
740027
Simplify the C library lookup by reusing the interpreter's.
740027
740027
chroot issue reported at https://bugzilla.redhat.com/1263341
740027
---
740027
diff --git a/tests/d_type-check b/tests/d_type-check
740027
index ff1eb60..1a2f76f 100644
740027
--- a/tests/d_type-check
740027
+++ b/tests/d_type-check
740027
@@ -1,13 +1,17 @@
740027
 #!/usr/bin/python
740027
-# Exit 0 if "." has useful d_type information, else 1.
740027
+# Exit 0 if "." and "./tempfile" have useful d_type information, else 1.
740027
 # Intended to exit 0 only on Linux/GNU systems.
740027
+import os
740027
 import sys
740027
+import tempfile
740027
 
740027
 fail = 1
740027
+fname = None
740027
+
740027
 try:
740027
   import ctypes
740027
 
740027
-  (DT_UNKNOWN, DT_DIR,) = (0, 4,)
740027
+  (DT_UNKNOWN, DT_DIR, DT_REG) = (0, 4, 8)
740027
 
740027
   class dirent(ctypes.Structure):
740027
     _fields_ = [
740027
@@ -17,20 +21,48 @@ try:
740027
       ("d_type", ctypes.c_ubyte),
740027
       ("d_name", ctypes.c_char*256)]
740027
 
740027
+  # Pass NULL to dlopen, assuming the python
740027
+  # interpreter is linked with the C runtime
740027
+  libc = ctypes.CDLL(None)
740027
+
740027
+  # Setup correct types for all args and returns
740027
+  # even if only passing, to avoid truncation etc.
740027
+  dirp = ctypes.c_void_p
740027
   direntp = ctypes.POINTER(dirent)
740027
 
740027
-  # FIXME: find a way to avoid hard-coding libc's so-name.
740027
-  libc = ctypes.cdll.LoadLibrary("libc.so.6")
740027
+  libc.readdir.argtypes = [dirp]
740027
   libc.readdir.restype = direntp
740027
 
740027
+  libc.opendir.restype = dirp
740027
+
740027
+  # Ensure a file is present
740027
+  f, fname = tempfile.mkstemp(dir='.')
740027
+  fname = os.path.basename(fname)
740027
+
740027
   dirp = libc.opendir(".")
740027
   if dirp:
740027
-    ep = libc.readdir(dirp)
740027
-    if ep:
740027
+    while True:
740027
+      ep = libc.readdir(dirp)
740027
+      if not ep: break
740027
+      d_type = ep.contents.d_type
740027
       name = ep.contents.d_name
740027
-      if (name == "." or name == "..") and ep.contents.d_type == DT_DIR:
740027
+      if name == "." or name == "..":
740027
+        if d_type != DT_DIR: break
740027
+      # Check files too since on XFS, only dirs have DT_DIR
740027
+      # while everything else has DT_UNKNOWN
740027
+      elif name == fname:
740027
+        if d_type == DT_REG:
740027
+          fail = 0
740027
+        break
740027
+      elif d_type != DT_DIR and d_type != DT_UNKNOWN:
740027
         fail = 0
740027
+        break
740027
+except:
740027
+  pass
740027
 
740027
+try:
740027
+  if fname:
740027
+    os.unlink(fname);
740027
 except:
740027
   pass
740027
 
740027
--
740027
cgit v0.9.0.2