/src/inode.h

http://ext3grep.googlecode.com/ · C Header · 131 lines · 83 code · 15 blank · 33 comment · 13 complexity · c9ba8849ee46d60ae77a2d68deea32e1 MD5 · raw file

  1. // ext3grep -- An ext3 file system investigation and undelete tool
  2. //
  3. //! @file inode.h Declaration of class InodePointer and function get_inode.
  4. //
  5. // Copyright (C) 2008, by
  6. //
  7. // Carlo Wood, Run on IRC <carlo@alinoe.com>
  8. // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
  9. // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
  10. //
  11. // This program is free software: you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published by
  13. // the Free Software Foundation, either version 2 of the License, or
  14. // (at your option) any later version.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License
  22. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. #ifndef INODE_H
  24. #define INODE_H
  25. #ifndef USE_PCH
  26. #include "ext3.h" // Needed for Inode
  27. #include "debug.h"
  28. #endif
  29. #include "globals.h"
  30. #include "load_meta_data.h"
  31. #if USE_MMAP
  32. void inode_mmap(int group);
  33. void inode_unmap(int group);
  34. #endif
  35. class InodePointer {
  36. private:
  37. Inode const* M_inode;
  38. int M_group;
  39. static Inode S_fake_inode;
  40. public:
  41. // Default constructor.
  42. InodePointer(void) : M_inode(NULL), M_group(-1) { }
  43. // Copy constructor.
  44. InodePointer(InodePointer const& ref) : M_inode(ref.M_inode), M_group(ref.M_group)
  45. {
  46. #if USE_MMAP
  47. if (M_group != -1)
  48. refs_to_mmap[M_group]++;
  49. #endif
  50. }
  51. // Create an InodePointer to a fake inode.
  52. InodePointer(int) : M_inode(&S_fake_inode), M_group(-1) { }
  53. // Destructor.
  54. ~InodePointer()
  55. {
  56. #if USE_MMAP
  57. if (M_group != -1)
  58. refs_to_mmap[M_group]--;
  59. #endif
  60. }
  61. InodePointer& operator=(InodePointer const& inode_reference)
  62. {
  63. M_inode = inode_reference.M_inode;
  64. #if USE_MMAP
  65. if (M_group != -1)
  66. refs_to_mmap[M_group]--;
  67. #endif
  68. M_group = inode_reference.M_group;
  69. #if USE_MMAP
  70. if (M_group != -1)
  71. refs_to_mmap[M_group]++;
  72. #endif
  73. return *this;
  74. }
  75. // Accessors.
  76. Inode const* operator->(void) const { return M_inode; }
  77. Inode const& operator*(void) const { return *M_inode; }
  78. private:
  79. friend InodePointer get_inode(uint32_t inode);
  80. InodePointer(Inode const& inode, int group) : M_inode(&inode), M_group(group)
  81. {
  82. ASSERT(M_group != -1);
  83. #if USE_MMAP
  84. refs_to_mmap[M_group]++;
  85. #endif
  86. }
  87. };
  88. inline unsigned int bit_to_all_inodes_group_index(unsigned int bit)
  89. {
  90. #if USE_MMAP
  91. // If bit is incremented by one, we need to skip inode_size_ bytes in the (mmap-ed) inode table.
  92. // Since the type of the table is Inode* the index needs to be incremented with the number of Inode structs that we need to skip.
  93. // Because both inode_size_ and sizeof(Inode) are a power of 2 and inode_size_ >= sizeof(Inode), this amounts to inode_size_ / sizeof(Inode)
  94. // index incrementation per bit.
  95. return bit * (inode_size_ / sizeof(Inode));
  96. #else
  97. // If no mmap is used, the table only contains the first 128 bytes of each inode.
  98. return bit;
  99. #endif
  100. }
  101. inline InodePointer get_inode(uint32_t inode)
  102. {
  103. int group = (inode - 1) / inodes_per_group_;
  104. unsigned int bit = inode - 1 - group * inodes_per_group_;
  105. // The bit in the bit mask must fit inside a single block.
  106. ASSERT(bit < 8U * block_size_);
  107. #if USE_MMAP
  108. if (all_inodes[group] == NULL)
  109. inode_mmap(group);
  110. #else
  111. if (block_bitmap[group] == NULL)
  112. load_meta_data(group);
  113. #endif
  114. return InodePointer(all_inodes[group][bit_to_all_inodes_group_index(bit)], group);
  115. }
  116. #endif // INODE_H