[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: how to check for dirent.d_type support from shell, python or perl?
From: |
Pádraig Brady |
Subject: |
Re: how to check for dirent.d_type support from shell, python or perl? |
Date: |
Fri, 13 May 2011 01:20:08 +0100 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.8) Gecko/20100227 Thunderbird/3.0.3 |
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)