/
lister.cpp
292 lines (257 loc) · 8.64 KB
/
lister.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
#include "lister.h"
#include "panelentry.h"
#include <time.h>
#include <QtCore>
int statusG = 0;
static char errorDetailsG[4096] = { 0 };
static QList<S3ListBucketContent> savedContents;
static QList<QString> savedCommonPrefixes;
static QList<QString> savedKeys;
void S3_init()
{
S3Status status;
const char *hostname = getenv("S3_HOSTNAME");
if ((status = S3_initialize("s3", S3_INIT_ALL, hostname))
!= S3StatusOK) {
fprintf(stderr, "Failed to initialize libs3: %s\n",
S3_get_status_name(status));
return;
}
}
int should_retry()
{
static int retriesG = 5;
if (retriesG--) {
static int retrySleepInterval = 1;
struct timespec ts = {retrySleepInterval,0};
// Sleep before next retry; start out with a 1 second sleep
nanosleep(&ts, NULL);
// Next sleep 1 second longer
retrySleepInterval++;
return 1;
}
return 0;
}
void printError()
{
if (statusG < S3StatusErrorAccessDenied) {
fprintf(stderr, "\nERROR: %s\n", S3_get_status_name((S3Status)statusG));
}
else {
fprintf(stderr, "\nERROR: %s\n", S3_get_status_name((S3Status)statusG));
fprintf(stderr, "%s\n", errorDetailsG);
}
}
// This callback does the same thing for every request type: prints out the
// properties if the user has requested them to be so
S3Status responsePropertiesCallback
(const S3ResponseProperties *properties, void *callbackData)
{
(void) callbackData;
(void) properties;
return S3StatusOK;
}
// This callback does the same thing for every request type: saves the status
// and error stuff in global variables
void responseCompleteCallback(S3Status status,
const S3ErrorDetails *error,
void *callbackData)
{
(void) callbackData;
statusG = status;
// Compose the error details message now, although we might not use it.
// Can't just save a pointer to [error] since it's not guaranteed to last
// beyond this callback
int len = 0;
if (error && error->message) {
len += snprintf(&(errorDetailsG[len]), sizeof(errorDetailsG) - len,
" Message: %s\n", error->message);
}
if (error && error->resource) {
len += snprintf(&(errorDetailsG[len]), sizeof(errorDetailsG) - len,
" Resource: %s\n", error->resource);
}
if (error && error->furtherDetails) {
len += snprintf(&(errorDetailsG[len]), sizeof(errorDetailsG) - len,
" Further Details: %s\n", error->furtherDetails);
}
if (error && error->extraDetailsCount) {
len += snprintf(&(errorDetailsG[len]), sizeof(errorDetailsG) - len,
"%s", " Extra Details:\n");
int i;
for (i = 0; i < error->extraDetailsCount; i++) {
len += snprintf(&(errorDetailsG[len]),
sizeof(errorDetailsG) - len, " %s: %s\n",
error->extraDetails[i].name,
error->extraDetails[i].value);
}
}
}
S3Status listBucketCallback(int isTruncated, const char *nextMarker,
int contentsCount,
const S3ListBucketContent *contents,
int commonPrefixesCount,
const char **commonPrefixes,
void *callbackData)
{
list_bucket_callback_data *data =
(list_bucket_callback_data *) callbackData;
data->isTruncated = isTruncated;
// This is tricky. S3 doesn't return the NextMarker if there is no
// delimiter. Why, I don't know, since it's still useful for paging
// through results. We want NextMarker to be the last content in the
// list, so set it to that if necessary.
if ((!nextMarker || !nextMarker[0]) && contentsCount) {
nextMarker = contents[contentsCount - 1].key;
}
if (nextMarker) {
snprintf(data->nextMarker, sizeof(data->nextMarker), "%s",
nextMarker);
}
else {
data->nextMarker[0] = 0;
}
// if (contentsCount && !data->keyCount) {
// printListBucketHeader(data->allDetails);
// }
int i;
for (i = 0; i < contentsCount; i++) {
const S3ListBucketContent *content = &(contents[i]);
char timebuf[256];
time_t t = (time_t) content->lastModified;
strftime(timebuf, sizeof(timebuf), "%Y-%m-%dT%H:%M:%SZ",
gmtime(&t));
char sizebuf[16];
if (content->size < 100000) {
sprintf(sizebuf, "%5llu", (unsigned long long) content->size);
}
else if (content->size < (1024 * 1024)) {
sprintf(sizebuf, "%4lluK",
((unsigned long long) content->size) / 1024ULL);
}
else if (content->size < (10 * 1024 * 1024)) {
float f = content->size;
f /= (1024 * 1024);
sprintf(sizebuf, "%1.2fM", f);
}
else if (content->size < (1024 * 1024 * 1024)) {
sprintf(sizebuf, "%4lluM",
((unsigned long long) content->size) /
(1024ULL * 1024ULL));
}
else {
float f = (content->size / 1024);
f /= (1024 * 1024);
sprintf(sizebuf, "%1.2fG", f);
}
savedContents.append(*content);
savedKeys.append(content->key);
}
data->keyCount += contentsCount;
for (i = 0; i < commonPrefixesCount; i++) {
savedCommonPrefixes.append(commonPrefixes[i]);
}
return S3StatusOK;
}
void list_bucket(const char *bucketName, const char *prefix,
const char *marker, const char *delimiter,
int maxkeys, const char* ak, const char* sk)
{
S3_init();
savedCommonPrefixes.clear();
savedContents.clear();
savedKeys.clear();
S3BucketContext bucketContext =
{
0,
bucketName,
S3ProtocolHTTPS,
S3UriStyleVirtualHost,
ak,
sk
};
S3ListBucketHandler listBucketHandler =
{
{ &responsePropertiesCallback, &responseCompleteCallback },
&listBucketCallback
};
list_bucket_callback_data data;
snprintf(data.nextMarker, sizeof(data.nextMarker), "%s", marker);
data.keyCount = 0;
data.allDetails = 0;
do {
data.isTruncated = 0;
do {
S3_list_bucket(&bucketContext, prefix, data.nextMarker,
delimiter, maxkeys, 0, &listBucketHandler, &data);
} while (S3_status_is_retryable((S3Status)statusG) && should_retry());
if (statusG != S3StatusOK) {
break;
}
} while (data.isTruncated && (!maxkeys || (data.keyCount < maxkeys)));
if (statusG == S3StatusOK) {
// if (!data.keyCount) {
// printListBucketHeader(allDetails);
// }
}
else {
printError();
}
S3_deinitialize();
}
QString
getDate(int i) {
if (i < savedCommonPrefixes.size()) return "";
i -= savedCommonPrefixes.size();
const S3ListBucketContent *content = &savedContents.at(i);
char timebuf[256];
time_t t = (time_t) content->lastModified;
strftime(timebuf, sizeof(timebuf), "%Y-%m-%dT%H:%M:%SZ",
gmtime(&t));
return QString(timebuf);
}
QString
getSize(int i) {
if (i < savedCommonPrefixes.size()) return "";
i -= savedCommonPrefixes.size();
const S3ListBucketContent *content = &savedContents.at(i);
char sizebuf[16];
if (content->size < 100000) {
sprintf(sizebuf, "%5llu", (unsigned long long) content->size);
}
else if (content->size < (1024 * 1024)) {
sprintf(sizebuf, "%4lluK",
((unsigned long long) content->size) / 1024ULL);
}
else if (content->size < (10 * 1024 * 1024)) {
float f = content->size;
f /= (1024 * 1024);
sprintf(sizebuf, "%1.2fM", f);
}
else if (content->size < (1024 * 1024 * 1024)) {
sprintf(sizebuf, "%4lluM",
((unsigned long long) content->size) /
(1024ULL * 1024ULL));
}
else {
float f = (content->size / 1024);
f /= (1024 * 1024);
sprintf(sizebuf, "%1.2fG", f);
}
return QString(sizebuf);
printf("%-50s %s %s\n", __func__, content->key, sizebuf);
}
QString
getName(int i) {
if (i >= savedCommonPrefixes.size()) {
i -= savedCommonPrefixes.size();
return savedKeys.at(i);
} else {
//const S3ListBucketContent *content = &;
return QString(savedCommonPrefixes.at(i));
}
}
int
getCount() {
return savedCommonPrefixes.size() + savedContents.size();
}