diff options
author | Andreas Kling <awesomekling@gmail.com> | 2018-12-25 00:27:39 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2018-12-25 00:27:39 +0100 |
commit | 6451b98ad44412a13fdc43133144e0dfc1080e67 (patch) | |
tree | 9a5486ee4869a3b226b619a9a1174e40627f8b3d | |
parent | 4f142b86ec8068cfc57a3b2b4f7d844d1179206a (diff) | |
download | serenity-6451b98ad44412a13fdc43133144e0dfc1080e67.zip |
Refactor FS::add_inode_to_directory() into Inode::add_child().
-rw-r--r-- | VirtualFileSystem/Ext2FileSystem.cpp | 38 | ||||
-rw-r--r-- | VirtualFileSystem/Ext2FileSystem.h | 1 | ||||
-rw-r--r-- | VirtualFileSystem/FileSystem.h | 1 | ||||
-rw-r--r-- | VirtualFileSystem/SyntheticFileSystem.cpp | 10 | ||||
-rw-r--r-- | VirtualFileSystem/SyntheticFileSystem.h | 1 |
5 files changed, 31 insertions, 20 deletions
diff --git a/VirtualFileSystem/Ext2FileSystem.cpp b/VirtualFileSystem/Ext2FileSystem.cpp index 38b6e82b4e..cb4ea0db6d 100644 --- a/VirtualFileSystem/Ext2FileSystem.cpp +++ b/VirtualFileSystem/Ext2FileSystem.cpp @@ -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 { }; diff --git a/VirtualFileSystem/Ext2FileSystem.h b/VirtualFileSystem/Ext2FileSystem.h index 429834408a..1c7b3dca3f 100644 --- a/VirtualFileSystem/Ext2FileSystem.h +++ b/VirtualFileSystem/Ext2FileSystem.h @@ -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(); diff --git a/VirtualFileSystem/FileSystem.h b/VirtualFileSystem/FileSystem.h index dcc8a4ddda..5bd7635417 100644 --- a/VirtualFileSystem/FileSystem.h +++ b/VirtualFileSystem/FileSystem.h @@ -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; } diff --git a/VirtualFileSystem/SyntheticFileSystem.cpp b/VirtualFileSystem/SyntheticFileSystem.cpp index 63994345aa..9be7277684 100644 --- a/VirtualFileSystem/SyntheticFileSystem.cpp +++ b/VirtualFileSystem/SyntheticFileSystem.cpp @@ -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; +} diff --git a/VirtualFileSystem/SyntheticFileSystem.h b/VirtualFileSystem/SyntheticFileSystem.h index 020e9e14eb..27062b31ca 100644 --- a/VirtualFileSystem/SyntheticFileSystem.h +++ b/VirtualFileSystem/SyntheticFileSystem.h @@ -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; |