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