Echo Writes Code

fs.hpp

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifndef CRUCIBLE_UNIX_FS_HPP
#define CRUCIBLE_UNIX_FS_HPP

#include "crucible/core/constant_error.inl"
#include "crucible/core/error_chain.inl"
#include "crucible/core/none.hpp"
#include "crucible/core/result.inl"
#include "crucible/unix/errno_error.inl"

#include <cstddef>
#include <set>
#include <string>
#include <vector>

namespace crucible::unix::fs {
  struct FStatFailed final : errno_error::ErrnoError<FStatFailed> {
    static constexpr char const function[] = "fstat";
  };

  struct MkdirFailed final : errno_error::ErrnoError<MkdirFailed> {
    static constexpr char const function[] = "mkdir";
  };

  struct OpenFailed final : errno_error::ErrnoError<OpenFailed> {
    static constexpr char const function[] = "open";
  };

  struct OpenDirFailed final : errno_error::ErrnoError<OpenDirFailed> {
    static constexpr char const function[] = "opendir";
  };

  struct ReadFailed final : errno_error::ErrnoError<ReadFailed> {
    static constexpr char const function[] = "read";
  };

  struct ReadDirFailed final : errno_error::ErrnoError<ReadDirFailed> {
    static constexpr char const function[] = "readdir";
  };

  struct RmdirFailed final : errno_error::ErrnoError<RmdirFailed> {
    static constexpr char const function[] = "rmdir";
  };

  struct StatFailed final : errno_error::ErrnoError<StatFailed> {
    static constexpr char const function[] = "stat";
  };

  struct UnlinkFailed final : errno_error::ErrnoError<UnlinkFailed> {
    static constexpr char const function[] = "unlink";
  };

  struct WriteFailed final : errno_error::ErrnoError<WriteFailed> {
    static constexpr char const function[] = "write";
  };

  struct IncompleteRead final : core::constant_error::ConstantError<IncompleteRead> {
    static constexpr char const message[] = "Incomplete read";
  };

  struct IncompleteWrite final : core::constant_error::ConstantError<IncompleteWrite> {
    static constexpr char const message[] = "Incomplete write";
  };

  struct NegativeFileSize final : core::constant_error::ConstantError<NegativeFileSize> {
    static constexpr char const message[] = "Negative file size";
  };

  struct FSError final : core::error_chain::ErrorChain<FSError, FStatFailed, MkdirFailed, OpenFailed, OpenDirFailed, ReadFailed, ReadDirFailed, RmdirFailed, StatFailed, UnlinkFailed, WriteFailed, IncompleteRead, IncompleteWrite, NegativeFileSize> {
    using ErrorChain::ErrorChain;

    static constexpr char const message[] = "Filesystem error";
  };

  template<typename T>
  using FSResult = core::result::Result<FSError, T>;

  enum class FilePermission {
    OwnerCanRead,
    OwnerCanWrite,
    OwnerCanExecute,
    GroupCanRead,
    GroupCanWrite,
    GroupCanExecute,
    WorldCanRead,
    WorldCanWrite,
    WorldCanExecute,
    SetUserID,
    SetGroupID,
    Sticky,
  };

  [[nodiscard]]
  auto create_directory(std::string const &path) -> FSResult<core::none::None>;

  [[nodiscard]]
  auto create_directory(std::string const &path, std::set<FilePermission> const &permissions) -> FSResult<core::none::None>;

  [[nodiscard]]
  auto create_file(std::string const &path, std::vector<std::byte> const &buffer) -> FSResult<core::none::None>;

  [[nodiscard]]
  auto create_file(std::string const &path, std::vector<std::byte> const &buffer, std::set<FilePermission> const &permissions) -> FSResult<core::none::None>;

  [[nodiscard]]
  auto delete_directory(std::string const &path) -> FSResult<core::none::None>;

  [[nodiscard]]
  auto delete_file(std::string const &path) -> FSResult<core::none::None>;

  [[nodiscard]]
  auto is_directory(std::string const &path) -> FSResult<bool>;

  [[nodiscard]]
  auto is_file(std::string const &path) -> FSResult<bool>;

  [[nodiscard]]
  auto read_directory(std::string const &path) -> FSResult<std::vector<std::string>>;

  [[nodiscard]]
  auto read_file_data(std::string const &path) -> FSResult<std::vector<std::byte>>;

  [[nodiscard]]
  auto read_file_size(std::string const &path) -> FSResult<std::size_t>;
}

#endif // CRUCIBLE_UNIX_FS_HPP