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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/*
* This source code is released into the public domain.
*/
module;
#include <coroutine>
#include <expected>
#include <filesystem>
#include <format>
#include <string>
#include <system_error>
#include <fcntl.h>
module liblfvm;
import nihil;
import nihil.ucl;
namespace lfvm {
auto make_disk_config(std::string_view name, std::string_view filename)
-> std::expected<disk_config, nihil::error>
{
if (name.empty())
return std::unexpected(nihil::error("disk name missing"));
if (filename.empty())
return std::unexpected(nihil::error("disk path missing"));
return disk_config(name, filename);
}
disk_config::disk_config(std::string_view name, std::filesystem::path path)
: m_name(name)
, m_path(std::move(path))
{
}
auto disk_config::name(this disk_config const &self) -> std::string_view
{
return self.m_name;
}
auto disk_config::path(this disk_config const &self) -> std::filesystem::path const &
{
return self.m_path;
}
auto disk_config::exists(this disk_config const &self) -> bool
{
auto ec = std::error_code();
return std::filesystem::exists(self.path(), ec);
}
auto disk_config::serialize(this disk_config const &self)
-> std::expected<std::string, nihil::error>
{
using namespace nihil::ucl;
using namespace std::literals;
auto uobj = map<object>();
uobj.insert({"name"sv, string(self.name())});
uobj.insert({"path"sv, string(self.path().string())});
return std::format("{:c}", uobj);
}
auto disk_config::deserialize(std::string_view text)
-> std::expected<disk_config, nihil::error>
{
using namespace nihil::ucl;
auto uobj = co_await parse(text);
// Name
auto name = std::string();
if (auto o = uobj.find("name"); o)
name = (co_await object_cast<string>(*o)).value();
else
co_return std::unexpected(nihil::error("missing name"));
// Path
auto path = std::string();
if (auto o = uobj.find("path"); o)
path = (co_await object_cast<string>(*o)).value();
else
co_return std::unexpected(nihil::error("missing path"));
auto disk = co_await make_disk_config(name, path);
co_return disk;
}
auto disk_load(context const &ctx, std::string_view name)
-> std::expected<disk_config, nihil::error>
{
auto filename = ctx.disk_config_file(name);
auto text = std::string();
co_await nihil::read_file(filename, std::back_inserter(text));
auto config = co_await disk_config::deserialize(text);
co_return config;
}
auto disk_create(context const &ctx, disk_config const &d)
-> std::expected<void, nihil::error>
{
auto ucltext = co_await d.serialize();
auto filename = ctx.disk_config_file(d.name());
/*
* We can't do the usual atomic write method of writing to a temporary
* file and renaming because we need to open the file with O_EXCL, so
* instead just attempt to delete the target file if something fails.
*/
auto file = co_await nihil::open_file(
filename, O_WRONLY|O_CREAT|O_EXCL, 0600);
auto file_guard = nihil::guard([&] {
std::filesystem::remove(filename);
});
co_await write(file, ucltext);
file_guard.release();
// TODO: Perhaps sync the file?
co_return {};
}
auto disk_save(context const &ctx, disk_config const &d)
-> std::expected<void, nihil::error>
{
auto ucltext = co_await d.serialize();
auto filename = ctx.disk_config_file(d.name());
co_await nihil::safe_write_file(filename, ucltext);
// TODO: Perhaps sync the file?
co_return {};
}
auto disk_list(context const &ctx)
-> std::expected<std::vector<std::string>, nihil::error>
{
auto ret = std::vector<std::string>();
auto files = std::filesystem::directory_iterator{ctx.diskconfdir()};
for (auto &&file : files) {
if (!file.is_regular_file())
continue;
auto path = file.path();
if (path.extension() != ".ucl")
continue;
path.replace_extension();
auto name = path.filename().string();
if (name[0] == '.')
continue;
ret.push_back(name);
}
return ret;
}
} // namespace lfvm
|