diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-03-06 22:30:13 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-03-06 22:30:13 +0100 |
commit | e56fe71dbc97a56f016f1de8aeedd4fa0105f4d7 (patch) | |
tree | 35b2b908c63d830fbabe468102f31dab79faa679 | |
parent | 028afabf6b22664c728f07234bc1dda0f8cb0fbe (diff) | |
download | serenity-e56fe71dbc97a56f016f1de8aeedd4fa0105f4d7.zip |
Kernel: And some more KResult/KResultOr<T> porting work.
-rw-r--r-- | Kernel/FileDescriptor.cpp | 2 | ||||
-rw-r--r-- | Kernel/FileDescriptor.h | 2 | ||||
-rw-r--r-- | Kernel/ProcFS.cpp | 47 | ||||
-rw-r--r-- | Kernel/Process.cpp | 11 | ||||
-rw-r--r-- | Kernel/Process.h | 6 | ||||
-rw-r--r-- | Kernel/VirtualFileSystem.cpp | 34 | ||||
-rw-r--r-- | Kernel/VirtualFileSystem.h | 5 |
7 files changed, 56 insertions, 51 deletions
diff --git a/Kernel/FileDescriptor.cpp b/Kernel/FileDescriptor.cpp index ea680ee909..a678afdfc3 100644 --- a/Kernel/FileDescriptor.cpp +++ b/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()) diff --git a/Kernel/FileDescriptor.h b/Kernel/FileDescriptor.h index 55b08d9af3..0b890c04d2 100644 --- a/Kernel/FileDescriptor.h +++ b/Kernel/FileDescriptor.h @@ -43,7 +43,7 @@ public: ByteBuffer read_entire_file(Process&); - String absolute_path(); + KResultOr<String> absolute_path(); bool is_directory() const; diff --git a/Kernel/ProcFS.cpp b/Kernel/ProcFS.cpp index 63f8b4369d..7919ff9031 100644 --- a/Kernel/ProcFS.cpp +++ b/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); diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 977ef02049..4d0fa7191d 100644 --- a/Kernel/Process.cpp +++ b/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; diff --git a/Kernel/Process.h b/Kernel/Process.h index 62bfb9b960..1130ded9c0 100644 --- a/Kernel/Process.h +++ b/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 }; diff --git a/Kernel/VirtualFileSystem.cpp b/Kernel/VirtualFileSystem.cpp index 773205dd16..1fb13612da 100644 --- a/Kernel/VirtualFileSystem.cpp +++ b/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()) diff --git a/Kernel/VirtualFileSystem.h b/Kernel/VirtualFileSystem.h index d72cf9fb3c..6baee90cd0 100644 --- a/Kernel/VirtualFileSystem.h +++ b/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); |