diff --git a/Kernel/Ext2FileSystem.cpp b/Kernel/Ext2FileSystem.cpp index ef0e24ef92..fe7b0af13e 100644 --- a/Kernel/Ext2FileSystem.cpp +++ b/Kernel/Ext2FileSystem.cpp @@ -613,7 +613,7 @@ bool Ext2FSInode::traverse_as_directory(Function Ext2FS::create_inode(InodeIdentifier parent_id, const String& n file_type = EXT2_FT_SYMLINK; // Try adding it to the directory first, in case the name is already in use. - bool success = parent_inode->add_child({ fsid(), inode_id }, name, file_type, error); - if (!success) + auto result = parent_inode->add_child({ fsid(), inode_id }, name, file_type); + if (result.is_error()) { + error = result; return { }; + } // Looks like we're good, time to update the inode bitmap and group+global inode counters. - success = set_inode_allocation_state(inode_id, true); + bool success = set_inode_allocation_state(inode_id, true); ASSERT(success); for (auto block_index : blocks) { diff --git a/Kernel/Ext2FileSystem.h b/Kernel/Ext2FileSystem.h index ca93ddca60..c6c8b57575 100644 --- a/Kernel/Ext2FileSystem.h +++ b/Kernel/Ext2FileSystem.h @@ -32,7 +32,7 @@ private: virtual String reverse_lookup(InodeIdentifier) override; virtual void flush_metadata() override; virtual ssize_t write_bytes(off_t, ssize_t, const byte* data, FileDescriptor*) override; - virtual bool add_child(InodeIdentifier child_id, const String& name, byte file_type, int& error) override; + virtual KResult add_child(InodeIdentifier child_id, const String& name, byte file_type) override; virtual KResult remove_child(const String& name) override; virtual RetainPtr parent() const override; virtual int set_atime(time_t) override; diff --git a/Kernel/FileSystem.h b/Kernel/FileSystem.h index 1fdfb56c08..6cc2dca2b7 100644 --- a/Kernel/FileSystem.h +++ b/Kernel/FileSystem.h @@ -93,7 +93,7 @@ public: virtual InodeIdentifier lookup(const String& name) = 0; virtual String reverse_lookup(InodeIdentifier) = 0; virtual ssize_t write_bytes(off_t, ssize_t, const byte* data, FileDescriptor*) = 0; - virtual bool add_child(InodeIdentifier child_id, const String& name, byte file_type, int& error) = 0; + virtual KResult add_child(InodeIdentifier child_id, const String& name, byte file_type) = 0; virtual KResult remove_child(const String& name) = 0; virtual RetainPtr parent() const = 0; virtual size_t directory_entry_count() const = 0; diff --git a/Kernel/ProcFS.cpp b/Kernel/ProcFS.cpp index 72c4d0d0e1..63f8b4369d 100644 --- a/Kernel/ProcFS.cpp +++ b/Kernel/ProcFS.cpp @@ -1047,13 +1047,12 @@ ssize_t ProcFSInode::write_bytes(off_t offset, ssize_t size, const byte* buffer, return 0; } -bool ProcFSInode::add_child(InodeIdentifier child_id, const String& name, byte file_type, int& error) +KResult ProcFSInode::add_child(InodeIdentifier child_id, const String& name, byte file_type) { (void)child_id; (void)name; (void)file_type; - error = -EPERM; - return false; + return KResult(-EPERM); } KResult ProcFSInode::remove_child(const String& name) diff --git a/Kernel/ProcFS.h b/Kernel/ProcFS.h index 563a1ef745..35b0264d84 100644 --- a/Kernel/ProcFS.h +++ b/Kernel/ProcFS.h @@ -83,7 +83,7 @@ private: virtual String reverse_lookup(InodeIdentifier) override; virtual void flush_metadata() override; virtual ssize_t write_bytes(off_t, ssize_t, const byte* buffer, FileDescriptor*) override; - virtual bool add_child(InodeIdentifier child_id, const String& name, byte file_type, int& error) override; + virtual KResult add_child(InodeIdentifier child_id, const String& name, byte file_type) override; virtual KResult remove_child(const String& name) override; virtual RetainPtr parent() const override; virtual size_t directory_entry_count() const override; diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index d8c8ec9dd5..0a8fc36ada 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -2119,10 +2119,7 @@ int Process::sys$link(const char* old_path, const char* new_path) return -EFAULT; if (!validate_read_str(new_path)) return -EFAULT; - int error; - if (!VFS::the().link(String(old_path), String(new_path), cwd_inode(), error)) - return error; - return 0; + return VFS::the().link(String(old_path), String(new_path), cwd_inode()); } int Process::sys$unlink(const char* pathname) diff --git a/Kernel/SyntheticFileSystem.cpp b/Kernel/SyntheticFileSystem.cpp index 670aa2fcb2..61dd4637d0 100644 --- a/Kernel/SyntheticFileSystem.cpp +++ b/Kernel/SyntheticFileSystem.cpp @@ -280,14 +280,12 @@ ssize_t SynthFSInode::write_bytes(off_t offset, ssize_t size, const byte* buffer return 0; } -bool SynthFSInode::add_child(InodeIdentifier child_id, const String& name, byte file_type, int& error) +KResult SynthFSInode::add_child(InodeIdentifier child_id, const String& name, byte file_type) { - (void) child_id; - (void) name; - (void) file_type; - (void) error; + (void)child_id; + (void)name; + (void)file_type; ASSERT_NOT_REACHED(); - return false; } KResult SynthFSInode::remove_child(const String& name) diff --git a/Kernel/SyntheticFileSystem.h b/Kernel/SyntheticFileSystem.h index d991076af9..f0cebaedbf 100644 --- a/Kernel/SyntheticFileSystem.h +++ b/Kernel/SyntheticFileSystem.h @@ -63,7 +63,7 @@ private: virtual String reverse_lookup(InodeIdentifier) override; virtual void flush_metadata() override; virtual ssize_t write_bytes(off_t, ssize_t, const byte* buffer, FileDescriptor*) override; - virtual bool add_child(InodeIdentifier child_id, const String& name, byte file_type, int& error) override; + virtual KResult add_child(InodeIdentifier child_id, const String& name, byte file_type) override; virtual KResult remove_child(const String& name) override; virtual RetainPtr parent() const override; virtual size_t directory_entry_count() const override; diff --git a/Kernel/VirtualFileSystem.cpp b/Kernel/VirtualFileSystem.cpp index 7971c5ad3b..4a2edea105 100644 --- a/Kernel/VirtualFileSystem.cpp +++ b/Kernel/VirtualFileSystem.cpp @@ -375,39 +375,31 @@ RetainPtr VFS::resolve_path_to_inode(const String& path, Inode& base, int return get_inode(inode_id); } -bool VFS::link(const String& old_path, const String& new_path, Inode& base, int& error) +KResult VFS::link(const String& old_path, const String& new_path, Inode& base) { - auto old_inode = resolve_path_to_inode(old_path, base, error); - if (!old_inode) - return false; + auto old_inode_or_error = resolve_path_to_inode(old_path, base); + if (old_inode_or_error.is_error()) + return old_inode_or_error.error(); + auto old_inode = old_inode_or_error.value(); RetainPtr parent_inode; - auto new_inode = resolve_path_to_inode(new_path, base, error, &parent_inode); - if (new_inode) { - error = -EEXIST; - return false; - } - if (!parent_inode) { - error = -ENOENT; - return false; - } - if (parent_inode->fsid() != old_inode->fsid()) { - error = -EXDEV; - return false; - } - if (parent_inode->fs().is_readonly()) { - error = -EROFS; - return false; - } - if (!parent_inode->metadata().may_write(*current)) { - error = -EACCES; - return false; - } + auto new_inode_or_error = resolve_path_to_inode(new_path, base, &parent_inode); + if (!new_inode_or_error.is_error()) + return KResult(-EEXIST); - if (!parent_inode->add_child(old_inode->identifier(), FileSystemPath(new_path).basename(), 0, error)) - return false; - error = 0; - return true; + if (!parent_inode) + return KResult(-ENOENT); + + if (parent_inode->fsid() != old_inode->fsid()) + return KResult(-EXDEV); + + if (parent_inode->fs().is_readonly()) + return KResult(-EROFS); + + if (!parent_inode->metadata().may_write(*current)) + return KResult(-EACCES); + + return parent_inode->add_child(old_inode->identifier(), FileSystemPath(new_path).basename(), 0); } KResult VFS::unlink(const String& path, Inode& base) diff --git a/Kernel/VirtualFileSystem.h b/Kernel/VirtualFileSystem.h index 1345217824..38b8593226 100644 --- a/Kernel/VirtualFileSystem.h +++ b/Kernel/VirtualFileSystem.h @@ -66,7 +66,7 @@ public: RetainPtr open(const String& path, int& error, int options, mode_t mode, Inode& base); RetainPtr create(const String& path, int& error, int options, mode_t mode, Inode& base); KResult mkdir(const String& path, mode_t mode, Inode& base); - bool link(const String& old_path, const String& new_path, Inode& base, int& error); + KResult link(const String& old_path, const String& new_path, Inode& base); KResult unlink(const String& path, Inode& base); KResult rmdir(const String& path, Inode& base); KResult chmod(const String& path, mode_t, Inode& base);