xref: /original-bsd/sys/ufs/ufs/dir.h (revision 4ba124f7)
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.3 (Berkeley) 07/08/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 
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.  Should use struct direct's,
92  * but the name field is MAXNAMLEN - 1, and this just won't do.
93  */
94 struct dirtemplate {
95 	u_int32_t	dot_ino;
96 	int16_t		dot_reclen;
97 	u_int8_t	dot_type;
98 	u_int8_t	dot_namlen;
99 	char		dot_name[4];	/* must be multiple of 4 */
100 	u_int32_t	dotdot_ino;
101 	int16_t		dotdot_reclen;
102 	u_int8_t	dotdot_type;
103 	u_int8_t	dotdot_namlen;
104 	char		dotdot_name[4];	/* ditto */
105 };
106 
107 /*
108  * This is the old format of directories, sanz type element.
109  */
110 struct odirtemplate {
111 	u_int32_t	dot_ino;
112 	int16_t		dot_reclen;
113 	u_int16_t	dot_namlen;
114 	char		dot_name[4];	/* must be multiple of 4 */
115 	u_int32_t	dotdot_ino;
116 	int16_t		dotdot_reclen;
117 	u_int16_t	dotdot_namlen;
118 	char		dotdot_name[4];	/* ditto */
119 };
120 #endif /* !_DIR_H_ */
121