2017-09-01 04:36:54 +08:00
|
|
|
/*********************************************************************
|
2018-12-07 05:29:57 +08:00
|
|
|
* Copyright 2018, UCAR/Unidata
|
2017-09-01 04:36:54 +08:00
|
|
|
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
|
|
|
|
*********************************************************************/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
#include "netcdf.h"
|
|
|
|
#include "ncuri.h"
|
|
|
|
#include "ncbytes.h"
|
2019-09-30 02:59:28 +08:00
|
|
|
#include "nclist.h"
|
2017-09-01 04:36:54 +08:00
|
|
|
#include "nclog.h"
|
2020-10-14 09:12:15 +08:00
|
|
|
#include "ncpathmgr.h"
|
2017-09-01 04:36:54 +08:00
|
|
|
|
|
|
|
#define NC_MAX_PATH 4096
|
|
|
|
|
|
|
|
#define LBRACKET '['
|
|
|
|
#define RBRACKET ']'
|
|
|
|
|
|
|
|
/**************************************************/
|
|
|
|
/**
|
|
|
|
* Provide a hidden interface to allow utilities
|
|
|
|
* to check if a given path name is really an ncdap4 url.
|
|
|
|
* If no, return null, else return basename of the url
|
|
|
|
* minus any extension.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
NC__testurl(const char* path, char** basenamep)
|
|
|
|
{
|
|
|
|
NCURI* uri;
|
|
|
|
int ok = NC_NOERR;
|
2019-09-30 02:59:28 +08:00
|
|
|
if(ncuriparse(path,&uri))
|
2017-09-01 04:36:54 +08:00
|
|
|
ok = NC_EURL;
|
|
|
|
else {
|
|
|
|
char* slash = (uri->path == NULL ? NULL : strrchr(uri->path, '/'));
|
|
|
|
char* dot;
|
|
|
|
if(slash == NULL) slash = (char*)path; else slash++;
|
|
|
|
slash = nulldup(slash);
|
|
|
|
if(slash == NULL)
|
|
|
|
dot = NULL;
|
|
|
|
else
|
|
|
|
dot = strrchr(slash, '.');
|
|
|
|
if(dot != NULL && dot != slash) *dot = '\0';
|
|
|
|
if(basenamep)
|
|
|
|
*basenamep=slash;
|
|
|
|
else if(slash)
|
|
|
|
free(slash);
|
|
|
|
}
|
|
|
|
ncurifree(uri);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return 1 if this machine is little endian */
|
|
|
|
int
|
|
|
|
NC_isLittleEndian(void)
|
|
|
|
{
|
|
|
|
union {
|
|
|
|
unsigned char bytes[SIZEOF_INT];
|
|
|
|
int i;
|
|
|
|
} u;
|
|
|
|
u.i = 1;
|
|
|
|
return (u.bytes[0] == 1 ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
NC_backslashEscape(const char* s)
|
|
|
|
{
|
|
|
|
const char* p;
|
|
|
|
char* q;
|
|
|
|
size_t len;
|
|
|
|
char* escaped = NULL;
|
|
|
|
|
|
|
|
len = strlen(s);
|
|
|
|
escaped = (char*)malloc(1+(2*len)); /* max is everychar is escaped */
|
|
|
|
if(escaped == NULL) return NULL;
|
|
|
|
for(p=s,q=escaped;*p;p++) {
|
|
|
|
char c = *p;
|
|
|
|
switch (c) {
|
|
|
|
case '\\':
|
|
|
|
case '/':
|
|
|
|
case '.':
|
|
|
|
case '@':
|
|
|
|
*q++ = '\\'; *q++ = '\\';
|
|
|
|
break;
|
|
|
|
default: *q++ = c; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
return escaped;
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
NC_backslashUnescape(const char* esc)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char* s;
|
|
|
|
const char* p;
|
|
|
|
char* q;
|
|
|
|
|
|
|
|
if(esc == NULL) return NULL;
|
|
|
|
len = strlen(esc);
|
|
|
|
s = (char*)malloc(len+1);
|
|
|
|
if(s == NULL) return NULL;
|
|
|
|
for(p=esc,q=s;*p;) {
|
|
|
|
switch (*p) {
|
|
|
|
case '\\':
|
|
|
|
p++;
|
|
|
|
/* fall thru */
|
|
|
|
default: *q++ = *p++; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
NC_entityescape(const char* s)
|
|
|
|
{
|
|
|
|
const char* p;
|
|
|
|
char* q;
|
|
|
|
size_t len;
|
|
|
|
char* escaped = NULL;
|
|
|
|
const char* entity;
|
|
|
|
|
|
|
|
len = strlen(s);
|
|
|
|
escaped = (char*)malloc(1+(6*len)); /* 6 = |'| */
|
|
|
|
if(escaped == NULL) return NULL;
|
|
|
|
for(p=s,q=escaped;*p;p++) {
|
|
|
|
char c = *p;
|
|
|
|
switch (c) {
|
|
|
|
case '&': entity = "&"; break;
|
|
|
|
case '<': entity = "<"; break;
|
|
|
|
case '>': entity = ">"; break;
|
|
|
|
case '"': entity = """; break;
|
|
|
|
case '\'': entity = "'"; break;
|
|
|
|
default : entity = NULL; break;
|
|
|
|
}
|
|
|
|
if(entity == NULL)
|
|
|
|
*q++ = c;
|
|
|
|
else {
|
|
|
|
len = strlen(entity);
|
|
|
|
memcpy(q,entity,len);
|
|
|
|
q+=len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
return escaped;
|
|
|
|
}
|
|
|
|
|
2021-04-22 04:59:15 +08:00
|
|
|
char*
|
|
|
|
/*
|
|
|
|
Depending on the platform, the shell will sometimes
|
|
|
|
pass an escaped octotherpe character without removing
|
|
|
|
the backslash. So this function is appropriate to be called
|
|
|
|
on possible url paths to unescape such cases. See e.g. ncgen.
|
|
|
|
*/
|
|
|
|
NC_shellUnescape(const char* esc)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char* s;
|
|
|
|
const char* p;
|
|
|
|
char* q;
|
|
|
|
|
|
|
|
if(esc == NULL) return NULL;
|
|
|
|
len = strlen(esc);
|
|
|
|
s = (char*)malloc(len+1);
|
|
|
|
if(s == NULL) return NULL;
|
|
|
|
for(p=esc,q=s;*p;) {
|
|
|
|
switch (*p) {
|
|
|
|
case '\\':
|
|
|
|
if(p[1] == '#')
|
|
|
|
p++;
|
|
|
|
/* fall thru */
|
|
|
|
default: *q++ = *p++; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-09-01 04:36:54 +08:00
|
|
|
/**
|
2017-09-03 08:09:36 +08:00
|
|
|
Wrap mktmp and return the generated path,
|
|
|
|
or null if failed.
|
|
|
|
Base is the base file path. XXXXX is appended
|
|
|
|
to allow mktmp add its unique id.
|
|
|
|
Return the generated path.
|
2017-09-01 04:36:54 +08:00
|
|
|
*/
|
|
|
|
|
2017-09-03 08:09:36 +08:00
|
|
|
char*
|
|
|
|
NC_mktmp(const char* base)
|
2017-09-01 04:36:54 +08:00
|
|
|
{
|
2021-05-15 01:33:03 +08:00
|
|
|
int fd = -1;
|
|
|
|
char* tmp = NULL;
|
|
|
|
size_t len;
|
2017-09-03 08:09:36 +08:00
|
|
|
|
2021-05-15 01:33:03 +08:00
|
|
|
len = strlen(base)+6+1;
|
|
|
|
if((tmp = (char*)malloc(len))==NULL)
|
|
|
|
goto done;
|
|
|
|
strncpy(tmp,base,len);
|
|
|
|
strlcat(tmp, "XXXXXX", len);
|
|
|
|
fd = NCmkstemp(tmp);
|
2017-09-01 04:36:54 +08:00
|
|
|
if(fd < 0) {
|
|
|
|
nclog(NCLOGERR, "Could not create temp file: %s",tmp);
|
2021-05-15 01:33:03 +08:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
if(fd >= 0) close(fd);
|
|
|
|
return tmp;
|
2017-09-01 04:36:54 +08:00
|
|
|
}
|
|
|
|
|
2018-10-11 03:32:17 +08:00
|
|
|
int
|
|
|
|
NC_readfile(const char* filename, NCbytes* content)
|
|
|
|
{
|
|
|
|
int ret = NC_NOERR;
|
|
|
|
FILE* stream = NULL;
|
|
|
|
char part[1024];
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
stream = NCfopen(filename,"rb");
|
|
|
|
#else
|
|
|
|
stream = NCfopen(filename,"r");
|
|
|
|
#endif
|
|
|
|
if(stream == NULL) {ret=errno; goto done;}
|
|
|
|
for(;;) {
|
|
|
|
size_t count = fread(part, 1, sizeof(part), stream);
|
|
|
|
if(count <= 0) break;
|
|
|
|
ncbytesappendn(content,part,count);
|
|
|
|
if(ferror(stream)) {ret = NC_EIO; goto done;}
|
|
|
|
if(feof(stream)) break;
|
|
|
|
}
|
|
|
|
ncbytesnull(content);
|
|
|
|
done:
|
|
|
|
if(stream) fclose(stream);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
NC_writefile(const char* filename, size_t size, void* content)
|
|
|
|
{
|
|
|
|
int ret = NC_NOERR;
|
|
|
|
FILE* stream = NULL;
|
|
|
|
void* p;
|
|
|
|
size_t remain;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
stream = NCfopen(filename,"wb");
|
|
|
|
#else
|
|
|
|
stream = NCfopen(filename,"w");
|
|
|
|
#endif
|
|
|
|
if(stream == NULL) {ret=errno; goto done;}
|
|
|
|
p = content;
|
|
|
|
remain = size;
|
|
|
|
while(remain > 0) {
|
|
|
|
size_t written = fwrite(p, 1, remain, stream);
|
|
|
|
if(ferror(stream)) {ret = NC_EIO; goto done;}
|
|
|
|
if(feof(stream)) break;
|
|
|
|
remain -= written;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
if(stream) fclose(stream);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-09-30 02:59:28 +08:00
|
|
|
/*
|
|
|
|
Parse a path as a url and extract the modelist.
|
|
|
|
If the path is not a URL, then return a NULL list.
|
|
|
|
If a URL, but modelist is empty or does not exist,
|
|
|
|
then return empty list.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC_getmodelist(const char* path, NClist** modelistp)
|
|
|
|
{
|
|
|
|
int stat=NC_NOERR;
|
|
|
|
NClist* modelist = NULL;
|
|
|
|
NCURI* uri = NULL;
|
|
|
|
const char* modestr = NULL;
|
|
|
|
const char* p = NULL;
|
|
|
|
const char* endp = NULL;
|
|
|
|
|
|
|
|
ncuriparse(path,&uri);
|
|
|
|
if(uri == NULL) goto done; /* not a uri */
|
|
|
|
|
|
|
|
/* Get the mode= arg from the fragment */
|
|
|
|
modelist = nclistnew();
|
This PR adds EXPERIMENTAL support for accessing data in the
cloud using a variant of the Zarr protocol and storage
format. This enhancement is generically referred to as "NCZarr".
The data model supported by NCZarr is netcdf-4 minus the user-defined
types and the String type. In this sense it is similar to the CDF-5
data model.
More detailed information about enabling and using NCZarr is
described in the document NUG/nczarr.md and in a
[Unidata Developer's blog entry](https://www.unidata.ucar.edu/blogs/developer/en/entry/overview-of-zarr-support-in).
WARNING: this code has had limited testing, so do use this version
for production work. Also, performance improvements are ongoing.
Note especially the following platform matrix of successful tests:
Platform | Build System | S3 support
------------------------------------
Linux+gcc | Automake | yes
Linux+gcc | CMake | yes
Visual Studio | CMake | no
Additionally, and as a consequence of the addition of NCZarr,
major changes have been made to the Filter API. NOTE: NCZarr
does not yet support filters, but these changes are enablers for
that support in the future. Note that it is possible
(probable?) that there will be some accidental reversions if the
changes here did not correctly mimic the existing filter testing.
In any case, previously filter ids and parameters were of type
unsigned int. In order to support the more general zarr filter
model, this was all converted to char*. The old HDF5-specific,
unsigned int operations are still supported but they are
wrappers around the new, char* based nc_filterx_XXX functions.
This entailed at least the following changes:
1. Added the files libdispatch/dfilterx.c and include/ncfilter.h
2. Some filterx utilities have been moved to libdispatch/daux.c
3. A new entry, "filter_actions" was added to the NCDispatch table
and the version bumped.
4. An overly complex set of structs was created to support funnelling
all of the filterx operations thru a single dispatch
"filter_actions" entry.
5. Move common code to from libhdf5 to libsrc4 so that it is accessible
to nczarr.
Changes directly related to Zarr:
1. Modified CMakeList.txt and configure.ac to support both C and C++
-- this is in support of S3 support via the awd-sdk libraries.
2. Define a size64_t type to support nczarr.
3. More reworking of libdispatch/dinfermodel.c to
support zarr and to regularize the structure of the fragments
section of a URL.
Changes not directly related to Zarr:
1. Make client-side filter registration be conditional, with default off.
2. Hack include/nc4internal.h to make some flags added by Ed be unique:
e.g. NC_CREAT, NC_INDEF, etc.
3. cleanup include/nchttp.h and libdispatch/dhttp.c.
4. Misc. changes to support compiling under Visual Studio including:
* Better testing under windows for dirent.h and opendir and closedir.
5. Misc. changes to the oc2 code to support various libcurl CURLOPT flags
and to centralize error reporting.
6. By default, suppress the vlen tests that have unfixed memory leaks; add option to enable them.
7. Make part of the nc_test/test_byterange.sh test be contingent on remotetest.unidata.ucar.edu being accessible.
Changes Left TO-DO:
1. fix provenance code, it is too HDF5 specific.
2020-06-29 08:02:47 +08:00
|
|
|
modestr = ncurifragmentlookup(uri,"mode");
|
2019-09-30 02:59:28 +08:00
|
|
|
if(modestr == NULL || strlen(modestr) == 0) goto done;
|
|
|
|
/* Parse the mode string at the commas or EOL */
|
|
|
|
p = modestr;
|
|
|
|
for(;;) {
|
|
|
|
char* s;
|
|
|
|
ptrdiff_t slen;
|
|
|
|
endp = strchr(p,',');
|
|
|
|
if(endp == NULL) endp = p + strlen(p);
|
|
|
|
slen = (endp - p);
|
|
|
|
if((s = malloc(slen+1)) == NULL) {stat = NC_ENOMEM; goto done;}
|
|
|
|
memcpy(s,p,slen);
|
|
|
|
s[slen] = '\0';
|
|
|
|
nclistpush(modelist,s);
|
|
|
|
if(*endp == '\0') break;
|
|
|
|
p = endp+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if(stat == NC_NOERR) {
|
|
|
|
if(modelistp) {*modelistp = modelist; modelist = NULL;}
|
|
|
|
}
|
|
|
|
ncurifree(uri);
|
|
|
|
nclistfree(modelist);
|
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Check "mode=" list for a path and return 1 if present, 0 otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC_testmode(const char* path, const char* tag)
|
|
|
|
{
|
|
|
|
int stat = NC_NOERR;
|
|
|
|
int found = 0;
|
|
|
|
int i;
|
|
|
|
NClist* modelist = NULL;
|
|
|
|
|
|
|
|
if((stat = NC_getmodelist(path, &modelist))) goto done;
|
|
|
|
for(i=0;i<nclistlength(modelist);i++) {
|
|
|
|
const char* value = nclistget(modelist,i);
|
|
|
|
if(strcasecmp(tag,value)==0) {found = 1; break;}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2020-02-17 03:59:33 +08:00
|
|
|
nclistfreeall(modelist);
|
2019-09-30 02:59:28 +08:00
|
|
|
return found;
|
|
|
|
}
|
2020-08-18 09:15:47 +08:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
int isinf(double x)
|
|
|
|
{
|
|
|
|
union { unsigned long long u; double f; } ieee754;
|
|
|
|
ieee754.f = x;
|
|
|
|
return ( (unsigned)(ieee754.u >> 32) & 0x7fffffff ) == 0x7ff00000 &&
|
|
|
|
( (unsigned)ieee754.u == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int isnan(double x)
|
|
|
|
{
|
|
|
|
union { unsigned long long u; double f; } ieee754;
|
|
|
|
ieee754.f = x;
|
|
|
|
return ( (unsigned)(ieee754.u >> 32) & 0x7fffffff ) +
|
|
|
|
( (unsigned)ieee754.u != 0 ) > 0x7ff00000;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /*APPLE*/
|
2021-04-22 04:59:15 +08:00
|
|
|
|