/src/netcdf-c/libdispatch/dinfermodel.c
Line | Count | Source |
1 | | /** |
2 | | * @file |
3 | | * |
4 | | * Infer as much as possible from the omode + path. |
5 | | * Rewrite the path to a canonical form. |
6 | | * |
7 | | * Copyright 2018 University Corporation for Atmospheric |
8 | | * Research/Unidata. See COPYRIGHT file for more info. |
9 | | */ |
10 | | #include "config.h" |
11 | | #include <stddef.h> |
12 | | #include <stdlib.h> |
13 | | #include <string.h> |
14 | | #ifdef HAVE_UNISTD_H |
15 | | #include <unistd.h> |
16 | | #endif |
17 | | #ifdef HAVE_SYS_TYPES_H |
18 | | #include <sys/types.h> |
19 | | #endif |
20 | | #ifndef _WIN32 |
21 | | #ifdef USE_HDF5 |
22 | | #include <hdf5.h> |
23 | | #endif /* USE_HDF5 */ |
24 | | #endif /* _WIN32 */ |
25 | | #ifdef HAVE_SYS_XATTR_H |
26 | | #include <sys/xattr.h> |
27 | | #endif |
28 | | |
29 | | #include "ncdispatch.h" |
30 | | #include "ncpathmgr.h" |
31 | | #include "netcdf_mem.h" |
32 | | #include "fbits.h" |
33 | | #include "ncbytes.h" |
34 | | #include "nclist.h" |
35 | | #include "nclog.h" |
36 | | #include "nchttp.h" |
37 | | #include "ncutil.h" |
38 | | #ifdef NETCDF_ENABLE_S3 |
39 | | #include "ncs3sdk.h" |
40 | | #endif |
41 | | |
42 | | #ifndef nulldup |
43 | | #define nulldup(x) ((x)?strdup(x):(x)) |
44 | | #endif |
45 | | |
46 | | #undef DEBUG |
47 | | |
48 | | /* If Defined, then use only stdio for all magic number io; |
49 | | otherwise use stdio or mpio as required. |
50 | | */ |
51 | | #undef USE_STDIO |
52 | | |
53 | | /** |
54 | | Sort info for open/read/close of |
55 | | file when searching for magic numbers |
56 | | */ |
57 | | struct MagicFile { |
58 | | const char* path; |
59 | | struct NCURI* uri; |
60 | | int omode; |
61 | | NCmodel* model; |
62 | | long long filelen; |
63 | | int use_parallel; |
64 | | int iss3; |
65 | | void* parameters; /* !NULL if inmemory && !diskless */ |
66 | | FILE* fp; |
67 | | #ifdef USE_PARALLEL |
68 | | MPI_File fh; |
69 | | #endif |
70 | | char* curlurl; /* url to use with CURLOPT_SET_URL */ |
71 | | NC_HTTP_STATE* state; |
72 | | #ifdef NETCDF_ENABLE_S3 |
73 | | NCS3INFO s3; |
74 | | void* s3client; |
75 | | char* errmsg; |
76 | | #endif |
77 | | }; |
78 | | |
79 | | /** @internal Magic number for HDF5 files. To be consistent with |
80 | | * H5Fis_hdf5, use the complete HDF5 magic number */ |
81 | | static char HDF5_SIGNATURE[MAGIC_NUMBER_LEN] = "\211HDF\r\n\032\n"; |
82 | | |
83 | 147 | #define modelcomplete(model) ((model)->impl != 0) |
84 | | |
85 | | #ifdef DEBUG |
86 | | static void dbgflush(void) |
87 | | { |
88 | | fflush(stdout); |
89 | | fflush(stderr); |
90 | | } |
91 | | |
92 | | static void |
93 | | fail(int err) |
94 | | { |
95 | | return; |
96 | | } |
97 | | |
98 | | static int |
99 | | check(int err) |
100 | | { |
101 | | if(err != NC_NOERR) |
102 | | fail(err); |
103 | | return err; |
104 | | } |
105 | | #else |
106 | 675 | #define check(err) (err) |
107 | | #endif |
108 | | |
109 | | /* |
110 | | Define a table of "mode=" string values |
111 | | from which the implementation can be inferred. |
112 | | Note that only cases that can currently |
113 | | take URLs are included. |
114 | | */ |
115 | | static struct FORMATMODES { |
116 | | const char* tag; |
117 | | const int impl; /* NC_FORMATX_XXX value */ |
118 | | const int format; /* NC_FORMAT_XXX value */ |
119 | | } formatmodes[] = { |
120 | | {"dap2",NC_FORMATX_DAP2,NC_FORMAT_CLASSIC}, |
121 | | {"dap4",NC_FORMATX_DAP4,NC_FORMAT_NETCDF4}, |
122 | | {"netcdf-3",NC_FORMATX_NC3,0}, /* Might be e.g. cdf5 */ |
123 | | {"classic",NC_FORMATX_NC3,0}, /* ditto */ |
124 | | {"netcdf-4",NC_FORMATX_NC4,NC_FORMAT_NETCDF4}, |
125 | | {"enhanced",NC_FORMATX_NC4,NC_FORMAT_NETCDF4}, |
126 | | /* "udf<N>" mode names are handled generically in processmodearg() */ |
127 | | {"nczarr",NC_FORMATX_NCZARR,NC_FORMAT_NETCDF4}, |
128 | | {"zarr",NC_FORMATX_NCZARR,NC_FORMAT_NETCDF4}, |
129 | | {"bytes",NC_FORMATX_NC4,NC_FORMAT_NETCDF4}, /* temporary until 3 vs 4 is determined */ |
130 | | {NULL,0}, |
131 | | }; |
132 | | |
133 | | /* Replace top-level name with defkey=defvalue */ |
134 | | static const struct MACRODEF { |
135 | | char* name; |
136 | | char* defkey; |
137 | | char* defvalues[4]; |
138 | | } macrodefs[] = { |
139 | | {"zarr","mode",{"nczarr","zarr",NULL}}, |
140 | | {"dap2","mode",{"dap2",NULL}}, |
141 | | {"dap4","mode",{"dap4",NULL}}, |
142 | | {"s3","mode",{"s3","nczarr",NULL}}, |
143 | | {"bytes","mode",{"bytes",NULL}}, |
144 | | {"xarray","mode",{"zarr", NULL}}, |
145 | | {"noxarray","mode",{"nczarr", "noxarray", NULL}}, |
146 | | {"zarr","mode",{"nczarr","zarr", NULL}}, |
147 | | {"gs3","mode",{"gs3","nczarr",NULL}}, /* Google S3 API */ |
148 | | {NULL,NULL,{NULL}} |
149 | | }; |
150 | | |
151 | | /* |
152 | | Mode inferences: if mode contains key value, then add the inferred value; |
153 | | Warning: be careful how this list is constructed to avoid infinite inferences. |
154 | | In order to (mostly) avoid that consequence, any attempt to |
155 | | infer a value that is already present will be ignored. |
156 | | This effectively means that the inference graph |
157 | | must be a DAG and may not have cycles. |
158 | | You have been warned. |
159 | | */ |
160 | | static const struct MODEINFER { |
161 | | char* key; |
162 | | char* inference; |
163 | | } modeinferences[] = { |
164 | | {"zarr","nczarr"}, |
165 | | {"xarray","zarr"}, |
166 | | {"noxarray","nczarr"}, |
167 | | {"noxarray","zarr"}, |
168 | | {NULL,NULL} |
169 | | }; |
170 | | |
171 | | /* Mode negations: if mode contains key, then remove all occurrences of the inference and repeat */ |
172 | | static const struct MODEINFER modenegations[] = { |
173 | | {"bytes","nczarr"}, /* bytes negates (nc)zarr */ |
174 | | {"bytes","zarr"}, |
175 | | {"noxarray","xarray"}, |
176 | | {NULL,NULL} |
177 | | }; |
178 | | |
179 | | /* Map FORMATX to readability to get magic number. |
180 | | * UDF formats (all readable) are handled generically in isreadable(). */ |
181 | | static struct Readable { |
182 | | int impl; |
183 | | int readable; |
184 | | } readable[] = { |
185 | | {NC_FORMATX_NC3,1}, |
186 | | {NC_FORMATX_NC_HDF5,1}, |
187 | | {NC_FORMATX_NC_HDF4,1}, |
188 | | {NC_FORMATX_PNETCDF,1}, |
189 | | {NC_FORMATX_DAP2,0}, |
190 | | {NC_FORMATX_DAP4,0}, |
191 | | {NC_FORMATX_NCZARR,0}, /* eventually make readable */ |
192 | | {0,0}, |
193 | | }; |
194 | | |
195 | | /* Return 1 if impl is a UDF format constant (NC_FORMATX_UDF(n) for |
196 | | * n in 0..NC_MAX_UDF_FORMATS-1), else 0. Note the gap in numbering: |
197 | | * NC_FORMATX_NCZARR=10 sits between UDF1=9 and UDF2=11. */ |
198 | | static int |
199 | | udfimpl(int impl) |
200 | 222 | { |
201 | 222 | return (impl >= NC_FORMATX_UDF0 && impl <= NC_FORMATX_UDF1) |
202 | 222 | || (impl >= NC_FORMATX_UDF2 && impl <= NC_FORMATX_UDF_MAX); |
203 | 222 | } |
204 | | |
205 | | /* Define the known URL protocols and their interpretation */ |
206 | | static struct NCPROTOCOLLIST { |
207 | | const char* protocol; |
208 | | const char* substitute; |
209 | | const char* fragments; /* arbitrary fragment arguments */ |
210 | | } ncprotolist[] = { |
211 | | {"http",NULL,NULL}, |
212 | | {"https",NULL,NULL}, |
213 | | {"file",NULL,NULL}, |
214 | | {"dods","http","mode=dap2"}, |
215 | | {"dap4","http","mode=dap4"}, |
216 | | {"s3","s3","mode=s3"}, |
217 | | {"gs3","gs3","mode=gs3"}, |
218 | | {NULL,NULL,NULL} /* Terminate search */ |
219 | | }; |
220 | | |
221 | | /* Forward */ |
222 | | static int NC_omodeinfer(int useparallel, int omode, NCmodel*); |
223 | | static int check_file_type(const char *path, int omode, int use_parallel, void *parameters, NCmodel* model, NCURI* uri); |
224 | | static int processuri(const char* path, NCURI** urip, NClist* fraglist); |
225 | | static int processmacros(NClist* fraglistp, NClist* expanded); |
226 | | static char* envvlist2string(NClist* pairs, const char*); |
227 | | static void set_default_mode(int* cmodep); |
228 | | static int parseonchar(const char* s, int ch, NClist* segments); |
229 | | static int mergelist(NClist** valuesp); |
230 | | |
231 | | static int openmagic(struct MagicFile* file); |
232 | | static int readmagic(struct MagicFile* file, size_t pos, char* magic); |
233 | | static int closemagic(struct MagicFile* file); |
234 | | static int NC_interpret_magic_number(char* magic, NCmodel* model); |
235 | | #ifdef DEBUG |
236 | | static void printmagic(const char* tag, char* magic,struct MagicFile*); |
237 | | static void printlist(NClist* list, const char* tag); |
238 | | #endif |
239 | | static int isreadable(NCURI*,NCmodel*); |
240 | | static char* list2string(NClist*); |
241 | | static int parsepair(const char* pair, char** keyp, char** valuep); |
242 | | static NClist* parsemode(const char* modeval); |
243 | | static const char* getmodekey(const NClist* envv); |
244 | | static int replacemode(NClist* envv, const char* newval); |
245 | | static void infernext(NClist* current, NClist* next); |
246 | | static int negateone(const char* mode, NClist* modes); |
247 | | static void cleanstringlist(NClist* strs, int caseinsensitive); |
248 | | static int isdaoscontainer(const char* path); |
249 | | |
250 | | /* |
251 | | If the path looks like a URL, then parse it, reformat it. |
252 | | */ |
253 | | static int |
254 | | processuri(const char* path, NCURI** urip, NClist* fraglenv) |
255 | 77 | { |
256 | 77 | int stat = NC_NOERR; |
257 | 77 | int found = 0; |
258 | 77 | NClist* tmp = NULL; |
259 | 77 | struct NCPROTOCOLLIST* protolist; |
260 | 77 | NCURI* uri = NULL; |
261 | 77 | size_t pathlen = strlen(path); |
262 | 77 | char* str = NULL; |
263 | 77 | const NClist* ufrags; |
264 | 77 | size_t i; |
265 | | |
266 | 77 | if(path == NULL || pathlen == 0) {stat = NC_EURL; goto done;} |
267 | | |
268 | | /* Defaults */ |
269 | 77 | if(urip) *urip = NULL; |
270 | | |
271 | 77 | ncuriparse(path,&uri); |
272 | 77 | if(uri == NULL) goto done; /* not url */ |
273 | | |
274 | | /* Look up the protocol */ |
275 | 0 | for(found=0,protolist=ncprotolist;protolist->protocol;protolist++) { |
276 | 0 | if(strcmp(uri->protocol,protolist->protocol) == 0) { |
277 | 0 | found = 1; |
278 | 0 | break; |
279 | 0 | } |
280 | 0 | } |
281 | 0 | if(!found) |
282 | 0 | {stat = NC_EINVAL; goto done;} /* unrecognized URL form */ |
283 | | |
284 | | /* process the corresponding fragments for that protocol */ |
285 | 0 | if(protolist->fragments != NULL) { |
286 | 0 | tmp = nclistnew(); |
287 | 0 | if((stat = parseonchar(protolist->fragments,'&',tmp))) goto done; |
288 | 0 | for(i=0;i<nclistlength(tmp);i++) { |
289 | 0 | char* key=NULL; |
290 | 0 | char* value=NULL; |
291 | 0 | if((stat = parsepair(nclistget(tmp,i),&key,&value))) goto done; |
292 | 0 | if(value == NULL) value = strdup(""); |
293 | 0 | nclistpush(fraglenv,key); |
294 | 0 | nclistpush(fraglenv,value); |
295 | 0 | } |
296 | 0 | nclistfreeall(tmp); tmp = NULL; |
297 | 0 | } |
298 | | |
299 | | /* Substitute the protocol in any case */ |
300 | 0 | if(protolist->substitute) ncurisetprotocol(uri,protolist->substitute); |
301 | | |
302 | | /* capture the fragments of the url */ |
303 | 0 | ufrags = (const NClist*)ncurifragmentparams(uri); |
304 | 0 | for(i=0;i<nclistlength(ufrags);i+=2) { |
305 | 0 | const char* key = nclistget(ufrags,i); |
306 | 0 | const char* value = nclistget(ufrags,i+1); |
307 | 0 | nclistpush(fraglenv,nulldup(key)); |
308 | 0 | value = (value==NULL?"":value); |
309 | 0 | nclistpush(fraglenv,strdup(value)); |
310 | 0 | } |
311 | 0 | if(urip) { |
312 | 0 | *urip = uri; |
313 | 0 | uri = NULL; |
314 | 0 | } |
315 | |
|
316 | 77 | done: |
317 | 77 | nclistfreeall(tmp); |
318 | 77 | nullfree(str); |
319 | 77 | if(uri != NULL) ncurifree(uri); |
320 | 77 | return check(stat); |
321 | 0 | } |
322 | | |
323 | | /* Split a key=value pair */ |
324 | | static int |
325 | | parsepair(const char* pair, char** keyp, char** valuep) |
326 | 0 | { |
327 | 0 | const char* p; |
328 | 0 | char* key = NULL; |
329 | 0 | char* value = NULL; |
330 | |
|
331 | 0 | if(pair == NULL) |
332 | 0 | return NC_EINVAL; /* empty pair */ |
333 | 0 | if(pair[0] == '\0' || pair[0] == '=') |
334 | 0 | return NC_EINVAL; /* no key */ |
335 | 0 | p = strchr(pair,'='); |
336 | 0 | if(p == NULL) { |
337 | 0 | value = NULL; |
338 | 0 | key = strdup(pair); |
339 | 0 | } else { |
340 | 0 | ptrdiff_t len = (p-pair); |
341 | 0 | if((key = malloc((size_t)len+1))==NULL) return NC_ENOMEM; |
342 | 0 | memcpy(key,pair,(size_t)len); |
343 | 0 | key[len] = '\0'; |
344 | 0 | if(p[1] == '\0') |
345 | 0 | value = NULL; |
346 | 0 | else |
347 | 0 | value = strdup(p+1); |
348 | 0 | } |
349 | 0 | if(keyp) {*keyp = key; key = NULL;}; |
350 | 0 | if(valuep) {*valuep = value; value = NULL;}; |
351 | 0 | nullfree(key); |
352 | 0 | nullfree(value); |
353 | 0 | return NC_NOERR; |
354 | 0 | } |
355 | | |
356 | | #if 0 |
357 | | static int |
358 | | parseurlmode(const char* modestr, NClist* list) |
359 | | { |
360 | | int stat = NC_NOERR; |
361 | | const char* p = NULL; |
362 | | const char* endp = NULL; |
363 | | |
364 | | if(modestr == NULL || *modestr == '\0') goto done; |
365 | | |
366 | | /* Split modestr at the commas or EOL */ |
367 | | p = modestr; |
368 | | for(;;) { |
369 | | char* s; |
370 | | ptrdiff_t slen; |
371 | | endp = strchr(p,','); |
372 | | if(endp == NULL) endp = p + strlen(p); |
373 | | slen = (endp - p); |
374 | | if((s = malloc(slen+1)) == NULL) {stat = NC_ENOMEM; goto done;} |
375 | | memcpy(s,p,slen); |
376 | | s[slen] = '\0'; |
377 | | nclistpush(list,s); |
378 | | if(*endp == '\0') break; |
379 | | p = endp+1; |
380 | | } |
381 | | |
382 | | done: |
383 | | return check(stat); |
384 | | } |
385 | | #endif |
386 | | |
387 | | /* Split a string at a given char */ |
388 | | static int |
389 | | parseonchar(const char* s, int ch, NClist* segments) |
390 | 0 | { |
391 | 0 | int stat = NC_NOERR; |
392 | 0 | const char* p = NULL; |
393 | 0 | const char* endp = NULL; |
394 | |
|
395 | 0 | if(s == NULL || *s == '\0') goto done; |
396 | | |
397 | 0 | p = s; |
398 | 0 | for(;;) { |
399 | 0 | char* q; |
400 | 0 | ptrdiff_t slen; |
401 | 0 | endp = strchr(p,ch); |
402 | 0 | if(endp == NULL) endp = p + strlen(p); |
403 | 0 | slen = (endp - p); |
404 | 0 | if((q = malloc((size_t)slen+1)) == NULL) {stat = NC_ENOMEM; goto done;} |
405 | 0 | memcpy(q,p,(size_t)slen); |
406 | 0 | q[slen] = '\0'; |
407 | 0 | nclistpush(segments,q); |
408 | 0 | if(*endp == '\0') break; |
409 | 0 | p = endp+1; |
410 | 0 | } |
411 | | |
412 | 0 | done: |
413 | 0 | return check(stat); |
414 | 0 | } |
415 | | |
416 | | /* Convert a key,value envv pairlist into a delimited string*/ |
417 | | static char* |
418 | | envvlist2string(NClist* envv, const char* delim) |
419 | 0 | { |
420 | 0 | size_t i; |
421 | 0 | NCbytes* buf = NULL; |
422 | 0 | char* result = NULL; |
423 | |
|
424 | 0 | if(envv == NULL || nclistlength(envv) == 0) return NULL; |
425 | 0 | buf = ncbytesnew(); |
426 | 0 | for(i=0;i<nclistlength(envv);i+=2) { |
427 | 0 | const char* key = nclistget(envv,i); |
428 | 0 | const char* val = nclistget(envv,i+1); |
429 | 0 | if(key == NULL || strlen(key) == 0) continue; |
430 | 0 | assert(val != NULL); |
431 | 0 | if(i > 0) ncbytescat(buf,"&"); |
432 | 0 | ncbytescat(buf,key); |
433 | 0 | if(val != NULL && val[0] != '\0') { |
434 | 0 | ncbytescat(buf,"="); |
435 | 0 | ncbytescat(buf,val); |
436 | 0 | } |
437 | 0 | } |
438 | 0 | result = ncbytesextract(buf); |
439 | 0 | ncbytesfree(buf); |
440 | 0 | return result; |
441 | 0 | } |
442 | | |
443 | | /* Given a mode= argument, fill in the impl */ |
444 | | static int |
445 | | processmodearg(const char* arg, NCmodel* model) |
446 | 0 | { |
447 | 0 | int stat = NC_NOERR; |
448 | 0 | struct FORMATMODES* format = formatmodes; |
449 | | /* Handle "udf<N>" mode names generically for all UDF slots */ |
450 | 0 | if(strncmp(arg,"udf",3)==0) { |
451 | 0 | char* endp = NULL; |
452 | 0 | long n = strtol(arg+3,&endp,10); |
453 | 0 | if(endp != arg+3 && *endp == '\0' && n >= 0 && n < NC_MAX_UDF_FORMATS) { |
454 | 0 | model->impl = NC_FORMATX_UDF((int)n); |
455 | 0 | return check(stat); |
456 | 0 | } |
457 | 0 | } |
458 | 0 | for(;format->tag;format++) { |
459 | 0 | if(strcmp(format->tag,arg)==0) { |
460 | 0 | model->impl = format->impl; |
461 | 0 | if(format->format != 0) model->format = format->format; |
462 | 0 | } |
463 | 0 | } |
464 | 0 | return check(stat); |
465 | 0 | } |
466 | | |
467 | | /* Given an envv fragment list, do macro replacement */ |
468 | | static int |
469 | | processmacros(NClist* fraglenv, NClist* expanded) |
470 | 0 | { |
471 | 0 | size_t i; |
472 | 0 | int stat = NC_NOERR; |
473 | 0 | const struct MACRODEF* macros = NULL; |
474 | |
|
475 | 0 | for(i=0;i<nclistlength(fraglenv);i+=2) { |
476 | 0 | int found = 0; |
477 | 0 | char* key = nclistget(fraglenv,i); |
478 | 0 | char* value = nclistget(fraglenv,i+1); |
479 | 0 | if(strlen(value) == 0) { /* must be a singleton */ |
480 | 0 | for(macros=macrodefs;macros->name;macros++) { |
481 | 0 | if(strcmp(macros->name,key)==0) { |
482 | 0 | char* const * p; |
483 | 0 | nclistpush(expanded,strdup(macros->defkey)); |
484 | 0 | for(p=macros->defvalues;*p;p++) |
485 | 0 | nclistpush(expanded,strdup(*p)); |
486 | 0 | found = 1; |
487 | 0 | break; |
488 | 0 | } |
489 | 0 | } |
490 | 0 | } |
491 | 0 | if(!found) {/* pass thru */ |
492 | 0 | nclistpush(expanded,strdup(key)); |
493 | 0 | nclistpush(expanded,strdup(value)); |
494 | 0 | } |
495 | 0 | } |
496 | |
|
497 | 0 | return check(stat); |
498 | 0 | } |
499 | | |
500 | | /* Process mode flag inferences */ |
501 | | static int |
502 | | processinferences(NClist* fraglenv) |
503 | 0 | { |
504 | 0 | int stat = NC_NOERR; |
505 | 0 | const char* modeval = NULL; |
506 | 0 | NClist* newmodes = nclistnew(); |
507 | 0 | NClist* currentmodes = NULL; |
508 | 0 | NClist* nextmodes = nclistnew(); |
509 | 0 | size_t i; |
510 | 0 | char* newmodeval = NULL; |
511 | | |
512 | | /* Get "mode" entry */ |
513 | 0 | if((modeval = getmodekey(fraglenv))==NULL) goto done; |
514 | | |
515 | | /* Get the mode as list */ |
516 | 0 | currentmodes = parsemode(modeval); |
517 | |
|
518 | | #ifdef DEBUG |
519 | | printlist(currentmodes,"processinferences: initial mode list"); |
520 | | #endif |
521 | | |
522 | | /* Do what amounts to breadth first inferencing down the inference DAG. */ |
523 | |
|
524 | 0 | for(;;) { |
525 | 0 | NClist* tmp = NULL; |
526 | | /* Compute the next set of inferred modes */ |
527 | | #ifdef DEBUG |
528 | | printlist(currentmodes,"processinferences: current mode list"); |
529 | | #endif |
530 | 0 | infernext(currentmodes,nextmodes); |
531 | | #ifdef DEBUG |
532 | | printlist(nextmodes,"processinferences: next mode list"); |
533 | | #endif |
534 | | /* move current modes into list of newmodes */ |
535 | 0 | for(i=0;i<nclistlength(currentmodes);i++) { |
536 | 0 | nclistpush(newmodes,nclistget(currentmodes,i)); |
537 | 0 | } |
538 | 0 | nclistsetlength(currentmodes,0); /* clear current mode list */ |
539 | 0 | if(nclistlength(nextmodes) == 0) break; /* nothing more to do */ |
540 | | #ifdef DEBUG |
541 | | printlist(newmodes,"processinferences: new mode list"); |
542 | | #endif |
543 | | /* Swap current and next */ |
544 | 0 | tmp = currentmodes; |
545 | 0 | currentmodes = nextmodes; |
546 | 0 | nextmodes = tmp; |
547 | 0 | tmp = NULL; |
548 | 0 | } |
549 | | /* cleanup any unused elements in currentmodes */ |
550 | 0 | nclistclearall(currentmodes); |
551 | | |
552 | | /* Ensure no duplicates */ |
553 | 0 | cleanstringlist(newmodes,1); |
554 | |
|
555 | | #ifdef DEBUG |
556 | | printlist(newmodes,"processinferences: final inferred mode list"); |
557 | | #endif |
558 | | |
559 | | /* Remove negative inferences */ |
560 | 0 | for(i=0;i<nclistlength(newmodes);i++) { |
561 | 0 | const char* mode = nclistget(newmodes,i); |
562 | 0 | negateone(mode,newmodes); |
563 | 0 | } |
564 | | |
565 | | /* Store new mode value */ |
566 | 0 | if((newmodeval = list2string(newmodes))== NULL) |
567 | 0 | {stat = NC_ENOMEM; goto done;} |
568 | 0 | if((stat=replacemode(fraglenv,newmodeval))) goto done; |
569 | 0 | modeval = NULL; |
570 | |
|
571 | 0 | done: |
572 | 0 | nullfree(newmodeval); |
573 | 0 | nclistfreeall(newmodes); |
574 | 0 | nclistfreeall(currentmodes); |
575 | 0 | nclistfreeall(nextmodes); |
576 | 0 | return check(stat); |
577 | 0 | } |
578 | | |
579 | | |
580 | | static int |
581 | | negateone(const char* mode, NClist* newmodes) |
582 | 0 | { |
583 | 0 | const struct MODEINFER* tests = modenegations; |
584 | 0 | int changed = 0; |
585 | 0 | for(;tests->key;tests++) { |
586 | 0 | if(strcasecmp(tests->key,mode)==0) { |
587 | | /* Find and remove all instances of the inference value */ |
588 | 0 | for(size_t i = nclistlength(newmodes); i-- > 0;) { |
589 | 0 | char* candidate = nclistget(newmodes,i); |
590 | 0 | if(strcasecmp(candidate,tests->inference)==0) { |
591 | 0 | nclistremove(newmodes,i); |
592 | 0 | nullfree(candidate); |
593 | 0 | changed = 1; |
594 | 0 | } |
595 | 0 | } |
596 | 0 | } |
597 | 0 | } |
598 | 0 | return changed; |
599 | 0 | } |
600 | | |
601 | | static void |
602 | | infernext(NClist* current, NClist* next) |
603 | 0 | { |
604 | 0 | size_t i; |
605 | 0 | for(i=0;i<nclistlength(current);i++) { |
606 | 0 | const struct MODEINFER* tests = NULL; |
607 | 0 | const char* cur = nclistget(current,i); |
608 | 0 | for(tests=modeinferences;tests->key;tests++) { |
609 | 0 | if(strcasecmp(tests->key,cur)==0) { |
610 | | /* Append the inferred mode unless dup */ |
611 | 0 | if(!nclistmatch(next,tests->inference,1)) |
612 | 0 | nclistpush(next,strdup(tests->inference)); |
613 | 0 | } |
614 | 0 | } |
615 | 0 | } |
616 | 0 | } |
617 | | |
618 | | /* |
619 | | Given a list of strings, remove nulls and duplicates |
620 | | */ |
621 | | static int |
622 | | mergelist(NClist** valuesp) |
623 | 0 | { |
624 | 0 | size_t i,j; |
625 | 0 | int stat = NC_NOERR; |
626 | 0 | NClist* values = *valuesp; |
627 | 0 | NClist* allvalues = nclistnew(); |
628 | 0 | NClist* newvalues = nclistnew(); |
629 | 0 | char* value = NULL; |
630 | |
|
631 | 0 | for(i=0;i<nclistlength(values);i++) { |
632 | 0 | char* val1 = nclistget(values,i); |
633 | | /* split on commas and put pieces into allvalues */ |
634 | 0 | if((stat=parseonchar(val1,',',allvalues))) goto done; |
635 | 0 | } |
636 | | /* Remove duplicates and "" */ |
637 | 0 | while(nclistlength(allvalues) > 0) { |
638 | 0 | value = nclistremove(allvalues,0); |
639 | 0 | if(strlen(value) == 0) { |
640 | 0 | nullfree(value); value = NULL; |
641 | 0 | } else { |
642 | 0 | for(j=0;j<nclistlength(newvalues);j++) { |
643 | 0 | char* candidate = nclistget(newvalues,j); |
644 | 0 | if(strcasecmp(candidate,value)==0) |
645 | 0 | {nullfree(value); value = NULL; break;} |
646 | 0 | } |
647 | 0 | } |
648 | 0 | if(value != NULL) {nclistpush(newvalues,value); value = NULL;} |
649 | 0 | } |
650 | | /* Make sure to have at least 1 value */ |
651 | 0 | if(nclistlength(newvalues)==0) nclistpush(newvalues,strdup("")); |
652 | 0 | *valuesp = values; values = NULL; |
653 | |
|
654 | 0 | done: |
655 | 0 | nclistfree(allvalues); |
656 | 0 | nclistfreeall(values); |
657 | 0 | nclistfreeall(newvalues); |
658 | 0 | return check(stat); |
659 | 0 | } |
660 | | |
661 | | static int |
662 | | lcontains(NClist* l, const char* key0) |
663 | 0 | { |
664 | 0 | size_t i; |
665 | 0 | for(i=0;i<nclistlength(l);i++) { |
666 | 0 | const char* key1 = nclistget(l,i); |
667 | 0 | if(strcasecmp(key0,key1)==0) return 1; |
668 | 0 | } |
669 | 0 | return 0; |
670 | 0 | } |
671 | | |
672 | | /* Warning values should not use nclistfreeall */ |
673 | | static void |
674 | | collectvaluesbykey(NClist* fraglenv, const char* key, NClist* values) |
675 | 0 | { |
676 | 0 | size_t i; |
677 | | /* collect all the values with the same key (including this one) */ |
678 | 0 | for(i=0;i<nclistlength(fraglenv);i+=2) { |
679 | 0 | const char* key2 = nclistget(fraglenv,i); |
680 | 0 | if(strcasecmp(key,key2)==0) { |
681 | 0 | const char* value2 = nclistget(fraglenv,i+1); |
682 | 0 | nclistpush(values,value2); value2 = NULL; |
683 | 0 | } |
684 | 0 | } |
685 | 0 | } |
686 | | |
687 | | /* Warning allkeys should not use nclistfreeall */ |
688 | | static void |
689 | | collectallkeys(NClist* fraglenv, NClist* allkeys) |
690 | 0 | { |
691 | 0 | size_t i; |
692 | | /* collect all the distinct keys */ |
693 | 0 | for(i=0;i<nclistlength(fraglenv);i+=2) { |
694 | 0 | char* key = nclistget(fraglenv,i); |
695 | 0 | if(!lcontains(allkeys,key)) { |
696 | 0 | nclistpush(allkeys,key); |
697 | 0 | } |
698 | 0 | } |
699 | 0 | } |
700 | | |
701 | | /* Given a fragment envv list, coalesce duplicate keys and remove duplicate values*/ |
702 | | static int |
703 | | cleanfragments(NClist* fraglenv, NClist* newlist) |
704 | 0 | { |
705 | 0 | size_t i; |
706 | 0 | int stat = NC_NOERR; |
707 | 0 | NClist* tmp = NULL; |
708 | 0 | NClist* allkeys = NULL; |
709 | 0 | NCbytes* buf = NULL; |
710 | 0 | char* key = NULL; |
711 | 0 | char* value = NULL; |
712 | |
|
713 | 0 | buf = ncbytesnew(); |
714 | 0 | allkeys = nclistnew(); |
715 | 0 | tmp = nclistnew(); |
716 | | |
717 | | /* collect all unique keys */ |
718 | 0 | collectallkeys(fraglenv,allkeys); |
719 | | /* Collect all values for same key across all fragment pairs */ |
720 | 0 | for(i=0;i<nclistlength(allkeys);i++) { |
721 | 0 | key = nclistget(allkeys,i); |
722 | 0 | collectvaluesbykey(fraglenv,key,tmp); |
723 | | /* merge the key values, remove duplicate */ |
724 | 0 | if((stat=mergelist(&tmp))) goto done; |
725 | | /* Construct key,value pair and insert into newlist */ |
726 | 0 | key = strdup(key); |
727 | 0 | nclistpush(newlist,key); |
728 | 0 | value = list2string(tmp); |
729 | 0 | nclistpush(newlist,value); |
730 | 0 | nclistclear(tmp); |
731 | 0 | } |
732 | 0 | done: |
733 | 0 | nclistfree(allkeys); |
734 | 0 | nclistfree(tmp); |
735 | 0 | ncbytesfree(buf); |
736 | 0 | return check(stat); |
737 | 0 | } |
738 | | |
739 | | /* process non-mode fragment keys in case they hold significance; currently not */ |
740 | | static int |
741 | | processfragmentkeys(const char* key, const char* value, NCmodel* model) |
742 | 0 | { |
743 | 0 | return NC_NOERR; |
744 | 0 | } |
745 | | |
746 | | /* |
747 | | Infer from the mode + useparallel |
748 | | only call if iscreate or file is not easily readable. |
749 | | */ |
750 | | static int |
751 | | NC_omodeinfer(int useparallel, int cmode, NCmodel* model) |
752 | 77 | { |
753 | 77 | int stat = NC_NOERR; |
754 | | |
755 | | /* If no format flags are set, then use default */ |
756 | 77 | if(!fIsSet(cmode,NC_FORMAT_ALL)) |
757 | 77 | set_default_mode(&cmode); |
758 | | |
759 | | /* Process the cmode; may override some already set flags. The |
760 | | * user-defined formats must be checked first. They may choose to |
761 | | * use some of the other flags, like NC_NETCDF4, so we must first |
762 | | * check NC_UDF_FLAG before checking for any other flag. */ |
763 | 77 | int udf_found = 0; |
764 | 77 | if(fIsSet(cmode, NC_UDF_FLAG)) { |
765 | | /* Extract the UDF slot number and convert to format constant */ |
766 | 0 | int udf_index = (cmode >> NC_UDF_NUM_SHIFT) & NC_UDF_NUM_MASK; |
767 | 0 | model->impl = NC_FORMATX_UDF(udf_index); |
768 | 0 | udf_found = 1; |
769 | 0 | } |
770 | | |
771 | 77 | if(udf_found) |
772 | 0 | { |
773 | 0 | if(fIsSet(cmode,NC_64BIT_OFFSET)) |
774 | 0 | { |
775 | 0 | model->format = NC_FORMAT_64BIT_OFFSET; |
776 | 0 | } |
777 | 0 | else if(fIsSet(cmode,NC_64BIT_DATA)) |
778 | 0 | { |
779 | 0 | model->format = NC_FORMAT_64BIT_DATA; |
780 | 0 | } |
781 | 0 | else if(fIsSet(cmode,NC_NETCDF4)) |
782 | 0 | { |
783 | 0 | if(fIsSet(cmode,NC_CLASSIC_MODEL)) |
784 | 0 | model->format = NC_FORMAT_NETCDF4_CLASSIC; |
785 | 0 | else |
786 | 0 | model->format = NC_FORMAT_NETCDF4; |
787 | 0 | } |
788 | 0 | if(! model->format) |
789 | 0 | model->format = NC_FORMAT_CLASSIC; |
790 | 0 | goto done; |
791 | 0 | } |
792 | | |
793 | 77 | if(fIsSet(cmode,NC_64BIT_OFFSET)) { |
794 | 0 | model->impl = NC_FORMATX_NC3; |
795 | 0 | model->format = NC_FORMAT_64BIT_OFFSET; |
796 | 0 | goto done; |
797 | 0 | } |
798 | | |
799 | 77 | if(fIsSet(cmode,NC_64BIT_DATA)) { |
800 | 0 | model->impl = NC_FORMATX_NC3; |
801 | 0 | model->format = NC_FORMAT_64BIT_DATA; |
802 | 0 | goto done; |
803 | 0 | } |
804 | | |
805 | 77 | if(fIsSet(cmode,NC_NETCDF4)) { |
806 | 0 | model->impl = NC_FORMATX_NC4; |
807 | 0 | if(fIsSet(cmode,NC_CLASSIC_MODEL)) |
808 | 0 | model->format = NC_FORMAT_NETCDF4_CLASSIC; |
809 | 0 | else |
810 | 0 | model->format = NC_FORMAT_NETCDF4; |
811 | 0 | goto done; |
812 | 0 | } |
813 | | |
814 | | /* Default to classic model */ |
815 | 77 | model->format = NC_FORMAT_CLASSIC; |
816 | 77 | model->impl = NC_FORMATX_NC3; |
817 | | |
818 | 77 | done: |
819 | | /* Apply parallel flag */ |
820 | 77 | if(useparallel) { |
821 | 0 | if(model->impl == NC_FORMATX_NC3) |
822 | 0 | model->impl = NC_FORMATX_PNETCDF; |
823 | 0 | } |
824 | 77 | return check(stat); |
825 | 77 | } |
826 | | |
827 | | /* |
828 | | If the mode flags do not necessarily specify the |
829 | | format, then default it by adding in appropriate flags. |
830 | | */ |
831 | | |
832 | | static void |
833 | | set_default_mode(int* modep) |
834 | 77 | { |
835 | 77 | int mode = *modep; |
836 | 77 | int dfaltformat; |
837 | | |
838 | 77 | dfaltformat = nc_get_default_format(); |
839 | 77 | switch (dfaltformat) { |
840 | 0 | case NC_FORMAT_64BIT_OFFSET: mode |= NC_64BIT_OFFSET; break; |
841 | 0 | case NC_FORMAT_64BIT_DATA: mode |= NC_64BIT_DATA; break; |
842 | 0 | case NC_FORMAT_NETCDF4: mode |= NC_NETCDF4; break; |
843 | 0 | case NC_FORMAT_NETCDF4_CLASSIC: mode |= (NC_NETCDF4|NC_CLASSIC_MODEL); break; |
844 | 77 | case NC_FORMAT_CLASSIC: /* fall thru */ |
845 | 77 | default: break; /* default to classic */ |
846 | 77 | } |
847 | 77 | *modep = mode; /* final result */ |
848 | 77 | } |
849 | | |
850 | | /**************************************************/ |
851 | | /* |
852 | | Infer model for this dataset using some |
853 | | combination of cmode, path, and reading the dataset. |
854 | | See the documentation in docs/internal.dox. |
855 | | |
856 | | @param path |
857 | | @param omode |
858 | | @param iscreate |
859 | | @param useparallel |
860 | | @param params |
861 | | @param model |
862 | | @param newpathp |
863 | | */ |
864 | | |
865 | | int |
866 | | NC_infermodel(const char* path, int* omodep, int iscreate, int useparallel, void* params, NCmodel* model, char** newpathp) |
867 | 77 | { |
868 | 77 | size_t i; |
869 | 77 | int stat = NC_NOERR; |
870 | 77 | NCURI* uri = NULL; |
871 | 77 | int omode = *omodep; |
872 | 77 | NClist* fraglenv = nclistnew(); |
873 | 77 | NClist* modeargs = nclistnew(); |
874 | 77 | char* sfrag = NULL; |
875 | 77 | const char* modeval = NULL; |
876 | 77 | char* abspath = NULL; |
877 | 77 | NClist* tmp = NULL; |
878 | | |
879 | | /* Phase 1: |
880 | | 1. convert special protocols to http|https |
881 | | 2. begin collecting fragments |
882 | | */ |
883 | 77 | if((stat = processuri(path, &uri, fraglenv))) goto done; |
884 | | |
885 | 77 | if(uri != NULL) { |
886 | | #ifdef DEBUG |
887 | | printlist(fraglenv,"processuri"); |
888 | | #endif |
889 | | |
890 | | /* Phase 2: Expand macros and add to fraglenv */ |
891 | 0 | nclistfreeall(tmp); |
892 | 0 | tmp = nclistnew(); |
893 | 0 | if((stat = processmacros(fraglenv,tmp))) goto done; |
894 | 0 | nclistfreeall(fraglenv); |
895 | 0 | fraglenv = tmp; tmp = NULL; |
896 | | #ifdef DEBUG |
897 | | printlist(fraglenv,"processmacros"); |
898 | | #endif |
899 | | /* Cleanup the fragment list */ |
900 | 0 | nclistfreeall(tmp); |
901 | 0 | tmp = nclistnew(); |
902 | 0 | if((stat = cleanfragments(fraglenv,tmp))) goto done; |
903 | 0 | nclistfreeall(fraglenv); |
904 | 0 | fraglenv = tmp; tmp = NULL; |
905 | | |
906 | | /* Phase 2a: Expand mode inferences and add to fraglenv */ |
907 | 0 | if((stat = processinferences(fraglenv))) goto done; |
908 | | #ifdef DEBUG |
909 | | printlist(fraglenv,"processinferences"); |
910 | | #endif |
911 | | |
912 | | /* Phase 3: coalesce duplicate fragment keys and remove duplicate values */ |
913 | 0 | nclistfreeall(tmp); |
914 | 0 | tmp = nclistnew(); |
915 | 0 | if((stat = cleanfragments(fraglenv,tmp))) goto done; |
916 | 0 | nclistfreeall(fraglenv); |
917 | 0 | fraglenv = tmp; tmp = NULL; |
918 | | #ifdef DEBUG |
919 | | printlist(fraglenv,"cleanfragments"); |
920 | | #endif |
921 | | |
922 | | /* Phase 4: Rebuild the url fragment and rebuilt the url */ |
923 | 0 | sfrag = envvlist2string(fraglenv,"&"); |
924 | 0 | nclistfreeall(fraglenv); fraglenv = NULL; |
925 | | #ifdef DEBUG |
926 | | fprintf(stderr,"frag final: %s\n",sfrag); |
927 | | #endif |
928 | 0 | ncurisetfragments(uri,sfrag); |
929 | 0 | nullfree(sfrag); sfrag = NULL; |
930 | |
|
931 | | #ifdef NETCDF_ENABLE_S3 |
932 | | /* If s3, then rebuild the url */ |
933 | | if(NC_iss3(uri,NULL)) { |
934 | | NCURI* newuri = NULL; |
935 | | if((stat = NC_s3urlrebuild(uri,NULL,&newuri))) goto done; |
936 | | ncurifree(uri); |
937 | | uri = newuri; |
938 | | } else |
939 | | #endif |
940 | 0 | if(strcmp(uri->protocol,"file")==0) { |
941 | | /* convert path to absolute */ |
942 | 0 | char* canon = NULL; |
943 | 0 | abspath = NCpathabsolute(uri->path); |
944 | 0 | if((stat = NCpathcanonical(abspath,&canon))) goto done; |
945 | 0 | nullfree(abspath); |
946 | 0 | abspath = canon; canon = NULL; |
947 | 0 | if((stat = ncurisetpath(uri,abspath))) goto done; |
948 | 0 | } |
949 | | |
950 | | /* rebuild the path */ |
951 | 0 | if(newpathp) { |
952 | 0 | *newpathp = ncuribuild(uri,NULL,NULL,NCURIALL); |
953 | | #ifdef DEBUG |
954 | | fprintf(stderr,"newpath=|%s|\n",*newpathp); fflush(stderr); |
955 | | #endif |
956 | 0 | } |
957 | | |
958 | | /* Phase 5: Process the mode key to see if we can tell the formatx */ |
959 | 0 | modeval = ncurifragmentlookup(uri,"mode"); |
960 | 0 | if(modeval != NULL) { |
961 | 0 | if((stat = parseonchar(modeval,',',modeargs))) goto done; |
962 | 0 | for(i=0;i<nclistlength(modeargs);i++) { |
963 | 0 | const char* arg = nclistget(modeargs,i); |
964 | 0 | if((stat=processmodearg(arg,model))) goto done; |
965 | 0 | } |
966 | 0 | } |
967 | | |
968 | | /* Phase 6: Process the non-mode keys to see if we can tell the formatx */ |
969 | 0 | if(!modelcomplete(model)) { |
970 | 0 | size_t i; |
971 | 0 | NClist* p = (NClist*)ncurifragmentparams(uri); /* envv format */ |
972 | 0 | for(i=0;i<nclistlength(p);i+=2) { |
973 | 0 | const char* key = nclistget(p,0); |
974 | 0 | const char* value = nclistget(p,1); |
975 | 0 | if((stat=processfragmentkeys(key,value,model))) goto done; |
976 | 0 | } |
977 | 0 | } |
978 | | |
979 | | /* Phase 7: Special cases: if this is a URL and model.impl is still not defined */ |
980 | | /* Phase7a: Default is DAP2 */ |
981 | 0 | if(!modelcomplete(model)) { |
982 | 0 | model->impl = NC_FORMATX_DAP2; |
983 | 0 | model->format = NC_FORMAT_NC3; |
984 | 0 | } |
985 | |
|
986 | 77 | } else {/* Not URL */ |
987 | 77 | if(newpathp) *newpathp = NULL; |
988 | 77 | } |
989 | | |
990 | | /* Phase 8: mode inference from mode flags */ |
991 | | /* The modeargs did not give us a model (probably not a URL). |
992 | | So look at the combination of mode flags and the useparallel flag */ |
993 | 77 | if(!modelcomplete(model)) { |
994 | 77 | if((stat = NC_omodeinfer(useparallel,omode,model))) goto done; |
995 | 77 | } |
996 | | |
997 | | /* Phase 9: Special case for file stored in DAOS container */ |
998 | 77 | if(isdaoscontainer(path) == NC_NOERR) { |
999 | | /* This is a DAOS container, so immediately assume it is HDF5. */ |
1000 | 0 | model->impl = NC_FORMATX_NC_HDF5; |
1001 | 0 | model->format = NC_FORMAT_NETCDF4; |
1002 | 77 | } else { |
1003 | | /* Phase 10: Infer from file content, if possible; |
1004 | | this has highest precedence, so it may override |
1005 | | previous decisions. Note that we do this last |
1006 | | because we need previously determined model info |
1007 | | to guess if this file is readable. |
1008 | | */ |
1009 | 77 | if(!iscreate && isreadable(uri,model)) { |
1010 | | /* Ok, we need to try to read the file */ |
1011 | 77 | if((stat = check_file_type(path, omode, useparallel, params, model, uri))) goto done; |
1012 | 77 | } |
1013 | 77 | } |
1014 | | |
1015 | | /* Need a decision */ |
1016 | 70 | if(!modelcomplete(model)) |
1017 | 0 | {stat = NC_ENOTNC; goto done;} |
1018 | | |
1019 | | /* Force flag consistency */ |
1020 | 70 | switch (model->impl) { |
1021 | 0 | case NC_FORMATX_NC4: |
1022 | 0 | case NC_FORMATX_NC_HDF4: |
1023 | 0 | case NC_FORMATX_DAP4: |
1024 | 0 | case NC_FORMATX_NCZARR: |
1025 | 0 | omode |= NC_NETCDF4; |
1026 | 0 | if(model->format == NC_FORMAT_NETCDF4_CLASSIC) |
1027 | 0 | omode |= NC_CLASSIC_MODEL; |
1028 | 0 | break; |
1029 | 70 | case NC_FORMATX_NC3: |
1030 | 70 | omode &= ~NC_NETCDF4; /* must be netcdf-3 (CDF-1, CDF-2, CDF-5) */ |
1031 | 70 | if(model->format == NC_FORMAT_64BIT_OFFSET) omode |= NC_64BIT_OFFSET; |
1032 | 45 | else if(model->format == NC_FORMAT_64BIT_DATA) omode |= NC_64BIT_DATA; |
1033 | 70 | break; |
1034 | 0 | case NC_FORMATX_PNETCDF: |
1035 | 0 | omode &= ~NC_NETCDF4; /* must be netcdf-3 (CDF-1, CDF-2, CDF-5) */ |
1036 | 0 | if(model->format == NC_FORMAT_64BIT_OFFSET) omode |= NC_64BIT_OFFSET; |
1037 | 0 | else if(model->format == NC_FORMAT_64BIT_DATA) omode |= NC_64BIT_DATA; |
1038 | 0 | break; |
1039 | 0 | case NC_FORMATX_DAP2: |
1040 | 0 | omode &= ~(NC_NETCDF4|NC_64BIT_OFFSET|NC_64BIT_DATA|NC_CLASSIC_MODEL); |
1041 | 0 | break; |
1042 | 0 | default: |
1043 | | /* UDF format constants are a range, so handle them here */ |
1044 | 0 | if(udfimpl(model->impl)) { |
1045 | 0 | if(model->format == NC_FORMAT_64BIT_OFFSET) |
1046 | 0 | omode |= NC_64BIT_OFFSET; |
1047 | 0 | else if(model->format == NC_FORMAT_64BIT_DATA) |
1048 | 0 | omode |= NC_64BIT_DATA; |
1049 | 0 | else if(model->format == NC_FORMAT_NETCDF4) |
1050 | 0 | omode |= NC_NETCDF4; |
1051 | 0 | else if(model->format == NC_FORMAT_NETCDF4_CLASSIC) |
1052 | 0 | omode |= NC_NETCDF4|NC_CLASSIC_MODEL; |
1053 | 0 | break; |
1054 | 0 | } |
1055 | 0 | {stat = NC_ENOTNC; goto done;} |
1056 | 70 | } |
1057 | | |
1058 | 77 | done: |
1059 | 77 | nullfree(sfrag); |
1060 | 77 | nullfree(abspath); |
1061 | 77 | ncurifree(uri); |
1062 | 77 | nclistfreeall(modeargs); |
1063 | 77 | nclistfreeall(fraglenv); |
1064 | 77 | nclistfreeall(tmp); |
1065 | 77 | *omodep = omode; /* in/out */ |
1066 | 77 | return check(stat); |
1067 | 70 | } |
1068 | | |
1069 | | static int |
1070 | | isreadable(NCURI* uri, NCmodel* model) |
1071 | 77 | { |
1072 | 77 | int canread = 0; |
1073 | 77 | struct Readable* r; |
1074 | | /* All UDF formats are readable */ |
1075 | 77 | if(udfimpl(model->impl)) |
1076 | 0 | return 1; |
1077 | | /* Step 1: Look up the implementation */ |
1078 | 77 | for(r=readable;r->impl;r++) { |
1079 | 77 | if(model->impl == r->impl) {canread = r->readable; break;} |
1080 | 77 | } |
1081 | | /* Step 2: check for bytes mode */ |
1082 | 77 | if(!canread && NC_testmode(uri,"bytes") && (model->impl == NC_FORMATX_NC4 || model->impl == NC_FORMATX_NC_HDF5)) |
1083 | 0 | canread = 1; |
1084 | 77 | return canread; |
1085 | 77 | } |
1086 | | |
1087 | | #if 0 |
1088 | | static char* |
1089 | | emptyify(char* s) |
1090 | | { |
1091 | | if(s == NULL) s = strdup(""); |
1092 | | return strdup(s); |
1093 | | } |
1094 | | |
1095 | | static const char* |
1096 | | nullify(const char* s) |
1097 | | { |
1098 | | if(s != NULL && strlen(s) == 0) |
1099 | | return NULL; |
1100 | | return s; |
1101 | | } |
1102 | | #endif |
1103 | | |
1104 | | /**************************************************/ |
1105 | | /* Envv list utilities */ |
1106 | | |
1107 | | static const char* |
1108 | | getmodekey(const NClist* envv) |
1109 | 0 | { |
1110 | 0 | size_t i; |
1111 | | /* Get "mode" entry */ |
1112 | 0 | for(i=0;i<nclistlength(envv);i+=2) { |
1113 | 0 | char* key = NULL; |
1114 | 0 | key = nclistget(envv,i); |
1115 | 0 | if(strcasecmp(key,"mode")==0) |
1116 | 0 | return nclistget(envv,i+1); |
1117 | 0 | } |
1118 | 0 | return NULL; |
1119 | 0 | } |
1120 | | |
1121 | | static int |
1122 | | replacemode(NClist* envv, const char* newval) |
1123 | 0 | { |
1124 | 0 | size_t i; |
1125 | | /* Get "mode" entry */ |
1126 | 0 | for(i=0;i<nclistlength(envv);i+=2) { |
1127 | 0 | char* key = NULL; |
1128 | 0 | char* val = NULL; |
1129 | 0 | key = nclistget(envv,i); |
1130 | 0 | if(strcasecmp(key,"mode")==0) { |
1131 | 0 | val = nclistget(envv,i+1); |
1132 | 0 | nclistset(envv,i+1,strdup(newval)); |
1133 | 0 | nullfree(val); |
1134 | 0 | return NC_NOERR; |
1135 | 0 | } |
1136 | 0 | } |
1137 | 0 | return NC_EINVAL; |
1138 | 0 | } |
1139 | | |
1140 | | static NClist* |
1141 | | parsemode(const char* modeval) |
1142 | 0 | { |
1143 | 0 | NClist* modes = nclistnew(); |
1144 | 0 | if(modeval) |
1145 | 0 | (void)parseonchar(modeval,',',modes);/* split on commas */ |
1146 | 0 | return modes; |
1147 | 0 | } |
1148 | | |
1149 | | /* Convert a list into a comma'd string */ |
1150 | | static char* |
1151 | | list2string(NClist* list) |
1152 | 0 | { |
1153 | 0 | size_t i; |
1154 | 0 | NCbytes* buf = NULL; |
1155 | 0 | char* result = NULL; |
1156 | |
|
1157 | 0 | if(list == NULL || nclistlength(list)==0) return strdup(""); |
1158 | 0 | buf = ncbytesnew(); |
1159 | 0 | for(i=0;i<nclistlength(list);i++) { |
1160 | 0 | const char* m = nclistget(list,i); |
1161 | 0 | if(m == NULL || strlen(m) == 0) continue; |
1162 | 0 | if(i > 0) ncbytescat(buf,","); |
1163 | 0 | ncbytescat(buf,m); |
1164 | 0 | } |
1165 | 0 | result = ncbytesextract(buf); |
1166 | 0 | ncbytesfree(buf); |
1167 | 0 | if(result == NULL) result = strdup(""); |
1168 | 0 | return result; |
1169 | 0 | } |
1170 | | |
1171 | | #if 0 |
1172 | | /* Given a comma separated string, remove duplicates; mostly used to cleanup mode list */ |
1173 | | static char* |
1174 | | cleancommalist(const char* commalist, int caseinsensitive) |
1175 | | { |
1176 | | NClist* tmp = nclistnew(); |
1177 | | char* newlist = NULL; |
1178 | | if(commalist == NULL || strlen(commalist)==0) return nulldup(commalist); |
1179 | | (void)parseonchar(commalist,',',tmp);/* split on commas */ |
1180 | | cleanstringlist(tmp,caseinsensitive); |
1181 | | newlist = list2string(tmp); |
1182 | | nclistfreeall(tmp); |
1183 | | return newlist; |
1184 | | } |
1185 | | #endif |
1186 | | |
1187 | | /* Given a list of strings, remove nulls and duplicated */ |
1188 | | static void |
1189 | | cleanstringlist(NClist* strs, int caseinsensitive) |
1190 | 0 | { |
1191 | 0 | if(nclistlength(strs) == 0) return; |
1192 | | /* Remove nulls */ |
1193 | 0 | for(size_t i = nclistlength(strs); i-->0;) { |
1194 | 0 | if(nclistget(strs,i)==NULL) nclistremove(strs,i); |
1195 | 0 | } |
1196 | 0 | if(nclistlength(strs) <= 1) return; |
1197 | | /* Remove duplicates*/ |
1198 | 0 | for(size_t i=0;i<nclistlength(strs);i++) { |
1199 | 0 | const char* value = nclistget(strs,i); |
1200 | | /* look ahead for duplicates */ |
1201 | 0 | for(size_t j=nclistlength(strs)-1;j>i;j--) { |
1202 | 0 | int match; |
1203 | 0 | const char* candidate = nclistget(strs,j); |
1204 | 0 | if(caseinsensitive) |
1205 | 0 | match = (strcasecmp(value,candidate) == 0); |
1206 | 0 | else |
1207 | 0 | match = (strcmp(value,candidate) == 0); |
1208 | 0 | if(match) {char* dup = nclistremove(strs,j); nullfree(dup);} |
1209 | 0 | } |
1210 | 0 | } |
1211 | 0 | } |
1212 | | |
1213 | | |
1214 | | /**************************************************/ |
1215 | | /** |
1216 | | * @internal Given an existing file, figure out its format and return |
1217 | | * that format value (NC_FORMATX_XXX) in model arg. Assume any path |
1218 | | * conversion was already performed at a higher level. |
1219 | | * |
1220 | | * @param path File name. |
1221 | | * @param flags |
1222 | | * @param use_parallel |
1223 | | * @param parameters |
1224 | | * @param model Pointer that gets the model to use for the dispatch table. |
1225 | | * @param version Pointer that gets version of the file. |
1226 | | * |
1227 | | * @return ::NC_NOERR No error. |
1228 | | * @author Dennis Heimbigner |
1229 | | */ |
1230 | | static int |
1231 | | check_file_type(const char *path, int omode, int use_parallel, |
1232 | | void *parameters, NCmodel* model, NCURI* uri) |
1233 | 77 | { |
1234 | 77 | char magic[NC_MAX_MAGIC_NUMBER_LEN]; |
1235 | 77 | int status = NC_NOERR; |
1236 | 77 | struct MagicFile magicinfo; |
1237 | | #ifdef _WIN32 |
1238 | | NC* nc = NULL; |
1239 | | #endif |
1240 | | |
1241 | 77 | memset((void*)&magicinfo,0,sizeof(magicinfo)); |
1242 | | |
1243 | | #ifdef _WIN32 /* including MINGW */ |
1244 | | /* Windows does not handle multiple handles to the same file very well. |
1245 | | So if file is already open/created, then find it and just get the |
1246 | | model from that. */ |
1247 | | if((nc = find_in_NCList_by_name(path)) != NULL) { |
1248 | | int format = 0; |
1249 | | /* Get the model from this NC */ |
1250 | | if((status = nc_inq_format_extended(nc->ext_ncid,&format,NULL))) goto done; |
1251 | | model->impl = format; |
1252 | | if((status = nc_inq_format(nc->ext_ncid,&format))) goto done; |
1253 | | model->format = format; |
1254 | | goto done; |
1255 | | } |
1256 | | #endif |
1257 | | |
1258 | 77 | magicinfo.path = path; /* do not free */ |
1259 | 77 | magicinfo.uri = uri; /* do not free */ |
1260 | 77 | magicinfo.omode = omode; |
1261 | 77 | magicinfo.model = model; /* do not free */ |
1262 | 77 | magicinfo.parameters = parameters; /* do not free */ |
1263 | | #ifdef USE_STDIO |
1264 | | magicinfo.use_parallel = 0; |
1265 | | #else |
1266 | 77 | magicinfo.use_parallel = use_parallel; |
1267 | 77 | #endif |
1268 | | |
1269 | 77 | if((status = openmagic(&magicinfo))) goto done; |
1270 | | |
1271 | | /* Verify we have a large enough file */ |
1272 | 77 | if(MAGIC_NUMBER_LEN >= (unsigned long long)magicinfo.filelen) |
1273 | 0 | {status = NC_ENOTNC; goto done;} |
1274 | 77 | if((status = readmagic(&magicinfo,0L,magic)) != NC_NOERR) { |
1275 | 0 | status = NC_ENOTNC; |
1276 | 0 | goto done; |
1277 | 0 | } |
1278 | | |
1279 | | /* Look at the magic number */ |
1280 | 77 | if(NC_interpret_magic_number(magic,model) == NC_NOERR |
1281 | 70 | && model->format != 0) { |
1282 | 70 | if (use_parallel && (model->format == NC_FORMAT_NC3 || model->impl == NC_FORMATX_NC3)) |
1283 | | /* this is called from nc_open_par() and file is classic */ |
1284 | 0 | model->impl = NC_FORMATX_PNETCDF; |
1285 | 70 | goto done; /* found something */ |
1286 | 70 | } |
1287 | | |
1288 | | /* Remaining case when implementation is an HDF5 file; |
1289 | | search forward at starting at 512 |
1290 | | and doubling to see if we have HDF5 magic number */ |
1291 | 7 | { |
1292 | 7 | size_t pos = 512L; |
1293 | 75 | for(;;) { |
1294 | 75 | if((pos+MAGIC_NUMBER_LEN) > (unsigned long long)magicinfo.filelen) |
1295 | 7 | {status = NC_ENOTNC; goto done;} |
1296 | 68 | if((status = readmagic(&magicinfo,pos,magic)) != NC_NOERR) |
1297 | 0 | {status = NC_ENOTNC; goto done; } |
1298 | 68 | NC_interpret_magic_number(magic,model); |
1299 | 68 | if(model->impl == NC_FORMATX_NC4) break; |
1300 | | /* double and try again */ |
1301 | 68 | pos = 2*pos; |
1302 | 68 | } |
1303 | 7 | } |
1304 | 77 | done: |
1305 | 77 | closemagic(&magicinfo); |
1306 | 77 | return check(status); |
1307 | 7 | } |
1308 | | |
1309 | | /** |
1310 | | \internal |
1311 | | \ingroup datasets |
1312 | | Provide open, read and close for use when searching for magic numbers |
1313 | | */ |
1314 | | static int |
1315 | | openmagic(struct MagicFile* file) |
1316 | 77 | { |
1317 | 77 | int status = NC_NOERR; |
1318 | 77 | if(fIsSet(file->omode,NC_INMEMORY)) { |
1319 | | /* Get its length */ |
1320 | 77 | NC_memio* meminfo = (NC_memio*)file->parameters; |
1321 | 77 | assert(meminfo != NULL); |
1322 | 77 | file->filelen = (long long)meminfo->size; |
1323 | 77 | goto done; |
1324 | 77 | } |
1325 | 0 | if(file->uri != NULL) { |
1326 | | #ifdef NETCDF_ENABLE_BYTERANGE |
1327 | | /* Construct a URL minus any fragment */ |
1328 | | file->curlurl = ncuribuild(file->uri,NULL,NULL,NCURISVC); |
1329 | | /* Open the curl handle */ |
1330 | | if((status=nc_http_open(file->path, &file->state))) goto done; |
1331 | | if((status=nc_http_size(file->state,&file->filelen))) goto done; |
1332 | | #else /*!BYTERANGE*/ |
1333 | 0 | {status = NC_ENOTBUILT;} |
1334 | 0 | #endif /*BYTERANGE*/ |
1335 | 0 | goto done; |
1336 | 0 | } |
1337 | | #ifdef USE_PARALLEL |
1338 | | if (file->use_parallel) { |
1339 | | int retval; |
1340 | | MPI_Offset size; |
1341 | | assert(file->parameters != NULL); |
1342 | | if((retval = MPI_File_open(((NC_MPI_INFO*)file->parameters)->comm, |
1343 | | (char*)file->path,MPI_MODE_RDONLY, |
1344 | | ((NC_MPI_INFO*)file->parameters)->info, |
1345 | | &file->fh)) != MPI_SUCCESS) { |
1346 | | #ifdef MPI_ERR_NO_SUCH_FILE |
1347 | | int errorclass; |
1348 | | MPI_Error_class(retval, &errorclass); |
1349 | | if (errorclass == MPI_ERR_NO_SUCH_FILE) |
1350 | | #ifdef NC_ENOENT |
1351 | | status = NC_ENOENT; |
1352 | | #else /*!NC_ENOENT*/ |
1353 | | status = errno; |
1354 | | #endif /*NC_ENOENT*/ |
1355 | | else |
1356 | | #endif /*MPI_ERR_NO_SUCH_FILE*/ |
1357 | | status = NC_EPARINIT; |
1358 | | file->fh = MPI_FILE_NULL; |
1359 | | goto done; |
1360 | | } |
1361 | | /* Get its length */ |
1362 | | if((retval=MPI_File_get_size(file->fh, &size)) != MPI_SUCCESS) |
1363 | | {status = NC_EPARINIT; goto done;} |
1364 | | file->filelen = (long long)size; |
1365 | | goto done; |
1366 | | } |
1367 | | #endif /* USE_PARALLEL */ |
1368 | 0 | { |
1369 | 0 | if (file->path == NULL || strlen(file->path) == 0) |
1370 | 0 | {status = NC_EINVAL; goto done;} |
1371 | 0 | file->fp = NCfopen(file->path, "r"); |
1372 | 0 | if(file->fp == NULL) |
1373 | 0 | {status = errno; goto done;} |
1374 | | /* Get its length */ |
1375 | 0 | { |
1376 | 0 | int fd = fileno(file->fp); |
1377 | | #ifdef _WIN32 |
1378 | | __int64 len64 = _filelengthi64(fd); |
1379 | | if(len64 < 0) |
1380 | | {status = errno; goto done;} |
1381 | | file->filelen = (long long)len64; |
1382 | | #else |
1383 | 0 | off_t size; |
1384 | 0 | size = lseek(fd, 0, SEEK_END); |
1385 | 0 | if(size == -1) |
1386 | 0 | {status = errno; goto done;} |
1387 | 0 | file->filelen = (long long)size; |
1388 | 0 | #endif |
1389 | 0 | } |
1390 | 0 | int retval2 = fseek(file->fp, 0L, SEEK_SET); |
1391 | 0 | if(retval2 != 0) |
1392 | 0 | {status = errno; goto done;} |
1393 | 0 | } |
1394 | 77 | done: |
1395 | 77 | return check(status); |
1396 | 0 | } |
1397 | | |
1398 | | static int |
1399 | | readmagic(struct MagicFile* file, size_t pos, char* magic) |
1400 | 145 | { |
1401 | 145 | int status = NC_NOERR; |
1402 | 145 | NCbytes* buf = ncbytesnew(); |
1403 | | |
1404 | 145 | memset(magic,0,MAGIC_NUMBER_LEN); |
1405 | 145 | if(fIsSet(file->omode,NC_INMEMORY)) { |
1406 | 145 | char* mempos; |
1407 | 145 | NC_memio* meminfo = (NC_memio*)file->parameters; |
1408 | 145 | if((pos + MAGIC_NUMBER_LEN) > meminfo->size) |
1409 | 0 | {status = NC_EINMEMORY; goto done;} |
1410 | 145 | mempos = ((char*)meminfo->memory) + pos; |
1411 | 145 | memcpy((void*)magic,mempos,MAGIC_NUMBER_LEN); |
1412 | | #ifdef DEBUG |
1413 | | printmagic("XXX: readmagic",magic,file); |
1414 | | #endif |
1415 | 145 | } else if(file->uri != NULL) { |
1416 | | #ifdef NETCDF_ENABLE_BYTERANGE |
1417 | | size64_t start = (size64_t)pos; |
1418 | | size64_t count = MAGIC_NUMBER_LEN; |
1419 | | status = nc_http_read(file->state, start, count, buf); |
1420 | | if (status == NC_NOERR) { |
1421 | | if (ncbyteslength(buf) != count) |
1422 | | status = NC_EINVAL; |
1423 | | else |
1424 | | memcpy(magic, ncbytescontents(buf), count); |
1425 | | } |
1426 | | #endif |
1427 | 0 | } else { |
1428 | | #ifdef USE_PARALLEL |
1429 | | if (file->use_parallel) { |
1430 | | MPI_Status mstatus; |
1431 | | int retval; |
1432 | | if((retval = MPI_File_read_at_all(file->fh, pos, magic, |
1433 | | MAGIC_NUMBER_LEN, MPI_CHAR, &mstatus)) != MPI_SUCCESS) |
1434 | | {status = NC_EPARINIT; goto done;} |
1435 | | } |
1436 | | else |
1437 | | #endif /* USE_PARALLEL */ |
1438 | 0 | { /* Ordinary read */ |
1439 | 0 | long i; |
1440 | 0 | i = fseek(file->fp, (long)pos, SEEK_SET); |
1441 | 0 | if (i < 0) { status = errno; goto done; } |
1442 | 0 | ncbytessetlength(buf, 0); |
1443 | 0 | if ((status = NC_readfileF(file->fp, buf, MAGIC_NUMBER_LEN))) goto done; |
1444 | 0 | memcpy(magic, ncbytescontents(buf), MAGIC_NUMBER_LEN); |
1445 | 0 | } |
1446 | 0 | } |
1447 | | |
1448 | 145 | done: |
1449 | 145 | ncbytesfree(buf); |
1450 | 145 | if(file && file->fp) clearerr(file->fp); |
1451 | 145 | return check(status); |
1452 | 145 | } |
1453 | | |
1454 | | /** |
1455 | | * Close the file opened to check for magic number. |
1456 | | * |
1457 | | * @param file pointer to the MagicFile struct for this open file. |
1458 | | * @returns NC_NOERR for success |
1459 | | * @returns NC_EPARINIT if there was a problem closing file with MPI |
1460 | | * (parallel builds only). |
1461 | | * @author Dennis Heimbigner |
1462 | | */ |
1463 | | static int |
1464 | | closemagic(struct MagicFile* file) |
1465 | 77 | { |
1466 | 77 | int status = NC_NOERR; |
1467 | | |
1468 | 77 | if(fIsSet(file->omode,NC_INMEMORY)) { |
1469 | | /* noop */ |
1470 | 77 | } else if(file->uri != NULL) { |
1471 | | #ifdef NETCDF_ENABLE_BYTERANGE |
1472 | | status = nc_http_close(file->state); |
1473 | | #endif |
1474 | 0 | nullfree(file->curlurl); |
1475 | 0 | } else { |
1476 | | #ifdef USE_PARALLEL |
1477 | | if (file->use_parallel) { |
1478 | | int retval; |
1479 | | if(file->fh != MPI_FILE_NULL |
1480 | | && (retval = MPI_File_close(&file->fh)) != MPI_SUCCESS) |
1481 | | {status = NC_EPARINIT; return status;} |
1482 | | } else |
1483 | | #endif |
1484 | 0 | { |
1485 | 0 | if(file->fp) fclose(file->fp); |
1486 | 0 | } |
1487 | 0 | } |
1488 | 77 | return status; |
1489 | 77 | } |
1490 | | |
1491 | | /*! |
1492 | | Interpret the magic number found in the header of a netCDF file. |
1493 | | This function interprets the magic number/string contained in the header of a netCDF file and sets the appropriate NC_FORMATX flags. |
1494 | | |
1495 | | @param[in] magic Pointer to a character array with the magic number block. |
1496 | | @param[out] model Pointer to an integer to hold the corresponding netCDF type. |
1497 | | @param[out] version Pointer to an integer to hold the corresponding netCDF version. |
1498 | | @returns NC_NOERR if a legitimate file type found |
1499 | | @returns NC_ENOTNC otherwise |
1500 | | |
1501 | | \internal |
1502 | | \ingroup datasets |
1503 | | |
1504 | | */ |
1505 | | static int |
1506 | | NC_interpret_magic_number(char* magic, NCmodel* model) |
1507 | 145 | { |
1508 | 145 | int status = NC_NOERR; |
1509 | 145 | int tmpimpl = 0; |
1510 | | /* Look at the magic number - save any UDF format on entry */ |
1511 | 145 | if(udfimpl(model->impl)) |
1512 | 0 | tmpimpl = model->impl; |
1513 | | |
1514 | | /* Use the complete magic number string for HDF5 */ |
1515 | 145 | if(memcmp(magic,HDF5_SIGNATURE,sizeof(HDF5_SIGNATURE))==0) { |
1516 | 0 | model->impl = NC_FORMATX_NC4; |
1517 | 0 | model->format = NC_FORMAT_NETCDF4; |
1518 | 0 | goto done; |
1519 | 0 | } |
1520 | 145 | if(magic[0] == '\016' && magic[1] == '\003' |
1521 | 1 | && magic[2] == '\023' && magic[3] == '\001') { |
1522 | 0 | model->impl = NC_FORMATX_NC_HDF4; |
1523 | 0 | model->format = NC_FORMAT_NETCDF4; |
1524 | 0 | goto done; |
1525 | 0 | } |
1526 | 145 | if(magic[0] == 'C' && magic[1] == 'D' && magic[2] == 'F') { |
1527 | 70 | if(magic[3] == '\001') { |
1528 | 22 | model->impl = NC_FORMATX_NC3; |
1529 | 22 | model->format = NC_FORMAT_CLASSIC; |
1530 | 22 | goto done; |
1531 | 22 | } |
1532 | 48 | if(magic[3] == '\002') { |
1533 | 25 | model->impl = NC_FORMATX_NC3; |
1534 | 25 | model->format = NC_FORMAT_64BIT_OFFSET; |
1535 | 25 | goto done; |
1536 | 25 | } |
1537 | 23 | if(magic[3] == '\005') { |
1538 | 23 | model->impl = NC_FORMATX_NC3; |
1539 | 23 | model->format = NC_FORMAT_64BIT_DATA; |
1540 | 23 | goto done; |
1541 | 23 | } |
1542 | 23 | } |
1543 | | /* No match */ |
1544 | 75 | if (!tmpimpl) |
1545 | 75 | status = NC_ENOTNC; |
1546 | | |
1547 | 75 | goto done; |
1548 | | |
1549 | 145 | done: |
1550 | | /* if model->impl was any UDF format on entry, make it so on exit */ |
1551 | 145 | if(tmpimpl) |
1552 | 0 | model->impl = tmpimpl; |
1553 | | /* if this is a UDF magic_number update the model->impl */ |
1554 | 9.42k | for(int i = 0; i < NC_MAX_UDF_FORMATS; i++) { |
1555 | 9.28k | if (strlen(UDF_magic_numbers[i]) && !strncmp(UDF_magic_numbers[i], magic, |
1556 | 0 | strlen(UDF_magic_numbers[i]))) |
1557 | 0 | { |
1558 | 0 | model->impl = NC_FORMATX_UDF(i); |
1559 | 0 | model->format = NC_FORMAT_NETCDF4; |
1560 | 0 | status = NC_NOERR; |
1561 | 0 | break; |
1562 | 0 | } |
1563 | 9.28k | } |
1564 | | |
1565 | 145 | return check(status); |
1566 | 145 | } |
1567 | | |
1568 | | /* Define macros to wrap getxattr and listxattrcalls */ |
1569 | | #ifdef __APPLE__ |
1570 | | #define GETXATTR(path,p,xvalue,xlen) getxattr(path, p, xvalue, (size_t)xlen, 0, 0); |
1571 | | #define LISTXATTR(path,xlist,xlen) listxattr(path, xlist, (size_t)xlen, 0) |
1572 | | #else |
1573 | | #define GETXATTR(path,p,xvalue,xlen) getxattr(path, p, xvalue, (size_t)xlen); |
1574 | | #define LISTXATTR(path,xlist,xlen) listxattr(path, xlist, (size_t)xlen) |
1575 | | #endif |
1576 | | |
1577 | | /* Return NC_NOERR if path is a DAOS container; return NC_EXXX otherwise */ |
1578 | | static int |
1579 | | isdaoscontainer(const char* path) |
1580 | 77 | { |
1581 | 77 | int stat = NC_ENOTNC; /* default is that this is not a DAOS container */ |
1582 | 77 | #ifndef _WIN32 |
1583 | | #ifdef USE_HDF5 |
1584 | | #if H5_VERSION_GE(1,12,0) |
1585 | | htri_t accessible; |
1586 | | hid_t fapl_id; |
1587 | | int rc; |
1588 | | /* Check for a DAOS container */ |
1589 | | if((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0) {stat = NC_EHDFERR; goto done;} |
1590 | | H5Pset_fapl_sec2(fapl_id); |
1591 | | accessible = H5Fis_accessible(path, fapl_id); |
1592 | | H5Pclose(fapl_id); /* Ignore any error */ |
1593 | | rc = 0; |
1594 | | if(accessible > 0) { |
1595 | | #ifdef HAVE_SYS_XATTR_H |
1596 | | ssize_t xlen; |
1597 | | xlen = LISTXATTR(path,NULL,0); |
1598 | | if(xlen > 0) { |
1599 | | char* xlist = NULL; |
1600 | | char* xvalue = NULL; |
1601 | | char* p; |
1602 | | char* endp; |
1603 | | if((xlist = (char*)calloc(1,(size_t)xlen))==NULL) |
1604 | | {stat = NC_ENOMEM; goto done;} |
1605 | | (void)LISTXATTR(path,xlist,xlen); |
1606 | | p = xlist; endp = p + xlen; /* delimit names */ |
1607 | | /* walk the list of xattr names */ |
1608 | | for(;p < endp;p += (strlen(p)+1)) { |
1609 | | /* The popen version looks for the string ".daos"; |
1610 | | It would be nice if we know whether that occurred |
1611 | | int the xattr's name or it value. |
1612 | | Oh well, we will do the general search */ |
1613 | | /* Look for '.daos' in the key */ |
1614 | | if(strstr(p,".daos") != NULL) {rc = 1; break;} /* success */ |
1615 | | /* Else get the p'th xattr's value size */ |
1616 | | xlen = GETXATTR(path,p,NULL,0); |
1617 | | if(xlen <= 0) continue; /* no value or not readable */ |
1618 | | nullfree(xvalue); |
1619 | | /* +1 because the value is not necessarily nul terminated */ |
1620 | | if((xvalue = (char*)calloc(1,(size_t)xlen+1))==NULL) |
1621 | | {nullfree(xlist); stat = NC_ENOMEM; goto done;} |
1622 | | /* Read the value */ |
1623 | | (void)GETXATTR(path,p,xvalue,xlen); |
1624 | | /* Look for '.daos' in the value */ |
1625 | | if(strstr(xvalue,".daos") != NULL) {rc = 1; break;} /* success */ |
1626 | | } |
1627 | | nullfree(xvalue); |
1628 | | nullfree(xlist); |
1629 | | } |
1630 | | #else /*!HAVE_SYS_XATTR_H*/ |
1631 | | |
1632 | | #ifdef HAVE_GETFATTR |
1633 | | { |
1634 | | FILE *fp; |
1635 | | char cmd[4096]; |
1636 | | memset(cmd,0,sizeof(cmd)); |
1637 | | snprintf(cmd,sizeof(cmd),"getfattr \"%s\" | grep -c '.daos'",path); |
1638 | | fp = popen(cmd, "r"); |
1639 | | if(fp != NULL) { |
1640 | | fscanf(fp, "%d", &rc); |
1641 | | pclose(fp); |
1642 | | } else { |
1643 | | rc = 0; /* Cannot test; assume not DAOS */ |
1644 | | } |
1645 | | } |
1646 | | } |
1647 | | #else /*!HAVE_GETFATTR*/ |
1648 | | /* We just can't test for DAOS container.*/ |
1649 | | rc = 0; |
1650 | | #endif /*HAVE_GETFATTR*/ |
1651 | | #endif /*HAVE_SYS_XATTR_H*/ |
1652 | | } |
1653 | | /* Test for DAOS container */ |
1654 | | stat = (rc == 1 ? NC_NOERR : NC_ENOTNC); |
1655 | | done: |
1656 | | #endif |
1657 | | #endif |
1658 | 77 | #endif |
1659 | | errno = 0; /* reset */ |
1660 | 77 | return stat; |
1661 | 77 | } |
1662 | | |
1663 | | #ifdef DEBUG |
1664 | | static void |
1665 | | printmagic(const char* tag, char* magic, struct MagicFile* f) |
1666 | | { |
1667 | | int i; |
1668 | | fprintf(stderr,"%s: ispar=%d magic=",tag,f->use_parallel); |
1669 | | for(i=0;i<MAGIC_NUMBER_LEN;i++) { |
1670 | | unsigned int c = (unsigned int)magic[i]; |
1671 | | c = c & 0x000000FF; |
1672 | | if(c == '\n') |
1673 | | fprintf(stderr," 0x%0x/'\\n'",c); |
1674 | | else if(c == '\r') |
1675 | | fprintf(stderr," 0x%0x/'\\r'",c); |
1676 | | else if(c < ' ') |
1677 | | fprintf(stderr," 0x%0x/'?'",c); |
1678 | | else |
1679 | | fprintf(stderr," 0x%0x/'%c'",c,c); |
1680 | | } |
1681 | | fprintf(stderr,"\n"); |
1682 | | fflush(stderr); |
1683 | | } |
1684 | | |
1685 | | static void |
1686 | | printlist(NClist* list, const char* tag) |
1687 | | { |
1688 | | int i; |
1689 | | fprintf(stderr,"%s:",tag); |
1690 | | for(i=0;i<nclistlength(list);i++) { |
1691 | | fprintf(stderr," %s",(char*)nclistget(list,i)); |
1692 | | fprintf(stderr,"[%p]",(char*)nclistget(list,i)); |
1693 | | } |
1694 | | fprintf(stderr,"\n"); |
1695 | | dbgflush(); |
1696 | | } |
1697 | | |
1698 | | |
1699 | | #endif |