forked from szatmary/PlotFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.cpp
166 lines (144 loc) · 6.8 KB
/
cli.cpp
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
#include "plotfs.hpp"
#include "CLI11.hpp"
std::vector<uint8_t> to_vector(const std::string& id)
{
std::vector<uint8_t> id_;
for (auto i = 0; i < id.length(); i += 2) {
std::string byteString = id.substr(i, 2);
char byte = (char)strtol(byteString.c_str(), NULL, 16);
id_.push_back(byte);
}
return id_;
}
int main(int argc, char** argv)
{
CLI::App app { "PlotFS" };
std::string config_path = default_config_path;
app.add_option("-c,--config", config_path, "Path to configuration json file");
bool init = false;
auto init_opt = app.add_flag("--init", init, "Initlize a new plotfs.bin file");
std::vector<std::string> add_plot;
std::string add_device, remove_device, remove_plot;
auto add_device_opt = app.add_option("--add_device", add_device, "Add a device or partition");
auto remove_device_opt = app.add_option("--remove_device", remove_device, "Rempove a device or partition");
auto add_plot_opt = app.add_option("--add_plot", add_plot, "Add a plot");
auto remove_plot_opt = app.add_option("--remove_plot", remove_plot, "Remove a plot");
bool list_plots = false, list_devices = false;
auto list_plots_opt = app.add_flag("--list_plots", list_plots, "List all plots");
auto list_devices_opt = app.add_flag("--list_devices", list_devices, "List all devices");
bool force = false, remove_source = false;
bool force_opt = app.add_flag("--force", force, "Force operation");
auto remove_source_opt = app.add_flag("--remove_source", remove_source, "Removes source plot file after adding");
init_opt->excludes(add_device_opt)->excludes(remove_device_opt)->excludes(add_plot_opt)->excludes(remove_plot_opt)->excludes(list_plots_opt)->excludes(list_devices_opt);
add_device_opt->excludes(remove_device_opt)->excludes(add_plot_opt)->excludes(remove_plot_opt)->excludes(list_plots_opt)->excludes(list_devices_opt)->excludes(init_opt);
remove_device_opt->excludes(add_device_opt)->excludes(add_plot_opt)->excludes(remove_plot_opt)->excludes(list_plots_opt)->excludes(list_devices_opt)->excludes(init_opt);
add_plot_opt->excludes(remove_plot_opt)->excludes(add_device_opt)->excludes(remove_device_opt)->excludes(list_plots_opt)->excludes(list_devices_opt)->excludes(init_opt);
remove_plot_opt->excludes(add_plot_opt)->excludes(add_device_opt)->excludes(remove_device_opt)->excludes(list_plots_opt)->excludes(list_devices_opt)->excludes(init_opt);
list_plots_opt->excludes(add_device_opt)->excludes(remove_device_opt)->excludes(add_plot_opt)->excludes(remove_plot_opt)->excludes(list_devices_opt)->excludes(init_opt); //->excludes(force_opt);
list_devices_opt->excludes(add_device_opt)->excludes(remove_device_opt)->excludes(add_plot_opt)->excludes(remove_plot_opt)->excludes(list_plots_opt)->excludes(init_opt); //->excludes(force_opt);
CLI11_PARSE(app, argc, argv);
if (init) {
if (!PlotFS::init(config_path, force)) {
std::cerr << "init failed" << std::endl;
return EXIT_FAILURE;
}
std::cerr << "initialized config at" << config_path << std::endl;
return EXIT_SUCCESS;
}
if (list_devices) {
auto g = PlotFS::loadGeometry(config_path);
if (!g) {
std::cerr << "Failed to load geometry" << std::endl;
return EXIT_FAILURE;
}
if (g->geom->devices()) {
for (const auto device : *g->geom->devices()) {
auto space = device->end() - device->begin();
if (g->geom->plots()) {
for (const auto plot : *g->geom->plots()) {
if (plot->shards()) { }
for (const auto shard : *plot->shards()) {
if (*shard->device_id() == *device->id()) {
space -= shard->end() - shard->begin();
}
}
}
}
auto size = device->end() - device->begin();
std::cout << to_string(*device->id()) << " " << space << "/" << size << " " << 100 - (space * 100 / size) << "% " << device->path()->c_str() << std::endl;
}
}
return EXIT_SUCCESS;
}
if (list_plots) {
auto g = PlotFS::loadGeometry(config_path);
if (!g) {
std::cerr << "Failed to load geometry" << std::endl;
return EXIT_FAILURE;
}
if (g->geom->plots()) {
for (const auto plot : *g->geom->plots()) {
uint64_t size = 0, shards = 0;
if (plot->shards()) {
shards = plot->shards()->size();
for (const auto shard : *plot->shards()) {
size += shard->end() - shard->begin();
}
}
std::cout << to_string(*plot->id()) << " " << size << " " << shards << std::endl;
}
}
return EXIT_SUCCESS;
}
if (!add_device.empty()) {
PlotFS plotfs(config_path);
if (!plotfs.isOpen()) {
std::cerr << "Could not open plotfs" << std::endl;
return EXIT_FAILURE;
}
return plotfs.addDevice(add_device, force) ? EXIT_SUCCESS : EXIT_FAILURE;
}
if (!remove_device.empty()) {
auto device_id = to_vector(remove_device);
PlotFS plotfs(config_path);
if (!plotfs.isOpen()) {
std::cerr << "Could not open plotfs" << std::endl;
return EXIT_FAILURE;
}
return plotfs.removeDevice(device_id) ? EXIT_SUCCESS : EXIT_FAILURE;
}
if (!add_plot.empty()) {
PlotFS plotfs(config_path);
if (!plotfs.isOpen()) {
std::cerr << "Could not open plotfs" << std::endl;
return EXIT_FAILURE;
}
for (const auto& plot_pah : add_plot) {
if (!plotfs.addPlot(plot_pah)) {
return EXIT_FAILURE;
}
if (remove_source) {
try {
std::error_code errc;
if (!std::filesystem::remove(plot_pah, errc)) {
std::cerr << "Could not remove source: " << errc.message() << std::endl;
} else {
std::cerr << "Removed " << plot_pah << std::endl;
}
} catch (const std::exception& e) {
std::cerr << "Could not remove source: " << e.what() << std::endl;
}
}
}
return EXIT_SUCCESS;
}
if (!remove_plot.empty()) {
auto plot_id = to_vector(remove_plot);
PlotFS plotfs(config_path);
if (!plotfs.isOpen()) {
std::cerr << "Could not open plotfs" << std::endl;
return EXIT_FAILURE;
}
return plotfs.removePlot(plot_id) ? EXIT_SUCCESS : EXIT_FAILURE;
}
}