/filesystems/unixfs/ancientfs/ancientfs_v1,2,3.h

http://macfuse.googlecode.com/ · C Header · 149 lines · 83 code · 25 blank · 41 comment · 9 complexity · a16f75c632ae1c59e2e08496324fa5e3 MD5 · raw file

  1. /*
  2. * Ancient UNIX File Systems for MacFUSE
  3. * Amit Singh
  4. * http://osxbook.com
  5. */
  6. #ifndef _ANCIENTFS_V123_H_
  7. #define _ANCIENTFS_V123_H_
  8. #include "unixfs_internal.h"
  9. #include "ancientfs.h"
  10. typedef int16_t a_int; /* ancient int */
  11. typedef a_int a_ino_t; /* ancient i-node type */
  12. #define BSIZE 512
  13. #define ROOTINO 41
  14. #define DIRSIZ 8
  15. /*
  16. * Disk blocks 0 and 1 are collectively known as the super-block
  17. */
  18. #define SUPERB (off_t)0
  19. #define SBSIZE BSIZE * 2
  20. struct filsys
  21. {
  22. a_int s_bmapsz; /* free storage map bytes; always even */
  23. uint8_t* s_bmap; /* free storage map */
  24. a_int s_imapsz; /* i-node map bytes; always even */
  25. uint8_t* s_imap; /* i-node map */
  26. } __attribute__((packed));
  27. /*
  28. * b-map
  29. *
  30. * There is one bit for each block on the device; the bit is "1" if the
  31. * block is free. Thus, if s_bmapsz is n, the blocks on the device are
  32. * numbered 0 through 8n - 1. In s_bmap, the bit for block k of the device
  33. * is in byte k/8 of the map; it is offset k (mod 8) bits from the right.
  34. * Note that bits exist for the super-block and the i-node list, even though
  35. * they are never allocated or freed.
  36. */
  37. /*
  38. * i-map
  39. *
  40. * s_imapsz is also even. I-numbers below 41 are reserved for special files,
  41. * and are never allocated. The first bit in s_imap refers to i-number 41.
  42. * Therefore, the byte number in s_imap for i-node i is (i - 41)/8. It is
  43. * offset (i - 41) (mod 8) bits from the right; unlike s_bmap, a "0" bit
  44. * indicates an available i-node.
  45. */
  46. struct dinode
  47. {
  48. a_int di_flags; /* flags (see below) */
  49. char di_nlink; /* number of links */
  50. char di_uid; /* user ID of owner */
  51. a_int di_size; /* size in bytes */
  52. a_int di_addr[8]; /* indirect block or contents block 1 through 8 */
  53. a_int di_crtime[2]; /* creation time */
  54. a_int di_mtime[2]; /* modification time */
  55. char di_unused[2]; /* unused */
  56. } __attribute__((packed)); /* 32 bytes */
  57. /*
  58. * I-numbers begin at 1, and the storage for i-nodes begins at disk block 2.
  59. * Also, i-nodes are 32 bytes long, so 16 of them fit into a block. Therefore,
  60. * i-node i is located in block (i + 31)/16 of the file system, and begins
  61. * 32 * ((i + 31)(mod 16)) bytes from its start.
  62. */
  63. #define INOPB 16 /* i-nodes per block */
  64. /* flags */
  65. #define IALLOC 0100000 /* i-node is allocated */
  66. #define IFMT 060000
  67. #define IFDIR 040000 /* directory */
  68. #define IMOD 020000 /* file has been modified (always on) */
  69. #define ILARG 010000 /* large file */
  70. #define ISUID 000040 /* set user ID on execution */
  71. #define IEXEC 000020 /* executable */
  72. #define IRUSR 000010 /* read, owner */
  73. #define IWUSR 000004 /* write, owner */
  74. #define IROTH 000002 /* read, non-owner */
  75. #define IWOTH 000001 /* write, non-owner */
  76. static inline int
  77. ancientfs_v123_mode(mode_t mode, uint32_t flags)
  78. {
  79. int newmode = 0;
  80. mode = mode & ~(IALLOC | ILARG | IMOD);
  81. if ((mode & IFMT) == 0)
  82. newmode |= S_IFREG;
  83. else
  84. newmode |= (mode & IFMT);
  85. if (mode & ISUID)
  86. newmode |= S_ISUID;
  87. if (mode & IEXEC)
  88. newmode |= S_IXUSR;
  89. if (mode & IRUSR)
  90. newmode |= S_IRUSR;
  91. if (mode & IWUSR)
  92. newmode |= S_IWUSR;
  93. if (mode & IROTH) {
  94. newmode |= S_IROTH;
  95. if (mode & IEXEC)
  96. newmode |= S_IXOTH;
  97. }
  98. if (mode & IWOTH)
  99. newmode |= S_IWOTH;
  100. return newmode;
  101. }
  102. /*
  103. * v1 00:00 Jan 1, 1971 /60
  104. * v2 00:00 Jan 1, 1971 /60
  105. * v3 00:00 Jan 1, 1972 /60
  106. *
  107. * modern 00:00 Jan 1, 1970 full seconds
  108. */
  109. static inline uint32_t
  110. ancientfs_v123_time(uint32_t t, uint32_t flags)
  111. {
  112. uint32_t cvt = t;
  113. cvt = cvt / 60; /* times were measured in sixtieths of a second */
  114. uint32_t epoch_years = (flags & ANCIENTFS_UNIX_V1) ? 1 :
  115. (flags & ANCIENTFS_UNIX_V2) ? 1 :
  116. (flags & ANCIENTFS_UNIX_V3) ? 2 : 0;
  117. cvt += (epoch_years * 365 * 24 * 3600); /* epoch fixup */
  118. return cvt;
  119. }
  120. struct dent {
  121. a_int u_ino; /* i-node table pointer */
  122. char u_name[DIRSIZ]; /* component name */
  123. } __attribute__((packed));
  124. #endif /* _ANCIENTFS_V123_H_ */