Browse Source

Kernel: Use KResult in link().

Andreas Kling 6 years ago
parent
commit
60d0a48be5

+ 8 - 7
Kernel/Ext2FileSystem.cpp

@@ -613,7 +613,7 @@ bool Ext2FSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntry&)
     return true;
 }
 
-bool Ext2FSInode::add_child(InodeIdentifier child_id, const String& name, byte file_type, int& error)
+KResult Ext2FSInode::add_child(InodeIdentifier child_id, const String& name, byte file_type)
 {
     LOCKER(m_lock);
     ASSERT(is_directory());
@@ -634,8 +634,7 @@ bool Ext2FSInode::add_child(InodeIdentifier child_id, const String& name, byte f
     });
     if (name_already_exists) {
         kprintf("Ext2FS: Name '%s' already exists in directory inode %u\n", name.characters(), index());
-        error = -EEXIST;
-        return false;
+        return KResult(-EEXIST);
     }
 
     auto child_inode = fs().get_inode(child_id);
@@ -646,7 +645,7 @@ bool Ext2FSInode::add_child(InodeIdentifier child_id, const String& name, byte f
     bool success = fs().write_directory_inode(index(), move(entries));
     if (success)
         m_lookup_cache.set(name, child_id.index());
-    return success;
+    return KSuccess;
 }
 
 KResult Ext2FSInode::remove_child(const String& name)
@@ -1139,12 +1138,14 @@ RetainPtr<Inode> 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) {

+ 1 - 1
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<Inode> parent() const override;
     virtual int set_atime(time_t) override;

+ 1 - 1
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<Inode> parent() const = 0;
     virtual size_t directory_entry_count() const = 0;

+ 2 - 3
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)

+ 1 - 1
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<Inode> parent() const override;
     virtual size_t directory_entry_count() const override;

+ 1 - 4
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)

+ 4 - 6
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)

+ 1 - 1
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<Inode> parent() const override;
     virtual size_t directory_entry_count() const override;

+ 21 - 29
Kernel/VirtualFileSystem.cpp

@@ -375,39 +375,31 @@ RetainPtr<Inode> 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<Inode> 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)

+ 1 - 1
Kernel/VirtualFileSystem.h

@@ -66,7 +66,7 @@ public:
     RetainPtr<FileDescriptor> open(const String& path, int& error, int options, mode_t mode, Inode& base);
     RetainPtr<FileDescriptor> 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);