aboutsummaryrefslogtreecommitdiff
path: root/kernel/kernel/vfs/driver_registry.hpp
blob: b463e086436fa6cdd19dda3e30d0bffe83333dc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef TEACHOS_KERNEL_VFS_DRIVER_REGISTRY_HPP
#define TEACHOS_KERNEL_VFS_DRIVER_REGISTRY_HPP

#include <kernel/vfs/filesystem.hpp>
#include <kernel/vfs/inode.hpp>

#include <kstd/flat_map.hpp>
#include <kstd/memory.hpp>
#include <kstd/result.hpp>
#include <kstd/string.hpp>
#include <kstd/vector.hpp>

#include <concepts>
#include <optional>
#include <string_view>

namespace kernel::vfs
{

  struct driver_descriptor
  {
    virtual ~driver_descriptor() = default;

    //! Get the name of the driver.
    [[nodiscard]] virtual auto name() const noexcept -> std::string_view = 0;

    //! Create a new instance of the driver represented by this descriptor.
    [[nodiscard]] virtual auto make_instance() const -> kstd::shared_ptr<filesystem> = 0;
  };

  template<std::derived_from<driver_descriptor> Type>
  struct driver_module
  {
    constexpr auto static instance = Type{};
    [[using gnu: section("filesystem_drivers"), used, visibility("hidden")]] constexpr auto static pointer{
      kstd::make_observer<driver_descriptor const>(&instance),
    };
  };

  struct driver_registry
  {
    constexpr driver_registry() noexcept = default;

    auto static init() -> void;

    auto static get() -> driver_registry &;

    //! Register a new filesystem.
    //!
    //! @param name The name of the filesystem.
    //! @param driver The filesystem driver to register.
    //! @return @c true iff. the driver was successfully added, @c false if not.
    auto add(std::string_view name, kstd::shared_ptr<filesystem> driver) -> bool;

    //! Find a registered filesystem driver
    //!
    //! @param name The name of the filesystem
    [[nodiscard]] auto find(std::string_view name) const -> std::optional<kstd::shared_ptr<filesystem>>;

    //! Match all currently registered filesystem drivers against a given inode.
    //!
    //! @param inode The inode to match on, if any.
    //! @return a match priority if th
    [[nodiscard]] auto match(kstd::shared_ptr<inode> const & inode) const -> kstd::result<kstd::shared_ptr<filesystem>>;

  private:
    //! A map from filesystem names to filesystem drivers.
    kstd::flat_map<std::string_view, kstd::shared_ptr<filesystem>> m_drivers{};
  };

}  // namespace kernel::vfs

#endif