doc: update the instructions for generating a coverage report
[coreutils.git] / tests / d_type-check
blob1a2f76f5110c1e7d3d7420c6611c36aa56072f12
1 #!/usr/bin/python
2 # Exit 0 if "." and "./tempfile" have useful d_type information, else 1.
3 # Intended to exit 0 only on Linux/GNU systems.
4 import os
5 import sys
6 import tempfile
8 fail = 1
9 fname = None
11 try:
12 import ctypes.util
14 (DT_UNKNOWN, DT_DIR, DT_REG) = (0, 4, 8)
16 class dirent(ctypes.Structure):
17 _fields_ = [
18 ("d_ino", ctypes.c_long),
19 ("d_off", ctypes.c_long),
20 ("d_reclen", ctypes.c_ushort),
21 ("d_type", ctypes.c_ubyte),
22 ("d_name", ctypes.c_char*256)]
24 # Pass NULL to dlopen, assuming the python
25 # interpreter is linked with the C runtime
26 libc = ctypes.CDLL(None)
28 # Setup correct types for all args and returns
29 # even if only passing, to avoid truncation etc.
30 dirp = ctypes.c_void_p
31 direntp = ctypes.POINTER(dirent)
33 libc.readdir.argtypes = [dirp]
34 libc.readdir.restype = direntp
36 libc.opendir.restype = dirp
38 # Ensure a file is present
39 f, fname = tempfile.mkstemp(dir='.')
40 fname = os.path.basename(fname)
42 dirp = libc.opendir(".")
43 if dirp:
44 while True:
45 ep = libc.readdir(dirp)
46 if not ep: break
47 d_type = ep.contents.d_type
48 name = ep.contents.d_name
49 if name == "." or name == "..":
50 if d_type != DT_DIR: break
51 # Check files too since on XFS, only dirs have DT_DIR
52 # while everything else has DT_UNKNOWN
53 elif name == fname:
54 if d_type == DT_REG:
55 fail = 0
56 break
57 elif d_type != DT_DIR and d_type != DT_UNKNOWN:
58 fail = 0
59 break
60 except:
61 pass
63 try:
64 if fname:
65 os.unlink(fname);
66 except:
67 pass
69 sys.exit(fail)