Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_dir.h revision 1.7
      1 /*	$NetBSD: ext2fs_dir.h,v 1.7 2003/10/05 17:48:49 bouyer Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)dir.h	8.4 (Berkeley) 8/10/94
     37  * Modified for ext2fs by Manuel Bouyer.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1997 Manuel Bouyer.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by Manuel Bouyer.
     54  * 4. The name of the author may not be used to endorse or promote products
     55  *    derived from this software without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     67  * SUCH DAMAGE.
     68  *
     69  *	@(#)dir.h	8.4 (Berkeley) 8/10/94
     70  * Modified for ext2fs by Manuel Bouyer.
     71  */
     72 
     73 #ifndef _UFS_EXT2FS_EXT2FS_DIR_H_
     74 #define	_UFS_EXT2FS_EXT2FS_DIR_H_
     75 
     76 /*
     77  * Theoretically, directories can be more than 2Gb in length, however, in
     78  * practice this seems unlikely. So, we define the type doff_t as a 32-bit
     79  * quantity to keep down the cost of doing lookup on a 32-bit machine.
     80  */
     81 #define	doff_t		int32_t
     82 #define	EXT2FS_MAXDIRSIZE	(0x7fffffff)
     83 
     84 /*
     85  * A directory consists of some number of blocks of e2fs_bsize bytes.
     86  *
     87  * Each block contains some number of directory entry
     88  * structures, which are of variable length.  Each directory entry has
     89  * a struct direct at the front of it, containing its inode number,
     90  * the length of the entry, and the length of the name contained in
     91  * the entry.  These are followed by the name padded to a 4 byte boundary
     92  * with null bytes.  All names are guaranteed null terminated.
     93  * The maximum length of a name in a directory is EXT2FS_MAXNAMLEN.
     94  *
     95  * The macro EXT2FS_DIRSIZ(fmt, dp) gives the amount of space required to
     96  * represent a directory entry.  Free space in a directory is represented by
     97  * entries which have dp->e2d_reclen > DIRSIZ(fmt, dp).  All d2fs_bsize bytes
     98  * in a directory block are claimed by the directory entries.  This
     99  * usually results in the last entry in a directory having a large
    100  * dp->e2d_reclen.  When entries are deleted from a directory, the
    101  * space is returned to the previous entry in the same directory
    102  * block by increasing its dp->e2d_reclen.  If the first entry of
    103  * a directory block is free, then its dp->e2d_ino is set to 0.
    104  * Entries other than the first in a directory do not normally have
    105  * dp->e2d_ino set to 0.
    106  * Ext2 rev 0 has a 16 bits e2d_namlen. For Ext2 vev 1 this has been split
    107  * into a 8 bits e2d_namlen and 8 bits e2d_type (looks like ffs, isnt't it ? :)
    108  * It's safe to use this for rev 0 as well because all ext2 are little-endian.
    109  */
    110 
    111 #define	EXT2FS_MAXNAMLEN	255
    112 
    113 struct	ext2fs_direct {
    114 	u_int32_t e2d_ino;		/* inode number of entry */
    115 	u_int16_t e2d_reclen;		/* length of this record */
    116 	u_int8_t e2d_namlen;		/* length of string in d_name */
    117 	u_int8_t e2d_type;		/* file type */
    118 	char	  e2d_name[EXT2FS_MAXNAMLEN];/* name with length <= EXT2FS_MAXNAMLEN */
    119 };
    120 
    121 /* Ext2 directory file types (not the same as FFS. Sigh. */
    122 #define EXT2_FT_UNKNOWN         0
    123 #define EXT2_FT_REG_FILE        1
    124 #define EXT2_FT_DIR             2
    125 #define EXT2_FT_CHRDEV          3
    126 #define EXT2_FT_BLKDEV          4
    127 #define EXT2_FT_FIFO            5
    128 #define EXT2_FT_SOCK            6
    129 #define EXT2_FT_SYMLINK         7
    130 
    131 #define EXT2_FT_MAX             8
    132 
    133 #define E2IFTODT(mode)    (((mode) & 0170000) >> 12)
    134 
    135 static __inline__ u_int8_t inot2ext2dt __P((u_int16_t))
    136     __attribute__((__unused__));
    137 static __inline__ u_int8_t
    138 inot2ext2dt(type)
    139 	u_int16_t type;
    140 {
    141 	switch(type) {
    142 	case E2IFTODT(EXT2_IFIFO):
    143 		return EXT2_FT_FIFO;
    144 	case E2IFTODT(EXT2_IFCHR):
    145 		return EXT2_FT_CHRDEV;
    146 	case E2IFTODT(EXT2_IFDIR):
    147 		return EXT2_FT_DIR;
    148 	case E2IFTODT(EXT2_IFBLK):
    149 		return EXT2_FT_BLKDEV;
    150 	case E2IFTODT(EXT2_IFREG):
    151 		return EXT2_FT_REG_FILE;
    152 	case E2IFTODT(EXT2_IFLNK):
    153 		return EXT2_FT_SYMLINK;
    154 	case E2IFTODT(EXT2_IFSOCK):
    155 		return EXT2_FT_SOCK;
    156 	default:
    157 		return 0;
    158 	}
    159 }
    160 
    161 /*
    162  * The EXT2FS_DIRSIZ macro gives the minimum record length which will hold
    163  * the directory entryfor a name len "len" (without the terminating null byte).
    164  * This requires the amount of space in struct direct
    165  * without the d_name field, plus enough space for the name without a
    166  * terminating null byte, rounded up to a 4 byte boundary.
    167  */
    168 #define EXT2FS_DIRSIZ(len) \
    169    (( 8 + len + 3) &~ 3)
    170 
    171 /*
    172  * Template for manipulating directories.  Should use struct direct's,
    173  * but the name field is EXT2FS_MAXNAMLEN - 1, and this just won't do.
    174  */
    175 struct ext2fs_dirtemplate {
    176 	u_int32_t	dot_ino;
    177 	int16_t		dot_reclen;
    178 	u_int8_t	dot_namlen;
    179 	u_int8_t	dot_type;
    180 	char		dot_name[4];	/* must be multiple of 4 */
    181 	u_int32_t	dotdot_ino;
    182 	int16_t		dotdot_reclen;
    183 	u_int8_t	dotdot_namlen;
    184 	u_int8_t	dotdot_type;
    185 	char		dotdot_name[4];	/* ditto */
    186 };
    187 
    188 #endif /* !_UFS_EXT2FS_EXT2FS_DIR_H_ */
    189