xref: /original-bsd/sys/ufs/ufs/dir.h (revision a76afa45)
1 /*
2  * Copyright (c) 1982, 1986, 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  *	@(#)dir.h	7.6 (Berkeley) 05/09/89
18  */
19 
20 #ifndef _DIR_
21 #define	_DIR_
22 
23 /*
24  * A directory consists of some number of blocks of DIRBLKSIZ
25  * bytes, where DIRBLKSIZ is chosen such that it can be transferred
26  * to disk in a single atomic operation (e.g. 512 bytes on most machines).
27  *
28  * Each DIRBLKSIZ byte block contains some number of directory entry
29  * structures, which are of variable length.  Each directory entry has
30  * a struct direct at the front of it, containing its inode number,
31  * the length of the entry, and the length of the name contained in
32  * the entry.  These are followed by the name padded to a 4 byte boundary
33  * with null bytes.  All names are guaranteed null terminated.
34  * The maximum length of a name in a directory is MAXNAMLEN.
35  *
36  * The macro DIRSIZ(dp) gives the amount of space required to represent
37  * a directory entry.  Free space in a directory is represented by
38  * entries which have dp->d_reclen > DIRSIZ(dp).  All DIRBLKSIZ bytes
39  * in a directory block are claimed by the directory entries.  This
40  * usually results in the last entry in a directory having a large
41  * dp->d_reclen.  When entries are deleted from a directory, the
42  * space is returned to the previous entry in the same directory
43  * block by increasing its dp->d_reclen.  If the first entry of
44  * a directory block is free, then its dp->d_ino is set to 0.
45  * Entries other than the first in a directory do not normally have
46  * dp->d_ino set to 0.
47  */
48 #define DIRBLKSIZ	DEV_BSIZE
49 #define	MAXNAMLEN	255
50 
51 struct	direct {
52 	u_long	d_ino;			/* inode number of entry */
53 	u_short	d_reclen;		/* length of this record */
54 	u_short	d_namlen;		/* length of string in d_name */
55 	char	d_name[MAXNAMLEN + 1];	/* name with length <= MAXNAMLEN */
56 };
57 
58 /*
59  * The DIRSIZ macro gives the minimum record length which will hold
60  * the directory entry.  This requires the amount of space in struct direct
61  * without the d_name field, plus enough space for the name with a terminating
62  * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
63  */
64 #undef DIRSIZ
65 #define DIRSIZ(dp) \
66     ((sizeof (struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
67 
68 #ifdef KERNEL
69 /*
70  * Template for manipulating directories.
71  * Should use struct direct's, but the name field
72  * is MAXNAMLEN - 1, and this just won't do.
73  */
74 struct dirtemplate {
75 	u_long	dot_ino;
76 	short	dot_reclen;
77 	short	dot_namlen;
78 	char	dot_name[4];		/* must be multiple of 4 */
79 	u_long	dotdot_ino;
80 	short	dotdot_reclen;
81 	short	dotdot_namlen;
82 	char	dotdot_name[4];		/* ditto */
83 };
84 #endif
85 
86 /*
87  * The following information should be obtained from <dirent.h>
88  * and is provided solely (and temporarily) for backward compatibility.
89  */
90 #ifndef KERNEL
91 #define d_fileno d_ino		/* compatibility with POSIX */
92 #ifndef DEV_BSIZE
93 #define	DEV_BSIZE	512
94 #endif
95 /*
96  * Definitions for library routines operating on directories.
97  */
98 typedef struct _dirdesc {
99 	int	dd_fd;
100 	long	dd_loc;
101 	long	dd_size;
102 	char	dd_buf[DIRBLKSIZ];
103 } DIR;
104 
105 #define dirfd(dirp)	((dirp)->dd_fd)
106 
107 #ifndef NULL
108 #define NULL 0
109 #endif
110 extern	DIR *opendir();
111 extern	struct direct *readdir();
112 extern	long telldir();
113 extern	void seekdir();
114 #define rewinddir(dirp)	seekdir((dirp), (long)0)
115 extern	void closedir();
116 #endif /* not KERNEL */
117 #endif /* _DIR_ */
118