-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3stor_test.cpp
More file actions
252 lines (213 loc) · 9.03 KB
/
s3stor_test.cpp
File metadata and controls
252 lines (213 loc) · 9.03 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* Project: s3stor_cpp
* Description: Test executable for S3Stor C++ library
*
* Ported from Go version by: Alexey Shvechkov <alexey@shvechkov.com>
* License: MIT
*
* © 2025 Alexey Shvechkov. All rights reserved.
*/
#include "s3stor_lib.h"
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <aws/core/Aws.h>
// Platform-specific includes
#ifdef _WIN32
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#else
#include <unistd.h>
#endif
void print_usage() {
std::cout << "Usage: s3stor_test <sync|ls|get|map|snapshot|delete-snapshot|cleanup-blocks|delete> [args...]" << std::endl;
std::cout << "\nCommands:" << std::endl;
std::cout << " sync <file_or_dir> - Upload files to S3 with deduplication" << std::endl;
std::cout << " ls [snapshot_id] - List files in global catalog or snapshot" << std::endl;
std::cout << " get [<snapshot_id>] <file_name> <dir> - Restore a file from snapshot or global catalog" << std::endl;
std::cout << " map [<snapshot_id>] <file_name> - Display block mappings for a file" << std::endl;
std::cout << " snapshot <dir> <snapshot_id> [files...] - Create a snapshot of specified files" << std::endl;
std::cout << " delete-snapshot <snapshot_id> - Delete a snapshot" << std::endl;
std::cout << " cleanup-blocks - Remove unreferenced blocks" << std::endl;
std::cout << " delete <file_name> - Remove file from global catalog" << std::endl;
}
#pragma warning(disable : 4996) // getenv is unsafe - using checked version
std::string get_env_or_default(const char* name, const std::string& default_value = "") {
const char* value = std::getenv(name);
return value ? std::string(value) : default_value;
}
#pragma warning(default : 4996) // optional: re-enable afterward
std::string get_hostname_safe() {
#ifdef _WIN32
WSADATA wsaData;
int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result != 0) {
return "unknown";
}
char hostname[256];
if (gethostname(hostname, sizeof(hostname)) == 0) {
WSACleanup();
return std::string(hostname);
}
WSACleanup();
return "unknown";
#else
char hostname[256];
if (gethostname(hostname, sizeof(hostname)) == 0) {
return std::string(hostname);
}
return "unknown";
#endif
}
int main(int argc, char* argv[]) {
// Initialize AWS SDK
Aws::SDKOptions options;
Aws::InitAPI(options);
{
// Setup configuration from environment
s3stor::S3ProviderConfig config;
config.provider = get_env_or_default("S3_PROVIDER", "aws");
config.bucket_name = get_env_or_default("S3_BUCKET");
config.endpoint = get_env_or_default("S3_ENDPOINT");
config.region = get_env_or_default("S3_REGION");
config.access_key_id = get_env_or_default("AWS_ACCESS_KEY_ID");
config.secret_access_key = get_env_or_default("AWS_SECRET_ACCESS_KEY");
if (config.provider == "wasabi") {
config.force_path_style = true;
if (config.region.empty()) {
config.region = "us-east-1";
}
}
// Validate required configuration
if (config.bucket_name.empty()) {
std::cerr << "Error: S3_BUCKET environment variable is required" << std::endl;
Aws::ShutdownAPI(options);
return 1;
}
if (config.region.empty()) {
std::cerr << "Error: S3_REGION environment variable is required" << std::endl;
Aws::ShutdownAPI(options);
return 1;
}
if (config.access_key_id.empty() || config.secret_access_key.empty()) {
std::cerr << "Error: AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables are required" << std::endl;
Aws::ShutdownAPI(options);
return 1;
}
if (argc < 2) {
print_usage();
Aws::ShutdownAPI(options);
return 1;
}
std::string command = argv[1];
// Create S3Stor instance
s3stor::S3Stor s3stor(config);
try {
if (command == "sync") {
if (argc < 3) {
std::cerr << "Usage: sync <file_or_dir>" << std::endl;
Aws::ShutdownAPI(options);
return 1;
}
std::string path = argv[2];
if (std::filesystem::is_directory(path)) {
s3stor.sync_dir(path);
} else if (std::filesystem::is_regular_file(path)) {
std::string base_dir = std::filesystem::path(path).parent_path().string();
if (base_dir.empty()) base_dir = ".";
s3stor.sync_file(path, base_dir);
} else {
std::cerr << "Error: Path does not exist: " << path << std::endl;
Aws::ShutdownAPI(options);
return 1;
}
} else if (command == "ls") {
if (argc == 3) {
std::string snapshot_id = argv[2];
s3stor.list_snapshot_files(snapshot_id);
} else {
s3stor.list_files();
}
} else if (command == "get") {
if (argc < 4 || argc > 5) {
std::cerr << "Usage: get [<snapshot_id>] <file_name> <output_dir>" << std::endl;
Aws::ShutdownAPI(options);
return 1;
}
std::string snapshot_id, file_name, out_dir;
if (argc == 5) {
snapshot_id = argv[2];
file_name = argv[3];
out_dir = argv[4];
} else {
snapshot_id = "";
file_name = argv[2];
out_dir = argv[3];
}
s3stor.get_file(snapshot_id, file_name, out_dir);
} else if (command == "map") {
if (argc < 3 || argc > 4) {
std::cerr << "Usage: map [<snapshot_id>] <file_name>" << std::endl;
Aws::ShutdownAPI(options);
return 1;
}
std::string snapshot_id, file_name;
if (argc == 4) {
snapshot_id = argv[2];
file_name = argv[3];
} else {
snapshot_id = "";
file_name = argv[2];
}
s3stor.get_file_map(snapshot_id, file_name);
} else if (command == "snapshot") {
if (argc < 4) {
std::cerr << "Usage: snapshot <dir> <snapshot_id> [changed_files...]" << std::endl;
Aws::ShutdownAPI(options);
return 1;
}
std::string dir = argv[2];
std::string snapshot_id = argv[3];
std::vector<std::string> changed_files;
for (int i = 4; i < argc; ++i) {
changed_files.push_back(argv[i]);
}
std::string hostname = get_hostname_safe();
s3stor.create_snapshot(dir, snapshot_id, hostname, changed_files);
} else if (command == "delete-snapshot") {
if (argc != 3) {
std::cerr << "Usage: delete-snapshot <snapshot_id>" << std::endl;
Aws::ShutdownAPI(options);
return 1;
}
std::string snapshot_id = argv[2];
s3stor.delete_snapshot(snapshot_id);
} else if (command == "cleanup-blocks") {
std::string hostname = get_hostname_safe();
s3stor.cleanup_blocks(hostname);
} else if (command == "delete") {
if (argc != 3) {
std::cerr << "Usage: delete <file_name>" << std::endl;
Aws::ShutdownAPI(options);
return 1;
}
std::string file_name = argv[2];
std::string hostname = get_hostname_safe();
s3stor.delete_file(hostname, file_name);
} else {
std::cerr << "Unknown command: " << command << std::endl;
print_usage();
Aws::ShutdownAPI(options);
return 1;
}
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
Aws::ShutdownAPI(options);
return 1;
}
}
// Shutdown AWS SDK
Aws::ShutdownAPI(options);
return 0;
}