forked from HDFGroup/hdf5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh5tools.c
2376 lines (2081 loc) · 83.6 KB
/
h5tools.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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the LICENSE file, which can be found at the root of the source code *
* distribution tree, or in https://www.hdfgroup.org/licenses. *
* If you do not have access to either file, you may request a copy from *
* [email protected]. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* Purpose: A library for routines that are common
* amongst the various HDF5 tools.
*/
#include "h5tools.h"
#include "h5tools_dump.h"
#include "h5tools_ref.h"
#include "h5tools_utils.h"
#include "H5private.h"
#ifdef H5_TOOLS_DEBUG
/* global debug variables */
int H5tools_INDENT_g = 0;
#endif
/* global variables */
H5E_auto2_t lib_func;
H5E_auto2_t tools_func;
void *lib_edata;
void *tools_edata;
hid_t H5tools_ERR_STACK_g = H5I_INVALID_HID;
hid_t H5tools_ERR_CLS_g = H5I_INVALID_HID;
hid_t H5E_tools_g = H5I_INVALID_HID;
hid_t H5E_tools_min_id_g = H5I_INVALID_HID;
hid_t H5E_tools_min_info_id_g = H5I_INVALID_HID;
hid_t H5E_tools_min_dbg_id_g = H5I_INVALID_HID;
FILE *rawattrstream = NULL; /* should initialize to stdout but gcc moans about it */
FILE *rawdatastream = NULL; /* should initialize to stdout but gcc moans about it */
FILE *rawinstream = NULL; /* should initialize to stdin but gcc moans about it */
FILE *rawoutstream = NULL; /* should initialize to stdout but gcc moans about it */
FILE *rawerrorstream = NULL; /* should initialize to stderr but gcc moans about it */
int bin_output; /* binary output */
int bin_form = 0; /* binary form, default NATIVE */
int region_output; /* region output */
int oid_output; /* oid output */
int data_output; /* data output */
int attr_data_output; /* attribute data output */
unsigned packed_bits_num; /* number of packed bits to display */
unsigned packed_data_offset; /* offset of packed bits to display */
unsigned packed_data_length; /* length of packed bits to display */
unsigned long long packed_data_mask; /* mask in which packed bits to display */
int enable_error_stack = 0; /* re-enable error stack; disable=0 enable=1 */
/* sort parameters */
H5_index_t sort_by = H5_INDEX_NAME; /* sort_by [creation_order | name] */
H5_iter_order_t sort_order = H5_ITER_INC; /* sort_order [ascending | descending] */
/* module-scoped variables */
static int h5tools_init_g; /* if h5tools lib has been initialized */
/* Names of VOL connectors */
const char *volnames[] = {
H5VL_NATIVE_NAME,
H5VL_PASSTHRU_NAME,
};
/* Names of VFDs. These names are always available so that
* the tools can emit special messages when a VFD is asked
* for by name but is not compiled into the library or is
* somehow otherwise not enabled.
*
*/
const char *drivernames[] = {
[SEC2_VFD_IDX] = "sec2", [DIRECT_VFD_IDX] = "direct", [LOG_VFD_IDX] = "log",
[WINDOWS_VFD_IDX] = "windows", [STDIO_VFD_IDX] = "stdio", [CORE_VFD_IDX] = "core",
[FAMILY_VFD_IDX] = "family", [SPLIT_VFD_IDX] = "split", [MULTI_VFD_IDX] = "multi",
[MPIO_VFD_IDX] = "mpio", [MIRROR_VFD_IDX] = "mirror", [SPLITTER_VFD_IDX] = "splitter",
[ROS3_VFD_IDX] = "ros3", [HDFS_VFD_IDX] = "hdfs", [SUBFILING_VFD_IDX] = H5FD_SUBFILING_NAME,
[ONION_VFD_IDX] = "onion",
};
#define NUM_VOLS (sizeof(volnames) / sizeof(volnames[0]))
#define NUM_DRIVERS (sizeof(drivernames) / sizeof(drivernames[0]))
/*-------------------------------------------------------------------------
* Function: h5tools_init
*
* Purpose: This should be called before any other h5tools function is called.
* Effect of any h5tools function called before this has been called is
* undetermined.
*
* Return None
*-------------------------------------------------------------------------
*/
void
h5tools_init(void)
{
/* Disable error reporting */
H5Eget_auto2(H5E_DEFAULT, &lib_func, &lib_edata);
H5Eset_auto2(H5E_DEFAULT, NULL, NULL);
if (!h5tools_init_g) {
H5TOOLS_INIT_ERROR();
if (!rawattrstream)
rawattrstream = stdout;
if (!rawdatastream)
rawdatastream = stdout;
if (!rawinstream)
rawinstream = stdin;
if (!rawoutstream)
rawoutstream = stdout;
if (!rawerrorstream)
rawerrorstream = stderr;
h5tools_dump_init();
h5tools_init_g++;
}
/* Disable tools error reporting */
H5Eget_auto2(H5tools_ERR_STACK_g, &tools_func, &tools_edata);
H5Eset_auto2(H5tools_ERR_STACK_g, NULL, NULL);
}
/*-------------------------------------------------------------------------
* Function: h5tools_error_report
*
* Purpose: Enable error stack reporting after command line is parsed.
*
* Return: None
*-------------------------------------------------------------------------
*/
void
h5tools_error_report(void)
{
if (h5tools_init_g) {
if (enable_error_stack > 0) {
H5Eset_auto2(H5E_DEFAULT, lib_func, lib_edata);
H5Eset_auto2(H5tools_ERR_STACK_g, tools_func, tools_edata);
}
}
}
/*-------------------------------------------------------------------------
* Function: h5tools_close
*
* Purpose: Close or release resources such as files opened by the library. This
* should be called after all other h5tools functions have been called.
* Effect of any h5tools function called after this has been called is
* undetermined.
*
* Return: None
*-------------------------------------------------------------------------
*/
void
h5tools_close(void)
{
if (h5tools_init_g) {
/* special case where only data is output to stdout */
if ((rawoutstream == NULL) && rawdatastream && (rawdatastream == stdout))
fprintf(rawdatastream, "\n");
if (tools_func)
H5Eprint2(H5tools_ERR_STACK_g, rawerrorstream);
if (rawattrstream && rawattrstream != stdout) {
if (fclose(rawattrstream))
perror("closing rawattrstream");
else
rawattrstream = NULL;
}
if (rawdatastream && rawdatastream != stdout) {
if (fclose(rawdatastream))
perror("closing rawdatastream");
else
rawdatastream = NULL;
}
if (rawinstream && rawinstream != stdin) {
if (fclose(rawinstream))
perror("closing rawinstream");
else
rawinstream = NULL;
}
if (rawoutstream && rawoutstream != stdout) {
if (fclose(rawoutstream))
perror("closing rawoutstream");
else
rawoutstream = NULL;
}
if (rawerrorstream && rawerrorstream != stderr) {
if (fclose(rawerrorstream))
perror("closing rawerrorstream");
else
rawerrorstream = NULL;
}
/* Clean up the reference path table, if it's been used */
term_ref_path_table();
/* Restore error stacks from init */
H5Eset_auto2(H5tools_ERR_STACK_g, tools_func, tools_edata);
H5Eset_auto2(H5E_DEFAULT, lib_func, lib_edata);
H5TOOLS_CLOSE_ERROR();
/* Shut down the library */
H5close();
h5tools_init_g = 0;
}
}
/*-------------------------------------------------------------------------
* Function: h5tools_set_data_output_file
*
* Purpose: Open fname as the output file for dataset raw data.
* Set rawdatastream as its file stream.
*
* Return: 0 -- succeeded
* negative -- failed
*-------------------------------------------------------------------------
*/
int
h5tools_set_data_output_file(const char *fname, int is_bin)
{
int retvalue = FAIL;
FILE *f; /* temporary holding place for the stream pointer
* so that rawdatastream is changed only when succeeded */
if (rawdatastream && rawdatastream != stdout) {
if (fclose(rawdatastream))
perror("closing rawdatastream");
else
rawdatastream = NULL;
}
/* First check if filename is string "NULL" */
if (fname != NULL) {
/* binary output */
if (is_bin) {
if ((f = fopen(fname, "wb")) != NULL) {
rawdatastream = f;
retvalue = SUCCEED;
}
}
else {
if ((f = fopen(fname, "w")) != NULL) {
rawdatastream = f;
retvalue = SUCCEED;
}
}
}
else {
rawdatastream = NULL;
retvalue = SUCCEED;
}
return retvalue;
}
/*-------------------------------------------------------------------------
* Function: h5tools_set_attr_output_file
*
* Purpose: Open fname as the output file for attribute raw data.
* Set rawattrstream as its file stream.
*
* Return: 0 -- succeeded
* negative -- failed
*-------------------------------------------------------------------------
*/
int
h5tools_set_attr_output_file(const char *fname, int is_bin)
{
int retvalue = FAIL;
FILE *f; /* temporary holding place for the stream pointer
* so that rawattrstream is changed only when succeeded */
if (rawattrstream && rawattrstream != stdout) {
if (fclose(rawattrstream))
perror("closing rawattrstream");
else
rawattrstream = NULL;
}
/* First check if filename is string "NULL" */
if (fname != NULL) {
/* binary output */
if (is_bin) {
if ((f = fopen(fname, "wb")) != NULL) {
rawattrstream = f;
retvalue = SUCCEED;
}
}
else {
if ((f = fopen(fname, "w")) != NULL) {
rawattrstream = f;
retvalue = SUCCEED;
}
}
}
else {
rawattrstream = NULL;
retvalue = SUCCEED;
}
return retvalue;
}
/*-------------------------------------------------------------------------
* Function: h5tools_set_input_file
*
* Purpose: Open fname as the input file for raw input.
* Set rawinstream as its file stream.
*
* Return: 0 -- succeeded
* negative -- failed
*
*-------------------------------------------------------------------------
*/
int
h5tools_set_input_file(const char *fname, int is_bin)
{
int retvalue = FAIL;
FILE *f; /* temporary holding place for the stream pointer
* so that rawinstream is changed only when succeeded */
if (rawinstream && rawinstream != stdin) {
if (fclose(rawinstream))
perror("closing rawinstream");
else
rawinstream = NULL;
}
/* First check if filename is string "NULL" */
if (fname != NULL) {
/* binary output */
if (is_bin) {
if ((f = fopen(fname, "rb")) != NULL) {
rawinstream = f;
retvalue = SUCCEED;
}
}
else {
if ((f = fopen(fname, "r")) != NULL) {
rawinstream = f;
retvalue = SUCCEED;
}
}
}
else {
rawinstream = NULL;
retvalue = SUCCEED;
}
return retvalue;
}
/*-------------------------------------------------------------------------
* Function: h5tools_set_output_file
*
* Purpose: Open fname as the output file for raw output.
* Set rawoutstream as its file stream.
*
* Return: 0 -- succeeded
* negative -- failed
*
*-------------------------------------------------------------------------
*/
int
h5tools_set_output_file(const char *fname, int is_bin)
{
int retvalue = FAIL;
FILE *f; /* temporary holding place for the stream pointer
* so that rawoutstream is changed only when succeeded */
if (rawoutstream && rawoutstream != stdout) {
if (fclose(rawoutstream))
perror("closing rawoutstream");
else
rawoutstream = NULL;
}
/* First check if filename is string "NULL" */
if (fname != NULL) {
/* binary output */
if (is_bin) {
if ((f = fopen(fname, "wb")) != NULL) {
rawoutstream = f;
retvalue = SUCCEED;
}
}
else {
if ((f = fopen(fname, "w")) != NULL) {
rawoutstream = f;
retvalue = SUCCEED;
}
}
}
else {
rawoutstream = NULL;
retvalue = SUCCEED;
}
return retvalue;
}
/*-------------------------------------------------------------------------
* Function: h5tools_set_error_file
*
* Purpose: Open fname as the error output file for dataset raw error.
* Set rawerrorstream as its file stream.
*
* Return: 0 -- succeeded
* negative -- failed
*-------------------------------------------------------------------------
*/
int
h5tools_set_error_file(const char *fname, int is_bin)
{
int retvalue = FAIL;
FILE *f; /* temporary holding place for the stream pointer
* so that rawerrorstream is changed only when succeeded */
if (rawerrorstream && rawerrorstream != stderr) {
if (fclose(rawerrorstream))
perror("closing rawerrorstream");
else
rawerrorstream = NULL;
}
/* First check if filename is string "NULL" */
if (fname != NULL) {
/* binary output */
if (is_bin) {
if ((f = fopen(fname, "wb")) != NULL) {
rawerrorstream = f;
retvalue = SUCCEED;
}
}
else {
if ((f = fopen(fname, "w")) != NULL) {
rawerrorstream = f;
retvalue = SUCCEED;
}
}
}
else {
rawerrorstream = NULL;
retvalue = SUCCEED;
}
return retvalue;
}
/*-------------------------------------------------------------------------
* Function: h5tools_set_fapl_vfd
*
* Purpose: Given a VFL driver name or ID, sets the appropriate driver on
* the specified FAPL.
*
* Return: positive - succeeded
* negative - failed
*-------------------------------------------------------------------------
*/
herr_t
h5tools_set_fapl_vfd(hid_t fapl_id, h5tools_vfd_info_t *vfd_info)
{
herr_t ret_value = SUCCEED;
switch (vfd_info->type) {
case VFD_BY_NAME:
/* Determine which driver the user wants to open the file with */
if (!strcmp(vfd_info->u.name, drivernames[SEC2_VFD_IDX])) {
/* SEC2 Driver */
if (H5Pset_fapl_sec2(fapl_id) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "H5Pset_fapl_sec2 failed");
}
else if (!strcmp(vfd_info->u.name, drivernames[DIRECT_VFD_IDX])) {
#ifdef H5_HAVE_DIRECT
/* Direct Driver */
if (H5Pset_fapl_direct(fapl_id, 1024, 4096, 8 * 4096) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "H5Pset_fapl_direct failed");
#else
H5TOOLS_GOTO_ERROR(FAIL, "Direct VFD is not enabled");
#endif
}
else if (!strcmp(vfd_info->u.name, drivernames[LOG_VFD_IDX])) {
unsigned long long log_flags = H5FD_LOG_LOC_IO | H5FD_LOG_ALLOC;
/* Log Driver */
if (H5Pset_fapl_log(fapl_id, NULL, log_flags, (size_t)0) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "H5Pset_fapl_log failed");
}
else if (!strcmp(vfd_info->u.name, drivernames[WINDOWS_VFD_IDX])) {
#ifdef H5_HAVE_WINDOWS
/* There is no Windows VFD - use SEC2 */
if (H5Pset_fapl_sec2(fapl_id) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "H5Pset_fapl_sec2 failed");
#else
H5TOOLS_GOTO_ERROR(FAIL, "Windows VFD is not enabled");
#endif
}
else if (!strcmp(vfd_info->u.name, drivernames[STDIO_VFD_IDX])) {
/* Stdio Driver */
if (H5Pset_fapl_stdio(fapl_id) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "H5Pset_fapl_stdio failed");
}
else if (!strcmp(vfd_info->u.name, drivernames[CORE_VFD_IDX])) {
/* Core Driver */
if (H5Pset_fapl_core(fapl_id, (size_t)H5_MB, true) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "H5Pset_fapl_core failed");
}
else if (!strcmp(vfd_info->u.name, drivernames[FAMILY_VFD_IDX])) {
/* FAMILY Driver */
/* Set member size to be 0 to indicate the current first member size
* is the member size.
*/
if (H5Pset_fapl_family(fapl_id, (hsize_t)0, H5P_DEFAULT) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "H5Pset_fapl_family failed");
}
else if (!strcmp(vfd_info->u.name, drivernames[SPLIT_VFD_IDX])) {
/* SPLIT Driver */
if (H5Pset_fapl_split(fapl_id, "-m.h5", H5P_DEFAULT, "-r.h5", H5P_DEFAULT) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "H5Pset_fapl_split failed");
}
else if (!strcmp(vfd_info->u.name, drivernames[MULTI_VFD_IDX])) {
/* MULTI Driver */
if (H5Pset_fapl_multi(fapl_id, NULL, NULL, NULL, NULL, true) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "H5Pset_fapl_multi failed");
}
else if (!strcmp(vfd_info->u.name, drivernames[MPIO_VFD_IDX])) {
#ifdef H5_HAVE_PARALLEL
int mpi_initialized, mpi_finalized;
/* MPI-I/O Driver */
/* check if MPI is available. */
MPI_Initialized(&mpi_initialized);
MPI_Finalized(&mpi_finalized);
if (mpi_initialized && !mpi_finalized) {
if (H5Pset_fapl_mpio(fapl_id, MPI_COMM_WORLD, MPI_INFO_NULL) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "H5Pset_fapl_mpio failed");
}
#else
H5TOOLS_GOTO_ERROR(FAIL, "MPI-I/O VFD is not enabled");
#endif /* H5_HAVE_PARALLEL */
}
else if (!strcmp(vfd_info->u.name, drivernames[ROS3_VFD_IDX])) {
#ifdef H5_HAVE_ROS3_VFD
if (!vfd_info->info)
H5TOOLS_GOTO_ERROR(FAIL, "Read-only S3 VFD info is invalid");
if (H5Pset_fapl_ros3(fapl_id, &((const H5FD_ros3_fapl_ext_t *)vfd_info->info)->fa) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "H5Pset_fapl_ros3() failed");
if (H5Pset_fapl_ros3_token(fapl_id, ((const H5FD_ros3_fapl_ext_t *)vfd_info->info)->token) <
0)
H5TOOLS_GOTO_ERROR(FAIL, "H5Pset_fapl_ros3_token() failed");
#else
H5TOOLS_GOTO_ERROR(FAIL, "Read-only S3 VFD is not enabled");
#endif
}
else if (!strcmp(vfd_info->u.name, drivernames[HDFS_VFD_IDX])) {
#ifdef H5_HAVE_LIBHDFS
if (!vfd_info->info)
H5TOOLS_GOTO_ERROR(FAIL, "HDFS VFD info is invalid");
if (H5Pset_fapl_hdfs(fapl_id, (H5FD_hdfs_fapl_t *)vfd_info->info) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "H5Pset_fapl_hdfs() failed");
#else
H5TOOLS_GOTO_ERROR(FAIL, "The HDFS VFD is not enabled");
#endif
}
else if (!strcmp(vfd_info->u.name, drivernames[SUBFILING_VFD_IDX])) {
#if defined(H5_HAVE_PARALLEL) && defined(H5_HAVE_SUBFILING_VFD)
if (H5Pset_fapl_subfiling(fapl_id, (const H5FD_subfiling_config_t *)vfd_info->info) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "H5Pset_fapl_subfiling() failed");
#else
H5TOOLS_GOTO_ERROR(FAIL, "The Subfiling VFD is not enabled");
#endif
}
else if (!strcmp(vfd_info->u.name, drivernames[ONION_VFD_IDX])) {
/* Onion driver */
if (!vfd_info->info)
H5TOOLS_GOTO_ERROR(FAIL, "Onion VFD info is invalid");
if (H5Pset_fapl_onion(fapl_id, (const H5FD_onion_fapl_info_t *)vfd_info->info) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "H5Pset_fapl_onion() failed");
}
else {
/*
* Try to load VFD plugin.
*
* Currently, driver configuration strings are unsupported.
*/
if (H5Pset_driver_by_name(fapl_id, vfd_info->u.name, (const char *)vfd_info->info) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "can't load VFD plugin by driver name '%s'", vfd_info->u.name);
}
break;
case VFD_BY_VALUE:
/*
* Try to load VFD plugin.
*
* Currently, driver configuration strings are unsupported.
*/
if (vfd_info->u.value == H5_VFD_SUBFILING) {
#if defined(H5_HAVE_PARALLEL) && defined(H5_HAVE_SUBFILING_VFD)
if (H5Pset_fapl_subfiling(fapl_id, (const H5FD_subfiling_config_t *)vfd_info->info) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "H5Pset_fapl_subfiling() failed");
#else
H5TOOLS_GOTO_ERROR(FAIL, "The Subfiling VFD is not enabled");
#endif
}
else {
if (H5Pset_driver_by_value(fapl_id, vfd_info->u.value, (const char *)vfd_info->info) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "can't load VFD plugin by driver value '%ld'",
(long int)vfd_info->u.value);
}
break;
default:
H5TOOLS_GOTO_ERROR(FAIL, "invalid VFD retrieval type");
}
done:
if (ret_value < 0) {
/* Clear error message unless asked for */
if ((H5tools_ERR_STACK_g >= 0) && (enable_error_stack <= 1))
H5Epop(H5tools_ERR_STACK_g, 1);
}
return ret_value;
}
/*-------------------------------------------------------------------------
* Function: h5tools_set_fapl_vol
*
* Purpose: Given a VOL connector name or ID, sets the appropriate
* connector on the specified FAPL.
*
* Return: positive - succeeded
* negative - failed
*-------------------------------------------------------------------------
*/
herr_t
h5tools_set_fapl_vol(hid_t fapl_id, h5tools_vol_info_t *vol_info)
{
htri_t connector_is_registered;
hid_t connector_id = H5I_INVALID_HID;
void *connector_info = NULL;
herr_t ret_value = SUCCEED;
switch (vol_info->type) {
case VOL_BY_NAME:
/* Retrieve VOL connector by name */
if ((connector_is_registered = H5VLis_connector_registered_by_name(vol_info->u.name)) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "can't check if VOL connector is registered");
if (connector_is_registered) {
if ((connector_id = H5VLget_connector_id_by_name(vol_info->u.name)) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "can't get VOL connector ID");
}
else {
/* Check for VOL connectors that ship with the library, then try
* registering by name if that fails.
*/
if (!strcmp(vol_info->u.name, H5VL_PASSTHRU_NAME))
connector_id = H5VL_PASSTHRU;
else {
/* NOTE: Not being able to pass in a VIPL may be a limitation for some
* connectors.
*/
if ((connector_id = H5VLregister_connector_by_name(vol_info->u.name, H5P_DEFAULT)) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "can't register VOL connector");
}
}
break;
case VOL_BY_VALUE:
/* Retrieve VOL connector by ID */
if ((connector_is_registered = H5VLis_connector_registered_by_value(vol_info->u.value)) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "can't check if VOL connector is registered");
if (connector_is_registered) {
if ((connector_id = H5VLget_connector_id_by_value(vol_info->u.value)) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "can't get VOL connector ID");
}
else {
/* Check for VOL connectors that ship with the library */
if (vol_info->u.value == H5VL_PASSTHRU_VALUE)
connector_id = H5VL_PASSTHRU;
else {
/* NOTE: Not being able to pass in a VIPL may be a limitation for some
* connectors.
*/
if ((connector_id = H5VLregister_connector_by_value(vol_info->u.value, H5P_DEFAULT)) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "can't register VOL connector");
}
}
break;
default:
H5TOOLS_GOTO_ERROR(FAIL, "invalid VOL retrieval type");
}
/* Convert the info string, if provided */
if (vol_info->info_string)
if (H5VLconnector_str_to_info(vol_info->info_string, connector_id, &connector_info) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "can't get VOL connector info from string");
/* Set the VOL connector on the fapl */
if (H5Pset_vol(fapl_id, connector_id, connector_info) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "can't set VOL connector on FAPL");
done:
if (connector_info)
if (H5VLfree_connector_info(connector_id, connector_info))
H5TOOLS_ERROR(FAIL, "failed to free VOL connector-specific info");
if (ret_value < 0) {
if (connector_id >= 0 && H5Idec_ref(connector_id) < 0)
H5TOOLS_ERROR(FAIL, "failed to decrement refcount on VOL connector ID");
/* Clear error message unless asked for */
if ((H5tools_ERR_STACK_g >= 0) && (enable_error_stack <= 1))
H5Epop(H5tools_ERR_STACK_g, 1);
}
return ret_value;
}
/*-------------------------------------------------------------------------
* Function: h5tools_get_new_fapl
*
* Purpose: Copies an input fapl.
*
* The returned fapl must be closed by the caller.
*
* Return: positive - succeeded
* negative - failed
*-------------------------------------------------------------------------
*/
hid_t
h5tools_get_new_fapl(hid_t prev_fapl_id)
{
hid_t new_fapl_id = H5I_INVALID_HID;
hid_t ret_value = H5I_INVALID_HID;
if (prev_fapl_id < 0)
H5TOOLS_GOTO_ERROR(FAIL, "invalid FAPL");
/* Make a copy of the FAPL or create one if H5P_DEFAULT is specified. */
if (H5P_DEFAULT == prev_fapl_id) {
if ((new_fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0)
H5TOOLS_GOTO_ERROR(H5I_INVALID_HID, "H5Pcreate failed");
}
else {
if ((new_fapl_id = H5Pcopy(prev_fapl_id)) < 0)
H5TOOLS_GOTO_ERROR(H5I_INVALID_HID, "H5Pcopy failed");
}
ret_value = new_fapl_id;
done:
if (ret_value < 0) {
if (new_fapl_id >= 0) {
H5Pclose(new_fapl_id);
new_fapl_id = H5I_INVALID_HID;
}
/* Clear error message unless asked for */
if ((H5tools_ERR_STACK_g >= 0) && (enable_error_stack <= 1))
H5Epop(H5tools_ERR_STACK_g, 1);
}
return ret_value;
}
/*-------------------------------------------------------------------------
* Function: h5tools_get_vfd_name
*
* Purpose: Given a FAPL, retrieves the name of the VFL driver set on it
* if using a native-terminal VOL connector. If a
* non-native-terminal VOL connector is set on the FAPL, the
* first byte of the returned driver name will be set to the null
* terminator.
*
* Return: SUCCEED/FAIL
*-------------------------------------------------------------------------
*/
herr_t
h5tools_get_vfd_name(hid_t fid, hid_t fapl_id, char *drivername, size_t drivername_size)
{
hid_t fapl_vol_id = H5I_INVALID_HID;
bool is_native = false;
herr_t ret_value = SUCCEED;
if (fapl_id < 0)
H5TOOLS_GOTO_ERROR(FAIL, "invalid FAPL");
if (!drivername)
H5TOOLS_GOTO_ERROR(FAIL, "drivername is NULL");
if (drivername && !drivername_size)
H5TOOLS_GOTO_ERROR(FAIL, "drivername_size must be non-zero");
/* Initialize the driver name */
drivername[0] = '\0';
if (fapl_id == H5P_DEFAULT)
fapl_id = H5P_FILE_ACCESS_DEFAULT;
/* Retrieve ID of the VOL connector set on the FAPL */
if (H5Pget_vol_id(fapl_id, &fapl_vol_id) < 0)
H5TOOLS_ERROR(FAIL, "failed to retrieve VOL ID from FAPL");
/* Query if the file ID is native-terminal */
if (H5VLobject_is_native(fid, &is_native) < 0)
H5TOOLS_ERROR(FAIL, "failed to determine if file ID is native-terminal");
if (is_native) {
const char *driver_name;
hid_t driver_id;
if ((driver_id = H5Pget_driver(fapl_id)) < 0)
H5TOOLS_GOTO_ERROR(FAIL, "failed to retrieve VFL driver ID from FAPL");
if (driver_id == H5FD_SEC2)
driver_name = drivernames[SEC2_VFD_IDX];
#ifdef H5_HAVE_DIRECT
else if (driver_id == H5FD_DIRECT)
driver_name = drivernames[DIRECT_VFD_IDX];
#endif
else if (driver_id == H5FD_LOG)
driver_name = drivernames[LOG_VFD_IDX];
#ifdef H5_HAVE_WINDOWS
else if (driver_id == H5FD_WINDOWS)
driver_name = drivernames[WINDOWS_VFD_IDX];
#endif
else if (driver_id == H5FD_STDIO)
driver_name = drivernames[STDIO_VFD_IDX];
else if (driver_id == H5FD_CORE)
driver_name = drivernames[CORE_VFD_IDX];
else if (driver_id == H5FD_FAMILY)
driver_name = drivernames[FAMILY_VFD_IDX];
else if (driver_id == H5FD_MULTI)
driver_name = drivernames[MULTI_VFD_IDX];
#ifdef H5_HAVE_PARALLEL
else if (driver_id == H5FD_MPIO)
driver_name = drivernames[MPIO_VFD_IDX];
#endif
#ifdef H5_HAVE_ROS3_VFD
else if (driver_id == H5FD_ROS3)
driver_name = drivernames[ROS3_VFD_IDX];
#endif
#ifdef H5_HAVE_LIBHDFS
else if (driver_id == H5FD_HDFS)
driver_name = drivernames[HDFS_VFD_IDX];
#endif
#ifdef H5_HAVE_SUBFILING_VFD
else if (driver_id == H5FD_SUBFILING)
driver_name = drivernames[SUBFILING_VFD_IDX];
#endif
else if (driver_id == H5FD_ONION)
driver_name = drivernames[ONION_VFD_IDX];
else
driver_name = "unknown";
strncpy(drivername, driver_name, drivername_size);
drivername[drivername_size - 1] = '\0';
}
done:
/* Close retrieved VOL ID */
if (fapl_vol_id >= 0)
if (H5VLclose(fapl_vol_id) < 0)
H5TOOLS_ERROR(FAIL, "failed to close VOL ID");
return ret_value;
}
/*-------------------------------------------------------------------------
* Function: h5tools_fopen
*
* Purpose: Opens file FNAME using the specified flags and FAPL.
*
* The 'use_specific_driver' parameter is used to control the
* VFD/VOL connector that this routine uses to open the file
* with. If 'use_specific_driver' is set to true, this routine
* assumes that the caller has already set a specific VFD or VOL
* connector on the given FAPL and will attempt to directly use
* the FAPL for opening the file. We assume that the caller knows
* what they are doing; if the file is unable to be opened using
* that FAPL, this routine will return H5I_INVALID_HID.
*
* However, if 'use_specific_driver' is set to false, this
* routine assumes that the caller HAS NOT set a specific VFD or
* VOL connector on the given FAPL and will instead loop through
* the various available VFL drivers and VOL connectors trying to
* open FNAME.
*
* The list of available VFL drivers is as follows:
* - If the HDF5 library is version 1.2 or less, then we have
* only the SEC2 driver to try out.
* - If the HDF5 library is greater than version 1.2, then we
* have the FAMILY, SPLIT, and MULTI drivers to play with.
*
* The list of available VOL connectors is as follows:
* - "Native" VOL connector
* - Pass-through VOL connector
*
* Return:
* On success, returns a file ID for the opened file. If DRIVERNAME is
* non-null and the native VOL connector is the terminal connector,
* then the first DRIVERNAME_SIZE-1 characters of the driver name are
* copied into the DRIVERNAME array and null terminated. If the
* native VOL connector is NOT the terminal connector, then the first
* byte of DRIVERNAME will be set to the null terminator.
*
* On failure, the function returns H5I_INVALID_HID and DRIVERNAME
* will not be set.
*-------------------------------------------------------------------------
*/
hid_t
h5tools_fopen(const char *fname, unsigned flags, hid_t fapl_id, bool use_specific_driver, char *drivername,
size_t drivername_size)
{
hid_t fid = H5I_INVALID_HID;
hid_t tmp_fapl_id = H5I_INVALID_HID;
hid_t used_fapl_id = H5I_INVALID_HID;
unsigned volnum, drivernum;
hid_t ret_value = H5I_INVALID_HID;
/*
* First try to open the file using just the given FAPL. If the
* HDF5_VOL_CONNECTOR environment variable has been set, this will
* allow us to attempt to open the file using the specified VOL
* connector before we go looping through all available ones,
* which will override any VOL connector set by use of the
* environment variable.
*/
/* Allow error stack display if --enable-error-stack has optional arg number */
if (enable_error_stack > 1) {
fid = H5Fopen(fname, flags, fapl_id);
}
else {
H5E_BEGIN_TRY
{
fid = H5Fopen(fname, flags, fapl_id);
}
H5E_END_TRY
}
/* If we succeeded in opening the file, we're done. */
if (fid >= 0) {
used_fapl_id = fapl_id;
H5TOOLS_GOTO_DONE(fid);
}
/*
* If we failed to open the file and the caller specified 'use_specific_driver'
* as true, we should return failure now since the file couldn't be opened with
* the VFL driver/VOL connector that was set on the FAPL by the caller.
*/
if (use_specific_driver)
H5TOOLS_GOTO_ERROR(H5I_INVALID_HID, "failed to open file using specified FAPL");
/*
* As a final resort, try to open the file using each of the available
* VOL connectors. When the native VOL connector is the current "terminal"
* connector being looked at, also try using each of the available VFL drivers.
*/
for (volnum = 0; volnum < NUM_VOLS; volnum++) {
h5tools_vol_info_t vol_info;
vol_info.type = VOL_BY_NAME;
vol_info.info_string = NULL;
vol_info.u.name = volnames[volnum];
/* TODO: For now, we have no way of determining if an arbitrary
* VOL connector is native-terminal so we only try VFDs with the
* actual native VOL connector.
*/
if (NATIVE_VOL_IDX == volnum) {
/*
* If using the native VOL connector, or a VOL connector which has the
* native connector as its terminal connector, loop through all of the
* VFL drivers as well.
*/
for (drivernum = 0; drivernum < NUM_DRIVERS; drivernum++) {
h5tools_vfd_info_t vfd_info;
/* Skip the log VFD as it prints out to standard out