DIR(5)
π« DIR(5) β the dir, dirent glow-up π«
okay so bestie, you've heard of dir and dirent? That's the whole directory file format situation, and we're gonna get into it. β¨
πΈ SYNOPSIS πΈ
#include <dirent.h>
π DESCRIPTION π
So like, directories are literally the moment when it comes to grouping your files in a cute lil hierarchy, while also hiding all the messy storage-medium details underneath. Iconic behavior honestly. π
A directory file is differentiated from a plain file by a flag in its inode(5) entry. It's made up of records β those are the directory entries β and each one is carrying info about a file plus a pointer to the file itself. And here's the tea: directory entries can contain other directories too, not just plain files. Those nested girlies are called subdirectories. When you stack them all up like that you get a hierarchy of directories and files, and that whole vibe is called a file system (or a file system tree if you're feeling fancy). π³
Every single directory file contains two special directory entries, and they're lowkey the main characters:
- one is a pointer to the directory itself, called dot
β.β - the other is a pointer to its parent directory, called dot-dot
β..β
Dot and dot-dot are valid pathnames fr fr. But heads up β the system root directory β/β has no parent, so its dot-dot just points to itself like dot does. No cap. π
File system nodes are ordinary directory files that have had a file system object grafted onto them β think a physical disk or a partitioned area of one of those disks. (See mount(2) and mount(8) if you wanna go deeper.)
π¦ the directory entry format π¦
okay so this part is defined in the file <sys/dirent.h>, and bestie listen β applications should not be including that directly, mmkay? π
#ifndef _SYS_DIRENT_H_
#define _SYS_DIRENT_H_
#include <machine/ansi.h>
/*
* The dirent structure defines the format of directory entries returned by
* the getdirentries(2) system call.
*
* A directory entry has a struct dirent at the front of it, containing its
* inode number, the length of the entry, and the length of the name
* contained in the entry. These are followed by the name padded to a 8
* byte boundary with null bytes. All names are guaranteed null terminated.
* The maximum length of a name in a directory is MAXNAMLEN.
* Explicit pad is added between the last member of the header and
* d_name, to avoid having the ABI padding in the end of dirent on
* LP64 arches. There is code depending on d_name being last. Also,
* keeping this pad for ILP32 architectures simplifies compat32 layer.
*/
struct dirent {
ino_t d_fileno; /* file number of entry */
off_t d_off; /* directory offset of the next entry */
__uint16_t d_reclen; /* length of this record */
__uint8_t d_type; /* file type, see below */
__uint8_t d_namlen; /* length of string in d_name */
__uint32_t d_pad0;
#if __BSD_VISIBLE
#define MAXNAMLEN 255
char d_name[MAXNAMLEN + 1]; /* name must be no longer than this */
#else
char d_name[255 + 1]; /* name must be no longer than this */
#endif
};
/*
* File types
*/
#define DT_UNKNOWN 0
#define DT_FIFO 1
#define DT_CHR 2
#define DT_DIR 4
#define DT_BLK 6
#define DT_REG 8
#define DT_LNK 10
#define DT_SOCK 12
#define DT_WHT 14
/*
* Convert between stat structure types and directory types.
*/
#define IFTODT(mode) (((mode) & 0170000) >> 12)
#define DTTOIF(dirtype) ((dirtype) << 12)
/*
* The _GENERIC_DIRSIZ macro gives the minimum record length which will hold
* the directory entry. This returns the amount of space in struct direct
* without the d_name field, plus enough space for the name with a terminating
* null byte (dp->d_namlen+1), rounded up to a 8 byte boundary.
*
* XXX although this macro is in the implementation namespace, it requires
* a manifest constant that is not.
*/
#define _GENERIC_DIRLEN(namlen) ((__offsetof(struct dirent, d_name) + (namlen) + 1 + 7) & ~7)
#define _GENERIC_DIRSIZ(dp) _GENERIC_DIRLEN((dp)->d_namlen)
#endif /* __BSD_VISIBLE */
#ifdef _KERNEL
#define GENERIC_DIRSIZ(dp) _GENERIC_DIRSIZ(dp)
#endif
#endif /* !_SYS_DIRENT_H_ */
π the file types, real quick
here's the whole squad of DT_* constants, they're giving type-check energy:
DT_UNKNOWN=0β who even knowsDT_FIFO=1β the pipeDT_CHR=2β character deviceDT_DIR=4β the directory iconDT_BLK=6β block deviceDT_REG=8β regular file, the classicDT_LNK=10β symlink slayDT_SOCK=12β socketDT_WHT=14β the whiteout
and these two macros are literally just converting between stat structure types and directory types: IFTODT(mode) and DTTOIF(dirtype), both right there in the code block above. π«Ά
π SEE ALSO π
π HISTORY π
so the dir file format appeared in Version 7 AT&T UNIX. vintage icon behavior, honestly. β
π BUGS π
okay so this is the part you gotta pay attention to bestie:
The usage of the memberd_typeofstruct direntis unportable as it is FreeBSD-specific. It also may fail on certain file systems, for example thecd9660file system. π
so like, don't be out here depending on d_type like it's guaranteed, because it'll lowkey betray you on some file systems. fr fr.
that's the whole page, bestie. this was Debian, November 14, 2018, DIR(5) β and honestly? directory file format has never looked this good. πβ¨π
π¦ where this page comes from
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (c) 1983, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms