1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 22:17:42 +00:00

Kernel: Tidy up TmpFS and TmpFSInode construction

- Use KResultOr<NonnullRefPtr<T>>
- Propagate errors
- Use TRY() at call sites
This commit is contained in:
Andreas Kling 2021-09-06 02:28:38 +02:00
parent 5e61382849
commit a8516681b7
3 changed files with 15 additions and 22 deletions

View file

@ -10,9 +10,9 @@
namespace Kernel { namespace Kernel {
RefPtr<TmpFS> TmpFS::create() KResultOr<NonnullRefPtr<TmpFS>> TmpFS::try_create()
{ {
return adopt_ref_if_nonnull(new (nothrow) TmpFS); return adopt_nonnull_ref_or_enomem(new (nothrow) TmpFS);
} }
TmpFS::TmpFS() TmpFS::TmpFS()
@ -25,9 +25,7 @@ TmpFS::~TmpFS()
KResult TmpFS::initialize() KResult TmpFS::initialize()
{ {
m_root_inode = TmpFSInode::create_root(*this); m_root_inode = TRY(TmpFSInode::try_create_root(*this));
if (!m_root_inode)
return ENOMEM;
return KSuccess; return KSuccess;
} }
@ -84,15 +82,14 @@ TmpFSInode::~TmpFSInode()
{ {
} }
RefPtr<TmpFSInode> TmpFSInode::create(TmpFS& fs, const InodeMetadata& metadata, InodeIdentifier parent) KResultOr<NonnullRefPtr<TmpFSInode>> TmpFSInode::try_create(TmpFS& fs, InodeMetadata const& metadata, InodeIdentifier parent)
{ {
auto inode = adopt_ref_if_nonnull(new (nothrow) TmpFSInode(fs, metadata, parent)); auto inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) TmpFSInode(fs, metadata, parent)));
if (inode) fs.register_inode(inode);
fs.register_inode(*inode);
return inode; return inode;
} }
RefPtr<TmpFSInode> TmpFSInode::create_root(TmpFS& fs) KResultOr<NonnullRefPtr<TmpFSInode>> TmpFSInode::try_create_root(TmpFS& fs)
{ {
InodeMetadata metadata; InodeMetadata metadata;
auto now = kgettimeofday().to_truncated_seconds(); auto now = kgettimeofday().to_truncated_seconds();
@ -100,7 +97,7 @@ RefPtr<TmpFSInode> TmpFSInode::create_root(TmpFS& fs)
metadata.ctime = now; metadata.ctime = now;
metadata.mtime = now; metadata.mtime = now;
metadata.mode = S_IFDIR | S_ISVTX | 0777; metadata.mode = S_IFDIR | S_ISVTX | 0777;
return create(fs, metadata, { fs.fsid(), 1 }); return try_create(fs, metadata, { fs.fsid(), 1 });
} }
InodeMetadata TmpFSInode::metadata() const InodeMetadata TmpFSInode::metadata() const
@ -272,13 +269,9 @@ KResultOr<NonnullRefPtr<Inode>> TmpFSInode::create_child(StringView name, mode_t
metadata.ctime = now; metadata.ctime = now;
metadata.mtime = now; metadata.mtime = now;
auto child = TmpFSInode::create(fs(), metadata, identifier()); auto child = TRY(TmpFSInode::try_create(fs(), metadata, identifier()));
if (!child) TRY(add_child(*child, name, mode));
return ENOMEM; return child;
auto result = add_child(*child, name, mode);
if (result.is_error())
return result;
return child.release_nonnull();
} }
KResult TmpFSInode::add_child(Inode& child, StringView const& name, mode_t) KResult TmpFSInode::add_child(Inode& child, StringView const& name, mode_t)

View file

@ -19,7 +19,7 @@ class TmpFS final : public FileSystem {
public: public:
virtual ~TmpFS() override; virtual ~TmpFS() override;
static RefPtr<TmpFS> create(); static KResultOr<NonnullRefPtr<TmpFS>> try_create();
virtual KResult initialize() override; virtual KResult initialize() override;
virtual StringView class_name() const override { return "TmpFS"sv; } virtual StringView class_name() const override { return "TmpFS"sv; }
@ -71,8 +71,8 @@ public:
private: private:
TmpFSInode(TmpFS& fs, const InodeMetadata& metadata, InodeIdentifier parent); TmpFSInode(TmpFS& fs, const InodeMetadata& metadata, InodeIdentifier parent);
static RefPtr<TmpFSInode> create(TmpFS&, const InodeMetadata& metadata, InodeIdentifier parent); static KResultOr<NonnullRefPtr<TmpFSInode>> try_create(TmpFS&, InodeMetadata const& metadata, InodeIdentifier parent);
static RefPtr<TmpFSInode> create_root(TmpFS&); static KResultOr<NonnullRefPtr<TmpFSInode>> try_create_root(TmpFS&);
void notify_watchers(); void notify_watchers();
struct Child { struct Child {

View file

@ -87,7 +87,7 @@ KResultOr<FlatPtr> Process::sys$mount(Userspace<const Syscall::SC_mount_params*>
} else if (fs_type == "sys"sv || fs_type == "SysFS"sv) { } else if (fs_type == "sys"sv || fs_type == "SysFS"sv) {
fs = SysFS::create(); fs = SysFS::create();
} else if (fs_type == "tmp"sv || fs_type == "TmpFS"sv) { } else if (fs_type == "tmp"sv || fs_type == "TmpFS"sv) {
fs = TmpFS::create(); fs = TRY(TmpFS::try_create());
} else if (fs_type == "iso9660"sv || fs_type == "ISO9660FS"sv) { } else if (fs_type == "iso9660"sv || fs_type == "ISO9660FS"sv) {
if (description_or_error.is_error()) if (description_or_error.is_error())
return EBADF; return EBADF;