Ver Fonte

Kernel: And some more KResult/KResultOr<T> porting work.

Andreas Kling há 6 anos atrás
pai
commit
e56fe71dbc

+ 1 - 1
Kernel/FileDescriptor.cpp

@@ -352,7 +352,7 @@ const char* to_string(SocketRole role)
     }
 }
 
-String FileDescriptor::absolute_path()
+KResultOr<String> FileDescriptor::absolute_path()
 {
     Stopwatch sw("absolute_path");
     if (is_tty())

+ 1 - 1
Kernel/FileDescriptor.h

@@ -43,7 +43,7 @@ public:
 
     ByteBuffer read_entire_file(Process&);
 
-    String absolute_path();
+    KResultOr<String> absolute_path();
 
     bool is_directory() const;
 

+ 34 - 13
Kernel/ProcFS.cpp

@@ -188,7 +188,10 @@ ByteBuffer procfs$pid_fds(InodeIdentifier identifier)
         auto* descriptor = process.file_descriptor(i);
         if (!descriptor)
             continue;
-        builder.appendf("% 3u %s\n", i, descriptor->absolute_path().characters());
+        auto result = descriptor->absolute_path();
+        if (result.is_error())
+            continue;
+        builder.appendf("% 3u %s\n", i, result.value().characters());
     }
     return builder.to_byte_buffer();
 }
@@ -203,7 +206,10 @@ ByteBuffer procfs$pid_fd_entry(InodeIdentifier identifier)
     auto* descriptor = process.file_descriptor(fd);
     if (!descriptor)
         return { };
-    return descriptor->absolute_path().to_byte_buffer();
+    auto result = descriptor->absolute_path();
+    if (result.is_error())
+        return { };
+    return result.value().to_byte_buffer();
 }
 
 ByteBuffer procfs$pid_vm(InodeIdentifier identifier)
@@ -331,7 +337,10 @@ ByteBuffer procfs$pid_exe(InodeIdentifier identifier)
     auto& process = handle->process();
     auto inode = process.executable_inode();
     ASSERT(inode);
-    return VFS::the().absolute_path(*inode).to_byte_buffer();
+    auto result = VFS::the().absolute_path(*inode);
+    if (result.is_error())
+        return { };
+    return result.value().to_byte_buffer();
 }
 
 ByteBuffer procfs$pid_cwd(InodeIdentifier identifier)
@@ -339,7 +348,10 @@ ByteBuffer procfs$pid_cwd(InodeIdentifier identifier)
     auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
     if (!handle)
         return { };
-    return VFS::the().absolute_path(handle->process().cwd_inode()).to_byte_buffer();
+    auto result = VFS::the().absolute_path(handle->process().cwd_inode());
+    if (result.is_error())
+        return { };
+    return result.value().to_byte_buffer();
 }
 
 ByteBuffer procfs$self(InodeIdentifier)
@@ -388,9 +400,12 @@ ByteBuffer procfs$mounts(InodeIdentifier)
             builder.appendf("/");
         else {
             builder.appendf("%u:%u", mount.host().fsid(), mount.host().index());
-            auto path = VFS::the().absolute_path(mount.host());
             builder.append(' ');
-            builder.append(path);
+            auto result = VFS::the().absolute_path(mount.host());
+            if (result.is_error())
+                builder.append("[error]");
+            else
+                builder.append(result.value());
         }
         builder.append('\n');
     });
@@ -411,7 +426,11 @@ ByteBuffer procfs$df(InodeIdentifier)
         if (!mount.host().is_valid())
             builder.append("/");
         else {
-            builder.append(VFS::the().absolute_path(mount.host()));
+            auto result = VFS::the().absolute_path(mount.host());
+            if (result.is_error())
+                builder.append("[Error]");
+            else
+                builder.append(result.value());
         }
         builder.append('\n');
     });
