Refactor FS::add_inode_to_directory() into Inode::add_child().

This commit is contained in:
Andreas Kling 2018-12-25 00:27:39 +01:00
parent 4f142b86ec
commit 6451b98ad4
Notes: sideshowbarker 2024-07-19 16:07:10 +09:00
5 changed files with 31 additions and 20 deletions

View File

@ -425,35 +425,32 @@ bool Ext2FSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntry&)
return true;
}
bool Ext2FS::add_inode_to_directory(InodeIndex parent, InodeIndex child, const String& name, byte fileType, int& error)
bool Ext2FSInode::add_child(InodeIdentifier child_id, const String& name, byte file_type, int& error)
{
auto e2inodeForDirectory = lookup_ext2_inode(parent);
ASSERT(e2inodeForDirectory);
ASSERT(isDirectory(e2inodeForDirectory->i_mode));
ASSERT(is_directory());
//#ifdef EXT2_DEBUG
dbgprintf("Ext2FS: Adding inode %u with name '%s' to directory %u\n", child, name.characters(), parent);
dbgprintf("Ext2FS: Adding inode %u with name '%s' to directory %u\n", child_id.index(), name.characters(), index());
//#endif
Vector<DirectoryEntry> entries;
bool nameAlreadyExists = false;
auto directory = get_inode({ id(), parent });
directory->traverse_as_directory([&] (auto& entry) {
Vector<FS::DirectoryEntry> entries;
bool name_already_exists = false;
traverse_as_directory([&] (auto& entry) {
if (!strcmp(entry.name, name.characters())) {
nameAlreadyExists = true;
name_already_exists = true;
return false;
}
entries.append(entry);
return true;
});
if (nameAlreadyExists) {
kprintf("Ext2FS: Name '%s' already exists in directory inode %u\n", name.characters(), parent);
if (name_already_exists) {
kprintf("Ext2FS: Name '%s' already exists in directory inode %u\n", name.characters(), index());
error = -EEXIST;
return false;
}
entries.append({ name.characters(), name.length(), { id(), child }, fileType });
return write_directory_inode(parent, move(entries));
entries.append({ name.characters(), name.length(), child_id, file_type });
return fs().write_directory_inode(index(), move(entries));
}
bool Ext2FS::write_directory_inode(unsigned directoryInode, Vector<DirectoryEntry>&& entries)
@ -847,23 +844,24 @@ RetainPtr<Inode> Ext2FS::create_directory(InodeIdentifier parent_id, const Strin
return inode;
}
RetainPtr<Inode> Ext2FS::create_inode(InodeIdentifier parentInode, const String& name, Unix::mode_t mode, unsigned size, int& error)
RetainPtr<Inode> Ext2FS::create_inode(InodeIdentifier parent_id, const String& name, Unix::mode_t mode, unsigned size, int& error)
{
ASSERT(parentInode.fsid() == id());
ASSERT(parent_id.fsid() == id());
auto parent_inode = get_inode(parent_id);
dbgprintf("Ext2FS: Adding inode '%s' (mode %u) to parent directory %u:\n", name.characters(), mode, parentInode.index());
dbgprintf("Ext2FS: Adding inode '%s' (mode %u) to parent directory %u:\n", name.characters(), mode, parent_inode->identifier().index());
// NOTE: This doesn't commit the inode allocation just yet!
auto inode_id = allocate_inode(0, 0);
if (!inode_id) {
kprintf("Ext2FS: createInode: allocateInode failed\n");
kprintf("Ext2FS: createInode: allocate_inode failed\n");
error = -ENOSPC;
return { };
}
auto blocks = allocate_blocks(group_index_from_inode(inode_id), ceilDiv(size, blockSize()));
if (blocks.is_empty()) {
kprintf("Ext2FS: createInode: allocateBlocks failed\n");
kprintf("Ext2FS: createInode: allocate_blocks failed\n");
error = -ENOSPC;
return { };
}
@ -885,7 +883,7 @@ RetainPtr<Inode> Ext2FS::create_inode(InodeIdentifier parentInode, const String&
fileType = EXT2_FT_SYMLINK;
// Try adding it to the directory first, in case the name is already in use.
bool success = add_inode_to_directory(parentInode.index(), inode_id, name, fileType, error);
bool success = parent_inode->add_child({ id(), inode_id }, name, fileType, error);
if (!success)
return { };

View File

@ -29,6 +29,7 @@ private:
virtual String reverse_lookup(InodeIdentifier) override;
virtual void flush_metadata() override;
virtual bool write(const ByteBuffer&) override;
virtual bool add_child(InodeIdentifier child_id, const String& name, byte file_type, int& error) override;
void populate_lookup_cache();

View File

@ -82,6 +82,7 @@ public:
virtual InodeIdentifier lookup(const String& name) = 0;
virtual String reverse_lookup(InodeIdentifier) = 0;
virtual bool write(const ByteBuffer&) = 0;
virtual bool add_child(InodeIdentifier child_id, const String& name, byte file_type, int& error) = 0;
bool is_metadata_dirty() const { return m_metadata_dirty; }

View File

@ -263,3 +263,13 @@ bool SynthFSInode::write(const ByteBuffer&)
ASSERT_NOT_REACHED();
return false;
}
bool SynthFSInode::add_child(InodeIdentifier child_id, const String& name, byte file_type, int& error)
{
(void) child_id;
(void) name;
(void) file_type;
(void) error;
ASSERT_NOT_REACHED();
return false;
}

View File

@ -53,6 +53,7 @@ private:
virtual String reverse_lookup(InodeIdentifier) override;
virtual void flush_metadata() override;
virtual bool write(const ByteBuffer&) override;
virtual bool add_child(InodeIdentifier child_id, const String& name, byte file_type, int& error) override;
SynthFS& fs();
const SynthFS& fs() const;