dir.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*-
  2. * Copyright (c) 1982, 1986, 1989, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. * (c) UNIX System Laboratories, Inc.
  5. * All or some portions of this file are derived from material licensed
  6. * to the University of California by American Telephone and Telegraph
  7. * Co. or Unix System Laboratories, Inc. and are reproduced herein with
  8. * the permission of UNIX System Laboratories, Inc.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * @(#)dir.h 8.2 (Berkeley) 1/21/94
  35. * $FreeBSD$
  36. */
  37. /*
  38. * Theoretically, directories can be more than 2Gb in length, however, in
  39. * practice this seems unlikely. So, we define the type doff_t as a 32-bit
  40. * quantity to keep down the cost of doing lookup on a 32-bit machine.
  41. */
  42. #define doff_t int32_t
  43. #define MAXDIRSIZE (0x7fffffff)
  44. /*
  45. * A directory consists of some number of blocks of DIRBLKSIZ
  46. * bytes, where DIRBLKSIZ is chosen such that it can be transferred
  47. * to disk in a single atomic operation (e.g. 512 bytes on most machines).
  48. *
  49. * Each DIRBLKSIZ byte block contains some number of directory entry
  50. * structures, which are of variable length. Each directory entry has
  51. * a struct direct at the front of it, containing its inode number,
  52. * the length of the entry, and the length of the name contained in
  53. * the entry. These are followed by the name padded to a 4 byte boundary
  54. * with null bytes. All names are guaranteed null terminated.
  55. * The maximum length of a name in a directory is UFS_MAXNAMLEN.
  56. *
  57. * The macro DIRSIZ(fmt, dp) gives the amount of space required to represent
  58. * a directory entry. Free space in a directory is represented by
  59. * entries which have dp->d_reclen > DIRSIZ(fmt, dp). All DIRBLKSIZ bytes
  60. * in a directory block are claimed by the directory entries. This
  61. * usually results in the last entry in a directory having a large
  62. * dp->d_reclen. When entries are deleted from a directory, the
  63. * space is returned to the previous entry in the same directory
  64. * block by increasing its dp->d_reclen. If the first entry of
  65. * a directory block is free, then its dp->d_ino is set to 0.
  66. * Entries other than the first in a directory do not normally have
  67. * dp->d_ino set to 0.
  68. */
  69. #define DIRBLKSIZ DEV_BSIZE
  70. #define UFS_MAXNAMLEN 255
  71. typedef struct Direct {
  72. uint32_t d_ino; /* inode number of entry */
  73. uint16_t d_reclen; /* length of this record */
  74. uint8_t d_type; /* file type, see below */
  75. uint8_t d_namlen; /* length of string in d_name */
  76. char d_name[UFS_MAXNAMLEN + 1];
  77. /* name with length <= UFS_MAXNAMLEN */
  78. } Direct;
  79. /*
  80. * File types
  81. */
  82. #define DT_UNKNOWN 0
  83. #define DT_FIFO 1
  84. #define DT_CHR 2
  85. #define DT_DIR 4
  86. #define DT_BLK 6
  87. #define DT_REG 8
  88. #define DT_LNK 10
  89. #define DT_SOCK 12
  90. #define DT_WHT 14
  91. /*
  92. * Convert between stat structure types and directory types.
  93. */
  94. #define IFTODT(mode) (((mode) & 0170000) >> 12)
  95. #define DTTOIF(dirtype) ((dirtype) << 12)
  96. /*
  97. * The DIRSIZ macro gives the minimum record length which will hold
  98. * the directory entry. This requires the amount of space in struct direct
  99. * without the d_name field, plus enough space for the name with a terminating
  100. * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
  101. */
  102. #define DIRECTSIZ(namlen) \
  103. ((offsetof(Direct, d_name) + \
  104. ((namlen)+1)*sizeof(((Direct *)0)->d_name[0]) + 3) & ~3)
  105. #define DIRSIZ(dp) \
  106. DIRECTSIZ((dp)->d_namlen)
  107. /*
  108. * Template for manipulating directories. Should use struct direct's,
  109. * but the name field is UFS_MAXNAMLEN - 1, and this just won't do.
  110. */
  111. struct dirtemplate {
  112. uint32_t dot_ino;
  113. int16_t dot_reclen;
  114. uint8_t dot_type;
  115. uint8_t dot_namlen;
  116. char dot_name[4]; /* must be multiple of 4 */
  117. uint32_t dotdot_ino;
  118. int16_t dotdot_reclen;
  119. uint8_t dotdot_type;
  120. uint8_t dotdot_namlen;
  121. char dotdot_name[4]; /* ditto */
  122. };