On 12/05/11 17:41, Jim Meyering wrote:
> I'd like to skip my new strace-vs-ls --color test on any
> system (OS and/or file system) that lacks dirent.d_type support.
> Ideally, it'd be a tiny bourne shell, python or perl script
> to tell me if calling readdir on a given directory (let's say ".")
> would provide struct dirent data including usable d_type values.
> [For reference, some file system types do not support that, e.g.,
> reiser3 and xfs, while others do, like ext4 and tmpfs. ]
>
> Unfortunately, so far it appears there is no way to get to
> the d_type member from any of those languages.
>
> Does anyone know a way that does not require compiling
> and running a C program?
>
> I though strace -v might help, but e.g., strace -v ls empty-dir shows
> only the getdents syscalls. No details about readdir-returned data.
Something based on this might work?
#!/usr/bin/python
# Return status indicates if d_type returned
import ctypes
import sys
(DT_UNKNOWN, DT_DIR,) = (0, 4,)
class dirent(ctypes.Structure):
_fields_ = [
("d_ino", ctypes.c_long),
("d_off", ctypes.c_long),
("d_reclen", ctypes.c_ushort),
("d_type", ctypes.c_ubyte),
("d_name", ctypes.c_char*256)]
direntp = ctypes.POINTER(dirent)
libc = ctypes.cdll.LoadLibrary("libc.so.6")
libc.readdir.restype = direntp
dirp = libc.opendir(".")
if dirp:
ep = libc.readdir(dirp)
else:
sys.exit(1)
sys.exit(ep.contents.d_type != DT_DIR)