@@ -553,11 +572,13 @@ ByteBuffer procfs$all(InodeIdentifier)
 ByteBuffer procfs$inodes(InodeIdentifier)
 {
     extern HashTable<Inode*>& all_inodes();
-    auto& vfs = VFS::the();
     StringBuilder builder;
     for (auto it : all_inodes()) {
         RetainPtr<Inode> inode = *it;
-        String path = vfs.absolute_path(*inode);
+        auto result = VFS::the().absolute_path(*inode);
+        if (result.is_error())
+            continue;
+        auto path = result.value();
         builder.appendf("Inode{K%x} %02u:%08u (%u) %s\n", inode.ptr(), inode->fsid(), inode->index(), inode->retain_count(), path.characters());
     }
     return builder.to_byte_buffer();
@@ -880,13 +901,13 @@ bool ProcFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntry&)
         }
         for (auto pid_child : Process::all_pids()) {
             char name[16];
-            size_t name_length = ksprintf(name, "%u", pid_child);
+            int name_length = ksprintf(name, "%u", pid_child);
             callback({ name, name_length, to_identifier(fsid(), PDI_Root, pid_child, FI_PID), 0 });
         }
         break;
 
     case FI_Root_sys:
-        for (size_t i = 0; i < fs().m_sys_entries.size(); ++i) {
+        for (int i = 0; i < fs().m_sys_entries.size(); ++i) {
             auto& entry = fs().m_sys_entries[i];
             callback({ entry.name, strlen(entry.name), sys_var_to_identifier(fsid(), i), 0 });
         }
@@ -913,7 +934,7 @@ bool ProcFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntry&)
         if (!handle)
             return false;
         auto& process = handle->process();
