123407Smckusick /* 263376Sbostic * Copyright (c) 1982, 1986, 1989, 1993 363376Sbostic * The Regents of the University of California. All rights reserved. 4*65774Sbostic * (c) UNIX System Laboratories, Inc. 5*65774Sbostic * All or some portions of this file are derived from material licensed 6*65774Sbostic * to the University of California by American Telephone and Telegraph 7*65774Sbostic * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8*65774Sbostic * the permission of UNIX System Laboratories, Inc. 923407Smckusick * 1044536Sbostic * %sccs.include.redist.c% 1136546Smckusick * 12*65774Sbostic * @(#)dir.h 8.2 (Berkeley) 01/21/94 1323407Smckusick */ 1454Sbill 1547658Sbostic #ifndef _DIR_H_ 1647658Sbostic #define _DIR_H_ 1737734Smckusick 186565Smckusic /* 198433Sroot * A directory consists of some number of blocks of DIRBLKSIZ 208433Sroot * bytes, where DIRBLKSIZ is chosen such that it can be transferred 218433Sroot * to disk in a single atomic operation (e.g. 512 bytes on most machines). 228433Sroot * 238433Sroot * Each DIRBLKSIZ byte block contains some number of directory entry 248433Sroot * structures, which are of variable length. Each directory entry has 258433Sroot * a struct direct at the front of it, containing its inode number, 268433Sroot * the length of the entry, and the length of the name contained in 278433Sroot * the entry. These are followed by the name padded to a 4 byte boundary 288433Sroot * with null bytes. All names are guaranteed null terminated. 298433Sroot * The maximum length of a name in a directory is MAXNAMLEN. 308433Sroot * 3154606Smckusick * The macro DIRSIZ(fmt, dp) gives the amount of space required to represent 328433Sroot * a directory entry. Free space in a directory is represented by 3354606Smckusick * entries which have dp->d_reclen > DIRSIZ(fmt, dp). All DIRBLKSIZ bytes 348433Sroot * in a directory block are claimed by the directory entries. This 358433Sroot * usually results in the last entry in a directory having a large 368433Sroot * dp->d_reclen. When entries are deleted from a directory, the 378433Sroot * space is returned to the previous entry in the same directory 388433Sroot * block by increasing its dp->d_reclen. If the first entry of 398433Sroot * a directory block is free, then its dp->d_ino is set to 0. 408433Sroot * Entries other than the first in a directory do not normally have 418433Sroot * dp->d_ino set to 0. 426565Smckusic */ 4336847Smarc #define DIRBLKSIZ DEV_BSIZE 4436847Smarc #define MAXNAMLEN 255 456565Smckusic 466565Smckusic struct direct { 478433Sroot u_long d_ino; /* inode number of entry */ 488433Sroot u_short d_reclen; /* length of this record */ 4954606Smckusick u_char d_type; /* file type, see below */ 5054606Smckusick u_char d_namlen; /* length of string in d_name */ 5137734Smckusick char d_name[MAXNAMLEN + 1]; /* name with length <= MAXNAMLEN */ 526565Smckusic }; 536565Smckusic 546565Smckusic /* 5554606Smckusick * File types 5654606Smckusick */ 5754606Smckusick #define DT_UNKNOWN 0 5854606Smckusick #define DT_FIFO 1 5954606Smckusick #define DT_CHR 2 6054606Smckusick #define DT_DIR 4 6154606Smckusick #define DT_BLK 6 6254606Smckusick #define DT_REG 8 6354606Smckusick #define DT_LNK 10 6454606Smckusick #define DT_SOCK 12 6554606Smckusick 6654606Smckusick /* 6754606Smckusick * Convert between stat structure types and directory types. 6854606Smckusick */ 6954606Smckusick #define IFTODT(mode) (((mode) & 0170000) >> 12) 7054606Smckusick #define DTTOIF(dirtype) ((dirtype) << 12) 7154606Smckusick 7254606Smckusick /* 738433Sroot * The DIRSIZ macro gives the minimum record length which will hold 748433Sroot * the directory entry. This requires the amount of space in struct direct 758433Sroot * without the d_name field, plus enough space for the name with a terminating 768433Sroot * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary. 776565Smckusic */ 7854606Smckusick #if (BYTE_ORDER == LITTLE_ENDIAN) 7954606Smckusick #define DIRSIZ(oldfmt, dp) \ 8054606Smckusick ((oldfmt) ? \ 8154606Smckusick ((sizeof (struct direct) - (MAXNAMLEN+1)) + (((dp)->d_type+1 + 3) &~ 3)) : \ 8254606Smckusick ((sizeof (struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))) 8354606Smckusick #else 8454606Smckusick #define DIRSIZ(oldfmt, dp) \ 858433Sroot ((sizeof (struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3)) 8654606Smckusick #endif 8754713Smckusick #define OLDDIRFMT 1 8854713Smckusick #define NEWDIRFMT 0 896565Smckusic 9036546Smckusick /* 9136546Smckusick * Template for manipulating directories. 9236546Smckusick * Should use struct direct's, but the name field 9336546Smckusick * is MAXNAMLEN - 1, and this just won't do. 9436546Smckusick */ 9536546Smckusick struct dirtemplate { 9636546Smckusick u_long dot_ino; 9736546Smckusick short dot_reclen; 9854606Smckusick u_char dot_type; 9954606Smckusick u_char dot_namlen; 10036546Smckusick char dot_name[4]; /* must be multiple of 4 */ 10136546Smckusick u_long dotdot_ino; 10236546Smckusick short dotdot_reclen; 10354606Smckusick u_char dotdot_type; 10454606Smckusick u_char dotdot_namlen; 10536546Smckusick char dotdot_name[4]; /* ditto */ 10636546Smckusick }; 10754606Smckusick 10854606Smckusick /* 10954606Smckusick * This is the old format of directories, sanz type element. 11054606Smckusick */ 11154606Smckusick struct odirtemplate { 11254606Smckusick u_long dot_ino; 11354606Smckusick short dot_reclen; 11454606Smckusick u_short dot_namlen; 11554606Smckusick char dot_name[4]; /* must be multiple of 4 */ 11654606Smckusick u_long dotdot_ino; 11754606Smckusick short dotdot_reclen; 11854606Smckusick u_short dotdot_namlen; 11954606Smckusick char dotdot_name[4]; /* ditto */ 12054606Smckusick }; 12147658Sbostic #endif /* !_DIR_H_ */ 122