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 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
|
/*
* Copyright 2018, University Corporation for Atmospheric Research
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*/
#undef DEBUG
/* Not sure this has any effect */
#define _LARGEFILE_SOURCE 1
#define _LARGEFILE64_SOURCE 1
#include "zincludes.h"
#include <errno.h>
#include <zip.h>
#include "fbits.h"
#include "ncpathmgr.h"
#undef CACHESEARCH
#define VERIFY
/*Mnemonic*/
#define FLAG_ISDIR 1
#define FLAG_CREATE 1
#define SKIPLAST 1
#define WHOLEPATH 0
#define NCZM_ZIP_V1 1
#define ZIP_PROPERTIES (NCZM_WRITEONCE|NCZM_ZEROSTART)
/*
Do a simple mapping of our simplified map model
to a zip-file
Every dataset is assumed to be rooted at some directory in the
zip file tree. So, its location is defined by some path to a
zip file representing the dataset.
For the object API, the mapping is as follows:
1. Every content-bearing object (e.g. .zgroup or .zarray) is mapped to a zip entry.
This means that if a key points to a content bearing object then
no other key can have that content bearing key as a suffix.
2. The meta data containing files are assumed to contain
UTF-8 character data.
3. The chunk containing files are assumed to contain raw unsigned 8-bit byte data.
4. The objects may or may not be compressed; this implementation writes uncompressed objects.
*/
/* define the var name containing an objects content */
#define ZCONTENT "data"
/* Define the "subclass" of NCZMAP */
typedef struct ZZMAP {
NCZMAP map;
char* root;
char* dataset; /* prefix for all keys in zip file */
zip_t* archive;
char** searchcache;
} ZZMAP;
typedef zip_int64_t ZINDEX;;
/* Forward */
static NCZMAP_API zapi;
static int zipclose(NCZMAP* map, int delete);
static int zzcreategroup(ZZMAP*, const char* key, int nskip);
static int zzlookupobj(ZZMAP*, const char* key, ZINDEX* fd);
static int zzlen(ZZMAP* zzmap, ZINDEX zindex, size64_t* lenp);
static int zipmaperr(ZZMAP* zzmap);
static int ziperr(zip_error_t* zerror);
static int ziperrno(int zerror);
static void freesearchcache(char** cache);
static int zzinitialized = 0;
static void
zipinitialize(void)
{
if(!zzinitialized) {
ZTRACE(7,NULL);
zzinitialized = 1;
(void)ZUNTRACE(NC_NOERR);
}
}
/* Define the Dataset level API */
/*
@param datasetpath abs path in the file tree of the root of the dataset'
might be a relative path.
@param mode the netcdf-c mode flags
@param flags extra flags
@param flags extra parameters
@param mapp return the map object in this
*/
static int
zipcreate(const char *path, int mode, size64_t flags, void* parameters, NCZMAP** mapp)
{
int stat = NC_NOERR;
ZZMAP* zzmap = NULL;
NCURI* url = NULL;
zip_flags_t zipflags = 0;
int zerrno = ZIP_ER_OK;
ZINDEX zindex = -1;
char* abspath = NULL;
NC_UNUSED(parameters);
ZTRACE(6,"path=%s mode=%d flag=%llu",path,mode,flags);
if(!zzinitialized) zipinitialize();
/* Fixup mode flags */
mode = (NC_NETCDF4 | NC_WRITE | mode);
/* path must be a url with file: protocol*/
ncuriparse(path,&url);
if(url == NULL)
{stat = NC_EURL; goto done;}
if(strcasecmp(url->protocol,"file") != 0)
{stat = NC_EURL; goto done;}
/* Build the zz state */
if((zzmap = calloc(1,sizeof(ZZMAP))) == NULL)
{stat = NC_ENOMEM; goto done;}
zzmap->map.format = NCZM_ZIP;
zzmap->map.url = ncuribuild(url,NULL,NULL,NCURIALL);
zzmap->map.flags = flags;
/* create => NC_WRITE */
zzmap->map.mode = mode;
zzmap->map.api = &zapi;
/* Since root is in canonical form, we need to convert to local form */
if((zzmap->root = NCpathcvt(url->path))==NULL)
{stat = NC_ENOMEM; goto done;}
/* Make the root path be absolute */
if((abspath = NCpathabsolute(zzmap->root)) == NULL)
{stat = NC_EURL; goto done;}
nullfree(zzmap->root);
zzmap->root = abspath;
abspath = NULL;
/* Extract the dataset name */
if((stat = nczm_basename(url->path,&zzmap->dataset))) goto done;
/* Set zip openflags */
zipflags |= ZIP_CREATE;
if(fIsSet(mode,NC_NOCLOBBER))
zipflags |= ZIP_EXCL;
else
zipflags |= ZIP_TRUNCATE;
#ifdef VERIFY
zipflags |= ZIP_CHECKCONS;
#endif
if((zzmap->archive = zip_open(zzmap->root,zipflags,&zerrno))==NULL)
{stat = ziperrno(zerrno); goto done;}
/* Tell it about the dataset as a dir */
if((zindex = zip_dir_add(zzmap->archive, zzmap->dataset, ZIP_FL_ENC_UTF_8))<0)
{stat = zipmaperr(zzmap); goto done;}
/* Dataset superblock will be written by higher layer */
if(mapp) {*mapp = (NCZMAP*)zzmap; zzmap = NULL;}
done:
nullfree(abspath);
ncurifree(url);
if(zzmap) zipclose((NCZMAP*)zzmap,1);
return ZUNTRACE(stat);
}
/*
@param datasetpath abs path in the file tree of the root of the dataset'
might be a relative path.
@param mode the netcdf-c mode flags
@param flags extra flags
@param flags extra parameters
@param mapp return the map object in this
*/
static int
zipopen(const char *path, int mode, size64_t flags, void* parameters, NCZMAP** mapp)
{
int stat = NC_NOERR;
ZZMAP* zzmap = NULL;
NCURI*url = NULL;
zip_flags_t zipflags = 0;
int zerrno = ZIP_ER_OK;
char* abspath = NULL;
NC_UNUSED(parameters);
ZTRACE(6,"path=%s mode=%d flags=%llu",path,mode,flags);
if(!zzinitialized) zipinitialize();
/* Fixup mode flags */
mode = (NC_NETCDF4 | mode);
/* path must be a url with file: protocol*/
ncuriparse(path,&url);
if(url == NULL)
{stat = NC_EURL; goto done;}
if(strcasecmp(url->protocol,"file") != 0)
{stat = NC_EURL; goto done;}
/* Build the zz state */
if((zzmap = calloc(1,sizeof(ZZMAP))) == NULL)
{stat = NC_ENOMEM; goto done;}
zzmap->map.format = NCZM_ZIP;
zzmap->map.url = ncuribuild(url,NULL,NULL,NCURIALL);
zzmap->map.flags = flags;
zzmap->map.mode = mode;
zzmap->map.api = (NCZMAP_API*)&zapi;
/* Since root is in canonical form, we need to convert to local form */
if((zzmap->root = NCpathcvt(url->path))==NULL)
{stat = NC_ENOMEM; goto done;}
/* Make the root path be absolute */
if((abspath = NCpathabsolute(zzmap->root)) == NULL)
{stat = NC_EURL; goto done;}
nullfree(zzmap->root);
zzmap->root = abspath;
abspath = NULL;
/* Set zip open flags */
zipflags |= ZIP_CHECKCONS;
if(!fIsSet(mode,NC_WRITE))
zipflags |= ZIP_RDONLY;
#ifdef VERIFY
zipflags |= ZIP_CHECKCONS;
#endif
/* Open the file */
if((zzmap->archive = zip_open(zzmap->root,zipflags,&zerrno))==NULL)
{stat = ziperrno(zerrno); goto done;}
/* Use entry 0 to obtain the dataset name */
{
const char* name;
zip_int64_t num_entries;
num_entries = zip_get_num_entries(zzmap->archive, (zip_flags_t)0);
if(num_entries == 0) {stat = NC_EEMPTY; goto done;}
/* get 0'th entry name */
if((name = zip_get_name(zzmap->archive, 0, (zip_flags_t)0))==NULL)
{stat = zipmaperr(zzmap); goto done;}
if(name[0] == '\0' || name[0] == '/')
{stat = NC_EBADID; goto done;}
/* Extract the first segment as the dataset name */
if((nczm_segment1(name,&zzmap->dataset))) goto done;
}
/* Dataset superblock will be read by higher layer */
if(mapp) {*mapp = (NCZMAP*)zzmap; zzmap = NULL;}
done:
nullfree(abspath);
ncurifree(url);
if(zzmap) zipclose((NCZMAP*)zzmap,0);
return ZUNTRACE(stat);
}
/**************************************************/
/* Object API */
static int
zipexists(NCZMAP* map, const char* key)
{
int stat = NC_NOERR;
ZZMAP* zzmap = (ZZMAP*)map;
ZINDEX zindex = -1;
ZTRACE(6,"map=%s key=%s",map->url,key);
switch(stat=zzlookupobj(zzmap,key,&zindex)) {
case NC_NOERR: break;
case NC_ENOOBJECT: stat = NC_EEMPTY; break;
case NC_EEMPTY: break;
default: break;
}
return ZUNTRACE(stat);
}
static int
ziplen(NCZMAP* map, const char* key, size64_t* lenp)
{
int stat = NC_NOERR;
ZZMAP* zzmap = (ZZMAP*)map;
size64_t len = 0;
ZINDEX zindex = -1;
ZTRACE(6,"map=%s key=%s",map->url,key);
switch(stat = zzlookupobj(zzmap,key,&zindex)) {
case NC_NOERR:
if((stat = zzlen(zzmap,zindex,&len))) goto done;
break;
case NC_ENOOBJECT: stat = NC_EEMPTY; len = 0; break;
case NC_EEMPTY: len = 0; break; /* |dir|==0 */
default: goto done;
}
if(lenp) *lenp = len;
done:
return ZUNTRACEX(stat,"len=%llu",(lenp?*lenp:777777777777));
}
static int
zipread(NCZMAP* map, const char* key, size64_t start, size64_t count, void* content)
{
int stat = NC_NOERR;
ZZMAP* zzmap = (ZZMAP*)map; /* cast to true type */
zip_file_t* zfile = NULL;
ZINDEX zindex = -1;
zip_flags_t zipflags = 0;
int zerrno;
size64_t endpoint;
char* buffer = NULL;
char* truekey = NULL;
zip_int64_t red = 0;
ZTRACE(6,"map=%s key=%s start=%llu count=%llu",map->url,key,start,count);
switch(stat = zzlookupobj(zzmap,key,&zindex)) {
case NC_NOERR: break;
case NC_ENOOBJECT: stat = NC_EEMPTY; /* fall thru */
case NC_EEMPTY: /* its a dir; fall thru*/
default: goto done;
}
/* Note, assume key[0] == '/' */
if((stat = nczm_appendn(&truekey,2,zzmap->dataset,key)))
goto done;
zfile = zip_fopen(zzmap->archive, truekey, zipflags);
if(zfile == NULL)
{stat = (zipmaperr(zzmap)); goto done;}
/* Ideally, we would like to seek to the start point,
but that will fail if the file is compressed, so
we need to read whole thing and extract what we need
*/
/* read data starting at zero */
if(start == 0) { /*optimize to read directly into content */
if((red = zip_fread(zfile, content, (zip_uint64_t)count)) < 0)
{stat = (zipmaperr(zzmap)); goto done;}
if(red < count) {stat = NC_EINTERNAL; goto done;}
} else {
endpoint = start + count;
if((buffer = malloc(endpoint))==NULL) /* consider caching this */
{stat = NC_ENOMEM; goto done;}
if((red = zip_fread(zfile, buffer, (zip_uint64_t)endpoint)) < 0)
{stat = (zipmaperr(zzmap)); goto done;}
if(red < endpoint) {stat = NC_EINTERNAL; goto done;}
/* Extract what we need */
memcpy(content,buffer+start,count);
}
done:
nullfree(truekey);
nullfree(buffer);
if(zfile != NULL && (zerrno=zip_fclose(zfile)) != 0)
{stat = ziperrno(zerrno);}
return ZUNTRACE(stat);
}
static int
zipwrite(NCZMAP* map, const char* key, size64_t start, size64_t count, const void* content)
{
int stat = NC_NOERR;
ZZMAP* zzmap = (ZZMAP*)map; /* cast to true type */
char* truekey = NULL;
zip_flags_t zflags = 0;
zip_source_t* zs = NULL;
ZINDEX zindex = -1;
zip_int32_t compression = 0;
zip_error_t zerror;
void* localbuffer = NULL;
ZTRACE(6,"map=%s key=%s start=%llu count=%llu",map->url,key,start,count);
zip_error_init(&zerror);
if(start != 0 && (ZIP_PROPERTIES & NCZM_ZEROSTART))
{stat = NC_EEDGE; goto done;}
/* Create directories */
if((stat = zzcreategroup(zzmap,key,SKIPLAST))) goto done;
switch(stat = zzlookupobj(zzmap,key,&zindex)) {
case NC_NOERR:
stat = NC_EOBJECT; //goto done; /* Zip files are write once */
zflags |= ZIP_FL_OVERWRITE;
break;
case NC_ENOOBJECT: stat = NC_NOERR; break;
case NC_EEMPTY: /* its a dir; fall thru */
default: goto done;
}
zflags |= ZIP_FL_ENC_UTF_8;
compression = ZIP_CM_STORE;
/* prepend the dataset to get truekey */
/* Note, assume key[0] == '/' */
if((stat = nczm_appendn(&truekey,2,zzmap->dataset,key)))
goto done;
if(count > 0) {
/* Apparently, the buffer to be written needs to be around at zip_close
so we need to make a local copy that will be freed by libzip after it is
no longer needed */
/* Duplicate the buffer */
if((localbuffer = malloc((size_t)count))==NULL)
{stat = NC_ENOMEM; goto done;}
memcpy(localbuffer,content,count);
}
if((zs = zip_source_buffer(zzmap->archive, localbuffer, (zip_uint64_t)count, 1)) == NULL)
{stat = zipmaperr(zzmap); goto done;}
if((zindex = zip_file_add(zzmap->archive, truekey, zs, zflags))<0)
{stat = zipmaperr(zzmap); goto done;}
zs = NULL; localbuffer = NULL;
if(zip_set_file_compression(zzmap->archive, zindex, compression, 0) < 0)
{stat = zipmaperr(zzmap); goto done;}
freesearchcache(zzmap->searchcache); zzmap->searchcache = NULL;
done:
if(zs) zip_source_free(zs);
nullfree(localbuffer);
zip_error_fini(&zerror);
nullfree(truekey);
return ZUNTRACE(stat);
}
static int
zipclose(NCZMAP* map, int delete)
{
int stat = NC_NOERR;
int zerrno = 0;
ZZMAP* zzmap = (ZZMAP*)map;
if(zzmap == NULL) return NC_NOERR;
ZTRACE(6,"map=%s delete=%d",map->url,delete);
/* Close the zip */
if(delete)
zip_discard(zzmap->archive);
else {
if((zerrno=zip_close(zzmap->archive)))
stat = ziperrno(zerrno);
}
if(delete)
NCremove(zzmap->root);
zzmap->archive = NULL;
nczm_clear(map);
nullfree(zzmap->root);
nullfree(zzmap->dataset);
zzmap->root = NULL;
freesearchcache(zzmap->searchcache);
free(zzmap);
return ZUNTRACE(stat);
}
/*
Return a list of full keys immediately under a specified prefix key.
In theory, the returned list should be sorted in lexical order,
but it possible that it is not.
Note that for zip, it is not possible to get just the keys of length n+1,
so, this search must get all keys and process them one by one.
@return NC_NOERR if success, even if no keys returned.
@return NC_EXXX return true error
*/
int
zipsearch(NCZMAP* map, const char* prefix0, NClist* matches)
{
int stat = NC_NOERR;
ZZMAP* zzmap = (ZZMAP*)map;
char* trueprefix = NULL;
size_t truelen;
zip_int64_t num_entries, i;
char** cache = NULL;
size_t prefixlen;
NClist* tmp = NULL;
ZTRACE(6,"map=%s prefix0=%s",map->url,prefix0);
/* prefix constraints:
1. prefix is "/"
2. or prefix has leading '/' and no trailing '/'
*/
/* Fix up the prefix; including adding the dataset name to the front */
if(prefix0 == NULL || strlen(prefix0)==0)
prefix0 = "/";
/* make sure that prefix0 has leading '/' */
if(prefix0[0] != '/')
{stat = NC_EINVAL; goto done;}
prefixlen = strlen(prefix0);
truelen = prefixlen+strlen(zzmap->dataset)+1; /* possible trailing '/'*/
if((trueprefix = (char*)malloc(truelen+1+1))==NULL) /* nul term */
{stat = NC_ENOMEM; goto done;}
/* Build the true prefix */
trueprefix[0] = '\0';
strlcat(trueprefix,zzmap->dataset,truelen+1);
strlcat(trueprefix,prefix0,truelen+1); /* recall prefix starts with '/' */
/* If the prefix did not end in '/', then add it */
if(prefixlen > 1 && prefix0[prefixlen-1] != '/')
strlcat(trueprefix,"/",truelen+1);
truelen = strlen(trueprefix);
/* Get number of entries */
num_entries = zip_get_num_entries(zzmap->archive, (zip_flags_t)0);
#ifdef CACHESEARCH
if(num_entries > 0 && zzmap->searchcache == NULL) {
/* Release the current cache */
freesearchcache(zzmap->searchcache);
zzmap->searchcache = NULL;
/* Re-build the searchcache */
if((cache = calloc(sizeof(char*),num_entries+1))==NULL)
{stat = NC_ENOMEM; goto done;}
for(i=0;i < num_entries; i++) {
const char *name = NULL;
/* get ith entry */
name = zip_get_name(zzmap->archive, i, (zip_flags_t)0);
/* Add to cache */
if((cache[i] = strdup(name))==NULL)
{stat = NC_ENOMEM; goto done;}
}
cache[num_entries] = NULL;
zzmap->searchcache = cache; cache = NULL;
}
#endif
#ifdef CACHESEARCH
if(zzmap->searchcache != NULL)
#endif
{
const char *key = NULL;
size_t keylen = 0;
char* match = NULL;
const char* p;
tmp = nclistnew();
/* Walk cache looking for names with prefix plus exactly one other segment */
for(i=0;i < num_entries; i++) {
/* get ith entry */
#ifdef CACHESEARCH
key = zzmap->searchcache[i];
#else
key = zip_get_name(zzmap->archive, i, (zip_flags_t)0);
#endif
keylen = strlen(key);
/* Does this name begin with trueprefix? */
if(keylen > 0
&& (keylen <= truelen || strncmp(key,trueprefix,truelen) != 0))
continue; /* no match */
/* skip trueprefix and extract first segment */
p = (key+truelen);
if(*p == '\0') continue; /* key is all there is, so ignore it */
/* get seg 1 */
if((nczm_segment1(p,&match))) goto done;
nclistpush(tmp,match); match = 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));
}
}
done:
nclistfreeall(tmp);
if(cache != NULL) freesearchcache(cache);
nullfree(trueprefix);
return ZUNTRACEX(stat,"|matches|=%d",(int)nclistlength(matches));
}
/**************************************************/
/* Utilities */
/* Guarantee existence of a group */
static int
zzcreategroup(ZZMAP* zzmap, const char* key, int nskip)
{
int stat = NC_NOERR;
int i, len;
char* fullpath = NULL;
NCbytes* path = ncbytesnew();
NClist* segments = nclistnew();
ZINDEX zindex;
zip_flags_t zipflags = ZIP_FL_ENC_UTF_8;
ZTRACE(7,"map=%s key=%s nskip=%d",zzmap->map.url,key,nskip);
if((stat=nczm_split(key,segments)))
goto done;
len = nclistlength(segments);
len -= nskip; /* leave off last nskip segments */
/* Start with the dataset */
ncbytescat(path,zzmap->dataset);
for(i=0;i<len;i++) {
const char* seg = nclistget(segments,i);
ncbytescat(path,"/");
ncbytescat(path,seg);
/* open and/or create the directory */
if((zindex = zip_dir_add(zzmap->archive, ncbytescontents(path), zipflags))<0) {
switch(stat = zipmaperr(zzmap)) {
case NC_EOBJECT: stat = NC_NOERR; break; /* ok */
default:
goto done;
}
}
}
done:
nullfree(fullpath);
ncbytesfree(path);
nclistfreeall(segments);
return ZUNTRACE(stat);
}
/* Lookup a key
@return NC_NOERR if found and is a content-bearing object
@return NC_ENOOBJECT if not found
@return NC_EEMPTY if a dir
*/
static int
zzlookupobj(ZZMAP* zzmap, const char* key, ZINDEX* zindex)
{
int stat = NC_NOERR;
char* zipfile = NULL;
char* zipdir = NULL;
ZTRACE(7,"map=%s key=%s",zzmap->map.url,key);
if(key == NULL)
{stat = NC_EINVAL; goto done;}
/* Note, assume key[0] == '/' */
if((stat = nczm_appendn(&zipfile,2,zzmap->dataset,key)))
goto done;
/* See if exists as a file */
if((*zindex = zip_name_locate(zzmap->archive, zipfile, 0))<0) {
/* do a second check to see if zippath as a dir */
if((stat = nczm_appendn(&zipdir,2,zipfile,"/")))
goto done;
if((*zindex = zip_name_locate(zzmap->archive, zipdir, 0))<0)
{stat = zipmaperr(zzmap); goto done;}
stat = NC_EEMPTY; /* signal a directory */
}
done:
nullfree(zipfile);
nullfree(zipdir);
return ZUNTRACE(stat);
}
/* Get length given the index */
static int
zzlen(ZZMAP* zzmap, ZINDEX zindex, size64_t* lenp)
{
int stat = NC_NOERR;
size64_t len = 0;
zip_stat_t statbuf;
zip_flags_t zipflags = 0;
assert(zindex >= 0);
ZTRACE(6,"zzmap=%s index=%llu",zzmap,zindex);
zip_stat_init(&statbuf);
if(zip_stat_index(zzmap->archive,zindex,zipflags,&statbuf) < 0)
{stat = (zipmaperr(zzmap)); goto done;}
assert(statbuf.valid & ZIP_STAT_SIZE);
len = statbuf.size; /* Always return uncompressed size */
if(lenp) *lenp = len;
done:
return ZUNTRACEX(stat,"len=%llu",(lenp?*lenp:777777777777));
}
static void
freesearchcache(char** cache)
{
char** p;
if(cache == NULL) return;
for(p=cache;*p;p++) {
free(*p);
}
free(cache);
}
/**************************************************/
/* External API objects */
NCZMAP_DS_API zmap_zip = {
NCZM_ZIP_V1,
ZIP_PROPERTIES,
zipcreate,
zipopen,
};
static NCZMAP_API zapi = {
NCZM_ZIP_V1,
zipclose,
zipexists,
ziplen,
zipread,
zipwrite,
zipsearch,
};
static int
zipmaperr(ZZMAP* zzmap)
{
zip_error_t* zerr = (zip_error_t*)zip_get_error(zzmap->archive);
return ziperr(zerr);
}
static int
ziperr(zip_error_t* zerror)
{
int zerrno = zip_error_code_zip(zerror);
return ziperrno(zerrno);
}
static int
ziperrno(int zerror)
{
int stat = NC_NOERR;
switch (zerror) {
case ZIP_ER_OK: stat = NC_NOERR; break;
case ZIP_ER_EXISTS: stat = NC_EOBJECT; break;
case ZIP_ER_MEMORY: stat = NC_ENOMEM; break;
case ZIP_ER_SEEK:
case ZIP_ER_READ:
case ZIP_ER_WRITE:
case ZIP_ER_TMPOPEN:
case ZIP_ER_CRC: stat = NC_EIO; break;
case ZIP_ER_ZIPCLOSED: stat = NC_EBADID; break;
case ZIP_ER_NOENT: stat = NC_ENOOBJECT; break;
case ZIP_ER_OPEN: stat = NC_EACCESS; break;
case ZIP_ER_INVAL: stat = NC_EINVAL; break;
case ZIP_ER_INTERNAL: stat = NC_EINTERNAL; break;
case ZIP_ER_REMOVE: stat = NC_ECANTREMOVE; break;
case ZIP_ER_DELETED: stat = NC_ENOOBJECT; break;
case ZIP_ER_RDONLY: stat = NC_EPERM; break;
case ZIP_ER_CHANGED: stat = NC_EOBJECT; break;
default: stat = NC_ENCZARR; break;
}
return stat;
}
|