Iros
 
Loading...
Searching...
No Matches
sync_file.h
Go to the documentation of this file.
1#pragma once
2
5#include "di/util/prelude.h"
7#include "dius/config.h"
8#include "dius/error.h"
10#include "dius/tty.h"
11
12#include DIUS_PLATFORM_PATH(system_call.h)
13
14namespace dius {
15#ifdef DIUS_PLATFORM_LINUX
16enum class Protection : int {
17 None = PROT_NONE,
18 Executable = PROT_EXEC,
19 Readable = PROT_READ,
20 Writeable = PROT_WRITE,
21};
22
24
25enum class MapFlags : int {
26 None = 0,
27 Shared = MAP_SHARED,
28 Private = MAP_PRIVATE,
29 Fixed = MAP_FIXED,
30 Anonymous = MAP_ANONYMOUS,
31 Stack = MAP_STACK,
32#ifdef MAP_POPULATE
33 Populate = MAP_POPULATE,
34#endif
35};
36
38#endif
39
40class SyncFile {
41public:
42 enum class Owned { Yes, No };
43
44 constexpr SyncFile() = default;
45
46 constexpr explicit SyncFile(Owned owned, int fd) : m_owned(owned), m_fd(fd) {}
47
48 constexpr SyncFile(SyncFile&& other)
49 : m_owned(di::exchange(other.m_owned, Owned::No)), m_fd(di::exchange(other.m_fd, -1)) {}
50
51 constexpr ~SyncFile() {
52 if (m_owned == Owned::No) {
53 return;
54 }
55 (void) this->close();
56 }
57
58 auto operator=(SyncFile&& other) -> SyncFile& {
59 (void) this->close();
60 m_owned = di::exchange(other.m_owned, Owned::No);
61 m_fd = di::exchange(other.m_fd, -1);
62 return *this;
63 }
64
65 constexpr auto valid() const -> bool { return m_fd != -1; }
66 constexpr explicit operator bool() const { return valid(); }
67
68 constexpr auto file_descriptor() const -> int { return m_fd; }
69 constexpr auto leak_file_descriptor() -> int {
70 m_owned = Owned::No;
71 return m_fd;
72 }
73
75
80
85
87
88#ifdef DIUS_PLATFORM_LINUX
89 auto map(u64 offset, size_t size, Protection protection, MapFlags flags) const
91#endif
92
93 auto flush() const -> di::Expected<void, di::GenericCode> { return {}; }
94
95 auto interactive_device() const -> bool { return true; }
96
100
103
104private:
105 Owned m_owned { Owned::No };
106 int m_fd { -1 };
107};
108
110
111auto open_sync(di::PathView path, OpenMode open_mode, u16 create_mode = 0666)
113auto open_psuedo_terminal_controller(OpenMode open_mode, tty::WindowSize size)
117
118inline auto stdin = SyncFile { SyncFile::Owned::No, 0 };
121}
#define DI_DEFINE_ENUM_BITWISE_OPERATIONS(Type)
Definition bitwise_enum.h:5
Definition function.h:365
Definition scope_exit.h:12
Definition expected_forward_declaration.h:8
Definition span_forward_declaration.h:10
Definition sync_file.h:40
constexpr ~SyncFile()
Definition sync_file.h:51
constexpr auto valid() const -> bool
Definition sync_file.h:65
constexpr SyncFile()=default
auto read_exactly(u64 offset, di::Span< di::Byte >) const -> di::Expected< void, di::GenericCode >
Definition sync_file.cpp:6
constexpr SyncFile(Owned owned, int fd)
Definition sync_file.h:46
auto get_tty_window_size() -> di::Expected< tty::WindowSize, di::GenericCode >
Definition sync_file.cpp:64
auto resize_file(u64 new_size) const -> di::Expected< void, di::GenericCode >
Definition sync_file.cpp:56
auto enter_raw_mode() -> di::Expected< RawModeToken, di::GenericCode >
Definition sync_file.cpp:72
auto operator=(SyncFile &&other) -> SyncFile &
Definition sync_file.h:58
auto read_some(u64 offset, di::Span< di::Byte >) const -> di::Expected< size_t, di::GenericCode >
Definition sync_file.cpp:44
constexpr SyncFile(SyncFile &&other)
Definition sync_file.h:48
constexpr auto file_descriptor() const -> int
Definition sync_file.h:68
auto flush() const -> di::Expected< void, di::GenericCode >
Definition sync_file.h:93
constexpr auto leak_file_descriptor() -> int
Definition sync_file.h:69
auto close() -> di::Expected< void, di::GenericCode >
Definition sync_file.cpp:30
auto set_tty_window_size(tty::WindowSize size) -> di::Expected< void, di::GenericCode >
Definition sync_file.cpp:60
Owned
Definition sync_file.h:42
@ Yes
Definition sync_file.h:42
@ No
Definition sync_file.h:42
auto interactive_device() const -> bool
Definition sync_file.h:95
auto write_exactly(u64 offset, di::Span< di::Byte const >) const -> di::Expected< void, di::GenericCode >
Definition sync_file.cpp:29
di::ScopeExit< di::Function< void()> > RawModeToken
Definition sync_file.h:101
auto get_psuedo_terminal_path() -> di::Expected< di::Path, di::GenericCode >
Definition sync_file.cpp:68
auto write_some(u64 offset, di::Span< di::Byte const >) const -> di::Expected< size_t, di::GenericCode >
Definition sync_file.cpp:52
constexpr usize size
Definition gfx_test.cpp:24
@ Readable
Definition file_implementation.h:47
@ None
Definition file_implementation.h:46
PathViewImpl< string::TransparentEncoding > PathView
Definition path_view.h:11
Stack(Con) -> Stack< T, Con >
@ Executable
Definition program_header.h:23
__UINT64_TYPE__ u64
Definition integers.h:12
__UINT16_TYPE__ u16
Definition integers.h:10
Expected< T, Error > Result
Definition result.h:8
StatusCode< platform::GenericDomain > GenericCode
Definition status_code_forward_declaration.h:13
Definition zstring_parser.h:9
constexpr auto exchange(T &object, U &&new_value) -> T
Definition exchange.h:8
Definition directory_entry.h:11
auto stderr
Definition sync_file.h:120
auto open_psuedo_terminal_controller(OpenMode open_mode, tty::WindowSize size) -> di::Expected< SyncFile, di::GenericCode >
Definition sync_file.cpp:95
auto open_sync(di::PathView path, OpenMode open_mode, u16 create_mode=0666) -> di::Expected< SyncFile, di::GenericCode >
Definition sync_file.cpp:76
auto read_to_string(di::PathView path) -> di::Result< di::String >
Definition sync_file.cpp:52
auto open_tempory_file() -> di::Expected< SyncFile, di::GenericCode >
Definition sync_file.cpp:99
auto stdout
Definition sync_file.h:119
OpenMode
Definition sync_file.h:109
@ Readonly
Definition sync_file.h:109
@ WriteNew
Definition sync_file.h:109
@ AppendOnly
Definition sync_file.h:109
@ AppendReadWrite
Definition sync_file.h:109
@ ReadWrite
Definition sync_file.h:109
@ WriteClobber
Definition sync_file.h:109
@ ReadWriteClobber
Definition sync_file.h:109
auto stdin
Definition sync_file.h:118
@ Fixed
Definition local_apic.h:82
Definition tty.h:6