-        for (size_t i = 0; i < process.max_open_file_descriptors(); ++i) {
+        for (int i = 0; i < process.max_open_file_descriptors(); ++i) {
             auto* descriptor = process.file_descriptor(i);
             if (!descriptor)
                 continue;
@@ -965,7 +986,7 @@ InodeIdentifier ProcFSInode::lookup(const String& name)
     }
 
     if (proc_file_type == FI_Root_sys) {
-        for (size_t i = 0; i < fs().m_sys_entries.size(); ++i) {
+        for (int i = 0; i < fs().m_sys_entries.size(); ++i) {
             auto& entry = fs().m_sys_entries[i];
             if (!strcmp(entry.name, name.characters()))
                 return sys_var_to_identifier(fsid(), i);

+ 6 - 5
Kernel/Process.cpp

@@ -1391,18 +1391,19 @@ int Process::sys$getcwd(char* buffer, ssize_t size)
         return -EINVAL;
     if (!validate_write(buffer, size))
         return -EFAULT;
-    auto path = VFS::the().absolute_path(cwd_inode());
-    if (path.is_null())
-        return -EINVAL;
+    auto path_or_error = VFS::the().absolute_path(cwd_inode());
+    if (path_or_error.is_error())
+        return path_or_error.error();
+    auto path = path_or_error.value();
     if (size < path.length() + 1)
         return -ERANGE;
     strcpy(buffer, path.characters());
     return 0;
 }
 
-size_t Process::number_of_open_file_descriptors() const
+int Process::number_of_open_file_descriptors() const
 {
-    size_t count = 0;
+    int count = 0;
     for (auto& descriptor : m_fds) {
         if (descriptor)
             ++count;

+ 3 - 3
Kernel/Process.h

@@ -276,8 +276,8 @@ public:
     Inode& cwd_inode();
     Inode* executable_inode() { return m_executable.ptr(); }
 
-    size_t number_of_open_file_descriptors() const;
-    size_t max_open_file_descriptors() const { return m_max_open_file_descriptors; }
+    int number_of_open_file_descriptors() const;
+    int max_open_file_descriptors() const { return m_max_open_file_descriptors; }
 
     void send_signal(byte signal, Process* sender);
 
@@ -366,7 +366,7 @@ private:
     Vector<int> m_select_exceptional_fds;
     timeval m_select_timeout;
     bool m_select_has_timeout { false };
-    size_t m_max_open_file_descriptors { 16 };
+    int m_max_open_file_descriptors { 16 };
     SignalActionData m_signal_action_data[32];
     dword m_pending_signals { 0 };
     dword m_signal_mask { 0 };

+ 9 - 25
Kernel/VirtualFileSystem.cpp

@@ -355,25 +355,6 @@ KResultOr<Retained<Inode>> VFS::resolve_path_to_inode(const String& path, Inode&
     return Retained<Inode>(*get_inode(result.value()));
 }
 
-RetainPtr<Inode> VFS::resolve_path_to_inode(const String& path, Inode& base, int& error, RetainPtr<Inode>* parent_inode)
-{
-    // FIXME: This won't work nicely across mount boundaries.
-    FileSystemPath p(path);
-    if (!p.is_valid()) {
-        error = -EINVAL;
-        return nullptr;
-    }
-    InodeIdentifier parent_id;
-    auto inode_id = old_resolve_path(path, base.identifier(), error, 0, &parent_id);
-    if (parent_inode && parent_id.is_valid())
-        *parent_inode = get_inode(parent_id);
-    if (!inode_id.is_valid()) {
-        error = -ENOENT;
-        return nullptr;
-    }
-    return get_inode(inode_id);
-}
-
 KResult VFS::link(const String& old_path, const String& new_path, Inode& base)
 {
     auto old_inode_or_error = resolve_path_to_inode(old_path, base);
@@ -497,17 +478,16 @@ RetainPtr<Inode> VFS::get_inode(InodeIdentifier inode_id)
     return inode_id.fs()->get_inode(inode_id);
 }
 
-String VFS::absolute_path(InodeIdentifier inode_id)
+KResultOr<String> VFS::absolute_path(InodeIdentifier inode_id)
 {
     auto inode = get_inode(inode_id);
     if (!inode)
-        return { };
+        return KResult(-EIO);
     return absolute_path(*inode);
 }
 
-String VFS::absolute_path(Inode& core_inode)
+KResultOr<String> VFS::absolute_path(Inode& core_inode)
 {
-    int error;
     Vector<InodeIdentifier> lineage;
     RetainPtr<Inode> inode = &core_inode;
     while (inode->identifier() != root_inode_id()) {
@@ -518,11 +498,15 @@ String VFS::absolute_path(Inode& core_inode)
 
         InodeIdentifier parent_id;
         if (inode->is_directory()) {
-            parent_id = old_resolve_path("..", inode->identifier(), error);
+            auto result = resolve_path("..", inode->identifier());
+            if (result.is_error())
+                return result.error();
+            parent_id = result.value();
         } else {
             parent_id = inode->parent()->identifier();
         }
-        ASSERT(parent_id.is_valid());
+        if (!parent_id.is_valid())
+            return KResult(-EIO);
         inode = get_inode(parent_id);
     }
     if (lineage.is_empty())

+ 2 - 3
Kernel/VirtualFileSystem.h

@@ -84,8 +84,8 @@ public:
     size_t mount_count() const { return m_mounts.size(); }
     void for_each_mount(Function<void(const Mount&)>) const;
 
-    String absolute_path(Inode&);
-    String absolute_path(InodeIdentifier);
+    KResultOr<String> absolute_path(Inode&);
+    KResultOr<String> absolute_path(InodeIdentifier);
 
     InodeIdentifier root_inode_id() const;
     Inode* root_inode() { return m_root_inode.ptr(); }
@@ -105,7 +105,6 @@ private:
     void traverse_directory_inode(Inode&, Function<bool(const FS::DirectoryEntry&)>);
     InodeIdentifier old_resolve_path(const String& path, InodeIdentifier base, int& error, int options = 0, InodeIdentifier* parent_id = nullptr);
     KResultOr<InodeIdentifier> resolve_path(const String& path, InodeIdentifier base, int options = 0, InodeIdentifier* parent_id = nullptr);
-    RetainPtr<Inode> resolve_path_to_inode(const String& path, Inode& base, int& error, RetainPtr<Inode>* parent_id = nullptr);
     KResultOr<Retained<Inode>> resolve_path_to_inode(const String& path, Inode& base, RetainPtr<Inode>* parent_id = nullptr, int options = 0);
     KResultOr<InodeIdentifier> resolve_symbolic_link(InodeIdentifier base, Inode& symlink_inode);