DIR(5)

πŸ“¦
version 15.1 Β· 3 versions
, in freebsd 15.1, nixpkgs unstable

πŸ’« 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 knows
  • DT_FIFO = 1 β€” the pipe
  • DT_CHR = 2 β€” character device
  • DT_DIR = 4 β€” the directory icon
  • DT_BLK = 6 β€” block device
  • DT_REG = 8 β€” regular file, the classic
  • DT_LNK = 10 β€” symlink slay
  • DT_SOCK = 12 β€” socket
  • DT_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 member d_type of struct dirent is unportable as it is FreeBSD-specific. It also may fail on certain file systems, for example the cd9660 file 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