xref: /original-bsd/sys/ufs/ufs/dir.h (revision deff14a8)
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.4 (Berkeley) 08/10/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_int32_t d_ino;		/* inode number of entry */
48 	u_int16_t d_reclen;		/* length of this record */
49 	u_int8_t  d_type; 		/* file type, see below */
50 	u_int8_t  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 #define	DT_WHT		14
66 
67 /*
68  * Convert between stat structure types and directory types.
69  */
70 #define	IFTODT(mode)	(((mode) & 0170000) >> 12)
71 #define	DTTOIF(dirtype)	((dirtype) << 12)
72 
73 /*
74  * The DIRSIZ macro gives the minimum record length which will hold
75  * the directory entry.  This requires the amount of space in struct direct
76  * without the d_name field, plus enough space for the name with a terminating
77  * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
78  */
79 #if (BYTE_ORDER == LITTLE_ENDIAN)
80 #define DIRSIZ(oldfmt, dp) \
81     ((oldfmt) ? \
82     ((sizeof(struct direct) - (MAXNAMLEN+1)) + (((dp)->d_type+1 + 3) &~ 3)) : \
83     ((sizeof(struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3)))
84 #else
85 #define DIRSIZ(oldfmt, dp) \
86     ((sizeof(struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
87 #endif
88 #define OLDDIRFMT	1
89 #define NEWDIRFMT	0
90 
91 /*
92  * Template for manipulating directories.  Should use struct direct's,
93  * but the name field is MAXNAMLEN - 1, and this just won't do.
94  */
95 struct dirtemplate {
96 	u_int32_t	dot_ino;
97 	int16_t		dot_reclen;
98 	u_int8_t	dot_type;
99 	u_int8_t	dot_namlen;
100 	char		dot_name[4];	/* must be multiple of 4 */
101 	u_int32_t	dotdot_ino;
102 	int16_t		dotdot_reclen;
103 	u_int8_t	dotdot_type;
104 	u_int8_t	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_int32_t	dot_ino;
113 	int16_t		dot_reclen;
114 	u_int16_t	dot_namlen;
115 	char		dot_name[4];	/* must be multiple of 4 */
116 	u_int32_t	dotdot_ino;
117 	int16_t		dotdot_reclen;
118 	u_int16_t	dotdot_namlen;
119 	char		dotdot_name[4];	/* ditto */
120 };
121 #endif /* !_DIR_H_ */
122