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
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 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 COPYING 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 *
* help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* Purpose: Code to implement a path table which stores plugin search paths.
*
* The path table is implemented as a dynamic, global array which
* will grow as new paths are inserted. The capacity of the path
* table never shrinks (though given the low number of paths
* expected and the low likelihood of paths being removed, this
* seems unlikely to be a problem). Inserts and removals rework
* the array so that there are no 'holes' in the in-use part
* of the array.
*
* Note that it's basically up to the user to manage the indexes
* when a complicated series of insert, overwrite, and, remove
* operations take place.
*/
/****************/
/* Module Setup */
/****************/
#include "H5PLmodule.h" /* This source code file is part of the H5PL module */
/***********/
/* Headers */
/***********/
#include "H5private.h" /* Generic Functions */
#include "H5Eprivate.h" /* Error handling */
#include "H5MMprivate.h" /* Memory management */
#include "H5PLpkg.h" /* Plugin */
/****************/
/* Local Macros */
/****************/
/* Initial capacity of the path table */
#define H5PL_INITIAL_PATH_CAPACITY 16
/* The amount to add to the capacity when the table is full */
#define H5PL_PATH_CAPACITY_ADD 16
/******************/
/* Local Typedefs */
/******************/
/********************/
/* Local Prototypes */
/********************/
static herr_t H5PL__insert_at(const char *path, unsigned int idx);
static herr_t H5PL__make_space_at(unsigned int idx);
static herr_t H5PL__replace_at(const char *path, unsigned int idx);
static herr_t H5PL__expand_path_table(void);
static herr_t H5PL__path_table_iterate_process_path(const char *plugin_path, H5PL_iterate_type_t iter_type,
H5PL_iterate_t iter_op, void *op_data);
static herr_t H5PL__find_plugin_in_path(const H5PL_search_params_t *search_params, hbool_t *found,
const char *dir, const void **plugin_info);
/*********************/
/* Package Variables */
/*********************/
/*****************************/
/* Library Private Variables */
/*****************************/
/*******************/
/* Local Variables */
/*******************/
/* Stored plugin paths to search */
static char **H5PL_paths_g = NULL;
/* The number of stored paths */
static unsigned H5PL_num_paths_g = 0;
/* The capacity of the path table */
static unsigned H5PL_path_capacity_g = H5PL_INITIAL_PATH_CAPACITY;
/*-------------------------------------------------------------------------
* Function: H5PL__insert_at()
*
* Purpose: Insert a path at a particular index in the path table.
* Does not clobber! Will move existing paths up to make
* room. Use H5PL__replace_at(index) if you want to clobber.
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
static herr_t
H5PL__insert_at(const char *path, unsigned int idx)
{
char *path_copy = NULL; /* copy of path string (for storing) */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Check args - Just assert on package functions */
HDassert(path);
HDassert(HDstrlen(path));
/* Expand the table if it is full */
if (H5PL_num_paths_g == H5PL_path_capacity_g)
if (H5PL__expand_path_table() < 0)
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't expand path table")
/* Copy the path for storage so the caller can dispose of theirs */
if (NULL == (path_copy = H5MM_strdup(path)))
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't make internal copy of path")
#ifdef H5_HAVE_WIN32_API
/* Clean up Microsoft Windows environment variables in the path string */
if (H5_expand_windows_env_vars(&path_copy))
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTCONVERT, FAIL, "can't expand environment variable string")
#endif /* H5_HAVE_WIN32_API */
/* If the table entry is in use, make some space */
if (H5PL_paths_g[idx])
if (H5PL__make_space_at(idx) < 0)
HGOTO_ERROR(H5E_PLUGIN, H5E_NOSPACE, FAIL, "unable to make space in the table for the new entry")
/* Insert the copy of the search path into the table at the specified index */
H5PL_paths_g[idx] = path_copy;
H5PL_num_paths_g++;
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5PL__insert_at() */
/*-------------------------------------------------------------------------
* Function: H5PL__make_space_at()
*
* Purpose: Free up a slot in the path table, moving existing path
* entries as necessary.
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
static herr_t
H5PL__make_space_at(unsigned int idx)
{
unsigned u; /* iterator */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE_NOERR
/* Check args - Just assert on package functions */
HDassert(idx < H5PL_path_capacity_g);
/* Copy the paths back to make a space */
for (u = H5PL_num_paths_g; u > idx; u--)
H5PL_paths_g[u] = H5PL_paths_g[u - 1];
H5PL_paths_g[idx] = NULL;
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5PL__make_space_at() */
/*-------------------------------------------------------------------------
* Function: H5PL__replace_at()
*
* Purpose: Replace a path at a particular index in the path table.
* The path in the table must exist and will be freed by this
* function.
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
static herr_t
H5PL__replace_at(const char *path, unsigned int idx)
{
char *path_copy = NULL; /* copy of path string (for storing) */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Check args - Just assert on package functions */
HDassert(path);
HDassert(HDstrlen(path));
/* Check that the table entry is in use */
if (!H5PL_paths_g[idx])
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTFREE, FAIL, "path entry at index %u in the table is NULL", idx)
/* Copy the path for storage so the caller can dispose of theirs */
if (NULL == (path_copy = H5MM_strdup(path)))
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't make internal copy of path")
#ifdef H5_HAVE_WIN32_API
/* Clean up Microsoft Windows environment variables in the path string */
if (H5_expand_windows_env_vars(&path_copy))
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTCONVERT, FAIL, "can't expand environment variable string")
#endif /* H5_HAVE_WIN32_API */
/* Free the existing path entry */
H5PL_paths_g[idx] = (char *)H5MM_xfree(H5PL_paths_g[idx]);
/* Copy the search path into the table at the specified index */
H5PL_paths_g[idx] = path_copy;
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5PL__replace_at() */
/*-------------------------------------------------------------------------
* Function: H5PL__create_path_table
*
* Purpose: Create the collection of paths that will be searched
* when loading plugins.
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
herr_t
H5PL__create_path_table(void)
{
char *env_var = NULL; /* Path string from environment variable */
char *paths = NULL; /* Delimited paths string. Either from the
* environment variable or the default.
*/
char *next_path = NULL; /* A path tokenized from the paths string */
char *lasts = NULL; /* Context pointer for strtok_r() call */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Allocate memory for the path table */
H5PL_num_paths_g = 0;
H5PL_path_capacity_g = H5PL_INITIAL_PATH_CAPACITY;
if (NULL == (H5PL_paths_g = (char **)H5MM_calloc((size_t)H5PL_path_capacity_g * sizeof(char *))))
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't allocate memory for path table")
/* Retrieve paths from HDF5_PLUGIN_PATH if the user sets it
* or from the default paths if it isn't set.
*/
env_var = HDgetenv(HDF5_PLUGIN_PATH);
if (NULL == env_var)
paths = H5MM_strdup(H5PL_DEFAULT_PATH);
else
paths = H5MM_strdup(env_var);
if (NULL == paths)
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't allocate memory for path copy")
/* Separate the paths and store them */
next_path = HDstrtok_r(paths, H5PL_PATH_SEPARATOR, &lasts);
while (next_path) {
/* Insert the path into the table */
if (H5PL__append_path(next_path) < 0)
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't insert path: %s", next_path)
/* Get the next path from the environment string */
next_path = HDstrtok_r(NULL, H5PL_PATH_SEPARATOR, &lasts);
} /* end while */
done:
if (paths)
paths = (char *)H5MM_xfree(paths);
/* Try to clean up on errors */
if (FAIL == ret_value) {
if (H5PL_paths_g)
H5PL_paths_g = (char **)H5MM_xfree(H5PL_paths_g);
H5PL_path_capacity_g = 0;
}
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5PL__create_path_table() */
/*-------------------------------------------------------------------------
* Function: H5PL__close_path_table
*
* Purpose: Close the collection of paths that will be searched
* when loading plugins.
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
herr_t
H5PL__close_path_table(void)
{
unsigned u; /* iterator */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE_NOERR
/* Free paths */
for (u = 0; u < H5PL_num_paths_g; u++)
if (H5PL_paths_g[u])
H5PL_paths_g[u] = (char *)H5MM_xfree(H5PL_paths_g[u]);
/* Free path table */
H5PL_paths_g = (char **)H5MM_xfree(H5PL_paths_g);
/* Reset values */
H5PL_num_paths_g = 0;
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5PL__close_path_table() */
/*-------------------------------------------------------------------------
* Function: H5PL__get_num_paths
*
* Purpose: Gets the number of plugin paths that have been stored.
*
* Return: Success: The number of paths
* Failure: Can't fail
*-------------------------------------------------------------------------
*/
unsigned
H5PL__get_num_paths(void)
{
FUNC_ENTER_PACKAGE_NOERR
FUNC_LEAVE_NOAPI(H5PL_num_paths_g)
} /* end H5PL__get_num_paths() */
/*-------------------------------------------------------------------------
* Function: H5PL__expand_path_table
*
* Purpose: Expand the path table when it's full.
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
static herr_t
H5PL__expand_path_table(void)
{
herr_t ret_value = SUCCEED;
FUNC_ENTER_PACKAGE
/* Update the capacity */
H5PL_path_capacity_g += H5PL_PATH_CAPACITY_ADD;
/* Resize the array */
if (NULL ==
(H5PL_paths_g = (char **)H5MM_realloc(H5PL_paths_g, (size_t)H5PL_path_capacity_g * sizeof(char *))))
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "allocating additional memory for path table failed")
/* Initialize the new memory */
HDmemset(H5PL_paths_g + H5PL_num_paths_g, 0, (size_t)H5PL_PATH_CAPACITY_ADD * sizeof(char *));
done:
/* Set the path capacity back if there were problems */
if (FAIL == ret_value)
H5PL_path_capacity_g -= H5PL_PATH_CAPACITY_ADD;
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5PL__expand_path_table() */
/*-------------------------------------------------------------------------
* Function: H5PL__append_path
*
* Purpose: Insert a path at the end of the table.
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
herr_t
H5PL__append_path(const char *path)
{
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Check args - Just assert on package functions */
HDassert(path);
HDassert(HDstrlen(path));
/* Insert the path at the end of the table */
if (H5PL__insert_at(path, H5PL_num_paths_g) < 0)
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTINSERT, FAIL, "unable to append search path")
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5PL__append_path() */
/*-------------------------------------------------------------------------
* Function: H5PL__prepend_path
*
* Purpose: Insert a path at the beginning of the table.
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
herr_t
H5PL__prepend_path(const char *path)
{
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Check args - Just assert on package functions */
HDassert(path);
HDassert(HDstrlen(path));
/* Insert the path at the beginning of the table */
if (H5PL__insert_at(path, 0) < 0)
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTINSERT, FAIL, "unable to prepend search path")
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5PL__prepend_path() */
/*-------------------------------------------------------------------------
* Function: H5PL__replace_path
*
* Purpose: Replace a path at particular index in the table.
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
herr_t
H5PL__replace_path(const char *path, unsigned int idx)
{
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Check args - Just assert on package functions */
HDassert(path);
HDassert(HDstrlen(path));
HDassert(idx < H5PL_path_capacity_g);
/* Insert the path at the requested index */
if (H5PL__replace_at(path, idx) < 0)
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTINSERT, FAIL, "unable to replace search path")
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5PL__replace_path() */
/*-------------------------------------------------------------------------
* Function: H5PL__insert_path
*
* Purpose: Insert a path at particular index in the table, moving
* any existing paths back to make space.
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
herr_t
H5PL__insert_path(const char *path, unsigned int idx)
{
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Check args - Just assert on package functions */
HDassert(path);
HDassert(HDstrlen(path));
HDassert(idx < H5PL_path_capacity_g);
/* Insert the path at the requested index */
if (H5PL__insert_at(path, idx) < 0)
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTINSERT, FAIL, "unable to insert search path")
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5PL__insert_path() */
/*-------------------------------------------------------------------------
* Function: H5PL__remove_path
*
* Purpose: Remove a path at particular index in the table, freeing
* the path string and moving the paths down to close the gap.
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
herr_t
H5PL__remove_path(unsigned int idx)
{
unsigned u; /* iterator */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Check args - Just assert on package functions */
HDassert(idx < H5PL_path_capacity_g);
/* Check if the path at that index is set */
if (!H5PL_paths_g[idx])
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTDELETE, FAIL, "search path at index %u is NULL", idx)
/* Delete the path */
H5PL_num_paths_g--;
H5PL_paths_g[idx] = (char *)H5MM_xfree(H5PL_paths_g[idx]);
/* Shift the paths down to close the gap */
for (u = idx; u < H5PL_num_paths_g; u++)
H5PL_paths_g[u] = H5PL_paths_g[u + 1];
/* Set the (former) last path to NULL */
H5PL_paths_g[H5PL_num_paths_g] = NULL;
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5PL__remove_path() */
/*-------------------------------------------------------------------------
* Function: H5PL__get_path
*
* Purpose: Get a pointer to a path at particular index in the table.
*
* Return: Success: A pointer to a path string stored in the table
* Failure: NULL
*
*-------------------------------------------------------------------------
*/
const char *
H5PL__get_path(unsigned int idx)
{
char *ret_value = NULL; /* Return value */
FUNC_ENTER_PACKAGE
/* Get the path at the requested index */
if (idx >= H5PL_num_paths_g)
HGOTO_ERROR(H5E_ARGS, H5E_BADRANGE, NULL, "path index %u is out of range in table", idx)
return H5PL_paths_g[idx];
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5PL__get_path() */
/*-------------------------------------------------------------------------
* Function: H5PL__path_table_iterate
*
* Purpose: Iterates over all the plugins in the plugin path table and
* calls the specified callback function on each plugin found.
*
* Return: H5_ITER_CONT if all plugins are processed successfully
* H5_ITER_STOP if short-circuit success occurs while
* processing plugins
* H5_ITER_ERROR if an error occurs while processing plugins
*
*-------------------------------------------------------------------------
*/
herr_t
H5PL__path_table_iterate(H5PL_iterate_type_t iter_type, H5PL_iterate_t iter_op, void *op_data)
{
unsigned int u;
herr_t ret_value = H5_ITER_CONT;
FUNC_ENTER_PACKAGE
for (u = 0; (u < H5PL_num_paths_g) && (ret_value == H5_ITER_CONT); u++) {
if ((ret_value =
H5PL__path_table_iterate_process_path(H5PL_paths_g[u], iter_type, iter_op, op_data)) < 0)
HGOTO_ERROR(H5E_PLUGIN, H5E_BADITER, H5_ITER_ERROR,
"can't iterate over plugins in plugin path '%s'", H5PL_paths_g[u]);
}
done:
FUNC_LEAVE_NOAPI(ret_value);
} /* end H5PL__path_table_iterate() */
/*-------------------------------------------------------------------------
* Function: H5PL__path_table_iterate_process_path
*
* Purpose: Iterates over all the plugins within a single plugin path
* entry in the plugin path table and calls the specified
* callback function on each plugin found. Two function
* definitions are for Unix and Windows.
*
* Return: H5_ITER_CONT if all plugins are processed successfully
* H5_ITER_STOP if short-circuit success occurs while
* processing plugins
* H5_ITER_ERROR if an error occurs while processing plugins
*
*-------------------------------------------------------------------------
*/
#ifndef H5_HAVE_WIN32_API
static herr_t
H5PL__path_table_iterate_process_path(const char *plugin_path, H5PL_iterate_type_t iter_type,
H5PL_iterate_t iter_op, void *op_data)
{
H5PL_type_t plugin_type;
const void *plugin_info = NULL;
hbool_t plugin_loaded;
char *path = NULL;
DIR *dirp = NULL; /* Directory stream */
struct dirent *dp = NULL; /* Directory entry */
herr_t ret_value = H5_ITER_CONT;
FUNC_ENTER_PACKAGE
HDassert(plugin_path);
HDassert(iter_op);
/* Open the directory - skip the path if the directory can't be opened */
if (!(dirp = HDopendir(plugin_path)))
HGOTO_DONE(H5_ITER_CONT)
/* Iterate through all entries in the directory */
while (NULL != (dp = HDreaddir(dirp))) {
/* The library we are looking for should be called libxxx.so... on Unix
* or libxxx.xxx.dylib on Mac.
*/
#ifndef __CYGWIN__
if (!HDstrncmp(dp->d_name, "lib", (size_t)3) &&
(HDstrstr(dp->d_name, ".so") || HDstrstr(dp->d_name, ".dylib"))) {
#else
if (!HDstrncmp(dp->d_name, "cyg", (size_t)3) && HDstrstr(dp->d_name, ".dll")) {
#endif
hbool_t plugin_matches;
h5_stat_t my_stat;
size_t len;
/* Allocate & initialize the path name */
len = HDstrlen(plugin_path) + HDstrlen(H5PL_PATH_SEPARATOR) + HDstrlen(dp->d_name) + 1 /*\0*/ +
4; /* Extra "+4" to quiet GCC warning - 2019/07/05, QAK */
if (NULL == (path = (char *)H5MM_calloc(len)))
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, H5_ITER_ERROR, "can't allocate memory for path")
HDsnprintf(path, len, "%s/%s", plugin_path, dp->d_name);
/* Get info for directory entry */
if (HDstat(path, &my_stat) == -1)
HGOTO_ERROR(H5E_FILE, H5E_CANTGET, H5_ITER_ERROR, "can't stat file %s -- error was: %s", path,
HDstrerror(errno))
/* If it is a directory, skip it */
if (S_ISDIR(my_stat.st_mode))
continue;
/* Attempt to open the dynamic library */
plugin_type = H5PL_TYPE_ERROR;
plugin_info = NULL;
plugin_loaded = FALSE;
if (H5PL__open(path, H5PL_TYPE_NONE, NULL, &plugin_loaded, &plugin_type, &plugin_info) < 0)
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTGET, H5_ITER_ERROR, "failed to open plugin '%s'", path);
/* Determine if we should process this plugin */
plugin_matches = (iter_type == H5PL_ITER_TYPE_ALL) ||
((iter_type == H5PL_ITER_TYPE_FILTER) && (plugin_type == H5PL_TYPE_FILTER)) ||
((iter_type == H5PL_ITER_TYPE_VOL) && (plugin_type == H5PL_TYPE_VOL)) ||
((iter_type == H5PL_ITER_TYPE_VFD) && (plugin_type == H5PL_TYPE_VFD));
/* If the plugin was successfully loaded, call supplied callback function on plugin */
if (plugin_loaded && plugin_matches && (ret_value = iter_op(plugin_type, plugin_info, op_data)))
break;
path = (char *)H5MM_xfree(path);
} /* end if */
} /* end while */
if (ret_value < 0)
HERROR(H5E_PLUGIN, H5E_CALLBACK, "callback operator function returned failure");
done:
if (dirp)
if (HDclosedir(dirp) < 0)
HDONE_ERROR(H5E_FILE, H5E_CLOSEERROR, H5_ITER_ERROR, "can't close directory: %s",
HDstrerror(errno))
path = (char *)H5MM_xfree(path);
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5PL__path_table_iterate_process_path() */
#else /* H5_HAVE_WIN32_API */
static herr_t
H5PL__path_table_iterate_process_path(const char *plugin_path, H5PL_iterate_type_t iter_type,
H5PL_iterate_t iter_op, void *op_data)
{
WIN32_FIND_DATAA fdFile;
HANDLE hFind = INVALID_HANDLE_VALUE;
H5PL_type_t plugin_type;
const void *plugin_info = NULL;
hbool_t plugin_loaded;
char *path = NULL;
char service[2048];
herr_t ret_value = H5_ITER_CONT;
FUNC_ENTER_PACKAGE
/* Check args - Just assert on package functions */
HDassert(plugin_path);
HDassert(iter_op);
/* Specify a file mask. *.* = We want everything! -
* skip the path if the directory can't be opened */
HDsnprintf(service, sizeof(service), "%s\\*.dll", plugin_path);
if ((hFind = FindFirstFileA(service, &fdFile)) == INVALID_HANDLE_VALUE)
HGOTO_DONE(H5_ITER_CONT)
/* Loop over all the files */
do {
/* Ignore '.' and '..' */
if (HDstrcmp(fdFile.cFileName, ".") != 0 && HDstrcmp(fdFile.cFileName, "..") != 0) {
hbool_t plugin_matches;
size_t len;
/* Allocate & initialize the path name */
len = HDstrlen(plugin_path) + HDstrlen(H5PL_PATH_SEPARATOR) + HDstrlen(fdFile.cFileName) + 1;
if (NULL == (path = (char *)H5MM_calloc(len)))
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, H5_ITER_ERROR, "can't allocate memory for path")
HDsnprintf(path, len, "%s\\%s", plugin_path, fdFile.cFileName);
/* Ignore directories */
if (fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
continue;
/* Attempt to open the dynamic library */
plugin_type = H5PL_TYPE_ERROR;
plugin_info = NULL;
plugin_loaded = FALSE;
if (H5PL__open(path, H5PL_TYPE_NONE, NULL, &plugin_loaded, &plugin_type, &plugin_info) < 0)
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTGET, H5_ITER_ERROR, "failed to open plugin '%s'", path);
/* Determine if we should process this plugin */
plugin_matches = (iter_type == H5PL_ITER_TYPE_ALL) ||
((iter_type == H5PL_ITER_TYPE_FILTER) && (plugin_type == H5PL_TYPE_FILTER)) ||
((iter_type == H5PL_ITER_TYPE_VOL) && (plugin_type == H5PL_TYPE_VOL)) ||
((iter_type == H5PL_ITER_TYPE_VFD) && (plugin_type == H5PL_TYPE_VFD));
/* If the plugin was successfully loaded, call supplied callback function on plugin */
if (plugin_loaded && plugin_matches && (ret_value = iter_op(plugin_type, plugin_info, op_data)))
break;
path = (char *)H5MM_xfree(path);
}
} while (FindNextFileA(hFind, &fdFile));
if (ret_value < 0)
HERROR(H5E_PLUGIN, H5E_CALLBACK, "callback operator function returned failure");
done:
if (hFind != INVALID_HANDLE_VALUE)
FindClose(hFind);
path = (char *)H5MM_xfree(path);
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5PL__path_table_iterate_process_path() */
#endif /* H5_HAVE_WIN32_API */
/*-------------------------------------------------------------------------
* Function: H5PL__find_plugin_in_path_table
*
* Purpose: Attempts to find a matching plugin in the file system
* using the paths stored in the path table.
*.
* The 'found' parameter will be set appropriately.
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
herr_t
H5PL__find_plugin_in_path_table(const H5PL_search_params_t *search_params, hbool_t *found,
const void **plugin_info)
{
unsigned int u; /* iterator */
herr_t ret_value = SUCCEED;
FUNC_ENTER_PACKAGE
/* Check args - Just assert on package functions */
HDassert(search_params);
HDassert(found);
HDassert(plugin_info);
/* Initialize output parameters */
*found = FALSE;
*plugin_info = NULL;
/* Loop over the paths in the table, checking for an appropriate plugin */
for (u = 0; u < H5PL_num_paths_g; u++) {
/* Search for the plugin in this path */
if (H5PL__find_plugin_in_path(search_params, found, H5PL_paths_g[u], plugin_info) < 0)
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTGET, FAIL, "search in path %s encountered an error",
H5PL_paths_g[u])
/* Break out if found */
if (*found) {
if (!plugin_info)
HGOTO_ERROR(H5E_PLUGIN, H5E_BADVALUE, FAIL, "plugin info should not be NULL")
break;
}
}
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5PL__find_plugin_in_path_table() */
/*-------------------------------------------------------------------------
* Function: H5PL__find_plugin_in_path
*
* Purpose: Given a path, this function opens the directory and invokes
* another function to go through all files to find the right
* plugin library. Two function definitions are for Unix and
* Windows.
*
* The found parameter will be set to TRUE and the info
* parameter will be filled in on success.
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
#ifndef H5_HAVE_WIN32_API
static herr_t
H5PL__find_plugin_in_path(const H5PL_search_params_t *search_params, hbool_t *found, const char *dir,
const void **plugin_info)
{
char *path = NULL;
DIR *dirp = NULL; /* Directory stream */
struct dirent *dp = NULL; /* Directory entry */
herr_t ret_value = SUCCEED;
FUNC_ENTER_PACKAGE
/* Check args - Just assert on package functions */
HDassert(search_params);
HDassert(found);
HDassert(dir);
HDassert(plugin_info);
/* Initialize the found parameter */
*found = FALSE;
/* Open the directory */
if (!(dirp = HDopendir(dir)))
HGOTO_ERROR(H5E_PLUGIN, H5E_OPENERROR, FAIL, "can't open directory (%s). Please verify its existence",
dir)
/* Iterate through all entries in the directory */
while (NULL != (dp = HDreaddir(dirp))) {
/* The library we are looking for should be called libxxx.so... on Unix
* or libxxx.xxx.dylib on Mac.
*/
#ifndef __CYGWIN__
if (!HDstrncmp(dp->d_name, "lib", (size_t)3) &&
(HDstrstr(dp->d_name, ".so") || HDstrstr(dp->d_name, ".dylib"))) {
#else
if (!HDstrncmp(dp->d_name, "cyg", (size_t)3) && HDstrstr(dp->d_name, ".dll")) {
#endif
h5_stat_t my_stat;
size_t len;
/* Allocate & initialize the path name */
len = HDstrlen(dir) + HDstrlen(H5PL_PATH_SEPARATOR) + HDstrlen(dp->d_name) + 1 /*\0*/ +
4; /* Extra "+4" to quiet GCC warning - 2019/07/05, QAK */
if (NULL == (path = (char *)H5MM_calloc(len)))
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't allocate memory for path")
HDsnprintf(path, len, "%s/%s", dir, dp->d_name);
/* Get info for directory entry */
if (HDstat(path, &my_stat) == -1)
HGOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "can't stat file %s -- error was: %s", path,
HDstrerror(errno))
/* If it is a directory, skip it */
if (S_ISDIR(my_stat.st_mode)) {
path = (char *)H5MM_xfree(path);
continue;
}
/* attempt to open the dynamic library */
if (H5PL__open(path, search_params->type, search_params->key, found, NULL, plugin_info) < 0)
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTGET, FAIL, "search in directory failed")
if (*found)
HGOTO_DONE(SUCCEED)
path = (char *)H5MM_xfree(path);
} /* end if */
} /* end while */
done:
if (dirp)
if (HDclosedir(dirp) < 0)
HDONE_ERROR(H5E_FILE, H5E_CLOSEERROR, FAIL, "can't close directory: %s", HDstrerror(errno))
path = (char *)H5MM_xfree(path);
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5PL__find_plugin_in_path() */
#else /* H5_HAVE_WIN32_API */
static herr_t
H5PL__find_plugin_in_path(const H5PL_search_params_t *search_params, hbool_t *found, const char *dir,
const void **plugin_info)
{
WIN32_FIND_DATAA fdFile;
HANDLE hFind = INVALID_HANDLE_VALUE;
char *path = NULL;
char service[2048];
herr_t ret_value = SUCCEED;
FUNC_ENTER_PACKAGE
/* Check args - Just assert on package functions */
HDassert(search_params);
HDassert(found);
HDassert(dir);
HDassert(plugin_info);
/* Initialize the found parameter */
*found = FALSE;
/* Specify a file mask. *.* = We want everything! */
HDsnprintf(service, sizeof(service), "%s\\*.dll", dir);
if ((hFind = FindFirstFileA(service, &fdFile)) == INVALID_HANDLE_VALUE)
HGOTO_ERROR(H5E_PLUGIN, H5E_OPENERROR, FAIL, "can't open directory")
/* Loop over all the files */
do {
/* Ignore '.' and '..' */
if (HDstrcmp(fdFile.cFileName, ".") != 0 && HDstrcmp(fdFile.cFileName, "..") != 0) {
/* XXX: Probably just continue here and move the code below over one tab */
size_t len;
/* Allocate & initialize the path name */
len = HDstrlen(dir) + HDstrlen(H5PL_PATH_SEPARATOR) + HDstrlen(fdFile.cFileName) + 1;
if (NULL == (path = (char *)H5MM_calloc(len)))
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't allocate memory for path")
HDsnprintf(path, len, "%s\\%s", dir, fdFile.cFileName);
/* Ignore directories */
if (fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
continue;
/* attempt to open the dynamic library */
if (H5PL__open(path, search_params->type, search_params->key, found, NULL, plugin_info) < 0)
HGOTO_ERROR(H5E_PLUGIN, H5E_CANTGET, FAIL, "search in directory failed")
if (*found)
HGOTO_DONE(SUCCEED)
path = (char *)H5MM_xfree(path);
}
} while (FindNextFileA(hFind, &fdFile));
done:
if (hFind != INVALID_HANDLE_VALUE)
FindClose(hFind);
if (path)
path = (char *)H5MM_xfree(path);
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5PL__find_plugin_in_path() */
#endif /* H5_HAVE_WIN32_API */
|