Ext2FileSystem.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. #include "DiskBackedFileSystem.h"
  3. #include "UnixTypes.h"
  4. #include <AK/Buffer.h>
  5. #include <AK/OwnPtr.h>
  6. struct ext2_group_desc;
  7. struct ext2_inode;
  8. struct ext2_super_block;
  9. class Ext2FileSystem final : public DiskBackedFileSystem {
  10. public:
  11. static RetainPtr<Ext2FileSystem> create(RetainPtr<DiskDevice>&&);
  12. virtual ~Ext2FileSystem() override;
  13. private:
  14. typedef unsigned BlockIndex;
  15. typedef unsigned GroupIndex;
  16. typedef unsigned InodeIndex;
  17. explicit Ext2FileSystem(RetainPtr<DiskDevice>&&);
  18. const ext2_super_block& superBlock() const;
  19. const ext2_group_desc& blockGroupDescriptor(unsigned groupIndex) const;
  20. unsigned firstBlockOfGroup(unsigned groupIndex) const;
  21. unsigned inodesPerBlock() const;
  22. unsigned inodesPerGroup() const;
  23. unsigned blocksPerGroup() const;
  24. unsigned inodeSize() const;
  25. OwnPtr<ext2_inode> lookupExt2Inode(unsigned) const;
  26. bool writeExt2Inode(unsigned, const ext2_inode&);
  27. ByteBuffer readBlockContainingInode(unsigned inode, unsigned& blockIndex, unsigned& offset) const;
  28. ByteBuffer readSuperBlock() const;
  29. bool writeSuperBlock(const ext2_super_block&);
  30. virtual bool initialize() override;
  31. virtual const char* className() const override;
  32. virtual InodeIdentifier rootInode() const override;
  33. virtual bool writeInode(InodeIdentifier, const ByteBuffer&) override;
  34. virtual bool enumerateDirectoryInode(InodeIdentifier, std::function<bool(const DirectoryEntry&)>) const override;
  35. virtual InodeMetadata inodeMetadata(InodeIdentifier) const override;
  36. virtual bool setModificationTime(InodeIdentifier, dword timestamp) override;
  37. virtual InodeIdentifier createInode(InodeIdentifier parentInode, const String& name, Unix::mode_t, unsigned size) override;
  38. virtual Unix::ssize_t readInodeBytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer) const override;
  39. virtual InodeIdentifier makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t) override;
  40. bool isDirectoryInode(unsigned) const;
  41. unsigned allocateInode(unsigned preferredGroup, unsigned expectedSize);
  42. Vector<BlockIndex> allocateBlocks(unsigned group, unsigned count);
  43. unsigned groupIndexFromInode(unsigned) const;
  44. Vector<unsigned> blockListForInode(const ext2_inode&) const;
  45. void dumpBlockBitmap(unsigned groupIndex) const;
  46. void dumpInodeBitmap(unsigned groupIndex) const;
  47. template<typename F> void traverseInodeBitmap(unsigned groupIndex, F) const;
  48. template<typename F> void traverseBlockBitmap(unsigned groupIndex, F) const;
  49. bool addInodeToDirectory(unsigned directoryInode, unsigned inode, const String& name, byte fileType);
  50. bool writeDirectoryInode(unsigned directoryInode, Vector<DirectoryEntry>&&);
  51. bool setInodeAllocationState(unsigned inode, bool);
  52. bool setBlockAllocationState(GroupIndex, BlockIndex, bool);
  53. bool modifyLinkCount(InodeIndex, int delta);
  54. unsigned m_blockGroupCount { 0 };
  55. mutable ByteBuffer m_cachedSuperBlock;
  56. mutable ByteBuffer m_cachedBlockGroupDescriptorTable;
  57. };