-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathzmap_s3sdk.c
590 lines (508 loc) · 15.4 KB
/
zmap_s3sdk.c
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*
* Copyright 2018, University Corporation for Atmospheric Research
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*/
#include "zincludes.h"
#include "zmap.h"
#include "ncs3sdk.h"
#undef S3DEBUG
/*
Map our simplified map model to an S3 bucket + objects.
For the API, the mapping is as follows:
1. A bucket contains multiple datasets.
2. A key to the root of a dataset is assumed to be with respect to
the bucket name from the original open/create url.
2. Containment is simulated using the S3 key conventions.
3. Every object (e.g. group or array) is mapped to an S3 object
4. Meta data objects (e.g. .zgroup, .zarray, etc) are kept as an S3 object.
5. Actual variable data (for e.g. chunks) is stored as
using an S3 object per chunk.
Given a key, one of three things is true.
1. The key points to a content-bearing object
2. The key has no associated object
This basically means that there is no notion of not-found:
all keys are assumed to exist, but may have no content.
Notes:
1. Our canonical URLs use path style rather than virtual-host, although
virtual-host URLs will be accepted anc converted to path-style.
*/
#undef DEBUG
#define DEBUGERRORS
#define NCZM_S3SDK_V1 1
#define ZS3_PROPERTIES (0)
/* Define the "subclass" of NCZMAP */
typedef struct ZS3MAP {
NCZMAP map;
NCS3INFO s3;
void* s3client;
char* errmsg;
} ZS3MAP;
/* Forward */
static NCZMAP_API nczs3sdkapi; // c++ will not allow static forward variables
static int zs3len(NCZMAP* map, const char* key, size64_t* lenp);
static void freevector(size_t nkeys, char** list);
static void zs3initialize(void);
static int s3clear(void* s3client, const char* bucket, const char* key);
static int maketruekey(const char* rootpath, const char* key, char** truekeyp);
static void
errclear(ZS3MAP* z3map)
{
if(z3map) {
if(z3map->errmsg)
free(z3map->errmsg);
z3map->errmsg = NULL;
}
}
#ifdef DEBUGERRORS
static void
reporterr(ZS3MAP* z3map)
{
if(z3map) {
if(z3map->errmsg) {
nclog(NCLOGERR,z3map->errmsg);
}
errclear(z3map);
}
}
#else
#define reporterr(map)
#endif
/* Define the Dataset level API */
static int zs3initialized = 0;
static void
zs3initialize(void)
{
if(!zs3initialized) {
ZTRACE(7,NULL);
zs3initialized = 1;
(void)ZUNTRACE(NC_NOERR);
}
}
void
NCZ_s3finalize(void)
{
zs3initialized = 0;
}
static int
zs3create(const char *path, int mode, size64_t flags, void* parameters, NCZMAP** mapp)
{
int stat = NC_NOERR;
ZS3MAP* z3map = NULL;
NCURI* url = NULL;
char* prefix = NULL;
char* truekey = NULL;
NC_UNUSED(flags);
NC_UNUSED(parameters);
ZTRACE(6,"path=%s mode=%d flag=%llu",path,mode,flags);
if(!zs3initialized) zs3initialize();
/* Build the z3 state */
if((z3map = (ZS3MAP*)calloc(1,sizeof(ZS3MAP))) == NULL)
{stat = NC_ENOMEM; goto done;}
z3map->map.format = NCZM_S3;
z3map->map.url = strdup(path);
z3map->map.mode = mode;
z3map->map.flags = flags;
z3map->map.api = (NCZMAP_API*)&nczs3sdkapi;
/* Parse the URL */
ncuriparse(path,&url);
if(url == NULL)
{stat = NC_EURL; goto done;}
/* Convert to canonical path-style */
if((stat = NC_s3urlprocess(url,&z3map->s3,NULL))) goto done;
/* Verify the root path */
if(z3map->s3.rootkey == NULL)
{stat = NC_EURL; goto done;}
z3map->s3client = NC_s3sdkcreateclient(&z3map->s3);
{
int exists = 0;
/* Does bucket already exist */
if((stat = NC_s3sdkbucketexists(z3map->s3client,z3map->s3.bucket,&exists, &z3map->errmsg))) goto done;
if(!exists) {
/* create it */
if((stat = NC_s3sdkbucketcreate(z3map->s3client,z3map->s3.region,z3map->s3.bucket,&z3map->errmsg)))
goto done;
}
/* The root object may or may not already exist */
switch (stat = NC_s3sdkinfo(z3map->s3client,z3map->s3.bucket,z3map->s3.rootkey,NULL,&z3map->errmsg)) {
case NC_EEMPTY: /* no such object */
stat = NC_NOERR; /* which is what we want */
errclear(z3map);
break;
case NC_NOERR:
stat = NC_EOBJECT;
errclear(z3map);
/* Delete objects inside root object tree */
s3clear(z3map->s3client,z3map->s3.bucket,z3map->s3.rootkey);
goto done; /* already exists */
default: reporterr(z3map); goto done;
}
}
if(mapp) *mapp = (NCZMAP*)z3map;
done:
reporterr(z3map);
ncurifree(url);
nullfree(prefix);
nullfree(truekey);
if(stat) nczmap_close((NCZMAP*)z3map,1);
return ZUNTRACE(stat);
}
/* The problem with open is that there
no obvious way to test for existence.
So, we assume that the dataset must have
some content. We look for that */
static int
zs3open(const char *path, int mode, size64_t flags, void* parameters, NCZMAP** mapp)
{
int stat = NC_NOERR;
ZS3MAP* z3map = NULL;
NCURI* url = NULL;
NClist* content = NULL;
size_t nkeys = 0;
NC_UNUSED(flags);
NC_UNUSED(parameters);
ZTRACE(6,"path=%s mode=%d flags=%llu",path,mode,flags);
if(!zs3initialized) zs3initialize();
/* Build the z3 state */
if((z3map = (ZS3MAP*)calloc(1,sizeof(ZS3MAP))) == NULL)
{stat = NC_ENOMEM; goto done;}
z3map->map.format = NCZM_S3;
z3map->map.url = strdup(path);
z3map->map.mode = mode;
z3map->map.flags = flags;
z3map->map.api = (NCZMAP_API*)&nczs3sdkapi;
/* Parse the URL */
if((stat = ncuriparse(path,&url))) goto done;
if(url == NULL)
{stat = NC_EURL; goto done;}
/* Convert to canonical path-style */
if((stat = NC_s3urlprocess(url,&z3map->s3,NULL))) goto done;
/* Verify root path */
if(z3map->s3.rootkey == NULL)
{stat = NC_EURL; goto done;}
z3map->s3client = NC_s3sdkcreateclient(&z3map->s3);
/* Search the root for content */
content = nclistnew();
if((stat = NC_s3sdkgetkeys(z3map->s3client,z3map->s3.bucket,z3map->s3.rootkey,&nkeys,NULL,&z3map->errmsg)))
goto done;
if(nkeys == 0) {
/* dataset does not actually exist; we choose to return ENOOBJECT instead of EEMPTY */
stat = NC_ENOOBJECT;
goto done;
}
if(mapp) *mapp = (NCZMAP*)z3map;
done:
reporterr(z3map);
nclistfreeall(content);
ncurifree(url);
if(stat) nczmap_close((NCZMAP*)z3map,0);
return ZUNTRACE(stat);
}
/* This uses url so we can get bucket */
static int
zs3truncate(const char *s3url)
{
int stat = NC_NOERR;
void* s3client = NULL;
NCURI* url = NULL;
NCURI* purl = NULL;
NCS3INFO info;
ZTRACE(6,"url=%s",s3url);
ncuriparse(s3url,&url);
if(url == NULL) {stat = NC_EURL; goto done;}
if((stat=NC_s3urlprocess(url,&info,&purl))) goto done;
if((s3client = NC_s3sdkcreateclient(&info))==NULL) {stat = NC_ES3; goto done;}
if((stat = s3clear(s3client,info.bucket,info.rootkey))) goto done;
done:
if(s3client) {stat=NC_s3sdkclose(s3client,&info,1,NULL);}
ncurifree(url);
ncurifree(purl);
(void)NC_s3clear(&info);
return stat;
}
/**************************************************/
/* Object API */
/*
@return NC_NOERR if key points to a content-bearing object.
@return NC_EEMPTY if object at key has no content.
@return NC_EXXX return true error
*/
static int
zs3exists(NCZMAP* map, const char* key)
{
int stat = NC_NOERR;
ZTRACE(6,"map=%s key=%s",map->url,key);
stat = zs3len(map,key,NULL);
return ZUNTRACE(stat);
}
/*
@return NC_NOERR if key points to a content-bearing object.
@return NC_EEMPTY if object at key has no content.
@return NC_EXXX return true error
*/
static int
zs3len(NCZMAP* map, const char* key, size64_t* lenp)
{
int stat = NC_NOERR;
ZS3MAP* z3map = (ZS3MAP*)map;
char* truekey = NULL;
ZTRACE(6,"map=%s key=%s",map->url,key);
if((stat = maketruekey(z3map->s3.rootkey,key,&truekey))) goto done;
switch (stat = NC_s3sdkinfo(z3map->s3client,z3map->s3.bucket,truekey,lenp,&z3map->errmsg)) {
case NC_NOERR: break;
case NC_EEMPTY:
if(lenp) *lenp = 0;
goto done;
default:
goto done;
}
done:
nullfree(truekey);
reporterr(z3map);
return ZUNTRACE(stat);
}
/*
@return NC_NOERR if object at key was read
@return NC_EEMPTY if object at key has no content.
@return NC_EXXX return true error
*/
static int
zs3read(NCZMAP* map, const char* key, size64_t start, size64_t count, void* content)
{
int stat = NC_NOERR;
ZS3MAP* z3map = (ZS3MAP*)map; /* cast to true type */
size64_t size = 0;
char* truekey = NULL;
ZTRACE(6,"map=%s key=%s start=%llu count=%llu",map->url,key,start,count);
if((stat = maketruekey(z3map->s3.rootkey,key,&truekey))) goto done;
switch (stat=NC_s3sdkinfo(z3map->s3client, z3map->s3.bucket, truekey, &size, &z3map->errmsg)) {
case NC_NOERR: break;
case NC_EEMPTY: goto done;
default: goto done;
}
/* Sanity checks */
if(start >= size || start+count > size)
{stat = NC_EEDGE; goto done;}
if(count > 0) {
if((stat = NC_s3sdkread(z3map->s3client, z3map->s3.bucket, truekey, start, count, content, &z3map->errmsg)))
goto done;
}
done:
nullfree(truekey);
reporterr(z3map);
return ZUNTRACE(stat);
}
/*
@return NC_NOERR if key content was written
@return NC_EEMPTY if object at key has no content.
@return NC_EXXX return true error
*/
static int
zs3write(NCZMAP* map, const char* key, size64_t count, const void* content)
{
int stat = NC_NOERR;
ZS3MAP* z3map = (ZS3MAP*)map; /* cast to true type */
char* chunk = NULL; /* use char* so we can do arithmetic with it */
char* truekey = NULL;
size64_t objsize;
ZTRACE(6,"map=%s key=%s count=%llu",map->url,key,count);
if((stat = maketruekey(z3map->s3.rootkey,key,&truekey))) goto done;
/* Apparently S3 has no write byterange operation, so we need to read the whole object,
copy data, and then rewrite */
switch (stat=NC_s3sdkinfo(z3map->s3client, z3map->s3.bucket, truekey, &objsize, &z3map->errmsg)) {
case NC_NOERR: /* Figure out the new size of the object */
break;
case NC_EEMPTY:
stat = NC_NOERR; /* reset */
break;
default: reporterr(z3map); goto done;
}
chunk = (char*)calloc(1,count); /* initialize it */
if(chunk == NULL)
{stat = NC_ENOMEM; goto done;}
/* overwrite the relevant part of the memory with the contents */
if(count > 0)
memcpy((char*)chunk,content,count);
/* (re-)write */
if((stat = NC_s3sdkwriteobject(z3map->s3client, z3map->s3.bucket, truekey, count, (void*)chunk, &z3map->errmsg)))
goto done;
done:
nullfree(truekey);
reporterr(z3map);
nullfree(chunk);
return ZUNTRACE(stat);
}
static int
zs3close(NCZMAP* map, int deleteit)
{
int stat = NC_NOERR;
ZS3MAP* z3map = (ZS3MAP*)map;
ZTRACE(6,"map=%s deleteit=%d",map->url, deleteit);
if(deleteit)
s3clear(z3map->s3client,z3map->s3.bucket,z3map->s3.rootkey);
if(z3map->s3client && z3map->s3.bucket && z3map->s3.rootkey) {
NC_s3sdkclose(z3map->s3client, &z3map->s3, deleteit, &z3map->errmsg);
}
reporterr(z3map);
z3map->s3client = NULL;
NC_s3clear(&z3map->s3);
nullfree(z3map->errmsg);
nczm_clear(map);
nullfree(map);
return ZUNTRACE(stat);
}
/*
Return a list of full keys immediately "below" a specified prefix,
but not including the prefix.
In theory, the returned list should be sorted in lexical order,
but it possible that it is not.
@return NC_NOERR if success, even if no keys returned.
@return NC_EXXX return true error
*/
static int
zs3search(NCZMAP* map, const char* prefix, NClist* matches)
{
int i,stat = NC_NOERR;
ZS3MAP* z3map = (ZS3MAP*)map;
char** list = NULL;
size_t nkeys;
NClist* tmp = NULL;
char* trueprefix = NULL;
char* newkey = NULL;
const char* p;
ZTRACE(6,"map=%s prefix0=%s",map->url,prefix);
if((stat = maketruekey(z3map->s3.rootkey,prefix,&trueprefix))) goto done;
if(*trueprefix != '/') return NC_EINTERNAL;
if((stat = NC_s3sdkgetkeys(z3map->s3client,z3map->s3.bucket,trueprefix,&nkeys,&list,&z3map->errmsg)))
goto done;
if(nkeys > 0) {
size_t tplen = strlen(trueprefix);
tmp = nclistnew();
/* Remove the trueprefix from the front of all the returned keys */
for(i=0;i<nkeys;i++) {
const char* l = list[i];
if(memcmp(trueprefix,l,tplen)==0) {
p = l+tplen; /* Point to start of suffix */
/* If the key is same as trueprefix, ignore it */
if(*p == '\0') continue;
if(nczm_segment1(p,&newkey)) goto done;
nclistpush(tmp,newkey); newkey = NULL;
}
}
/* Now remove duplicates */
for(i=0;i<nclistlength(tmp);i++) {
int j;
int duplicate = 0;
const char* is = nclistget(tmp,i);
for(j=0;j<nclistlength(matches);j++) {
const char* js = nclistget(matches,j);
if(strcmp(js,is)==0) {duplicate = 1; break;} /* duplicate */
}
if(!duplicate)
nclistpush(matches,strdup(is));
}
nclistfreeall(tmp); tmp = NULL;
}
#ifdef DEBUG
for(i=0;i<nclistlength(matches);i++) {
const char* is = nclistget(matches,i);
fprintf(stderr,"search: %s\n",is);
}
#endif
done:
nullfree(newkey);
nullfree(trueprefix);
reporterr(z3map);
nclistfreeall(tmp);
freevector(nkeys,list);
return ZUNTRACEX(stat,"|matches|=%d",(int)nclistlength(matches));
}
/**************************************************/
/* S3 Utilities */
/*
Remove all objects in bucket with keys which have
rootkey as prefix; rootkey is a truekey
*/
static int
s3clear(void* s3client, const char* bucket, const char* rootkey)
{
int stat = NC_NOERR;
char** list = NULL;
size_t nkeys = 0;
if(s3client && bucket && rootkey) {
if((stat = NC_s3sdksearch(s3client, bucket, rootkey, &nkeys, &list, NULL)))
goto done;
if(list != NULL) {
size_t i;
for(i=0;i<nkeys;i++) {
char* p = list[i];
/* If the key is the rootkey, skip it */
if(strcmp(rootkey,p)==0) continue;
#ifdef S3DEBUG
fprintf(stderr,"s3clear: %s\n",p);
#endif
if((stat = NC_s3sdkdeletekey(s3client, bucket, p, NULL)))
goto done;
}
}
}
done:
NCZ_freestringvec(nkeys,list);
return THROW(stat);
}
/* Prefix key with path to root to make true key */
static int
maketruekey(const char* rootpath, const char* key, char** truekeyp)
{
int stat = NC_NOERR;
char* truekey = NULL;
size_t len, rootlen, keylen;
if(truekeyp == NULL) goto done;
rootlen = strlen(rootpath);
keylen = strlen(key);
len = (rootlen+keylen+1+1+1);
truekey = (char*)malloc(len+1);
if(truekey == NULL) {stat = NC_ENOMEM; goto done;}
truekey[0] = '\0';
if(rootpath[0] != '/')
strlcat(truekey,"/",len+1);
strlcat(truekey,rootpath,len+1);
if(rootpath[rootlen-1] != '/')
strlcat(truekey,"/",len+1);
if(key[0] == '/') key++;
strlcat(truekey,key,len+1);
if(key[keylen-1] == '/') /* remove any trailing '/' */
truekey[strlen(truekey)-1] = '\0';
*truekeyp = truekey; truekey = NULL;
done:
nullfree(truekey);
return stat;
}
static void
freevector(size_t nkeys, char** list)
{
size_t i;
if(list) {
for(i=0;i<nkeys;i++) nullfree(list[i]);
nullfree(list);
}
}
/**************************************************/
/* External API objects */
NCZMAP_DS_API zmap_s3sdk;
NCZMAP_DS_API zmap_s3sdk = {
NCZM_S3SDK_V1,
ZS3_PROPERTIES,
zs3create,
zs3open,
zs3truncate,
};
static NCZMAP_API
nczs3sdkapi = {
NCZM_S3SDK_V1,
zs3close,
zs3exists,
zs3len,
zs3read,
zs3write,
zs3search,
};