ext2fs_dir.h 6.4 KB

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