-
Notifications
You must be signed in to change notification settings - Fork 0
/
cotto.cpp
339 lines (300 loc) · 14.1 KB
/
cotto.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <iostream>
#include <getopt.h>
#include <unistd.h>
#include "vendor/otto.h"
int error(const std::string message, const OttoStatusCode code)
{
std::cerr << "[ERROR] " << message << std::endl;
std::cerr << "[CODE] " << code << std::endl;
return code;
}
int downloadError(const OttoStatusCode code)
{
const char *message;
switch (code) {
case OTTO_TRANSFER_UNAUTHORIZED:
message = "The client is not allowed to use the API.";
break;
case OTTO_TRANSFER_NOT_FOUND:
message = "The OTTER server did not find the object.";
break;
default:
message = "Error occurred while downloading. Check otto.log for details.";
break;
}
return error(message, code);
}
class CottoBlockwise {
private:
OttoInstanzHandle instance;
OttoZertifikatHandle certificateHandle;
OttoRueckgabepufferHandle contentHandle;
OttoEmpfangHandle downloadHandle;
public:
CottoBlockwise(const char* pathLog, const char* pathCertificate, const char* certificatePassword) {
// Create instance
const OttoStatusCode statusCodeInstanceCreate = OttoInstanzErzeugen(pathLog, NULL, NULL, &instance);
if (statusCodeInstanceCreate != OTTO_OK) {
error("Could not create an Otto instance. Check otto.log for details.", statusCodeInstanceCreate);
}
// Open certificate
const OttoStatusCode statusCodeCertificateOpen = OttoZertifikatOeffnen(instance, pathCertificate, certificatePassword, &certificateHandle);
if (statusCodeCertificateOpen != OTTO_OK) {
error("Could not open certificate: " + std::string(pathCertificate), statusCodeCertificateOpen);
}
std::cout << "[INFO] Using certificate path: " << pathCertificate << std::endl;
}
~CottoBlockwise() {
// End download
if (downloadHandle != NULL) {
const OttoStatusCode statusCodeDownloadEnd = OttoEmpfangBeenden(downloadHandle);
if (statusCodeDownloadEnd != OTTO_OK) {
error("Could not end download.", statusCodeDownloadEnd);
}
}
// Close certificate
if (certificateHandle != NULL) {
const OttoStatusCode statusCodeCertificateClose = OttoZertifikatSchliessen(certificateHandle);
if (statusCodeCertificateClose != OTTO_OK) {
error("Could not close certificate handle", statusCodeCertificateClose);
}
}
// Release content buffer
if (contentHandle != NULL) {
const OttoStatusCode statusCodeContentRelease = OttoRueckgabepufferFreigeben(contentHandle);
if (statusCodeContentRelease != OTTO_OK) {
error("Could not release content handle.", statusCodeContentRelease);
}
}
// Destroy instance
if (instance != NULL) {
const OttoStatusCode statusCodeInstanceDestroy = OttoInstanzFreigeben(instance);
if (statusCodeInstanceDestroy != OTTO_OK) {
error("Could not destroy the Otto instance. Check otto.log for details.", statusCodeInstanceDestroy);
}
}
}
int workflow(const char* objectUuid, const char* developerId, const char* fileExtension, const char* pathDownload) {
// Start download
const OttoStatusCode statusCodeDownloadStart = OttoEmpfangBeginnen(instance, objectUuid, certificateHandle, developerId, &downloadHandle);
if (statusCodeDownloadStart != OTTO_OK) {
return error("Could not start download. Check otto.log", statusCodeDownloadStart);
}
// Create content buffer
const OttoStatusCode statusCodeContentHandleCreate = OttoRueckgabepufferErzeugen(instance, &contentHandle);
if (statusCodeContentHandleCreate != OTTO_OK) {
return error("Could not create handle for content.", statusCodeContentHandleCreate);
}
const std::string filepath = std::string(pathDownload) + "/" + std::string(objectUuid) + "." + std::string(fileExtension);
FILE *file = fopen(filepath.c_str(), "ab"); // Open file for appending in binary mode
if (!file) {
std::cerr << "Failed to open file: " << filepath << std::endl;
return 6;
}
// Continue download
bool downloadContinue = true;
OttoStatusCode statusCodeDownloadContinue = OTTO_UNBEKANNTER_FEHLER;
while (downloadContinue == true) {
statusCodeDownloadContinue = OttoEmpfangFortsetzen(downloadHandle, contentHandle);
if (statusCodeDownloadContinue != OTTO_OK) {
downloadContinue = false;
break;
}
uint64_t contentSize = OttoRueckgabepufferGroesse(contentHandle);
if (contentSize <= 0) {
downloadContinue = false;
break;
}
std::cout << "[INFO] Downloaded: " << contentSize << " Bytes" << std::endl;
const byteChar* contentBlock = OttoRueckgabepufferInhalt(contentHandle);
size_t contentWrite = fwrite(contentBlock, 1, contentSize, file);
if (contentWrite != contentSize) {
std::cerr << "Failed to write to file: " << filepath << std::endl;
fclose(file);
break;
}
}
if (fclose(file) != 0) {
std::cerr << "Failed to close file: " << filepath << std::endl;
return 7;
}
if (statusCodeDownloadContinue != OTTO_OK) {
unlink(filepath.c_str());
return downloadError(statusCodeDownloadContinue);
}
std::cout << "[INFO] Downloaded content saved in: " << filepath << std::endl;
return EXIT_SUCCESS;
}
};
class CottoInMemory {
private:
OttoInstanzHandle instance;
OttoRueckgabepufferHandle contentHandle;
const char* pathCertificate;
const char* certificatePassword;
public:
CottoInMemory(const char* pathLog, const char* providedPathCertificate, const char* providedCertificatePassword) {
// Create instance
const OttoStatusCode statusCodeInstanceCreate = OttoInstanzErzeugen(pathLog, NULL, NULL, &instance);
if (statusCodeInstanceCreate != OTTO_OK) {
error("Could not create an Otto instance. Check otto.log for details.", statusCodeInstanceCreate);
}
// Create content buffer
const OttoStatusCode statusCodeContentHandleCreate = OttoRueckgabepufferErzeugen(instance, &contentHandle);
if (statusCodeContentHandleCreate != OTTO_OK) {
error("Could not create handle for content.", statusCodeContentHandleCreate);
}
pathCertificate = providedPathCertificate;
certificatePassword = providedCertificatePassword;
}
~CottoInMemory() {
// Release content buffer
if (contentHandle != NULL) {
const OttoStatusCode statusCodeContentRelease = OttoRueckgabepufferFreigeben(contentHandle);
if (statusCodeContentRelease != OTTO_OK) {
error("Could not release content handle.", statusCodeContentRelease);
}
}
// Destroy instance
if (instance != NULL) {
const OttoStatusCode statusCodeInstanceDestroy = OttoInstanzFreigeben(instance);
if (statusCodeInstanceDestroy != OTTO_OK) {
error("Could not destroy the Otto instance. Check otto.log for details.", statusCodeInstanceDestroy);
}
}
}
int workflow(const char* objectUuid, const uint32_t size, const char* developerId, const char* fileExtension, const char* pathDownload) const {
const std::string filepath = std::string(pathDownload) + "/" + std::string(objectUuid) + "." + std::string(fileExtension);
FILE *file = fopen(filepath.c_str(), "ab"); // Open file for appending in binary mode
if (!file) {
std::cerr << "Failed to open file: " << filepath << std::endl;
return 6;
}
std::cout << "[INFO] Using certificate path: " << pathCertificate << std::endl;
OttoStatusCode statusCodeDownload = OttoDatenAbholen(
instance,
objectUuid,
size,
pathCertificate,
certificatePassword,
developerId,
NULL,
contentHandle
);
if (statusCodeDownload != OTTO_OK) {
unlink(filepath.c_str());
return downloadError(statusCodeDownload);
}
uint64_t contentSize = OttoRueckgabepufferGroesse(contentHandle);
std::cout << "[INFO] Downloaded: " << contentSize << " Bytes" << std::endl;
if (contentSize > 0) {
const byteChar* contentBlock = OttoRueckgabepufferInhalt(contentHandle);
size_t contentWrite = fwrite(contentBlock, 1, contentSize, file);
if (contentWrite != contentSize) {
std::cerr << "Failed to write to file: " << filepath << std::endl;
fclose(file);
}
}
if (fclose(file) != 0) {
std::cerr << "Failed to close file: " << filepath << std::endl;
return 7;
}
std::cout << "[INFO] Downloaded content saved in: " << filepath << std::endl;
return EXIT_SUCCESS;
}
};
int main(const int argc, char *argv[]) {
if (argc < 2) {
std::cerr << "Usage:" << std::endl;
std::cerr << "" << argv[0] << std::endl;
std::cerr << " -u objectUuid\t\tUUID of object to download (mandatory)" << std::endl;
std::cerr << " -m size\t\tAllocate provided Bytes of memory and download object in-memory (optional, max: 10485760 Bytes), when not provided or exceeds max download blockwise" << std::endl;
std::cerr << " -e extension\t\tSet filename extension of downloaded content [optional, default: \"txt\"]" << std::endl;
std::cerr << " -p password\t\tPassword for certificate [optional, default: \"123456\"]" << std::endl;
std::cerr << " -f\t\t\tForce file overwriting [optional, default: false]" << std::endl;
return 1;
}
int option;
const char* objectUuid = NULL;
uint32_t memorySizeAllocation = 0;
const char* fileExtension = "txt";
const char* certificatePassword = "123456";
bool forceOverwrite = false;
while ((option = getopt(argc, argv, "u:m:e:p:f")) != -1) {
switch (option) {
case 'u':
objectUuid = optarg;
break;
case 'm':
char *endPtr;
memorySizeAllocation = std::strtoul(optarg, &endPtr, 10);
if (*endPtr != '\0' || memorySizeAllocation <= 0) {
std::cerr << "Invalid memory size allocation: " << optarg << std::endl;
return 9;
}
break;
case 'e':
fileExtension = optarg;
break;
case 'p':
certificatePassword = optarg;
break;
case 'f':
forceOverwrite = true;
break;
default:
std::cerr << "Invalid option: -" << static_cast<char>(optopt) << std::endl;
return 2;
}
}
if (objectUuid == NULL) {
std::cerr << "Object UUID is missing. Please provide it with the -u flag." << std::endl;
return 3;
}
const char* pathDownload = ".";
const char* envPathDownload = getenv("PATH_DOWNLOAD");
if (envPathDownload != NULL) {
pathDownload = envPathDownload;
}
// Check if file already exists
const std::string filepath = std::string(pathDownload) + "/" + std::string(objectUuid) + "." + std::string(fileExtension);
if (access(filepath.c_str(), F_OK) != -1) {
if (forceOverwrite) {
unlink(filepath.c_str());
} else {
char userChoice;
std::cout << "File: " << filepath << " already exists. Do you want to overwrite it? (y/n)" << std::endl;
std::cin >> userChoice;
if (userChoice == 'y' || userChoice == 'Y') {
unlink(filepath.c_str());
} else {
std::cerr << "File: " << filepath << " was not overwritten. Stopping." << std::endl;
return 5;
}
}
}
const char* developerId = getenv("DEVELOPER_ID");
if (developerId == NULL || std::string(developerId) == "") {
std::cerr << "DEVELOPER_ID environment variable missing. Please set it accordingly." << std::endl;
return 4;
}
const char* pathCertificate = "certificate/test-softorg-pse.pfx";
const char* envPathCertificate = getenv("PATH_CERTIFICATE");
if (envPathCertificate != NULL) {
pathCertificate = envPathCertificate;
}
const char* pathLog = ".";
const char* envPathLog = getenv("PATH_LOG");
if (envPathLog != NULL) {
pathLog = envPathLog;
}
if (memorySizeAllocation > 0 && memorySizeAllocation <= 10485760) {
std::cout << "[INFO] Using simplified in-memory data retrieval for objects smaller than 10485760 Bytes (10 MiB)" << std::endl;
CottoInMemory cotto(pathLog, pathCertificate, certificatePassword);
return cotto.workflow(objectUuid, memorySizeAllocation, developerId, fileExtension, pathDownload);
} else {
std::cout << "[INFO] Using blockwise data retrieval" << std::endl;
CottoBlockwise cotto(pathLog, pathCertificate, certificatePassword);
return cotto.workflow(objectUuid, developerId, fileExtension, pathDownload);
}
}