mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-23 01:32:14 -05:00
Move readEntireInode() up to FileSystem (from ext2.)
It's just a wrapper around multiple calls to readInodeBytes() now.
This commit is contained in:
parent
0286b5ea48
commit
9528edab92
Notes:
sideshowbarker
2024-07-19 18:48:03 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/9528edab922
9 changed files with 39 additions and 52 deletions
|
@ -336,38 +336,6 @@ Unix::ssize_t Ext2FileSystem::readInodeBytes(InodeIdentifier inode, Unix::off_t
|
|||
return nread;
|
||||
}
|
||||
|
||||
ByteBuffer Ext2FileSystem::readInode(InodeIdentifier inode) const
|
||||
{
|
||||
ASSERT(inode.fileSystemID() == id());
|
||||
|
||||
auto e2inode = lookupExt2Inode(inode.index());
|
||||
if (!e2inode) {
|
||||
printf("[ext2fs] readInode: metadata lookup for inode %u failed\n", inode.index());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto contents = ByteBuffer::createUninitialized(e2inode->i_size);
|
||||
|
||||
Unix::ssize_t nread;
|
||||
byte buffer[512];
|
||||
byte* out = contents.pointer();
|
||||
Unix::off_t offset = 0;
|
||||
for (;;) {
|
||||
nread = readInodeBytes(inode, offset, sizeof(buffer), buffer);
|
||||
if (nread <= 0)
|
||||
break;
|
||||
memcpy(out, buffer, nread);
|
||||
out += nread;
|
||||
offset += nread;
|
||||
}
|
||||
if (nread < 0) {
|
||||
printf("[ext2fs] readInode: ERROR: %d\n", nread);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return contents;
|
||||
}
|
||||
|
||||
bool Ext2FileSystem::writeInode(InodeIdentifier inode, const ByteBuffer& data)
|
||||
{
|
||||
ASSERT(inode.fileSystemID() == id());
|
||||
|
@ -412,7 +380,7 @@ bool Ext2FileSystem::enumerateDirectoryInode(InodeIdentifier inode, std::functio
|
|||
printf("[ext2fs] Enumerating directory contents of inode %u:\n", inode.index());
|
||||
#endif
|
||||
|
||||
auto buffer = readInode(inode);
|
||||
auto buffer = readEntireInode(inode);
|
||||
ASSERT(buffer);
|
||||
auto* entry = reinterpret_cast<ext2_dir_entry_2*>(buffer.pointer());
|
||||
|
||||
|
|
|
@ -35,7 +35,6 @@ private:
|
|||
virtual bool initialize() override;
|
||||
virtual const char* className() const override;
|
||||
virtual InodeIdentifier rootInode() const override;
|
||||
virtual ByteBuffer readInode(InodeIdentifier) const override;
|
||||
virtual bool writeInode(InodeIdentifier, const ByteBuffer&) override;
|
||||
virtual bool enumerateDirectoryInode(InodeIdentifier, std::function<bool(const DirectoryEntry&)>) const override;
|
||||
virtual InodeMetadata inodeMetadata(InodeIdentifier) const override;
|
||||
|
|
|
@ -104,6 +104,6 @@ ByteBuffer FileHandle::readEntireFile()
|
|||
return buffer;
|
||||
}
|
||||
|
||||
return m_vnode->fileSystem()->readInode(m_vnode->inode);
|
||||
return m_vnode->fileSystem()->readEntireInode(m_vnode->inode);
|
||||
}
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ FileSystem* FileSystem::fromID(dword id)
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
InodeIdentifier FileSystem::childOfDirectoryInodeWithName(InodeIdentifier inode, const String& name)
|
||||
InodeIdentifier FileSystem::childOfDirectoryInodeWithName(InodeIdentifier inode, const String& name) const
|
||||
{
|
||||
InodeIdentifier foundInode;
|
||||
enumerateDirectoryInode(inode, [&] (const DirectoryEntry& entry) {
|
||||
|
@ -42,3 +42,35 @@ InodeIdentifier FileSystem::childOfDirectoryInodeWithName(InodeIdentifier inode,
|
|||
return foundInode;
|
||||
}
|
||||
|
||||
ByteBuffer FileSystem::readEntireInode(InodeIdentifier inode) const
|
||||
{
|
||||
ASSERT(inode.fileSystemID() == id());
|
||||
|
||||
auto metadata = inodeMetadata(inode);
|
||||
if (!metadata.isValid()) {
|
||||
printf("[fs] readInode: metadata lookup for inode %u failed\n", inode.index());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto contents = ByteBuffer::createUninitialized(metadata.size);
|
||||
|
||||
Unix::ssize_t nread;
|
||||
byte buffer[512];
|
||||
byte* out = contents.pointer();
|
||||
Unix::off_t offset = 0;
|
||||
for (;;) {
|
||||
nread = readInodeBytes(inode, offset, sizeof(buffer), buffer);
|
||||
if (nread <= 0)
|
||||
break;
|
||||
memcpy(out, buffer, nread);
|
||||
out += nread;
|
||||
offset += nread;
|
||||
}
|
||||
if (nread < 0) {
|
||||
printf("[fs] readInode: ERROR: %d\n", nread);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return contents;
|
||||
}
|
||||
|
||||
|
|
|
@ -25,7 +25,6 @@ public:
|
|||
virtual bool initialize() = 0;
|
||||
virtual const char* className() const = 0;
|
||||
virtual InodeIdentifier rootInode() const = 0;
|
||||
virtual ByteBuffer readInode(InodeIdentifier) const = 0;
|
||||
virtual bool writeInode(InodeIdentifier, const ByteBuffer&) = 0;
|
||||
virtual InodeMetadata inodeMetadata(InodeIdentifier) const = 0;
|
||||
|
||||
|
@ -41,7 +40,8 @@ public:
|
|||
virtual bool setModificationTime(InodeIdentifier, dword timestamp) = 0;
|
||||
virtual InodeIdentifier createInode(InodeIdentifier parentInode, const String& name, word mode) = 0;
|
||||
|
||||
InodeIdentifier childOfDirectoryInodeWithName(InodeIdentifier, const String& name);
|
||||
InodeIdentifier childOfDirectoryInodeWithName(InodeIdentifier, const String& name) const;
|
||||
ByteBuffer readEntireInode(InodeIdentifier) const;
|
||||
|
||||
protected:
|
||||
FileSystem();
|
||||
|
|
|
@ -5,5 +5,5 @@ ByteBuffer InodeIdentifier::readEntireFile() const
|
|||
{
|
||||
if (!fileSystem())
|
||||
return { };
|
||||
return fileSystem()->readInode(*this);
|
||||
return fileSystem()->readEntireInode(*this);
|
||||
}
|
||||
|
|
|
@ -62,17 +62,6 @@ InodeIdentifier SyntheticFileSystem::rootInode() const
|
|||
return { id(), 1 };
|
||||
}
|
||||
|
||||
ByteBuffer SyntheticFileSystem::readInode(InodeIdentifier inode) const
|
||||
{
|
||||
ASSERT(inode.fileSystemID() == id());
|
||||
#ifdef SYNTHFS_DEBUG
|
||||
printf("[synthfs] readInode %u\n", inode.index());
|
||||
#endif
|
||||
ASSERT(inode.index() != 1);
|
||||
ASSERT(inode.index() <= m_files.size());
|
||||
return m_files[inode.index() - 1]->data;
|
||||
}
|
||||
|
||||
bool SyntheticFileSystem::enumerateDirectoryInode(InodeIdentifier inode, std::function<bool(const DirectoryEntry&)> callback) const
|
||||
{
|
||||
ASSERT(inode.fileSystemID() == id());
|
||||
|
|
|
@ -12,7 +12,6 @@ public:
|
|||
virtual bool initialize() override;
|
||||
virtual const char* className() const override;
|
||||
virtual InodeIdentifier rootInode() const override;
|
||||
virtual ByteBuffer readInode(InodeIdentifier) const override;
|
||||
virtual bool writeInode(InodeIdentifier, const ByteBuffer&) override;
|
||||
virtual bool enumerateDirectoryInode(InodeIdentifier, std::function<bool(const DirectoryEntry&)>) const override;
|
||||
virtual InodeMetadata inodeMetadata(InodeIdentifier) const override;
|
||||
|
|
|
@ -285,7 +285,7 @@ void VirtualFileSystem::listDirectory(const String& path)
|
|||
if (metadata.isDirectory()) {
|
||||
printf("/");
|
||||
} else if (metadata.isSymbolicLink()) {
|
||||
auto symlinkContents = directoryInode.fileSystem()->readInode(metadata.inode);
|
||||
auto symlinkContents = directoryInode.fileSystem()->readEntireInode(metadata.inode);
|
||||
printf(" -> %s", String((const char*)symlinkContents.pointer(), symlinkContents.size()).characters());
|
||||
}
|
||||
printf("\n");
|
||||
|
|
Loading…
Reference in a new issue