1715ba46bSmckusick /* 276403e9cSbostic * Copyright (c) 1982, 1986, 1989, 1993 376403e9cSbostic * The Regents of the University of California. All rights reserved. 4a257ce37Sbostic * (c) UNIX System Laboratories, Inc. 5a257ce37Sbostic * All or some portions of this file are derived from material licensed 6a257ce37Sbostic * to the University of California by American Telephone and Telegraph 7a257ce37Sbostic * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8a257ce37Sbostic * the permission of UNIX System Laboratories, Inc. 9715ba46bSmckusick * 1088f5dc9aSbostic * %sccs.include.redist.c% 117bc49bb9Smckusick * 12*0e5ea0e7Smckusick * @(#)dir.h 8.5 (Berkeley) 04/27/95 13715ba46bSmckusick */ 143f558a57Smckusic 15d0368a43Sbostic #ifndef _DIR_H_ 16d0368a43Sbostic #define _DIR_H_ 17ef5e499aSmckusick 183f558a57Smckusic /* 19*0e5ea0e7Smckusick * Theoretically, directories can be more than 2Gb in length, however, in 20*0e5ea0e7Smckusick * practice this seems unlikely. So, we define the type doff_t as a 32-bit 21*0e5ea0e7Smckusick * quantity to keep down the cost of doing lookup on a 32-bit machine. 22*0e5ea0e7Smckusick */ 23*0e5ea0e7Smckusick #define doff_t int32_t 24*0e5ea0e7Smckusick #define MAXDIRSIZE (0x7fffffff) 25*0e5ea0e7Smckusick 26*0e5ea0e7Smckusick /* 2733984602Sroot * A directory consists of some number of blocks of DIRBLKSIZ 2833984602Sroot * bytes, where DIRBLKSIZ is chosen such that it can be transferred 2933984602Sroot * to disk in a single atomic operation (e.g. 512 bytes on most machines). 3033984602Sroot * 3133984602Sroot * Each DIRBLKSIZ byte block contains some number of directory entry 3233984602Sroot * structures, which are of variable length. Each directory entry has 3333984602Sroot * a struct direct at the front of it, containing its inode number, 3433984602Sroot * the length of the entry, and the length of the name contained in 3533984602Sroot * the entry. These are followed by the name padded to a 4 byte boundary 3633984602Sroot * with null bytes. All names are guaranteed null terminated. 3733984602Sroot * The maximum length of a name in a directory is MAXNAMLEN. 3833984602Sroot * 390a81b6c4Smckusick * The macro DIRSIZ(fmt, dp) gives the amount of space required to represent 4033984602Sroot * a directory entry. Free space in a directory is represented by 410a81b6c4Smckusick * entries which have dp->d_reclen > DIRSIZ(fmt, dp). All DIRBLKSIZ bytes 4233984602Sroot * in a directory block are claimed by the directory entries. This 4333984602Sroot * usually results in the last entry in a directory having a large 4433984602Sroot * dp->d_reclen. When entries are deleted from a directory, the 4533984602Sroot * space is returned to the previous entry in the same directory 4633984602Sroot * block by increasing its dp->d_reclen. If the first entry of 4733984602Sroot * a directory block is free, then its dp->d_ino is set to 0. 4833984602Sroot * Entries other than the first in a directory do not normally have 4933984602Sroot * dp->d_ino set to 0. 503f558a57Smckusic */ 513f558a57Smckusic #define DIRBLKSIZ DEV_BSIZE 522f93c53dSmarc #define MAXNAMLEN 255 533f558a57Smckusic 543f558a57Smckusic struct direct { 55020ff3ebSmckusick u_int32_t d_ino; /* inode number of entry */ 56020ff3ebSmckusick u_int16_t d_reclen; /* length of this record */ 57020ff3ebSmckusick u_int8_t d_type; /* file type, see below */ 58020ff3ebSmckusick u_int8_t d_namlen; /* length of string in d_name */ 59ef5e499aSmckusick char d_name[MAXNAMLEN + 1];/* name with length <= MAXNAMLEN */ 604779a110Sbill }; 613f558a57Smckusic 6233984602Sroot /* 630a81b6c4Smckusick * File types 640a81b6c4Smckusick */ 650a81b6c4Smckusick #define DT_UNKNOWN 0 660a81b6c4Smckusick #define DT_FIFO 1 670a81b6c4Smckusick #define DT_CHR 2 680a81b6c4Smckusick #define DT_DIR 4 690a81b6c4Smckusick #define DT_BLK 6 700a81b6c4Smckusick #define DT_REG 8 710a81b6c4Smckusick #define DT_LNK 10 720a81b6c4Smckusick #define DT_SOCK 12 73769bd007Spendry #define DT_WHT 14 740a81b6c4Smckusick 750a81b6c4Smckusick /* 760a81b6c4Smckusick * Convert between stat structure types and directory types. 770a81b6c4Smckusick */ 780a81b6c4Smckusick #define IFTODT(mode) (((mode) & 0170000) >> 12) 790a81b6c4Smckusick #define DTTOIF(dirtype) ((dirtype) << 12) 800a81b6c4Smckusick 810a81b6c4Smckusick /* 8233984602Sroot * The DIRSIZ macro gives the minimum record length which will hold 8333984602Sroot * the directory entry. This requires the amount of space in struct direct 8433984602Sroot * without the d_name field, plus enough space for the name with a terminating 8533984602Sroot * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary. 8633984602Sroot */ 870a81b6c4Smckusick #if (BYTE_ORDER == LITTLE_ENDIAN) 880a81b6c4Smckusick #define DIRSIZ(oldfmt, dp) \ 890a81b6c4Smckusick ((oldfmt) ? \ 900a81b6c4Smckusick ((sizeof(struct direct) - (MAXNAMLEN+1)) + (((dp)->d_type+1 + 3) &~ 3)) : \ 910a81b6c4Smckusick ((sizeof(struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))) 920a81b6c4Smckusick #else 930a81b6c4Smckusick #define DIRSIZ(oldfmt, dp) \ 9433984602Sroot ((sizeof(struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3)) 950a81b6c4Smckusick #endif 9668d906a5Smckusick #define OLDDIRFMT 1 9768d906a5Smckusick #define NEWDIRFMT 0 9833984602Sroot 997bc49bb9Smckusick /* 100020ff3ebSmckusick * Template for manipulating directories. Should use struct direct's, 101020ff3ebSmckusick * but the name field is MAXNAMLEN - 1, and this just won't do. 1027bc49bb9Smckusick */ 1037bc49bb9Smckusick struct dirtemplate { 104020ff3ebSmckusick u_int32_t dot_ino; 105020ff3ebSmckusick int16_t dot_reclen; 106020ff3ebSmckusick u_int8_t dot_type; 107020ff3ebSmckusick u_int8_t dot_namlen; 1087bc49bb9Smckusick char dot_name[4]; /* must be multiple of 4 */ 109020ff3ebSmckusick u_int32_t dotdot_ino; 110020ff3ebSmckusick int16_t dotdot_reclen; 111020ff3ebSmckusick u_int8_t dotdot_type; 112020ff3ebSmckusick u_int8_t dotdot_namlen; 1130a81b6c4Smckusick char dotdot_name[4]; /* ditto */ 1140a81b6c4Smckusick }; 1150a81b6c4Smckusick 1160a81b6c4Smckusick /* 1170a81b6c4Smckusick * This is the old format of directories, sanz type element. 1180a81b6c4Smckusick */ 1190a81b6c4Smckusick struct odirtemplate { 120020ff3ebSmckusick u_int32_t dot_ino; 121020ff3ebSmckusick int16_t dot_reclen; 122020ff3ebSmckusick u_int16_t dot_namlen; 1230a81b6c4Smckusick char dot_name[4]; /* must be multiple of 4 */ 124020ff3ebSmckusick u_int32_t dotdot_ino; 125020ff3ebSmckusick int16_t dotdot_reclen; 126020ff3ebSmckusick u_int16_t dotdot_namlen; 1277bc49bb9Smckusick char dotdot_name[4]; /* ditto */ 1287bc49bb9Smckusick }; 129d0368a43Sbostic #endif /* !_DIR_H_ */ 130