2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2020-01-24 16:45:29 +03:00
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-08-15 18:16:45 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Kernel/FileSystem/FileSystem.h>
|
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
|
|
|
#include <Kernel/KBuffer.h>
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-08-15 18:16:45 +03:00
|
|
|
class TmpFSInode;
|
|
|
|
|
2021-07-11 00:20:38 +02:00
|
|
|
class TmpFS final : public FileSystem {
|
2019-08-15 18:16:45 +03:00
|
|
|
friend class TmpFSInode;
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~TmpFS() override;
|
2021-11-08 00:51:39 +01:00
|
|
|
static ErrorOr<NonnullRefPtr<TmpFS>> try_create();
|
|
|
|
virtual ErrorOr<void> initialize() override;
|
2019-08-15 18:16:45 +03:00
|
|
|
|
2021-07-17 20:59:06 +02:00
|
|
|
virtual StringView class_name() const override { return "TmpFS"sv; }
|
2019-08-15 18:16:45 +03:00
|
|
|
|
2019-12-15 19:33:39 +01:00
|
|
|
virtual bool supports_watchers() const override { return true; }
|
|
|
|
|
2021-07-18 01:50:47 +02:00
|
|
|
virtual Inode& root_inode() override;
|
2019-08-15 18:16:45 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
TmpFS();
|
|
|
|
|
|
|
|
RefPtr<TmpFSInode> m_root_inode;
|
|
|
|
|
2022-01-11 00:51:05 +01:00
|
|
|
HashMap<InodeIndex, TmpFSInode*> m_inodes;
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<NonnullRefPtr<Inode>> get_inode(InodeIdentifier identifier) const;
|
2019-08-15 18:16:45 +03:00
|
|
|
void register_inode(TmpFSInode&);
|
|
|
|
void unregister_inode(InodeIdentifier);
|
|
|
|
|
|
|
|
unsigned m_next_inode_index { 1 };
|
|
|
|
unsigned next_inode_index();
|
|
|
|
};
|
|
|
|
|
|
|
|
class TmpFSInode final : public Inode {
|
|
|
|
friend class TmpFS;
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~TmpFSInode() override;
|
|
|
|
|
|
|
|
TmpFS& fs() { return static_cast<TmpFS&>(Inode::fs()); }
|
|
|
|
const TmpFS& fs() const { return static_cast<const TmpFS&>(Inode::fs()); }
|
|
|
|
|
|
|
|
// ^Inode
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override;
|
2019-08-15 18:16:45 +03:00
|
|
|
virtual InodeMetadata metadata() const override;
|
2021-11-10 15:42:39 +01:00
|
|
|
virtual ErrorOr<void> traverse_as_directory(Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const override;
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<NonnullRefPtr<Inode>> lookup(StringView name) override;
|
|
|
|
virtual ErrorOr<void> flush_metadata() override;
|
|
|
|
virtual ErrorOr<size_t> write_bytes(off_t, size_t, const UserOrKernelBuffer& buffer, OpenFileDescription*) override;
|
|
|
|
virtual ErrorOr<NonnullRefPtr<Inode>> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override;
|
2021-11-11 00:55:02 +01:00
|
|
|
virtual ErrorOr<void> add_child(Inode&, StringView name, mode_t) override;
|
|
|
|
virtual ErrorOr<void> remove_child(StringView name) override;
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<void> chmod(mode_t) override;
|
|
|
|
virtual ErrorOr<void> chown(UserID, GroupID) override;
|
|
|
|
virtual ErrorOr<void> truncate(u64) override;
|
|
|
|
virtual ErrorOr<void> set_atime(time_t) override;
|
|
|
|
virtual ErrorOr<void> set_ctime(time_t) override;
|
|
|
|
virtual ErrorOr<void> set_mtime(time_t) override;
|
2022-01-11 00:51:05 +01:00
|
|
|
virtual void remove_from_secondary_lists() override;
|
2019-08-15 18:16:45 +03:00
|
|
|
|
|
|
|
private:
|
2021-08-31 22:15:44 -07:00
|
|
|
TmpFSInode(TmpFS& fs, const InodeMetadata& metadata, InodeIdentifier parent);
|
2021-11-08 00:51:39 +01:00
|
|
|
static ErrorOr<NonnullRefPtr<TmpFSInode>> try_create(TmpFS&, InodeMetadata const& metadata, InodeIdentifier parent);
|
|
|
|
static ErrorOr<NonnullRefPtr<TmpFSInode>> try_create_root(TmpFS&);
|
2020-06-24 23:35:56 +03:00
|
|
|
|
2021-07-18 13:47:18 +02:00
|
|
|
struct Child {
|
|
|
|
NonnullOwnPtr<KString> name;
|
|
|
|
NonnullRefPtr<TmpFSInode> inode;
|
|
|
|
IntrusiveListNode<Child> list_node {};
|
2021-09-09 16:30:59 +04:30
|
|
|
using List = IntrusiveList<&Child::list_node>;
|
2021-07-18 13:47:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Child* find_child_by_name(StringView);
|
|
|
|
|
2019-08-15 18:16:45 +03:00
|
|
|
InodeMetadata m_metadata;
|
|
|
|
InodeIdentifier m_parent;
|
|
|
|
|
2020-12-18 13:34:57 +01:00
|
|
|
OwnPtr<KBuffer> m_content;
|
2021-07-18 13:47:18 +02:00
|
|
|
|
|
|
|
Child::List m_children;
|
2019-08-15 18:16:45 +03:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
}
|