The directory entry format is defined in the file
<sys/dirent.h>, which is also included by
<dirent.h>. The format is represented by the
dirent structure, which contains the following entries:
ino_t d_fileno;
uint16_t d_reclen;
uint16_t d_namlen;
uint8_t d_type;
char d_name[MAXNAMLEN + 1];
These are:
1.
The
d_fileno entry is a number which is unique for each distinct file in the filesystem. Files that are linked by hard links (see
link(2)) have the same
d_fileno. If
d_fileno is zero, the entry refers to a deleted file. The type
ino_t is defined in
<sys/types.h>.
2.
The d_reclen entry is the length, in bytes, of the directory record.
3.
The d_namlen entry specifies the length of the file name excluding the NUL. Thus the actual size of d_name may vary from 1 to MAXNAMLEN + 1.
4.
The d_type is the type of the file.
5.
The d_name entry contains a NUL-terminated file name.
The following table lists the types available for
d_type and the corresponding ones used in the
struct stat (see
stat(2)), respectively:
DT_UNKNOWN
-
unknown file type
DT_FIFO
S_IFIFO
named pipe
DT_CHR
S_IFCHR
character device
DT_BLK
S_IFBLK
block device
DT_REG
S_IFREG
regular file
DT_LNK
S_IFLNK
symbolic link
DT_SOCK
S_IFSOCK
UNIX domain socket
DT_WHT
S_IFWHT
dummy “whiteout inode”
The
DT_WHT type is internal to the implementation and should not be seen in normal user applications. The macros
DTTOIF() and
IFTODT() can be used to convert from
struct dirent types to
struct stat types, and vice versa.