xref: /freebsd/share/man/man5/dir.5 (revision a7ce3d543b5630adcff911eab6de3b6018ac3efa)
1afe61c15SRodney W. Grimes.\" Copyright (c) 1983, 1991, 1993
2afe61c15SRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
3afe61c15SRodney W. Grimes.\"
4afe61c15SRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
5afe61c15SRodney W. Grimes.\" modification, are permitted provided that the following conditions
6afe61c15SRodney W. Grimes.\" are met:
7afe61c15SRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
8afe61c15SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
9afe61c15SRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
10afe61c15SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
11afe61c15SRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
12dda5b397SEitan Adler.\" 3. Neither the name of the University nor the names of its contributors
13afe61c15SRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
14afe61c15SRodney W. Grimes.\"    without specific prior written permission.
15afe61c15SRodney W. Grimes.\"
16afe61c15SRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17afe61c15SRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18afe61c15SRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19afe61c15SRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20afe61c15SRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21afe61c15SRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22afe61c15SRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23afe61c15SRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24afe61c15SRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25afe61c15SRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26afe61c15SRodney W. Grimes.\" SUCH DAMAGE.
27afe61c15SRodney W. Grimes.\"
28afe61c15SRodney W. Grimes.\"     @(#)dir.5	8.3 (Berkeley) 4/19/94
297f3dea24SPeter Wemm.\" $FreeBSD$
30afe61c15SRodney W. Grimes.\"
31*a7ce3d54SBryan Drewery.Dd June 20, 2018
32afe61c15SRodney W. Grimes.Dt DIR 5
333d45e180SRuslan Ermilov.Os
34afe61c15SRodney W. Grimes.Sh NAME
35afe61c15SRodney W. Grimes.Nm dir ,
36afe61c15SRodney W. Grimes.Nm dirent
37afe61c15SRodney W. Grimes.Nd directory file format
38afe61c15SRodney W. Grimes.Sh SYNOPSIS
3932eef9aeSRuslan Ermilov.In dirent.h
40afe61c15SRodney W. Grimes.Sh DESCRIPTION
41afe61c15SRodney W. GrimesDirectories provide a convenient hierarchical method of grouping
42afe61c15SRodney W. Grimesfiles while obscuring the underlying details of the storage medium.
43afe61c15SRodney W. GrimesA directory file is differentiated from a plain file
44afe61c15SRodney W. Grimesby a flag in its
45afe61c15SRodney W. Grimes.Xr inode 5
46afe61c15SRodney W. Grimesentry.
47afe61c15SRodney W. GrimesIt consists of records (directory entries) each of which contains
48afe61c15SRodney W. Grimesinformation about a file and a pointer to the file itself.
49afe61c15SRodney W. GrimesDirectory entries may contain other directories
504a8d0283SMike Pritchardas well as plain files; such nested directories are referred to as
51afe61c15SRodney W. Grimessubdirectories.
52afe61c15SRodney W. GrimesA hierarchy of directories and files is formed in this manner
53afe61c15SRodney W. Grimesand is called a file system (or referred to as a file system tree).
54afe61c15SRodney W. Grimes.\" An entry in this tree,
55afe61c15SRodney W. Grimes.\" nested or not nested,
56afe61c15SRodney W. Grimes.\" is a pathname.
57afe61c15SRodney W. Grimes.Pp
58afe61c15SRodney W. GrimesEach directory file contains two special directory entries; one is a pointer
59afe61c15SRodney W. Grimesto the directory itself
60afe61c15SRodney W. Grimescalled dot
613136363fSRuslan Ermilov.Ql .\&
62afe61c15SRodney W. Grimesand the other a pointer to its parent directory called dot-dot
63afe61c15SRodney W. Grimes.Ql \&.. .
64afe61c15SRodney W. GrimesDot and dot-dot
65afe61c15SRodney W. Grimesare valid pathnames, however,
66afe61c15SRodney W. Grimesthe system root directory
67afe61c15SRodney W. Grimes.Ql / ,
68afe61c15SRodney W. Grimeshas no parent and dot-dot points to itself like dot.
69afe61c15SRodney W. Grimes.Pp
70afe61c15SRodney W. GrimesFile system nodes are ordinary directory files on which has
71afe61c15SRodney W. Grimesbeen grafted a file system object, such as a physical disk or a
72afe61c15SRodney W. Grimespartitioned area of such a disk.
73afe61c15SRodney W. Grimes(See
74906c1e27SMike Pritchard.Xr mount 2
75afe61c15SRodney W. Grimesand
76afe61c15SRodney W. Grimes.Xr mount 8 . )
77afe61c15SRodney W. Grimes.Pp
78afe61c15SRodney W. GrimesThe directory entry format is defined in the file
79743d5d51SRuslan Ermilov.In sys/dirent.h
805825b074SBruce Evans(which should not be included directly by applications):
81afe61c15SRodney W. Grimes.Bd -literal
825825b074SBruce Evans#ifndef	_SYS_DIRENT_H_
835825b074SBruce Evans#define	_SYS_DIRENT_H_
84afe61c15SRodney W. Grimes
85b6c7fd53SBruce Evans#include <machine/ansi.h>
86b6c7fd53SBruce Evans
87afe61c15SRodney W. Grimes/*
885825b074SBruce Evans * The dirent structure defines the format of directory entries returned by
895825b074SBruce Evans * the getdirentries(2) system call.
905825b074SBruce Evans *
91afe61c15SRodney W. Grimes * A directory entry has a struct dirent at the front of it, containing its
92afe61c15SRodney W. Grimes * inode number, the length of the entry, and the length of the name
93*a7ce3d54SBryan Drewery * contained in the entry.  These are followed by the name padded to a 8
94afe61c15SRodney W. Grimes * byte boundary with null bytes.  All names are guaranteed null terminated.
95afe61c15SRodney W. Grimes * The maximum length of a name in a directory is MAXNAMLEN.
9669921123SKonstantin Belousov * Explicit pad is added between the last member of the header and
9769921123SKonstantin Belousov * d_name, to avoid having the ABI padding in the end of dirent on
9869921123SKonstantin Belousov * LP64 arches.  There is code depending on d_name being last.  Also,
9969921123SKonstantin Belousov * keeping this pad for ILP32 architectures simplifies compat32 layer.
100afe61c15SRodney W. Grimes */
101afe61c15SRodney W. Grimes
102afe61c15SRodney W. Grimesstruct dirent {
10369921123SKonstantin Belousov	ino_t      d_fileno;		/* file number of entry */
10469921123SKonstantin Belousov	off_t      d_off;		/* directory offset of entry */
105b6c7fd53SBruce Evans	__uint16_t d_reclen;		/* length of this record */
106b6c7fd53SBruce Evans	__uint8_t  d_type;		/* file type, see below */
107b6c7fd53SBruce Evans	__uint8_t  d_namlen;		/* length of string in d_name */
10869921123SKonstantin Belousov	__uint32_t d_pad0;
10969921123SKonstantin Belousov#if __BSD_VISIBLE
110afe61c15SRodney W. Grimes#define	MAXNAMLEN	255
1115825b074SBruce Evans	char	d_name[MAXNAMLEN + 1];	/* name must be no longer than this */
11269921123SKonstantin Belousov#else
11369921123SKonstantin Belousov	char	d_name[255 + 1];	/* name must be no longer than this */
114afe61c15SRodney W. Grimes#endif
115afe61c15SRodney W. Grimes};
116afe61c15SRodney W. Grimes
1175f9a0f67SMike Pritchard/*
1185f9a0f67SMike Pritchard * File types
1195f9a0f67SMike Pritchard */
1205f9a0f67SMike Pritchard#define	DT_UNKNOWN	 0
1215f9a0f67SMike Pritchard#define	DT_FIFO		 1
1225f9a0f67SMike Pritchard#define	DT_CHR		 2
1235f9a0f67SMike Pritchard#define	DT_DIR		 4
1245f9a0f67SMike Pritchard#define	DT_BLK		 6
1255f9a0f67SMike Pritchard#define	DT_REG		 8
1265825b074SBruce Evans#define	DT_LNK		10
1275f9a0f67SMike Pritchard#define	DT_SOCK		12
1285f9a0f67SMike Pritchard#define	DT_WHT		14
1295f9a0f67SMike Pritchard
1305825b074SBruce Evans/*
1315825b074SBruce Evans * Convert between stat structure types and directory types.
1325825b074SBruce Evans */
1335825b074SBruce Evans#define	IFTODT(mode)	(((mode) & 0170000) >> 12)
1345825b074SBruce Evans#define	DTTOIF(dirtype)	((dirtype) << 12)
135afe61c15SRodney W. Grimes
1365825b074SBruce Evans/*
1375825b074SBruce Evans * The _GENERIC_DIRSIZ macro gives the minimum record length which will hold
13869921123SKonstantin Belousov * the directory entry.  This returns the amount of space in struct direct
1395825b074SBruce Evans * without the d_name field, plus enough space for the name with a terminating
14069921123SKonstantin Belousov * null byte (dp->d_namlen+1), rounded up to a 8 byte boundary.
14169921123SKonstantin Belousov *
14269921123SKonstantin Belousov * XXX although this macro is in the implementation namespace, it requires
14369921123SKonstantin Belousov * a manifest constant that is not.
1445825b074SBruce Evans */
14569921123SKonstantin Belousov#define	_GENERIC_DIRLEN(namlen)					\
14669921123SKonstantin Belousov	((__offsetof(struct dirent, d_name) + (namlen) + 1 + 7) & ~7)
14769921123SKonstantin Belousov#define	_GENERIC_DIRSIZ(dp)	_GENERIC_DIRLEN((dp)->d_namlen)
14869921123SKonstantin Belousov#endif /* __BSD_VISIBLE */
149afe61c15SRodney W. Grimes
150b6c7fd53SBruce Evans#ifdef _KERNEL
1515825b074SBruce Evans#define	GENERIC_DIRSIZ(dp)	_GENERIC_DIRSIZ(dp)
152afe61c15SRodney W. Grimes#endif
153afe61c15SRodney W. Grimes
1545825b074SBruce Evans#endif /* !_SYS_DIRENT_H_ */
155afe61c15SRodney W. Grimes.Ed
156afe61c15SRodney W. Grimes.Sh SEE ALSO
157e0e5145cSWolfram Schneider.Xr fs 5 ,
158afe61c15SRodney W. Grimes.Xr inode 5
159afe61c15SRodney W. Grimes.Sh HISTORY
160afe61c15SRodney W. GrimesA
161afe61c15SRodney W. Grimes.Nm
162afe61c15SRodney W. Grimesfile format appeared in
163afe61c15SRodney W. Grimes.At v7 .
1649cbda590SRuslan Ermilov.Sh BUGS
1659cbda590SRuslan ErmilovThe usage of the member d_type of struct dirent is unportable as it is
1669cbda590SRuslan Ermilov.Fx Ns -specific .
1679cbda590SRuslan ErmilovIt also may fail on certain file systems, for example the cd9660 file system.
168