xref: /original-bsd/sys/ufs/ufs/dir.h (revision 3705696b)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)dir.h	8.1 (Berkeley) 06/11/93
8  */
9 
10 #ifndef _DIR_H_
11 #define	_DIR_H_
12 
13 /*
14  * A directory consists of some number of blocks of DIRBLKSIZ
15  * bytes, where DIRBLKSIZ is chosen such that it can be transferred
16  * to disk in a single atomic operation (e.g. 512 bytes on most machines).
17  *
18  * Each DIRBLKSIZ byte block contains some number of directory entry
19  * structures, which are of variable length.  Each directory entry has
20  * a struct direct at the front of it, containing its inode number,
21  * the length of the entry, and the length of the name contained in
22  * the entry.  These are followed by the name padded to a 4 byte boundary
23  * with null bytes.  All names are guaranteed null terminated.
24  * The maximum length of a name in a directory is MAXNAMLEN.
25  *
26  * The macro DIRSIZ(fmt, dp) gives the amount of space required to represent
27  * a directory entry.  Free space in a directory is represented by
28  * entries which have dp->d_reclen > DIRSIZ(fmt, dp).  All DIRBLKSIZ bytes
29  * in a directory block are claimed by the directory entries.  This
30  * usually results in the last entry in a directory having a large
31  * dp->d_reclen.  When entries are deleted from a directory, the
32  * space is returned to the previous entry in the same directory
33  * block by increasing its dp->d_reclen.  If the first entry of
34  * a directory block is free, then its dp->d_ino is set to 0.
35  * Entries other than the first in a directory do not normally have
36  * dp->d_ino set to 0.
37  */
38 #define DIRBLKSIZ	DEV_BSIZE
39 #define	MAXNAMLEN	255
40 
41 struct	direct {
42 	u_long	d_ino;			/* inode number of entry */
43 	u_short	d_reclen;		/* length of this record */
44 	u_char	d_type; 		/* file type, see below */
45 	u_char	d_namlen;		/* length of string in d_name */
46 	char	d_name[MAXNAMLEN + 1];	/* name with length <= MAXNAMLEN */
47 };
48 
49 /*
50  * File types
51  */
52 #define	DT_UNKNOWN	 0
53 #define	DT_FIFO		 1
54 #define	DT_CHR		 2
55 #define	DT_DIR		 4
56 #define	DT_BLK		 6
57 #define	DT_REG		 8
58 #define	DT_LNK		10
59 #define	DT_SOCK		12
60 
61 /*
62  * Convert between stat structure types and directory types.
63  */
64 #define	IFTODT(mode)	(((mode) & 0170000) >> 12)
65 #define	DTTOIF(dirtype)	((dirtype) << 12)
66 
67 /*
68  * The DIRSIZ macro gives the minimum record length which will hold
69  * the directory entry.  This requires the amount of space in struct direct
70  * without the d_name field, plus enough space for the name with a terminating
71  * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
72  */
73 #if (BYTE_ORDER == LITTLE_ENDIAN)
74 #define DIRSIZ(oldfmt, dp) \
75     ((oldfmt) ? \
76     ((sizeof (struct direct) - (MAXNAMLEN+1)) + (((dp)->d_type+1 + 3) &~ 3)) : \
77     ((sizeof (struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3)))
78 #else
79 #define DIRSIZ(oldfmt, dp) \
80     ((sizeof (struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
81 #endif
82 #define OLDDIRFMT	1
83 #define NEWDIRFMT	0
84 
85 /*
86  * Template for manipulating directories.
87  * Should use struct direct's, but the name field
88  * is MAXNAMLEN - 1, and this just won't do.
89  */
90 struct dirtemplate {
91 	u_long	dot_ino;
92 	short	dot_reclen;
93 	u_char	dot_type;
94 	u_char	dot_namlen;
95 	char	dot_name[4];		/* must be multiple of 4 */
96 	u_long	dotdot_ino;
97 	short	dotdot_reclen;
98 	u_char	dotdot_type;
99 	u_char	dotdot_namlen;
100 	char	dotdot_name[4];		/* ditto */
101 };
102 
103 /*
104  * This is the old format of directories, sanz type element.
105  */
106 struct odirtemplate {
107 	u_long	dot_ino;
108 	short	dot_reclen;
109 	u_short	dot_namlen;
110 	char	dot_name[4];		/* must be multiple of 4 */
111 	u_long	dotdot_ino;
112 	short	dotdot_reclen;
113 	u_short	dotdot_namlen;
114 	char	dotdot_name[4];		/* ditto */
115 };
116 #endif /* !_DIR_H_ */
117