-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathdinfermodel.c
1714 lines (1573 loc) · 46.1 KB
/
dinfermodel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
*
* Infer as much as possible from the omode + path.
* Rewrite the path to a canonical form.
*
* Copyright 2018 University Corporation for Atmospheric
* Research/Unidata. See COPYRIGHT file for more info.
*/
#include "config.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifndef _WIN32
#ifdef USE_HDF5
#include <hdf5.h>
#endif /* USE_HDF5 */
#endif /* _WIN32 */
#ifdef HAVE_SYS_XATTR_H
#include <sys/xattr.h>
#endif
#include "ncdispatch.h"
#include "ncpathmgr.h"
#include "netcdf_mem.h"
#include "fbits.h"
#include "ncbytes.h"
#include "nclist.h"
#include "nclog.h"
#include "ncrc.h"
#include "nchttp.h"
#ifdef NETCDF_ENABLE_S3
#include "ncs3sdk.h"
#endif
#ifndef nulldup
#define nulldup(x) ((x)?strdup(x):(x))
#endif
#undef DEBUG
/* If Defined, then use only stdio for all magic number io;
otherwise use stdio or mpio as required.
*/
#undef USE_STDIO
/**
Sort info for open/read/close of
file when searching for magic numbers
*/
struct MagicFile {
const char* path;
struct NCURI* uri;
int omode;
NCmodel* model;
long long filelen;
int use_parallel;
int iss3;
void* parameters; /* !NULL if inmemory && !diskless */
FILE* fp;
#ifdef USE_PARALLEL
MPI_File fh;
#endif
char* curlurl; /* url to use with CURLOPT_SET_URL */
NC_HTTP_STATE* state;
#ifdef NETCDF_ENABLE_S3
NCS3INFO s3;
void* s3client;
char* errmsg;
#endif
};
/** @internal Magic number for HDF5 files. To be consistent with
* H5Fis_hdf5, use the complete HDF5 magic number */
static char HDF5_SIGNATURE[MAGIC_NUMBER_LEN] = "\211HDF\r\n\032\n";
#define modelcomplete(model) ((model)->impl != 0)
#ifdef DEBUG
static void dbgflush(void)
{
fflush(stdout);
fflush(stderr);
}
static void
fail(int err)
{
return;
}
static int
check(int err)
{
if(err != NC_NOERR)
fail(err);
return err;
}
#else
#define check(err) (err)
#endif
/*
Define a table of "mode=" string values
from which the implementation can be inferred.
Note that only cases that can currently
take URLs are included.
*/
static struct FORMATMODES {
const char* tag;
const int impl; /* NC_FORMATX_XXX value */
const int format; /* NC_FORMAT_XXX value */
} formatmodes[] = {
{"dap2",NC_FORMATX_DAP2,NC_FORMAT_CLASSIC},
{"dap4",NC_FORMATX_DAP4,NC_FORMAT_NETCDF4},
{"netcdf-3",NC_FORMATX_NC3,0}, /* Might be e.g. cdf5 */
{"classic",NC_FORMATX_NC3,0}, /* ditto */
{"netcdf-4",NC_FORMATX_NC4,NC_FORMAT_NETCDF4},
{"enhanced",NC_FORMATX_NC4,NC_FORMAT_NETCDF4},
{"udf0",NC_FORMATX_UDF0,0},
{"udf1",NC_FORMATX_UDF1,0},
{"nczarr",NC_FORMATX_NCZARR,NC_FORMAT_NETCDF4},
{"zarr",NC_FORMATX_NCZARR,NC_FORMAT_NETCDF4},
{"bytes",NC_FORMATX_NC4,NC_FORMAT_NETCDF4}, /* temporary until 3 vs 4 is determined */
{NULL,0},
};
/* Replace top-level name with defkey=defvalue */
static const struct MACRODEF {
char* name;
char* defkey;
char* defvalues[4];
} macrodefs[] = {
{"zarr","mode",{"nczarr","zarr",NULL}},
{"dap2","mode",{"dap2",NULL}},
{"dap4","mode",{"dap4",NULL}},
{"s3","mode",{"s3","nczarr",NULL}},
{"bytes","mode",{"bytes",NULL}},
{"xarray","mode",{"zarr", NULL}},
{"noxarray","mode",{"nczarr", "noxarray", NULL}},
{"zarr","mode",{"nczarr","zarr", NULL}},
{"gs3","mode",{"gs3","nczarr",NULL}}, /* Google S3 API */
{NULL,NULL,{NULL}}
};
/*
Mode inferences: if mode contains key value, then add the inferred value;
Warning: be careful how this list is constructed to avoid infinite inferences.
In order to (mostly) avoid that consequence, any attempt to
infer a value that is already present will be ignored.
This effectively means that the inference graph
must be a DAG and may not have cycles.
You have been warned.
*/
static const struct MODEINFER {
char* key;
char* inference;
} modeinferences[] = {
{"zarr","nczarr"},
{"xarray","zarr"},
{"noxarray","nczarr"},
{"noxarray","zarr"},
{NULL,NULL}
};
/* Mode negations: if mode contains key, then remove all occurrences of the inference and repeat */
static const struct MODEINFER modenegations[] = {
{"bytes","nczarr"}, /* bytes negates (nc)zarr */
{"bytes","zarr"},
{"noxarray","xarray"},
{NULL,NULL}
};
/* Map FORMATX to readability to get magic number */
static struct Readable {
int impl;
int readable;
} readable[] = {
{NC_FORMATX_NC3,1},
{NC_FORMATX_NC_HDF5,1},
{NC_FORMATX_NC_HDF4,1},
{NC_FORMATX_PNETCDF,1},
{NC_FORMATX_DAP2,0},
{NC_FORMATX_DAP4,0},
{NC_FORMATX_UDF0,1},
{NC_FORMATX_UDF1,1},
{NC_FORMATX_NCZARR,0}, /* eventually make readable */
{0,0},
};
/* Define the known URL protocols and their interpretation */
static struct NCPROTOCOLLIST {
const char* protocol;
const char* substitute;
const char* fragments; /* arbitrary fragment arguments */
} ncprotolist[] = {
{"http",NULL,NULL},
{"https",NULL,NULL},
{"file",NULL,NULL},
{"dods","http","mode=dap2"},
{"dap4","http","mode=dap4"},
{"s3","s3","mode=s3"},
{"gs3","gs3","mode=gs3"},
{NULL,NULL,NULL} /* Terminate search */
};
/* Forward */
static int NC_omodeinfer(int useparallel, int omode, NCmodel*);
static int check_file_type(const char *path, int omode, int use_parallel, void *parameters, NCmodel* model, NCURI* uri);
static int processuri(const char* path, NCURI** urip, NClist* fraglist);
static int processmacros(NClist* fraglistp, NClist* expanded);
static char* envvlist2string(NClist* pairs, const char*);
static void set_default_mode(int* cmodep);
static int parseonchar(const char* s, int ch, NClist* segments);
static int mergelist(NClist** valuesp);
static int openmagic(struct MagicFile* file);
static int readmagic(struct MagicFile* file, size_t pos, char* magic);
static int closemagic(struct MagicFile* file);
static int NC_interpret_magic_number(char* magic, NCmodel* model);
#ifdef DEBUG
static void printmagic(const char* tag, char* magic,struct MagicFile*);
static void printlist(NClist* list, const char* tag);
#endif
static int isreadable(NCURI*,NCmodel*);
static char* list2string(NClist*);
static int parsepair(const char* pair, char** keyp, char** valuep);
static NClist* parsemode(const char* modeval);
static const char* getmodekey(const NClist* envv);
static int replacemode(NClist* envv, const char* newval);
static void infernext(NClist* current, NClist* next);
static int negateone(const char* mode, NClist* modes);
static void cleanstringlist(NClist* strs, int caseinsensitive);
static int isdaoscontainer(const char* path);
/*
If the path looks like a URL, then parse it, reformat it.
*/
static int
processuri(const char* path, NCURI** urip, NClist* fraglenv)
{
int stat = NC_NOERR;
int found = 0;
NClist* tmp = NULL;
struct NCPROTOCOLLIST* protolist;
NCURI* uri = NULL;
size_t pathlen = strlen(path);
char* str = NULL;
const NClist* ufrags;
size_t i;
if(path == NULL || pathlen == 0) {stat = NC_EURL; goto done;}
/* Defaults */
if(urip) *urip = NULL;
ncuriparse(path,&uri);
if(uri == NULL) goto done; /* not url */
/* Look up the protocol */
for(found=0,protolist=ncprotolist;protolist->protocol;protolist++) {
if(strcmp(uri->protocol,protolist->protocol) == 0) {
found = 1;
break;
}
}
if(!found)
{stat = NC_EINVAL; goto done;} /* unrecognized URL form */
/* process the corresponding fragments for that protocol */
if(protolist->fragments != NULL) {
tmp = nclistnew();
if((stat = parseonchar(protolist->fragments,'&',tmp))) goto done;
for(i=0;i<nclistlength(tmp);i++) {
char* key=NULL;
char* value=NULL;
if((stat = parsepair(nclistget(tmp,i),&key,&value))) goto done;
if(value == NULL) value = strdup("");
nclistpush(fraglenv,key);
nclistpush(fraglenv,value);
}
nclistfreeall(tmp); tmp = NULL;
}
/* Substitute the protocol in any case */
if(protolist->substitute) ncurisetprotocol(uri,protolist->substitute);
/* capture the fragments of the url */
ufrags = (const NClist*)ncurifragmentparams(uri);
for(i=0;i<nclistlength(ufrags);i+=2) {
const char* key = nclistget(ufrags,i);
const char* value = nclistget(ufrags,i+1);
nclistpush(fraglenv,nulldup(key));
value = (value==NULL?"":value);
nclistpush(fraglenv,strdup(value));
}
if(urip) {
*urip = uri;
uri = NULL;
}
done:
nclistfreeall(tmp);
nullfree(str);
if(uri != NULL) ncurifree(uri);
return check(stat);
}
/* Split a key=value pair */
static int
parsepair(const char* pair, char** keyp, char** valuep)
{
const char* p;
char* key = NULL;
char* value = NULL;
if(pair == NULL)
return NC_EINVAL; /* empty pair */
if(pair[0] == '\0' || pair[0] == '=')
return NC_EINVAL; /* no key */
p = strchr(pair,'=');
if(p == NULL) {
value = NULL;
key = strdup(pair);
} else {
ptrdiff_t len = (p-pair);
if((key = malloc((size_t)len+1))==NULL) return NC_ENOMEM;
memcpy(key,pair,(size_t)len);
key[len] = '\0';
if(p[1] == '\0')
value = NULL;
else
value = strdup(p+1);
}
if(keyp) {*keyp = key; key = NULL;};
if(valuep) {*valuep = value; value = NULL;};
nullfree(key);
nullfree(value);
return NC_NOERR;
}
#if 0
static int
parseurlmode(const char* modestr, NClist* list)
{
int stat = NC_NOERR;
const char* p = NULL;
const char* endp = NULL;
if(modestr == NULL || *modestr == '\0') goto done;
/* Split modestr 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(list,s);
if(*endp == '\0') break;
p = endp+1;
}
done:
return check(stat);
}
#endif
/* Split a string at a given char */
static int
parseonchar(const char* s, int ch, NClist* segments)
{
int stat = NC_NOERR;
const char* p = NULL;
const char* endp = NULL;
if(s == NULL || *s == '\0') goto done;
p = s;
for(;;) {
char* q;
ptrdiff_t slen;
endp = strchr(p,ch);
if(endp == NULL) endp = p + strlen(p);
slen = (endp - p);
if((q = malloc((size_t)slen+1)) == NULL) {stat = NC_ENOMEM; goto done;}
memcpy(q,p,(size_t)slen);
q[slen] = '\0';
nclistpush(segments,q);
if(*endp == '\0') break;
p = endp+1;
}
done:
return check(stat);
}
/* Convert a key,value envv pairlist into a delimited string*/
static char*
envvlist2string(NClist* envv, const char* delim)
{
size_t i;
NCbytes* buf = NULL;
char* result = NULL;
if(envv == NULL || nclistlength(envv) == 0) return NULL;
buf = ncbytesnew();
for(i=0;i<nclistlength(envv);i+=2) {
const char* key = nclistget(envv,i);
const char* val = nclistget(envv,i+1);
if(key == NULL || strlen(key) == 0) continue;
assert(val != NULL);
if(i > 0) ncbytescat(buf,"&");
ncbytescat(buf,key);
if(val != NULL && val[0] != '\0') {
ncbytescat(buf,"=");
ncbytescat(buf,val);
}
}
result = ncbytesextract(buf);
ncbytesfree(buf);
return result;
}
/* Given a mode= argument, fill in the impl */
static int
processmodearg(const char* arg, NCmodel* model)
{
int stat = NC_NOERR;
struct FORMATMODES* format = formatmodes;
for(;format->tag;format++) {
if(strcmp(format->tag,arg)==0) {
model->impl = format->impl;
if(format->format != 0) model->format = format->format;
}
}
return check(stat);
}
/* Given an envv fragment list, do macro replacement */
static int
processmacros(NClist* fraglenv, NClist* expanded)
{
size_t i;
int stat = NC_NOERR;
const struct MACRODEF* macros = NULL;
for(i=0;i<nclistlength(fraglenv);i+=2) {
int found = 0;
char* key = nclistget(fraglenv,i);
char* value = nclistget(fraglenv,i+1);
if(strlen(value) == 0) { /* must be a singleton */
for(macros=macrodefs;macros->name;macros++) {
if(strcmp(macros->name,key)==0) {
char* const * p;
nclistpush(expanded,strdup(macros->defkey));
for(p=macros->defvalues;*p;p++)
nclistpush(expanded,strdup(*p));
found = 1;
break;
}
}
}
if(!found) {/* pass thru */
nclistpush(expanded,strdup(key));
nclistpush(expanded,strdup(value));
}
}
return check(stat);
}
/* Process mode flag inferences */
static int
processinferences(NClist* fraglenv)
{
int stat = NC_NOERR;
const char* modeval = NULL;
NClist* newmodes = nclistnew();
NClist* currentmodes = NULL;
NClist* nextmodes = nclistnew();
size_t i;
char* newmodeval = NULL;
/* Get "mode" entry */
if((modeval = getmodekey(fraglenv))==NULL) goto done;
/* Get the mode as list */
currentmodes = parsemode(modeval);
#ifdef DEBUG
printlist(currentmodes,"processinferences: initial mode list");
#endif
/* Do what amounts to breadth first inferencing down the inference DAG. */
for(;;) {
NClist* tmp = NULL;
/* Compute the next set of inferred modes */
#ifdef DEBUG
printlist(currentmodes,"processinferences: current mode list");
#endif
infernext(currentmodes,nextmodes);
#ifdef DEBUG
printlist(nextmodes,"processinferences: next mode list");
#endif
/* move current modes into list of newmodes */
for(i=0;i<nclistlength(currentmodes);i++) {
nclistpush(newmodes,nclistget(currentmodes,i));
}
nclistsetlength(currentmodes,0); /* clear current mode list */
if(nclistlength(nextmodes) == 0) break; /* nothing more to do */
#ifdef DEBUG
printlist(newmodes,"processinferences: new mode list");
#endif
/* Swap current and next */
tmp = currentmodes;
currentmodes = nextmodes;
nextmodes = tmp;
tmp = NULL;
}
/* cleanup any unused elements in currentmodes */
nclistclearall(currentmodes);
/* Ensure no duplicates */
cleanstringlist(newmodes,1);
#ifdef DEBUG
printlist(newmodes,"processinferences: final inferred mode list");
#endif
/* Remove negative inferences */
for(i=0;i<nclistlength(newmodes);i++) {
const char* mode = nclistget(newmodes,i);
negateone(mode,newmodes);
}
/* Store new mode value */
if((newmodeval = list2string(newmodes))== NULL)
{stat = NC_ENOMEM; goto done;}
if((stat=replacemode(fraglenv,newmodeval))) goto done;
modeval = NULL;
done:
nullfree(newmodeval);
nclistfreeall(newmodes);
nclistfreeall(currentmodes);
nclistfreeall(nextmodes);
return check(stat);
}
static int
negateone(const char* mode, NClist* newmodes)
{
const struct MODEINFER* tests = modenegations;
int changed = 0;
for(;tests->key;tests++) {
if(strcasecmp(tests->key,mode)==0) {
/* Find and remove all instances of the inference value */
for(size_t i = nclistlength(newmodes); i-- > 0;) {
char* candidate = nclistget(newmodes,i);
if(strcasecmp(candidate,tests->inference)==0) {
nclistremove(newmodes,i);
nullfree(candidate);
changed = 1;
}
}
}
}
return changed;
}
static void
infernext(NClist* current, NClist* next)
{
size_t i;
for(i=0;i<nclistlength(current);i++) {
const struct MODEINFER* tests = NULL;
const char* cur = nclistget(current,i);
for(tests=modeinferences;tests->key;tests++) {
if(strcasecmp(tests->key,cur)==0) {
/* Append the inferred mode unless dup */
if(!nclistmatch(next,tests->inference,1))
nclistpush(next,strdup(tests->inference));
}
}
}
}
/*
Given a list of strings, remove nulls and duplicates
*/
static int
mergelist(NClist** valuesp)
{
size_t i,j;
int stat = NC_NOERR;
NClist* values = *valuesp;
NClist* allvalues = nclistnew();
NClist* newvalues = nclistnew();
char* value = NULL;
for(i=0;i<nclistlength(values);i++) {
char* val1 = nclistget(values,i);
/* split on commas and put pieces into allvalues */
if((stat=parseonchar(val1,',',allvalues))) goto done;
}
/* Remove duplicates and "" */
while(nclistlength(allvalues) > 0) {
value = nclistremove(allvalues,0);
if(strlen(value) == 0) {
nullfree(value); value = NULL;
} else {
for(j=0;j<nclistlength(newvalues);j++) {
char* candidate = nclistget(newvalues,j);
if(strcasecmp(candidate,value)==0)
{nullfree(value); value = NULL; break;}
}
}
if(value != NULL) {nclistpush(newvalues,value); value = NULL;}
}
/* Make sure to have at least 1 value */
if(nclistlength(newvalues)==0) nclistpush(newvalues,strdup(""));
*valuesp = values; values = NULL;
done:
nclistfree(allvalues);
nclistfreeall(values);
nclistfreeall(newvalues);
return check(stat);
}
static int
lcontains(NClist* l, const char* key0)
{
size_t i;
for(i=0;i<nclistlength(l);i++) {
const char* key1 = nclistget(l,i);
if(strcasecmp(key0,key1)==0) return 1;
}
return 0;
}
/* Warning values should not use nclistfreeall */
static void
collectvaluesbykey(NClist* fraglenv, const char* key, NClist* values)
{
size_t i;
/* collect all the values with the same key (including this one) */
for(i=0;i<nclistlength(fraglenv);i+=2) {
const char* key2 = nclistget(fraglenv,i);
if(strcasecmp(key,key2)==0) {
const char* value2 = nclistget(fraglenv,i+1);
nclistpush(values,value2); value2 = NULL;
}
}
}
/* Warning allkeys should not use nclistfreeall */
static void
collectallkeys(NClist* fraglenv, NClist* allkeys)
{
size_t i;
/* collect all the distinct keys */
for(i=0;i<nclistlength(fraglenv);i+=2) {
char* key = nclistget(fraglenv,i);
if(!lcontains(allkeys,key)) {
nclistpush(allkeys,key);
}
}
}
/* Given a fragment envv list, coalesce duplicate keys and remove duplicate values*/
static int
cleanfragments(NClist* fraglenv, NClist* newlist)
{
size_t i;
int stat = NC_NOERR;
NClist* tmp = NULL;
NClist* allkeys = NULL;
NCbytes* buf = NULL;
char* key = NULL;
char* value = NULL;
buf = ncbytesnew();
allkeys = nclistnew();
tmp = nclistnew();
/* collect all unique keys */
collectallkeys(fraglenv,allkeys);
/* Collect all values for same key across all fragment pairs */
for(i=0;i<nclistlength(allkeys);i++) {
key = nclistget(allkeys,i);
collectvaluesbykey(fraglenv,key,tmp);
/* merge the key values, remove duplicate */
if((stat=mergelist(&tmp))) goto done;
/* Construct key,value pair and insert into newlist */
key = strdup(key);
nclistpush(newlist,key);
value = list2string(tmp);
nclistpush(newlist,value);
nclistclear(tmp);
}
done:
nclistfree(allkeys);
nclistfree(tmp);
ncbytesfree(buf);
return check(stat);
}
/* process non-mode fragment keys in case they hold significance; currently not */
static int
processfragmentkeys(const char* key, const char* value, NCmodel* model)
{
return NC_NOERR;
}
/*
Infer from the mode + useparallel
only call if iscreate or file is not easily readable.
*/
static int
NC_omodeinfer(int useparallel, int cmode, NCmodel* model)
{
int stat = NC_NOERR;
/* If no format flags are set, then use default */
if(!fIsSet(cmode,NC_FORMAT_ALL))
set_default_mode(&cmode);
/* Process the cmode; may override some already set flags. The
* user-defined formats must be checked first. They may choose to
* use some of the other flags, like NC_NETCDF4, so we must first
* check NC_UDF0 and NC_UDF1 before checking for any other
* flag. */
if(fIsSet(cmode, NC_UDF0) || fIsSet(cmode, NC_UDF1))
{
if(fIsSet(cmode, NC_UDF0))
{
model->impl = NC_FORMATX_UDF0;
} else {
model->impl = NC_FORMATX_UDF1;
}
if(fIsSet(cmode,NC_64BIT_OFFSET))
{
model->format = NC_FORMAT_64BIT_OFFSET;
}
else if(fIsSet(cmode,NC_64BIT_DATA))
{
model->format = NC_FORMAT_64BIT_DATA;
}
else if(fIsSet(cmode,NC_NETCDF4))
{
if(fIsSet(cmode,NC_CLASSIC_MODEL))
model->format = NC_FORMAT_NETCDF4_CLASSIC;
else
model->format = NC_FORMAT_NETCDF4;
}
if(! model->format)
model->format = NC_FORMAT_CLASSIC;
goto done;
}
if(fIsSet(cmode,NC_64BIT_OFFSET)) {
model->impl = NC_FORMATX_NC3;
model->format = NC_FORMAT_64BIT_OFFSET;
goto done;
}
if(fIsSet(cmode,NC_64BIT_DATA)) {
model->impl = NC_FORMATX_NC3;
model->format = NC_FORMAT_64BIT_DATA;
goto done;
}
if(fIsSet(cmode,NC_NETCDF4)) {
model->impl = NC_FORMATX_NC4;
if(fIsSet(cmode,NC_CLASSIC_MODEL))
model->format = NC_FORMAT_NETCDF4_CLASSIC;
else
model->format = NC_FORMAT_NETCDF4;
goto done;
}
/* Default to classic model */
model->format = NC_FORMAT_CLASSIC;
model->impl = NC_FORMATX_NC3;
done:
/* Apply parallel flag */
if(useparallel) {
if(model->impl == NC_FORMATX_NC3)
model->impl = NC_FORMATX_PNETCDF;
}
return check(stat);
}
/*
If the mode flags do not necessarily specify the
format, then default it by adding in appropriate flags.
*/
static void
set_default_mode(int* modep)
{
int mode = *modep;
int dfaltformat;
dfaltformat = nc_get_default_format();
switch (dfaltformat) {
case NC_FORMAT_64BIT_OFFSET: mode |= NC_64BIT_OFFSET; break;
case NC_FORMAT_64BIT_DATA: mode |= NC_64BIT_DATA; break;
case NC_FORMAT_NETCDF4: mode |= NC_NETCDF4; break;
case NC_FORMAT_NETCDF4_CLASSIC: mode |= (NC_NETCDF4|NC_CLASSIC_MODEL); break;
case NC_FORMAT_CLASSIC: /* fall thru */
default: break; /* default to classic */
}
*modep = mode; /* final result */
}
/**************************************************/
/*
Infer model for this dataset using some
combination of cmode, path, and reading the dataset.
See the documentation in docs/internal.dox.
@param path
@param omode
@param iscreate
@param useparallel
@param params
@param model
@param newpathp
*/
int
NC_infermodel(const char* path, int* omodep, int iscreate, int useparallel, void* params, NCmodel* model, char** newpathp)
{
size_t i;
int stat = NC_NOERR;
NCURI* uri = NULL;
int omode = *omodep;
NClist* fraglenv = nclistnew();
NClist* modeargs = nclistnew();
char* sfrag = NULL;
const char* modeval = NULL;
char* abspath = NULL;
NClist* tmp = NULL;
/* Phase 1:
1. convert special protocols to http|https
2. begin collecting fragments
*/
if((stat = processuri(path, &uri, fraglenv))) goto done;
if(uri != NULL) {
#ifdef DEBUG
printlist(fraglenv,"processuri");
#endif
/* Phase 2: Expand macros and add to fraglenv */
nclistfreeall(tmp);
tmp = nclistnew();
if((stat = processmacros(fraglenv,tmp))) goto done;
nclistfreeall(fraglenv);
fraglenv = tmp; tmp = NULL;
#ifdef DEBUG
printlist(fraglenv,"processmacros");
#endif
/* Cleanup the fragment list */
nclistfreeall(tmp);
tmp = nclistnew();
if((stat = cleanfragments(fraglenv,tmp))) goto done;
nclistfreeall(fraglenv);
fraglenv = tmp; tmp = NULL;
/* Phase 2a: Expand mode inferences and add to fraglenv */
if((stat = processinferences(fraglenv))) goto done;
#ifdef DEBUG
printlist(fraglenv,"processinferences");
#endif
/* Phase 3: coalesce duplicate fragment keys and remove duplicate values */
nclistfreeall(tmp);
tmp = nclistnew();
if((stat = cleanfragments(fraglenv,tmp))) goto done;
nclistfreeall(fraglenv);
fraglenv = tmp; tmp = NULL;
#ifdef DEBUG
printlist(fraglenv,"cleanfragments");
#endif
/* Phase 4: Rebuild the url fragment and rebuilt the url */
sfrag = envvlist2string(fraglenv,"&");
nclistfreeall(fraglenv); fraglenv = NULL;
#ifdef DEBUG
fprintf(stderr,"frag final: %s\n",sfrag);
#endif
ncurisetfragments(uri,sfrag);
nullfree(sfrag); sfrag = NULL;
#ifdef NETCDF_ENABLE_S3
/* If s3, then rebuild the url */
if(NC_iss3(uri,NULL)) {
NCURI* newuri = NULL;
if((stat = NC_s3urlrebuild(uri,NULL,&newuri))) goto done;
ncurifree(uri);
uri = newuri;
} else
#endif
if(strcmp(uri->protocol,"file")==0) {
/* convert path to absolute */
char* canon = NULL;
abspath = NCpathabsolute(uri->path);
if((stat = NCpathcanonical(abspath,&canon))) goto done;
nullfree(abspath);
abspath = canon; canon = NULL;
if((stat = ncurisetpath(uri,abspath))) goto done;
}
/* rebuild the path */
if(newpathp) {
*newpathp = ncuribuild(uri,NULL,NULL,NCURIALL);
#ifdef DEBUG
fprintf(stderr,"newpath=|%s|\n",*newpathp); fflush(stderr);
#endif
}
/* Phase 5: Process the mode key to see if we can tell the formatx */
modeval = ncurifragmentlookup(uri,"mode");
if(modeval != NULL) {
if((stat = parseonchar(modeval,',',modeargs))) goto done;
for(i=0;i<nclistlength(modeargs);i++) {
const char* arg = nclistget(modeargs,i);
if((stat=processmodearg(arg,model))) goto done;
}
}
/* Phase 6: Process the non-mode keys to see if we can tell the formatx */
if(!modelcomplete(model)) {
size_t i;
NClist* p = (NClist*)ncurifragmentparams(uri); /* envv format */
for(i=0;i<nclistlength(p);i+=2) {
const char* key = nclistget(p,0);
const char* value = nclistget(p,1);
if((stat=processfragmentkeys(key,value,model))) goto done;
}
}
/* Phase 7: Special cases: if this is a URL and model.impl is still not defined */
/* Phase7a: Default is DAP2 */
if(!modelcomplete(model)) {
model->impl = NC_FORMATX_DAP2;
model->format = NC_FORMAT_NC3;
}
} else {/* Not URL */
if(newpathp) *newpathp = NULL;
}
/* Phase 8: mode inference from mode flags */
/* The modeargs did not give us a model (probably not a URL).
So look at the combination of mode flags and the useparallel flag */
if(!modelcomplete(model)) {
if((stat = NC_omodeinfer(useparallel,omode,model))) goto done;
}
/* Phase 9: Special case for file stored in DAOS container */
if(isdaoscontainer(path) == NC_NOERR) {
/* This is a DAOS container, so immediately assume it is HDF5. */
model->impl = NC_FORMATX_NC_HDF5;
model->format = NC_FORMAT_NETCDF4;
} else {
/* Phase 10: Infer from file content, if possible;
this has highest precedence, so it may override
previous decisions. Note that we do this last
because we need previously determined model info
to guess if this file is readable.
*/
if(!iscreate && isreadable(uri,model)) {
/* Ok, we need to try to read the file */
if((stat = check_file_type(path, omode, useparallel, params, model, uri))) goto done;
}
}
/* Need a decision */
if(!modelcomplete(model))
{stat = NC_ENOTNC; goto done;}