contur2
Loading...
Searching...
No Matches
file_descriptor.h
Go to the documentation of this file.
1
3
4#pragma once
5
6#include <cstdint>
7#include <memory>
8
9#include "contur/core/error.h"
10
11#include "contur/fs/inode.h"
12
13namespace contur {
14
17 {
19 std::int32_t value = -1;
20
23 [[nodiscard]] constexpr bool valid() const noexcept
24 {
25 return value >= 0;
26 }
27
32 friend constexpr bool operator==(FileDescriptor lhs, FileDescriptor rhs) noexcept
33 {
34 return lhs.value == rhs.value;
35 }
36 };
37
39 enum class OpenMode : std::uint8_t
40 {
41 None = 0,
42 Read = 1U << 0,
43 Write = 1U << 1,
44 Create = 1U << 2,
45 Truncate = 1U << 3,
46 Append = 1U << 4,
47 };
48
50 [[nodiscard]] constexpr OpenMode operator|(OpenMode lhs, OpenMode rhs) noexcept
51 {
52 return static_cast<OpenMode>(static_cast<std::uint8_t>(lhs) | static_cast<std::uint8_t>(rhs));
53 }
54
56 [[nodiscard]] constexpr OpenMode operator&(OpenMode lhs, OpenMode rhs) noexcept
57 {
58 return static_cast<OpenMode>(static_cast<std::uint8_t>(lhs) & static_cast<std::uint8_t>(rhs));
59 }
60
65 [[nodiscard]] constexpr bool hasOpenMode(OpenMode value, OpenMode flag) noexcept
66 {
67 return static_cast<std::uint8_t>(value & flag) != 0;
68 }
69
82
85 {
86 public:
89
94
98 [[nodiscard]] Result<FileDescriptor> open(OpenFileState state);
99
103 [[nodiscard]] Result<void> close(FileDescriptor fd);
104
108 [[nodiscard]] Result<OpenFileState> get(FileDescriptor fd) const;
109
114 [[nodiscard]] Result<void> set(FileDescriptor fd, OpenFileState state);
115
119 [[nodiscard]] bool contains(FileDescriptor fd) const noexcept;
120
123 [[nodiscard]] std::size_t openCount() const noexcept;
124
125 private:
126 struct Impl;
127 std::unique_ptr<Impl> impl_;
128 };
129
130} // namespace contur
FileDescriptorTable(const FileDescriptorTable &)=delete
FileDescriptorTable & operator=(const FileDescriptorTable &)=delete
std::size_t openCount() const noexcept
Count of currently open descriptors.
Result< FileDescriptor > open(OpenFileState state)
Creates a new descriptor entry.
Result< void > set(FileDescriptor fd, OpenFileState state)
Replaces state for descriptor.
FileDescriptorTable(FileDescriptorTable &&) noexcept
Result< OpenFileState > get(FileDescriptor fd) const
Returns state for descriptor.
Result< void > close(FileDescriptor fd)
Removes a descriptor entry.
std::unique_ptr< Impl > impl_
bool contains(FileDescriptor fd) const noexcept
Returns whether descriptor exists.
A result type that holds either a success value of type T or an ErrorCode.
Definition error.h:104
Error codes and Result<T> type for fallible operations.
Inode types for the SimpleFS file-system simulation.
Definition block.h:15
constexpr OpenMode operator|(OpenMode lhs, OpenMode rhs) noexcept
Returns bitwise OR combination of open-mode flags.
constexpr OpenMode operator&(OpenMode lhs, OpenMode rhs) noexcept
Returns bitwise AND combination of open-mode flags.
constexpr InodeId INVALID_INODE_ID
Sentinel value indicating an invalid inode identifier.
Definition types.h:55
constexpr bool hasOpenMode(OpenMode value, OpenMode flag) noexcept
Returns whether the provided mode set contains a flag.
std::uint32_t InodeId
Unique identifier for a filesystem inode.
Definition types.h:33
OpenMode
Open mode bit flags.
Descriptor handle returned by open().
std::int32_t value
Underlying descriptor value.
friend constexpr bool operator==(FileDescriptor lhs, FileDescriptor rhs) noexcept
Equality comparison for descriptor values.
constexpr bool valid() const noexcept
Returns true when descriptor is valid.
Runtime state for one open descriptor.
InodeId inodeId
Target inode associated with descriptor.
std::size_t offset
Current byte offset.
OpenMode mode
Open mode flags for this descriptor.