xref: /freebsd/share/man/man5/dir.5 (revision 97759ccc715c4b365432c16d763c50eecfcb1100)
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.\"
28*1c4ca778SKonstantin Belousov.Dd November 14, 2018
29afe61c15SRodney W. Grimes.Dt DIR 5
303d45e180SRuslan Ermilov.Os
31afe61c15SRodney W. Grimes.Sh NAME
32afe61c15SRodney W. Grimes.Nm dir ,
33afe61c15SRodney W. Grimes.Nm dirent
34afe61c15SRodney W. Grimes.Nd directory file format
35afe61c15SRodney W. Grimes.Sh SYNOPSIS
3632eef9aeSRuslan Ermilov.In dirent.h
37afe61c15SRodney W. Grimes.Sh DESCRIPTION
38afe61c15SRodney W. GrimesDirectories provide a convenient hierarchical method of grouping
39afe61c15SRodney W. Grimesfiles while obscuring the underlying details of the storage medium.
40afe61c15SRodney W. GrimesA directory file is differentiated from a plain file
41afe61c15SRodney W. Grimesby a flag in its
42afe61c15SRodney W. Grimes.Xr inode 5
43afe61c15SRodney W. Grimesentry.
44afe61c15SRodney W. GrimesIt consists of records (directory entries) each of which contains
45afe61c15SRodney W. Grimesinformation about a file and a pointer to the file itself.
46afe61c15SRodney W. GrimesDirectory entries may contain other directories
474a8d0283SMike Pritchardas well as plain files; such nested directories are referred to as
48afe61c15SRodney W. Grimessubdirectories.
49afe61c15SRodney W. GrimesA hierarchy of directories and files is formed in this manner
50afe61c15SRodney W. Grimesand is called a file system (or referred to as a file system tree).
51afe61c15SRodney W. Grimes.\" An entry in this tree,
52afe61c15SRodney W. Grimes.\" nested or not nested,
53afe61c15SRodney W. Grimes.\" is a pathname.
54afe61c15SRodney W. Grimes.Pp
55afe61c15SRodney W. GrimesEach directory file contains two special directory entries; one is a pointer
56afe61c15SRodney W. Grimesto the directory itself
57afe61c15SRodney W. Grimescalled dot
583136363fSRuslan Ermilov.Ql .\&
59afe61c15SRodney W. Grimesand the other a pointer to its parent directory called dot-dot
60afe61c15SRodney W. Grimes.Ql \&.. .
61afe61c15SRodney W. GrimesDot and dot-dot
62afe61c15SRodney W. Grimesare valid pathnames, however,
63afe61c15SRodney W. Grimesthe system root directory
64afe61c15SRodney W. Grimes.Ql / ,
65afe61c15SRodney W. Grimeshas no parent and dot-dot points to itself like dot.
66afe61c15SRodney W. Grimes.Pp
67afe61c15SRodney W. GrimesFile system nodes are ordinary directory files on which has
68afe61c15SRodney W. Grimesbeen grafted a file system object, such as a physical disk or a
69afe61c15SRodney W. Grimespartitioned area of such a disk.
70afe61c15SRodney W. Grimes(See
71906c1e27SMike Pritchard.Xr mount 2
72afe61c15SRodney W. Grimesand
73afe61c15SRodney W. Grimes.Xr mount 8 . )
74afe61c15SRodney W. Grimes.Pp
75afe61c15SRodney W. GrimesThe directory entry format is defined in the file
76743d5d51SRuslan Ermilov.In sys/dirent.h
775825b074SBruce Evans(which should not be included directly by applications):
78afe61c15SRodney W. Grimes.Bd -literal
795825b074SBruce Evans#ifndef	_SYS_DIRENT_H_
805825b074SBruce Evans#define	_SYS_DIRENT_H_
81afe61c15SRodney W. Grimes
82b6c7fd53SBruce Evans#include <machine/ansi.h>
83b6c7fd53SBruce Evans
84afe61c15SRodney W. Grimes/*
855825b074SBruce Evans * The dirent structure defines the format of directory entries returned by
865825b074SBruce Evans * the getdirentries(2) system call.
875825b074SBruce Evans *
88afe61c15SRodney W. Grimes * A directory entry has a struct dirent at the front of it, containing its
89afe61c15SRodney W. Grimes * inode number, the length of the entry, and the length of the name
90a7ce3d54SBryan Drewery * contained in the entry.  These are followed by the name padded to a 8
91afe61c15SRodney W. Grimes * byte boundary with null bytes.  All names are guaranteed null terminated.
92afe61c15SRodney W. Grimes * The maximum length of a name in a directory is MAXNAMLEN.
9369921123SKonstantin Belousov * Explicit pad is added between the last member of the header and
9469921123SKonstantin Belousov * d_name, to avoid having the ABI padding in the end of dirent on
9569921123SKonstantin Belousov * LP64 arches.  There is code depending on d_name being last.  Also,
9669921123SKonstantin Belousov * keeping this pad for ILP32 architectures simplifies compat32 layer.
97afe61c15SRodney W. Grimes */
98afe61c15SRodney W. Grimes
99afe61c15SRodney W. Grimesstruct dirent {
10069921123SKonstantin Belousov	ino_t      d_fileno;		/* file number of entry */
101*1c4ca778SKonstantin Belousov	off_t      d_off;		/* directory offset of the next entry */
102b6c7fd53SBruce Evans	__uint16_t d_reclen;		/* length of this record */
103b6c7fd53SBruce Evans	__uint8_t  d_type;		/* file type, see below */
104b6c7fd53SBruce Evans	__uint8_t  d_namlen;		/* length of string in d_name */
10569921123SKonstantin Belousov	__uint32_t d_pad0;
10669921123SKonstantin Belousov#if __BSD_VISIBLE
107afe61c15SRodney W. Grimes#define	MAXNAMLEN	255
1085825b074SBruce Evans	char	d_name[MAXNAMLEN + 1];	/* name must be no longer than this */
10969921123SKonstantin Belousov#else
11069921123SKonstantin Belousov	char	d_name[255 + 1];	/* name must be no longer than this */
111afe61c15SRodney W. Grimes#endif
112afe61c15SRodney W. Grimes};
113afe61c15SRodney W. Grimes
1145f9a0f67SMike Pritchard/*
1155f9a0f67SMike Pritchard * File types
1165f9a0f67SMike Pritchard */
1175f9a0f67SMike Pritchard#define	DT_UNKNOWN	 0
1185f9a0f67SMike Pritchard#define	DT_FIFO		 1
1195f9a0f67SMike Pritchard#define	DT_CHR		 2
1205f9a0f67SMike Pritchard#define	DT_DIR		 4
1215f9a0f67SMike Pritchard#define	DT_BLK		 6
1225f9a0f67SMike Pritchard#define	DT_REG		 8
1235825b074SBruce Evans#define	DT_LNK		10
1245f9a0f67SMike Pritchard#define	DT_SOCK		12
1255f9a0f67SMike Pritchard#define	DT_WHT		14
1265f9a0f67SMike Pritchard
1275825b074SBruce Evans/*
1285825b074SBruce Evans * Convert between stat structure types and directory types.
1295825b074SBruce Evans */
1305825b074SBruce Evans#define	IFTODT(mode)	(((mode) & 0170000) >> 12)
1315825b074SBruce Evans#define	DTTOIF(dirtype)	((dirtype) << 12)
132afe61c15SRodney W. Grimes
1335825b074SBruce Evans/*
1345825b074SBruce Evans * The _GENERIC_DIRSIZ macro gives the minimum record length which will hold
13569921123SKonstantin Belousov * the directory entry.  This returns the amount of space in struct direct
1365825b074SBruce Evans * without the d_name field, plus enough space for the name with a terminating
13769921123SKonstantin Belousov * null byte (dp->d_namlen+1), rounded up to a 8 byte boundary.
13869921123SKonstantin Belousov *
13969921123SKonstantin Belousov * XXX although this macro is in the implementation namespace, it requires
14069921123SKonstantin Belousov * a manifest constant that is not.
1415825b074SBruce Evans */
14269921123SKonstantin Belousov#define	_GENERIC_DIRLEN(namlen)					\
14369921123SKonstantin Belousov	((__offsetof(struct dirent, d_name) + (namlen) + 1 + 7) & ~7)
14469921123SKonstantin Belousov#define	_GENERIC_DIRSIZ(dp)	_GENERIC_DIRLEN((dp)->d_namlen)
14569921123SKonstantin Belousov#endif /* __BSD_VISIBLE */
146afe61c15SRodney W. Grimes
147b6c7fd53SBruce Evans#ifdef _KERNEL
1485825b074SBruce Evans#define	GENERIC_DIRSIZ(dp)	_GENERIC_DIRSIZ(dp)
149afe61c15SRodney W. Grimes#endif
150afe61c15SRodney W. Grimes
1515825b074SBruce Evans#endif /* !_SYS_DIRENT_H_ */
152afe61c15SRodney W. Grimes.Ed
153afe61c15SRodney W. Grimes.Sh SEE ALSO
154e0e5145cSWolfram Schneider.Xr fs 5 ,
155afe61c15SRodney W. Grimes.Xr inode 5
156afe61c15SRodney W. Grimes.Sh HISTORY
157afe61c15SRodney W. GrimesA
158afe61c15SRodney W. Grimes.Nm
159afe61c15SRodney W. Grimesfile format appeared in
160afe61c15SRodney W. Grimes.At v7 .
1619cbda590SRuslan Ermilov.Sh BUGS
1629cbda590SRuslan ErmilovThe usage of the member d_type of struct dirent is unportable as it is
1639cbda590SRuslan Ermilov.Fx Ns -specific .
1649cbda590SRuslan ErmilovIt also may fail on certain file systems, for example the cd9660 file system.
165