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
|
/*
* Copyright 2018, University Corporation for Atmospheric Research
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#if defined(_WIN32) && !defined(__MINGW32__)
#include "XGetopt.h"
#endif
#include "zincludes.h"
#include "ncpathmgr.h"
#undef DEBUG
#define AWSHOST ".amazonaws.com"
typedef enum S3op {
S3_NONE=0,
S3_HOST=1,
S3_BUCKET=2,
S3_KEY=3,
} S3op;
/* Command line options */
struct S3options {
int debug;
S3op op;
char* url;
} s3options;
/*Forward*/
static int processurl(S3op op, const char* url, char** piece);
static void
zs3usage(void)
{
fprintf(stderr,"usage: zs3parse [-h|-b|-k] <url>|<file>\n");
exit(1);
}
int
main(int argc, char** argv)
{
int stat = NC_NOERR;
int c;
char* piece = NULL;
memset((void*)&s3options,0,sizeof(s3options));
while ((c = getopt(argc, argv, "vhbk")) != EOF) {
switch(c) {
case 'b':
s3options.op = S3_BUCKET;
break;
case 'h':
s3options.op = S3_HOST;
break;
case 'k':
s3options.op = S3_KEY;
break;
case 'v':
zs3usage();
goto done;
case '?':
fprintf(stderr,"unknown option: %c\n",c);
goto fail;
}
}
/* get url|file argument */
argc -= optind;
argv += optind;
if (argc > 1) {
fprintf(stderr, "zs3parse: only one url|file argument permitted\n");
goto fail;
}
if (argc == 0) {
fprintf(stderr, "zs3parse: no url|file specified\n");
goto fail;
}
s3options.url = strdup(argv[0]);
stat = processurl(s3options.op, s3options.url, &piece);
if(stat == NC_NOERR) {
if(piece == NULL) goto fail;
printf("%s",piece);
}
done:
nullfree(piece);
/* Reclaim s3options */
nullfree(s3options.url);
if(stat)
fprintf(stderr,"fail: %s\n",nc_strerror(stat));
return (stat ? 1 : 0);
fail:
stat = NC_EINVAL;
goto done;
}
static int
processurl(S3op op, const char* surl, char** piece)
{
int stat = NC_NOERR;
NClist* segments = NULL;
NCbytes* buf = ncbytesnew();
char* value = NULL;
char* host = NULL;
char* bucket = NULL;
char* prefix = NULL;
NCURI* url = NULL;
ncuriparse(surl,&url);
if(url == NULL)
{stat = NC_EURL; goto done;}
/* do some verification */
if(strcmp(url->protocol,"https") != 0
&& strcmp(url->protocol,"http") != 0)
{stat = NC_EURL; goto done;}
if(url->host == NULL || strlen(url->host) == 0)
{stat = NC_EURL; goto done;}
if((host = strdup(url->host))==NULL)
{stat = NC_ENOMEM; goto done;}
/* We have to process the path to get the bucket,
and remove it from the path */
if(url->path == NULL || strlen(url->path) == 0)
{stat = NC_EURL; goto done;}
/* split the path by "/" */
nclistfreeall(segments);
segments = nclistnew();
if((stat = nczm_split_delim(url->path,'/',segments))) goto done;
if(nclistlength(segments) == 0)
{stat = NC_EURL; goto done;}
bucket = ((char*)nclistremove(segments,0));
if((stat = nczm_join(segments,&prefix))) goto done;
nclistfreeall(segments); segments = NULL;
switch (op) {
case S3_HOST: value = host; host = NULL; break;
case S3_BUCKET: value = bucket; bucket = NULL; break;
case S3_KEY: value = prefix; prefix = NULL; break;
default: stat = NC_EURL; goto done;
}
if(piece) {*piece = value; value = NULL;}
done:
ncurifree(url);
nullfree(value);
nullfree(host);
nullfree(bucket);
nullfree(prefix);
ncbytesfree(buf);
nclistfreeall(segments);
return stat;
}
|