diff options
author | Andreas Kling <kling@serenityos.org> | 2021-08-14 13:32:35 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-08-14 13:34:59 +0200 |
commit | ef2720bcad5840a6e174492eb55046e9edacc782 (patch) | |
tree | 81b8bdaf03b1ec32fae5b648f864d4f703077eed /Kernel/FileSystem | |
parent | 459115a59c05ef478b2f99f4089343ef64d65a1c (diff) | |
download | serenity-ef2720bcad5840a6e174492eb55046e9edacc782.zip |
Kernel: Make Inode::lookup() return a KResultOr<NonnullRefPtr<Inode>>
This allows file systems to return arbitrary error codes instead of just
an Inode or not an Inode.
Diffstat (limited to 'Kernel/FileSystem')
-rw-r--r-- | Kernel/FileSystem/DevFS.cpp | 22 | ||||
-rw-r--r-- | Kernel/FileSystem/DevFS.h | 8 | ||||
-rw-r--r-- | Kernel/FileSystem/DevPtsFS.cpp | 11 | ||||
-rw-r--r-- | Kernel/FileSystem/DevPtsFS.h | 2 | ||||
-rw-r--r-- | Kernel/FileSystem/Ext2FileSystem.cpp | 14 | ||||
-rw-r--r-- | Kernel/FileSystem/Ext2FileSystem.h | 2 | ||||
-rw-r--r-- | Kernel/FileSystem/ISO9660FileSystem.cpp | 11 | ||||
-rw-r--r-- | Kernel/FileSystem/ISO9660FileSystem.h | 2 | ||||
-rw-r--r-- | Kernel/FileSystem/Inode.h | 2 | ||||
-rw-r--r-- | Kernel/FileSystem/Plan9FileSystem.cpp | 4 | ||||
-rw-r--r-- | Kernel/FileSystem/Plan9FileSystem.h | 2 | ||||
-rw-r--r-- | Kernel/FileSystem/ProcFS.cpp | 31 | ||||
-rw-r--r-- | Kernel/FileSystem/ProcFS.h | 10 | ||||
-rw-r--r-- | Kernel/FileSystem/SysFS.cpp | 6 | ||||
-rw-r--r-- | Kernel/FileSystem/SysFS.h | 4 | ||||
-rw-r--r-- | Kernel/FileSystem/TmpFS.cpp | 15 | ||||
-rw-r--r-- | Kernel/FileSystem/TmpFS.h | 2 | ||||
-rw-r--r-- | Kernel/FileSystem/VirtualFileSystem.cpp | 7 |
18 files changed, 83 insertions, 72 deletions
diff --git a/Kernel/FileSystem/DevFS.cpp b/Kernel/FileSystem/DevFS.cpp index 23332fa695..ed4c595900 100644 --- a/Kernel/FileSystem/DevFS.cpp +++ b/Kernel/FileSystem/DevFS.cpp @@ -93,7 +93,7 @@ KResult DevFSInode::traverse_as_directory(Function<bool(FileSystem::DirectoryEnt VERIFY_NOT_REACHED(); } -RefPtr<Inode> DevFSInode::lookup(StringView) +KResultOr<NonnullRefPtr<Inode>> DevFSInode::lookup(StringView) { VERIFY_NOT_REACHED(); } @@ -206,15 +206,6 @@ InodeMetadata DevFSDirectoryInode::metadata() const metadata.mtime = mepoch; return metadata; } -KResult DevFSDirectoryInode::traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const -{ - return EINVAL; -} - -RefPtr<Inode> DevFSDirectoryInode::lookup(StringView) -{ - return nullptr; -} DevFSRootDirectoryInode::DevFSRootDirectoryInode(DevFS& fs) : DevFSDirectoryInode(fs) @@ -242,7 +233,8 @@ KResult DevFSRootDirectoryInode::traverse_as_directory(Function<bool(FileSystem: } return KSuccess; } -RefPtr<Inode> DevFSRootDirectoryInode::lookup(StringView name) + +KResultOr<NonnullRefPtr<Inode>> DevFSRootDirectoryInode::lookup(StringView name) { MutexLocker locker(fs().m_lock); for (auto& subdirectory : m_subdirectories) { @@ -259,7 +251,7 @@ RefPtr<Inode> DevFSRootDirectoryInode::lookup(StringView name) return device_node; } } - return nullptr; + return ENOENT; } KResultOr<NonnullRefPtr<Inode>> DevFSRootDirectoryInode::create_child(StringView name, mode_t mode, dev_t, uid_t, gid_t) { @@ -395,10 +387,12 @@ KResult DevFSPtsDirectoryInode::traverse_as_directory(Function<bool(FileSystem:: callback({ "..", identifier(), 0 }); return KSuccess; } -RefPtr<Inode> DevFSPtsDirectoryInode::lookup(StringView) + +KResultOr<NonnullRefPtr<Inode>> DevFSPtsDirectoryInode::lookup(StringView) { - return nullptr; + return ENOENT; } + DevFSPtsDirectoryInode::~DevFSPtsDirectoryInode() { } diff --git a/Kernel/FileSystem/DevFS.h b/Kernel/FileSystem/DevFS.h index 557804e671..0f81af6394 100644 --- a/Kernel/FileSystem/DevFS.h +++ b/Kernel/FileSystem/DevFS.h @@ -55,7 +55,7 @@ protected: DevFSInode(DevFS&); virtual KResultOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, FileDescription*) const override; virtual KResult traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const override; - virtual RefPtr<Inode> lookup(StringView name) override; + virtual KResultOr<NonnullRefPtr<Inode>> lookup(StringView name) override; virtual void flush_metadata() override; virtual KResultOr<size_t> write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, FileDescription*) override; virtual KResultOr<NonnullRefPtr<Inode>> create_child(StringView name, mode_t, dev_t, uid_t, gid_t) override; @@ -119,8 +119,6 @@ protected: DevFSDirectoryInode(DevFS&); // ^Inode virtual InodeMetadata metadata() const override; - virtual KResult traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const override; - virtual RefPtr<Inode> lookup(StringView name) override; NonnullRefPtrVector<DevFSDeviceInode> m_devices; }; @@ -136,7 +134,7 @@ public: private: explicit DevFSPtsDirectoryInode(DevFS&); virtual KResult traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const override; - virtual RefPtr<Inode> lookup(StringView name) override; + virtual KResultOr<NonnullRefPtr<Inode>> lookup(StringView name) override; virtual InodeMetadata metadata() const override; }; @@ -151,7 +149,7 @@ private: explicit DevFSRootDirectoryInode(DevFS&); virtual KResultOr<NonnullRefPtr<Inode>> create_child(StringView name, mode_t, dev_t, uid_t, gid_t) override; virtual KResult traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const override; - virtual RefPtr<Inode> lookup(StringView name) override; + virtual KResultOr<NonnullRefPtr<Inode>> lookup(StringView name) override; virtual InodeMetadata metadata() const override; NonnullRefPtrVector<DevFSDirectoryInode> m_subdirectories; diff --git a/Kernel/FileSystem/DevPtsFS.cpp b/Kernel/FileSystem/DevPtsFS.cpp index 80fc243211..ee5c2a4b46 100644 --- a/Kernel/FileSystem/DevPtsFS.cpp +++ b/Kernel/FileSystem/DevPtsFS.cpp @@ -136,21 +136,24 @@ KResult DevPtsFSInode::traverse_as_directory(Function<bool(FileSystem::Directory return KSuccess; } -RefPtr<Inode> DevPtsFSInode::lookup(StringView name) +KResultOr<NonnullRefPtr<Inode>> DevPtsFSInode::lookup(StringView name) { VERIFY(identifier().index() == 1); if (name == "." || name == "..") - return this; + return *this; auto& fs = static_cast<DevPtsFS&>(this->fs()); auto pty_index = name.to_uint(); if (pty_index.has_value() && s_ptys->contains(pty_index.value())) { - return fs.get_inode({ fsid(), pty_index_to_inode_index(pty_index.value()) }); + auto inode = fs.get_inode({ fsid(), pty_index_to_inode_index(pty_index.value()) }); + if (!inode) + return ENOENT; + return inode.release_nonnull(); } - return {}; + return ENOENT; } void DevPtsFSInode::flush_metadata() diff --git a/Kernel/FileSystem/DevPtsFS.h b/Kernel/FileSystem/DevPtsFS.h index 1fe8bad9a7..68c539bb42 100644 --- a/Kernel/FileSystem/DevPtsFS.h +++ b/Kernel/FileSystem/DevPtsFS.h @@ -50,7 +50,7 @@ private: virtual KResultOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, FileDescription*) const override; virtual InodeMetadata metadata() const override; virtual KResult traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const override; - virtual RefPtr<Inode> lookup(StringView name) override; + virtual KResultOr<NonnullRefPtr<Inode>> lookup(StringView name) override; virtual void flush_metadata() override; virtual KResultOr<size_t> write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, FileDescription*) override; virtual KResultOr<NonnullRefPtr<Inode>> create_child(StringView name, mode_t, dev_t, uid_t, gid_t) override; diff --git a/Kernel/FileSystem/Ext2FileSystem.cpp b/Kernel/FileSystem/Ext2FileSystem.cpp index 20876dc21d..b34582c934 100644 --- a/Kernel/FileSystem/Ext2FileSystem.cpp +++ b/Kernel/FileSystem/Ext2FileSystem.cpp @@ -1630,12 +1630,12 @@ KResult Ext2FSInode::populate_lookup_cache() const return KSuccess; } -RefPtr<Inode> Ext2FSInode::lookup(StringView name) +KResultOr<NonnullRefPtr<Inode>> Ext2FSInode::lookup(StringView name) { VERIFY(is_directory()); dbgln_if(EXT2_DEBUG, "Ext2FSInode[{}]:lookup(): Looking up '{}'", identifier(), name); - if (populate_lookup_cache().is_error()) - return {}; + if (auto result = populate_lookup_cache(); result.is_error()) + return result; InodeIndex inode_index; { @@ -1643,11 +1643,15 @@ RefPtr<Inode> Ext2FSInode::lookup(StringView name) auto it = m_lookup_cache.find(name.hash(), [&](auto& entry) { return entry.key == name; }); if (it == m_lookup_cache.end()) { dbgln_if(EXT2_DEBUG, "Ext2FSInode[{}]:lookup(): '{}' not found", identifier(), name); - return {}; + return ENOENT; } inode_index = it->value; } - return fs().get_inode({ fsid(), inode_index }); + + auto inode = fs().get_inode({ fsid(), inode_index }); + if (!inode) + return ENOENT; + return inode.release_nonnull(); } void Ext2FSInode::one_ref_left() diff --git a/Kernel/FileSystem/Ext2FileSystem.h b/Kernel/FileSystem/Ext2FileSystem.h index d3f1161087..b434f63a15 100644 --- a/Kernel/FileSystem/Ext2FileSystem.h +++ b/Kernel/FileSystem/Ext2FileSystem.h @@ -41,7 +41,7 @@ private: virtual KResultOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, FileDescription*) const override; virtual InodeMetadata metadata() const override; virtual KResult traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const override; - virtual RefPtr<Inode> lookup(StringView name) override; + virtual KResultOr<NonnullRefPtr<Inode>> lookup(StringView name) override; virtual void flush_metadata() override; virtual KResultOr<size_t> write_bytes(off_t, size_t, const UserOrKernelBuffer& data, FileDescription*) override; virtual KResultOr<NonnullRefPtr<Inode>> create_child(StringView name, mode_t, dev_t, uid_t, gid_t) override; diff --git a/Kernel/FileSystem/ISO9660FileSystem.cpp b/Kernel/FileSystem/ISO9660FileSystem.cpp index 41e1e47600..3d2b852f0c 100644 --- a/Kernel/FileSystem/ISO9660FileSystem.cpp +++ b/Kernel/FileSystem/ISO9660FileSystem.cpp @@ -532,7 +532,7 @@ KResult ISO9660Inode::traverse_as_directory(Function<bool(FileSystem::DirectoryE return KSuccess; } -RefPtr<Inode> ISO9660Inode::lookup(StringView name) +KResultOr<NonnullRefPtr<Inode>> ISO9660Inode::lookup(StringView name) { auto& file_system = static_cast<ISO9660FS const&>(fs()); RefPtr<Inode> inode; @@ -558,11 +558,12 @@ RefPtr<Inode> ISO9660Inode::lookup(StringView name) return RecursionDecision::Continue; }); - if (traversal_result.is_error()) { - return {}; - } + if (traversal_result.is_error()) + return traversal_result; - return inode; + if (!inode) + return ENOENT; + return inode.release_nonnull(); } void ISO9660Inode::flush_metadata() diff --git a/Kernel/FileSystem/ISO9660FileSystem.h b/Kernel/FileSystem/ISO9660FileSystem.h index 65a160cd44..e4a46d3f32 100644 --- a/Kernel/FileSystem/ISO9660FileSystem.h +++ b/Kernel/FileSystem/ISO9660FileSystem.h @@ -351,7 +351,7 @@ public: virtual KResultOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, FileDescription*) const override; virtual InodeMetadata metadata() const override; virtual KResult traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const override; - virtual RefPtr<Inode> lookup(StringView name) override; + virtual KResultOr<NonnullRefPtr<Inode>> lookup(StringView name) override; virtual void flush_metadata() override; virtual KResultOr<size_t> write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, FileDescription*) override; virtual KResultOr<NonnullRefPtr<Inode>> create_child(StringView name, mode_t, dev_t, uid_t, gid_t) override; diff --git a/Kernel/FileSystem/Inode.h b/Kernel/FileSystem/Inode.h index c4c49909bd..cf77df7507 100644 --- a/Kernel/FileSystem/Inode.h +++ b/Kernel/FileSystem/Inode.h @@ -53,7 +53,7 @@ public: virtual void did_seek(FileDescription&, off_t) { } virtual KResultOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, FileDescription*) const = 0; virtual KResult traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const = 0; - virtual RefPtr<Inode> lookup(StringView name) = 0; + virtual KResultOr<NonnullRefPtr<Inode>> lookup(StringView name) = 0; virtual KResultOr<size_t> write_bytes(off_t, size_t, const UserOrKernelBuffer& data, FileDescription*) = 0; virtual KResultOr<NonnullRefPtr<Inode>> create_child(StringView name, mode_t, dev_t, uid_t, gid_t) = 0; virtual KResult add_child(Inode&, const StringView& name, mode_t) = 0; diff --git a/Kernel/FileSystem/Plan9FileSystem.cpp b/Kernel/FileSystem/Plan9FileSystem.cpp index 729182f39b..657ba67a5e 100644 --- a/Kernel/FileSystem/Plan9FileSystem.cpp +++ b/Kernel/FileSystem/Plan9FileSystem.cpp @@ -914,7 +914,7 @@ KResult Plan9FSInode::traverse_as_directory(Function<bool(FileSystem::DirectoryE } } -RefPtr<Inode> Plan9FSInode::lookup(StringView name) +KResultOr<NonnullRefPtr<Inode>> Plan9FSInode::lookup(StringView name) { u32 newfid = fs().allocate_fid(); Plan9FS::Message message { fs(), Plan9FS::Message::Type::Twalk }; @@ -922,7 +922,7 @@ RefPtr<Inode> Plan9FSInode::lookup(StringView name) auto result = fs().post_message_and_wait_for_a_reply(message); if (result.is_error()) - return nullptr; + return result; return Plan9FSInode::create(fs(), newfid); } diff --git a/Kernel/FileSystem/Plan9FileSystem.h b/Kernel/FileSystem/Plan9FileSystem.h index 3dbc699b0e..9301fca091 100644 --- a/Kernel/FileSystem/Plan9FileSystem.h +++ b/Kernel/FileSystem/Plan9FileSystem.h @@ -159,7 +159,7 @@ public: virtual KResultOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, FileDescription*) const override; virtual KResultOr<size_t> write_bytes(off_t, size_t, const UserOrKernelBuffer& data, FileDescription*) override; virtual KResult traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const override; - virtual RefPtr<Inode> lookup(StringView name) override; + virtual KResultOr<NonnullRefPtr<Inode>> lookup(StringView name) override; virtual KResultOr<NonnullRefPtr<Inode>> create_child(StringView name, mode_t, dev_t, uid_t, gid_t) override; virtual KResult add_child(Inode&, const StringView& name, mode_t) override; virtual KResult remove_child(const StringView& name) override; diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp index e18fb1e92a..59c0864fe8 100644 --- a/Kernel/FileSystem/ProcFS.cpp +++ b/Kernel/FileSystem/ProcFS.cpp @@ -146,7 +146,7 @@ KResult ProcFSGlobalInode::traverse_as_directory(Function<bool(FileSystem::Direc VERIFY_NOT_REACHED(); } -RefPtr<Inode> ProcFSGlobalInode::lookup(StringView) +KResultOr<NonnullRefPtr<Inode>> ProcFSGlobalInode::lookup(StringView) { VERIFY_NOT_REACHED(); } @@ -200,12 +200,12 @@ KResult ProcFSDirectoryInode::traverse_as_directory(Function<bool(FileSystem::Di return m_associated_component->traverse_as_directory(procfs().fsid(), move(callback)); } -RefPtr<Inode> ProcFSDirectoryInode::lookup(StringView name) +KResultOr<NonnullRefPtr<Inode>> ProcFSDirectoryInode::lookup(StringView name) { MutexLocker locker(procfs().m_lock); auto component = m_associated_component->lookup(name); if (!component) - return {}; + return ENOMEM; return component->to_inode(procfs()); } @@ -288,12 +288,12 @@ KResult ProcFSProcessDirectoryInode::traverse_as_directory(Function<bool(FileSys return process->traverse_as_directory(procfs().fsid(), move(callback)); } -RefPtr<Inode> ProcFSProcessDirectoryInode::lookup(StringView name) +KResultOr<NonnullRefPtr<Inode>> ProcFSProcessDirectoryInode::lookup(StringView name) { MutexLocker locker(procfs().m_lock); auto process = Process::from_pid(associated_pid()); if (!process) - return nullptr; + return ESRCH; if (name == "fd") return ProcFSProcessSubDirectoryInode::create(procfs(), SegmentedProcFSIndex::ProcessSubDirectory::FileDescriptions, associated_pid()); if (name == "stacks") @@ -314,7 +314,7 @@ RefPtr<Inode> ProcFSProcessDirectoryInode::lookup(StringView name) return ProcFSProcessPropertyInode::create_for_pid_property(procfs(), SegmentedProcFSIndex::MainProcessProperty::VirtualMemoryStats, associated_pid()); if (name == "root") return ProcFSProcessPropertyInode::create_for_pid_property(procfs(), SegmentedProcFSIndex::MainProcessProperty::RootLink, associated_pid()); - return nullptr; + return ENOENT; } NonnullRefPtr<ProcFSProcessSubDirectoryInode> ProcFSProcessSubDirectoryInode::create(const ProcFS& procfs, SegmentedProcFSIndex::ProcessSubDirectory sub_directory_type, ProcessID pid) @@ -376,21 +376,26 @@ KResult ProcFSProcessSubDirectoryInode::traverse_as_directory(Function<bool(File VERIFY_NOT_REACHED(); } -RefPtr<Inode> ProcFSProcessSubDirectoryInode::lookup(StringView name) +KResultOr<NonnullRefPtr<Inode>> ProcFSProcessSubDirectoryInode::lookup(StringView name) { MutexLocker locker(procfs().m_lock); auto process = Process::from_pid(associated_pid()); if (!process) - return {}; + return ESRCH; + RefPtr<Inode> inode; switch (m_sub_directory_type) { case SegmentedProcFSIndex::ProcessSubDirectory::FileDescriptions: - return process->lookup_file_descriptions_directory(procfs(), name); + inode = process->lookup_file_descriptions_directory(procfs(), name); + break; case SegmentedProcFSIndex::ProcessSubDirectory::Stacks: - return process->lookup_stacks_directory(procfs(), name); + inode = process->lookup_stacks_directory(procfs(), name); + break; default: VERIFY_NOT_REACHED(); } - VERIFY_NOT_REACHED(); + if (!inode) + return ENOENT; + return inode.release_nonnull(); } NonnullRefPtr<ProcFSProcessPropertyInode> ProcFSProcessPropertyInode::create_for_file_description_link(const ProcFS& procfs, unsigned file_description_index, ProcessID pid) @@ -514,9 +519,9 @@ KResultOr<size_t> ProcFSProcessPropertyInode::read_bytes(off_t offset, size_t co return nread; } -RefPtr<Inode> ProcFSProcessPropertyInode::lookup(StringView) +KResultOr<NonnullRefPtr<Inode>> ProcFSProcessPropertyInode::lookup(StringView) { - VERIFY_NOT_REACHED(); + return EINVAL; } static KResult build_from_cached_data(KBufferBuilder& builder, ProcFSInodeData& cached_data) diff --git a/Kernel/FileSystem/ProcFS.h b/Kernel/FileSystem/ProcFS.h index 49b4d89b44..d03eb57c71 100644 --- a/Kernel/FileSystem/ProcFS.h +++ b/Kernel/FileSystem/ProcFS.h @@ -83,7 +83,7 @@ protected: virtual void did_seek(FileDescription&, off_t) override final; virtual InodeMetadata metadata() const override; virtual KResult traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const override; - virtual RefPtr<Inode> lookup(StringView) override; + virtual KResultOr<NonnullRefPtr<Inode>> lookup(StringView) override; NonnullRefPtr<ProcFSExposedComponent> m_associated_component; }; @@ -111,7 +111,7 @@ protected: // ^Inode virtual InodeMetadata metadata() const override; virtual KResult traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const override; - virtual RefPtr<Inode> lookup(StringView name) override; + virtual KResultOr<NonnullRefPtr<Inode>> lookup(StringView name) override; }; class ProcFSProcessAssociatedInode : public ProcFSInode { @@ -142,7 +142,7 @@ private: virtual InodeMetadata metadata() const override; virtual KResult traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const override; virtual KResultOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, FileDescription*) const override final; - virtual RefPtr<Inode> lookup(StringView name) override; + virtual KResultOr<NonnullRefPtr<Inode>> lookup(StringView name) override; }; class ProcFSProcessSubDirectoryInode final : public ProcFSProcessAssociatedInode { @@ -159,7 +159,7 @@ private: virtual InodeMetadata metadata() const override; virtual KResult traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const override; virtual KResultOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, FileDescription*) const override final; - virtual RefPtr<Inode> lookup(StringView name) override; + virtual KResultOr<NonnullRefPtr<Inode>> lookup(StringView name) override; const SegmentedProcFSIndex::ProcessSubDirectory m_sub_directory_type; }; @@ -182,7 +182,7 @@ private: virtual InodeMetadata metadata() const override; virtual KResult traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const override; virtual KResultOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, FileDescription*) const override final; - virtual RefPtr<Inode> lookup(StringView name) override final; + virtual KResultOr<NonnullRefPtr<Inode>> lookup(StringView name) override final; KResult refresh_data(FileDescription& description); KResult try_to_acquire_data(Process& process, KBufferBuilder& builder) const; diff --git a/Kernel/FileSystem/SysFS.cpp b/Kernel/FileSystem/SysFS.cpp index 677ba4baee..77488554c5 100644 --- a/Kernel/FileSystem/SysFS.cpp +++ b/Kernel/FileSystem/SysFS.cpp @@ -106,7 +106,7 @@ KResult SysFSInode::traverse_as_directory(Function<bool(FileSystem::DirectoryEnt VERIFY_NOT_REACHED(); } -RefPtr<Inode> SysFSInode::lookup(StringView) +KResultOr<NonnullRefPtr<Inode>> SysFSInode::lookup(StringView) { VERIFY_NOT_REACHED(); } @@ -195,12 +195,12 @@ KResult SysFSDirectoryInode::traverse_as_directory(Function<bool(FileSystem::Dir return m_associated_component->traverse_as_directory(fs().fsid(), move(callback)); } -RefPtr<Inode> SysFSDirectoryInode::lookup(StringView name) +KResultOr<NonnullRefPtr<Inode>> SysFSDirectoryInode::lookup(StringView name) { MutexLocker locker(fs().m_lock); auto component = m_associated_component->lookup(name); if (!component) - return {}; + return ENOENT; return component->to_inode(fs()); } diff --git a/Kernel/FileSystem/SysFS.h b/Kernel/FileSystem/SysFS.h index d52d6e77f3..836a0220c9 100644 --- a/Kernel/FileSystem/SysFS.h +++ b/Kernel/FileSystem/SysFS.h @@ -85,7 +85,7 @@ protected: SysFSInode(SysFS const&, SysFSComponent const&); virtual KResultOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, FileDescription*) const override; virtual KResult traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const override; - virtual RefPtr<Inode> lookup(StringView name) override; + virtual KResultOr<NonnullRefPtr<Inode>> lookup(StringView name) override; virtual void flush_metadata() override; virtual InodeMetadata metadata() const override; virtual KResultOr<size_t> write_bytes(off_t, size_t, UserOrKernelBuffer const&, FileDescription*) override; @@ -114,7 +114,7 @@ protected: // ^Inode virtual InodeMetadata metadata() const override; virtual KResult traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const override; - virtual RefPtr<Inode> lookup(StringView name) override; + virtual KResultOr<NonnullRefPtr<Inode>> lookup(StringView name) override; }; } diff --git a/Kernel/FileSystem/TmpFS.cpp b/Kernel/FileSystem/TmpFS.cpp index 2cebe79013..ba62609c79 100644 --- a/Kernel/FileSystem/TmpFS.cpp +++ b/Kernel/FileSystem/TmpFS.cpp @@ -193,19 +193,24 @@ KResultOr<size_t> TmpFSInode::write_bytes(off_t offset, size_t size, const UserO return size; } -RefPtr<Inode> TmpFSInode::lookup(StringView name) +KResultOr<NonnullRefPtr<Inode>> TmpFSInode::lookup(StringView name) { MutexLocker locker(m_inode_lock, Mutex::Mode::Shared); VERIFY(is_directory()); if (name == ".") - return this; - if (name == "..") - return fs().get_inode(m_parent); + return *this; + if (name == "..") { + auto inode = fs().get_inode(m_parent); + // FIXME: If this cannot fail, we should probably VERIFY here instead. + if (!inode) + return ENOENT; + return inode.release_nonnull(); + } auto* child = find_child_by_name(name); if (!child) - return {}; + return ENOENT; return child->inode; } diff --git a/Kernel/FileSystem/TmpFS.h b/Kernel/FileSystem/TmpFS.h index 31edcbae3f..9aa96aea18 100644 --- a/Kernel/FileSystem/TmpFS.h +++ b/Kernel/FileSystem/TmpFS.h @@ -55,7 +55,7 @@ public: virtual KResultOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, FileDescription*) const override; virtual InodeMetadata metadata() const override; virtual KResult traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const override; - virtual RefPtr<Inode> lookup(StringView name) override; + virtual KResultOr<NonnullRefPtr<Inode>> lookup(StringView name) override; virtual void flush_metadata() override; virtual KResultOr<size_t> write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, FileDescription*) override; virtual KResultOr<NonnullRefPtr<Inode>> create_child(StringView name, mode_t, dev_t, uid_t, gid_t) override; diff --git a/Kernel/FileSystem/VirtualFileSystem.cpp b/Kernel/FileSystem/VirtualFileSystem.cpp index 4a0152918e..df338469ce 100644 --- a/Kernel/FileSystem/VirtualFileSystem.cpp +++ b/Kernel/FileSystem/VirtualFileSystem.cpp @@ -965,16 +965,17 @@ KResultOr<NonnullRefPtr<Custody>> VirtualFileSystem::resolve_path_without_veil(S } // Okay, let's look up this part. - auto child_inode = parent.inode().lookup(part); - if (!child_inode) { + auto child_or_error = parent.inode().lookup(part); + if (child_or_error.is_error()) { if (out_parent) { // ENOENT with a non-null parent custody signals to caller that // we found the immediate parent of the file, but the file itself // does not exist yet. *out_parent = have_more_parts ? nullptr : &parent; } - return ENOENT; + return child_or_error.error(); } + auto child_inode = child_or_error.release_value(); int mount_flags_for_child = parent.mount_flags(); |