1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 05:48:12 +00:00

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

This commit is contained in:
Andreas Kling 2019-03-06 22:30:13 +01:00
parent 028afabf6b
commit e56fe71dbc
7 changed files with 56 additions and 51 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -355,25 +355,6 @@ KResultOr<Retained<Inode>> VFS::resolve_path_to_inode(const String& path, Inode&
return Retained<Inode>(*get_inode(result.value())); 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) 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); 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); 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); auto inode = get_inode(inode_id);
if (!inode) if (!inode)
return { }; return KResult(-EIO);
return absolute_path(*inode); return absolute_path(*inode);
} }
String VFS::absolute_path(Inode& core_inode) KResultOr<String> VFS::absolute_path(Inode& core_inode)
{ {
int error;
Vector<InodeIdentifier> lineage; Vector<InodeIdentifier> lineage;
RetainPtr<Inode> inode = &core_inode; RetainPtr<Inode> inode = &core_inode;
while (inode->identifier() != root_inode_id()) { while (inode->identifier() != root_inode_id()) {
@ -518,11 +498,15 @@ String VFS::absolute_path(Inode& core_inode)
InodeIdentifier parent_id; InodeIdentifier parent_id;
if (inode->is_directory()) { 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 { } else {
parent_id = inode->parent()->identifier(); parent_id = inode->parent()->identifier();
} }
ASSERT(parent_id.is_valid()); if (!parent_id.is_valid())
return KResult(-EIO);
inode = get_inode(parent_id); inode = get_inode(parent_id);
} }
if (lineage.is_empty()) if (lineage.is_empty())

View file

@ -84,8 +84,8 @@ public:
size_t mount_count() const { return m_mounts.size(); } size_t mount_count() const { return m_mounts.size(); }
void for_each_mount(Function<void(const Mount&)>) const; void for_each_mount(Function<void(const Mount&)>) const;
String absolute_path(Inode&); KResultOr<String> absolute_path(Inode&);
String absolute_path(InodeIdentifier); KResultOr<String> absolute_path(InodeIdentifier);
InodeIdentifier root_inode_id() const; InodeIdentifier root_inode_id() const;
Inode* root_inode() { return m_root_inode.ptr(); } Inode* root_inode() { return m_root_inode.ptr(); }
@ -105,7 +105,6 @@ private:
void traverse_directory_inode(Inode&, Function<bool(const FS::DirectoryEntry&)>); 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); 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); 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<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); KResultOr<InodeIdentifier> resolve_symbolic_link(InodeIdentifier base, Inode& symlink_inode);