Coverage Report

Created: 2026-11-23 00:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hdf5/src/H5PLpath.c
Line
Count
Source
1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
 * Copyright by The HDF Group.                                               *
3
 * All rights reserved.                                                      *
4
 *                                                                           *
5
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
6
 * terms governing use, modification, and redistribution, is contained in    *
7
 * the LICENSE file, which can be found at the root of the source code       *
8
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
9
 * If you do not have access to either file, you may request a copy from     *
10
 * help@hdfgroup.org.                                                        *
11
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13
/*
14
 * Purpose: Code to implement a path table which stores plugin search paths.
15
 *
16
 *          The path table is implemented as a dynamic, global array which
17
 *          will grow as new paths are inserted. The capacity of the path
18
 *          table never shrinks (though given the low number of paths
19
 *          expected and the low likelihood of paths being removed, this
20
 *          seems unlikely to be a problem). Inserts and removals rework
21
 *          the array so that there are no 'holes' in the in-use part
22
 *          of the array.
23
 *
24
 *          Note that it's basically up to the user to manage the indexes
25
 *          when a complicated series of insert, overwrite, and, remove
26
 *          operations take place.
27
 */
28
29
/****************/
30
/* Module Setup */
31
/****************/
32
33
#include "H5PLmodule.h" /* This source code file is part of the H5PL module */
34
35
/***********/
36
/* Headers */
37
/***********/
38
#include "H5private.h"   /* Generic Functions            */
39
#include "H5Eprivate.h"  /* Error handling               */
40
#include "H5MMprivate.h" /* Memory management            */
41
#include "H5PLpkg.h"     /* Plugin                       */
42
43
/****************/
44
/* Local Macros */
45
/****************/
46
47
/* Initial capacity of the path table */
48
6
#define H5PL_INITIAL_PATH_CAPACITY 16
49
50
/* The amount to add to the capacity when the table is full */
51
0
#define H5PL_PATH_CAPACITY_ADD 16
52
53
/******************/
54
/* Local Typedefs */
55
/******************/
56
57
/********************/
58
/* Local Prototypes */
59
/********************/
60
61
static herr_t H5PL__insert_at(const char *path, unsigned int idx);
62
static herr_t H5PL__make_space_at(unsigned int idx);
63
static herr_t H5PL__replace_at(const char *path, unsigned int idx);
64
static herr_t H5PL__expand_path_table(void);
65
static herr_t H5PL__path_table_iterate_process_path(const char *plugin_path, H5PL_iterate_type_t iter_type,
66
                                                    H5PL_iterate_t iter_op, void *op_data);
67
static herr_t H5PL__find_plugin_in_path(const H5PL_search_params_t *search_params, bool *found,
68
                                        const char *dir, const void **plugin_info);
69
70
/*********************/
71
/* Package Variables */
72
/*********************/
73
74
/*****************************/
75
/* Library Private Variables */
76
/*****************************/
77
78
/*******************/
79
/* Local Variables */
80
/*******************/
81
82
/* Stored plugin paths to search */
83
static char **H5PL_paths_g = NULL;
84
85
/* The number of stored paths */
86
static unsigned H5PL_num_paths_g = 0;
87
88
/* The capacity of the path table */
89
static unsigned H5PL_path_capacity_g = H5PL_INITIAL_PATH_CAPACITY;
90
91
/*-------------------------------------------------------------------------
92
 * Function:    H5PL__insert_at()
93
 *
94
 * Purpose:     Insert a path at a particular index in the path table.
95
 *              Does not clobber! Will move existing paths up to make
96
 *              room. Use H5PL__replace_at(index) if you want to clobber.
97
 *
98
 * Return:      SUCCEED/FAIL
99
 *
100
 *-------------------------------------------------------------------------
101
 */
102
static herr_t
103
H5PL__insert_at(const char *path, unsigned int idx)
104
12
{
105
12
    char  *path_copy = NULL;    /* copy of path string (for storing) */
106
12
    herr_t ret_value = SUCCEED; /* Return value */
107
108
12
    FUNC_ENTER_PACKAGE
109
110
    /* Check args - Just assert on package functions */
111
12
    assert(path);
112
12
    assert(strlen(path));
113
114
    /* Expand the table if it is full */
115
12
    if (H5PL_num_paths_g == H5PL_path_capacity_g)
116
0
        if (H5PL__expand_path_table() < 0)
117
0
            HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't expand path table");
118
119
    /* Copy the path for storage so the caller can dispose of theirs */
120
12
    if (NULL == (path_copy = H5MM_strdup(path)))
121
0
        HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't make internal copy of path");
122
123
#ifdef H5_HAVE_WIN32_API
124
    /* Clean up Microsoft Windows environment variables in the path string */
125
    if (H5_expand_windows_env_vars(&path_copy))
126
        HGOTO_ERROR(H5E_PLUGIN, H5E_CANTCONVERT, FAIL, "can't expand environment variable string");
127
#endif /* H5_HAVE_WIN32_API */
128
129
    /* If the table entry is in use, make some space */
130
12
    if (H5PL_paths_g[idx])
131
0
        if (H5PL__make_space_at(idx) < 0)
132
0
            HGOTO_ERROR(H5E_PLUGIN, H5E_NOSPACE, FAIL, "unable to make space in the table for the new entry");
133
134
    /* Insert the copy of the search path into the table at the specified index */
135
12
    H5PL_paths_g[idx] = path_copy;
136
12
    H5PL_num_paths_g++;
137
138
12
done:
139
12
    FUNC_LEAVE_NOAPI(ret_value)
140
12
} /* end H5PL__insert_at() */
141
142
/*-------------------------------------------------------------------------
143
 * Function:    H5PL__make_space_at()
144
 *
145
 * Purpose:     Free up a slot in the path table, moving existing path
146
 *              entries as necessary.
147
 *
148
 * Return:      SUCCEED/FAIL
149
 *
150
 *-------------------------------------------------------------------------
151
 */
152
static herr_t
153
H5PL__make_space_at(unsigned int idx)
154
0
{
155
0
    unsigned u;                   /* iterator */
156
0
    herr_t   ret_value = SUCCEED; /* Return value */
157
158
0
    FUNC_ENTER_PACKAGE_NOERR
159
160
    /* Check args - Just assert on package functions */
161
0
    assert(idx < H5PL_path_capacity_g);
162
163
    /* Copy the paths back to make a space  */
164
0
    for (u = H5PL_num_paths_g; u > idx; u--)
165
0
        H5PL_paths_g[u] = H5PL_paths_g[u - 1];
166
167
0
    H5PL_paths_g[idx] = NULL;
168
169
0
    FUNC_LEAVE_NOAPI(ret_value)
170
0
} /* end H5PL__make_space_at() */
171
172
/*-------------------------------------------------------------------------
173
 * Function:    H5PL__replace_at()
174
 *
175
 * Purpose:     Replace a path at a particular index in the path table.
176
 *              The path in the table must exist and will be freed by this
177
 *              function.
178
 *
179
 * Return:      SUCCEED/FAIL
180
 *
181
 *-------------------------------------------------------------------------
182
 */
183
static herr_t
184
H5PL__replace_at(const char *path, unsigned int idx)
185
0
{
186
0
    char  *path_copy = NULL;    /* copy of path string (for storing) */
187
0
    herr_t ret_value = SUCCEED; /* Return value */
188
189
0
    FUNC_ENTER_PACKAGE
190
191
    /* Check args - Just assert on package functions */
192
0
    assert(path);
193
0
    assert(strlen(path));
194
195
    /* Check that the table entry is in use */
196
0
    if (!H5PL_paths_g[idx])
197
0
        HGOTO_ERROR(H5E_PLUGIN, H5E_CANTFREE, FAIL, "path entry at index %u in the table is NULL", idx);
198
199
    /* Copy the path for storage so the caller can dispose of theirs */
200
0
    if (NULL == (path_copy = H5MM_strdup(path)))
201
0
        HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't make internal copy of path");
202
203
#ifdef H5_HAVE_WIN32_API
204
    /* Clean up Microsoft Windows environment variables in the path string */
205
    if (H5_expand_windows_env_vars(&path_copy))
206
        HGOTO_ERROR(H5E_PLUGIN, H5E_CANTCONVERT, FAIL, "can't expand environment variable string");
207
#endif /* H5_HAVE_WIN32_API */
208
209
    /* Free the existing path entry */
210
0
    H5PL_paths_g[idx] = (char *)H5MM_xfree(H5PL_paths_g[idx]);
211
212
    /* Copy the search path into the table at the specified index */
213
0
    H5PL_paths_g[idx] = path_copy;
214
215
0
done:
216
0
    FUNC_LEAVE_NOAPI(ret_value)
217
0
} /* end H5PL__replace_at() */
218
219
/*-------------------------------------------------------------------------
220
 * Function:    H5PL__create_path_table
221
 *
222
 * Purpose:     Create the collection of paths that will be searched
223
 *              when loading plugins.
224
 *
225
 * Return:      SUCCEED/FAIL
226
 *
227
 *-------------------------------------------------------------------------
228
 */
229
herr_t
230
H5PL__create_path_table(void)
231
6
{
232
6
    char *env_var = NULL;       /* Path string from environment variable */
233
6
    char *paths   = NULL;       /* Delimited paths string. Either from the
234
                                 * environment variable or the default.
235
                                 */
236
6
    char  *next_path = NULL;    /* A path tokenized from the paths string */
237
6
    char  *lasts     = NULL;    /* Context pointer for strtok_r() call */
238
6
    herr_t ret_value = SUCCEED; /* Return value */
239
240
6
    FUNC_ENTER_PACKAGE
241
242
    /* Allocate memory for the path table */
243
6
    H5PL_num_paths_g     = 0;
244
6
    H5PL_path_capacity_g = H5PL_INITIAL_PATH_CAPACITY;
245
6
    if (NULL == (H5PL_paths_g = (char **)H5MM_calloc((size_t)H5PL_path_capacity_g * sizeof(char *))))
246
0
        HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't allocate memory for path table");
247
248
    /* Retrieve paths from HDF5_PLUGIN_PATH if the user sets it
249
     * or from the default paths if it isn't set.
250
     */
251
6
    env_var = getenv(HDF5_PLUGIN_PATH);
252
6
    if (NULL == env_var)
253
6
        paths = H5MM_strdup(H5PL_DEFAULT_PATH);
254
0
    else
255
0
        paths = H5MM_strdup(env_var);
256
257
6
    if (NULL == paths)
258
0
        HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't allocate memory for path copy");
259
260
    /* Separate the paths and store them */
261
6
    next_path = HDstrtok_r(paths, H5PL_PATH_SEPARATOR, &lasts);
262
18
    while (next_path) {
263
264
        /* Insert the path into the table */
265
12
        if (H5PL__append_path(next_path) < 0)
266
0
            HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't insert path: %s", next_path);
267
268
        /* Get the next path from the environment string */
269
12
        next_path = HDstrtok_r(NULL, H5PL_PATH_SEPARATOR, &lasts);
270
12
    } /* end while */
271
272
6
done:
273
6
    if (paths)
274
6
        paths = (char *)H5MM_xfree(paths);
275
276
    /* Try to clean up on errors */
277
6
    if (FAIL == ret_value) {
278
0
        if (H5PL_paths_g)
279
0
            H5PL_paths_g = (char **)H5MM_xfree(H5PL_paths_g);
280
0
        H5PL_path_capacity_g = 0;
281
0
    }
282
283
6
    FUNC_LEAVE_NOAPI(ret_value)
284
6
} /* end H5PL__create_path_table() */
285
286
/*-------------------------------------------------------------------------
287
 * Function:    H5PL__close_path_table
288
 *
289
 * Purpose:     Close the collection of paths that will be searched
290
 *              when loading plugins.
291
 *
292
 * Return:      SUCCEED/FAIL
293
 *
294
 *-------------------------------------------------------------------------
295
 */
296
herr_t
297
H5PL__close_path_table(void)
298
12
{
299
12
    unsigned u;                   /* iterator */
300
12
    herr_t   ret_value = SUCCEED; /* Return value */
301
302
12
    FUNC_ENTER_PACKAGE_NOERR
303
304
    /* Free paths */
305
24
    for (u = 0; u < H5PL_num_paths_g; u++)
306
12
        if (H5PL_paths_g[u])
307
12
            H5PL_paths_g[u] = (char *)H5MM_xfree(H5PL_paths_g[u]);
308
309
    /* Free path table */
310
12
    H5PL_paths_g = (char **)H5MM_xfree(H5PL_paths_g);
311
312
    /* Reset values */
313
12
    H5PL_num_paths_g = 0;
314
315
12
    FUNC_LEAVE_NOAPI(ret_value)
316
317
12
} /* end H5PL__close_path_table() */
318
319
/*-------------------------------------------------------------------------
320
 * Function:    H5PL__get_num_paths
321
 *
322
 * Purpose:     Gets the number of plugin paths that have been stored.
323
 *
324
 * Return:      Success:    The number of paths
325
 *              Failure:   Can't fail
326
 *-------------------------------------------------------------------------
327
 */
328
unsigned
329
H5PL__get_num_paths(void)
330
0
{
331
0
    FUNC_ENTER_PACKAGE_NOERR
332
333
0
    FUNC_LEAVE_NOAPI(H5PL_num_paths_g)
334
335
0
} /* end H5PL__get_num_paths() */
336
337
/*-------------------------------------------------------------------------
338
 * Function:    H5PL__expand_path_table
339
 *
340
 * Purpose:     Expand the path table when it's full.
341
 *
342
 * Return:      SUCCEED/FAIL
343
 *
344
 *-------------------------------------------------------------------------
345
 */
346
static herr_t
347
H5PL__expand_path_table(void)
348
0
{
349
0
    herr_t ret_value = SUCCEED;
350
351
0
    FUNC_ENTER_PACKAGE
352
353
    /* Update the capacity */
354
0
    H5PL_path_capacity_g += H5PL_PATH_CAPACITY_ADD;
355
356
    /* Resize the array */
357
0
    if (NULL ==
358
0
        (H5PL_paths_g = (char **)H5MM_realloc(H5PL_paths_g, (size_t)H5PL_path_capacity_g * sizeof(char *))))
359
0
        HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "allocating additional memory for path table failed");
360
361
    /* Initialize the new memory */
362
0
    memset(H5PL_paths_g + H5PL_num_paths_g, 0, (size_t)H5PL_PATH_CAPACITY_ADD * sizeof(char *));
363
364
0
done:
365
    /* Set the path capacity back if there were problems */
366
0
    if (FAIL == ret_value)
367
0
        H5PL_path_capacity_g -= H5PL_PATH_CAPACITY_ADD;
368
369
0
    FUNC_LEAVE_NOAPI(ret_value)
370
0
} /* end H5PL__expand_path_table() */
371
372
/*-------------------------------------------------------------------------
373
 * Function:    H5PL__append_path
374
 *
375
 * Purpose:     Insert a path at the end of the table.
376
 *
377
 * Return:      SUCCEED/FAIL
378
 *
379
 *-------------------------------------------------------------------------
380
 */
381
herr_t
382
H5PL__append_path(const char *path)
383
12
{
384
12
    herr_t ret_value = SUCCEED; /* Return value */
385
386
12
    FUNC_ENTER_PACKAGE
387
388
    /* Check args - Just assert on package functions */
389
12
    assert(path);
390
12
    assert(strlen(path));
391
392
    /* Insert the path at the end of the table */
393
12
    if (H5PL__insert_at(path, H5PL_num_paths_g) < 0)
394
0
        HGOTO_ERROR(H5E_PLUGIN, H5E_CANTINSERT, FAIL, "unable to append search path");
395
396
12
done:
397
12
    FUNC_LEAVE_NOAPI(ret_value)
398
12
} /* end H5PL__append_path() */
399
400
/*-------------------------------------------------------------------------
401
 * Function:    H5PL__prepend_path
402
 *
403
 * Purpose:     Insert a path at the beginning of the table.
404
 *
405
 * Return:      SUCCEED/FAIL
406
 *
407
 *-------------------------------------------------------------------------
408
 */
409
herr_t
410
H5PL__prepend_path(const char *path)
411
0
{
412
0
    herr_t ret_value = SUCCEED; /* Return value */
413
414
0
    FUNC_ENTER_PACKAGE
415
416
    /* Check args - Just assert on package functions */
417
0
    assert(path);
418
0
    assert(strlen(path));
419
420
    /* Insert the path at the beginning of the table */
421
0
    if (H5PL__insert_at(path, 0) < 0)
422
0
        HGOTO_ERROR(H5E_PLUGIN, H5E_CANTINSERT, FAIL, "unable to prepend search path");
423
424
0
done:
425
0
    FUNC_LEAVE_NOAPI(ret_value)
426
0
} /* end H5PL__prepend_path() */
427
428
/*-------------------------------------------------------------------------
429
 * Function:    H5PL__replace_path
430
 *
431
 * Purpose:     Replace a path at particular index in the table.
432
 *
433
 * Return:      SUCCEED/FAIL
434
 *
435
 *-------------------------------------------------------------------------
436
 */
437
herr_t
438
H5PL__replace_path(const char *path, unsigned int idx)
439
0
{
440
0
    herr_t ret_value = SUCCEED; /* Return value */
441
442
0
    FUNC_ENTER_PACKAGE
443
444
    /* Check args - Just assert on package functions */
445
0
    assert(path);
446
0
    assert(strlen(path));
447
0
    assert(idx < H5PL_path_capacity_g);
448
449
    /* Insert the path at the requested index */
450
0
    if (H5PL__replace_at(path, idx) < 0)
451
0
        HGOTO_ERROR(H5E_PLUGIN, H5E_CANTINSERT, FAIL, "unable to replace search path");
452
453
0
done:
454
0
    FUNC_LEAVE_NOAPI(ret_value)
455
0
} /* end H5PL__replace_path() */
456
457
/*-------------------------------------------------------------------------
458
 * Function:    H5PL__insert_path
459
 *
460
 * Purpose:     Insert a path at particular index in the table, moving
461
 *              any existing paths back to make space.
462
 *
463
 * Return:      SUCCEED/FAIL
464
 *
465
 *-------------------------------------------------------------------------
466
 */
467
herr_t
468
H5PL__insert_path(const char *path, unsigned int idx)
469
0
{
470
0
    herr_t ret_value = SUCCEED; /* Return value */
471
472
0
    FUNC_ENTER_PACKAGE
473
474
    /* Check args - Just assert on package functions */
475
0
    assert(path);
476
0
    assert(strlen(path));
477
0
    assert(idx < H5PL_path_capacity_g);
478
479
    /* Insert the path at the requested index */
480
0
    if (H5PL__insert_at(path, idx) < 0)
481
0
        HGOTO_ERROR(H5E_PLUGIN, H5E_CANTINSERT, FAIL, "unable to insert search path");
482
483
0
done:
484
0
    FUNC_LEAVE_NOAPI(ret_value)
485
0
} /* end H5PL__insert_path() */
486
487
/*-------------------------------------------------------------------------
488
 * Function:    H5PL__remove_path
489
 *
490
 * Purpose:     Remove a path at particular index in the table, freeing
491
 *              the path string and moving the paths down to close the gap.
492
 *
493
 * Return:      SUCCEED/FAIL
494
 *
495
 *-------------------------------------------------------------------------
496
 */
497
herr_t
498
H5PL__remove_path(unsigned int idx)
499
0
{
500
0
    unsigned u;                   /* iterator */
501
0
    herr_t   ret_value = SUCCEED; /* Return value */
502
503
0
    FUNC_ENTER_PACKAGE
504
505
    /* Check args - Just assert on package functions */
506
0
    assert(idx < H5PL_path_capacity_g);
507
508
    /* Check if the path at that index is set */
509
0
    if (!H5PL_paths_g[idx])
510
0
        HGOTO_ERROR(H5E_PLUGIN, H5E_CANTDELETE, FAIL, "search path at index %u is NULL", idx);
511
512
    /* Delete the path */
513
0
    H5PL_num_paths_g--;
514
0
    H5PL_paths_g[idx] = (char *)H5MM_xfree(H5PL_paths_g[idx]);
515
516
    /* Shift the paths down to close the gap */
517
0
    for (u = idx; u < H5PL_num_paths_g; u++)
518
0
        H5PL_paths_g[u] = H5PL_paths_g[u + 1];
519
520
    /* Set the (former) last path to NULL */
521
0
    H5PL_paths_g[H5PL_num_paths_g] = NULL;
522
523
0
done:
524
0
    FUNC_LEAVE_NOAPI(ret_value)
525
0
} /* end H5PL__remove_path() */
526
527
/*-------------------------------------------------------------------------
528
 * Function:    H5PL__get_path
529
 *
530
 * Purpose:     Get a pointer to a path at particular index in the table.
531
 *
532
 * Return:      Success:    A pointer to a path string stored in the table
533
 *              Failure:    NULL
534
 *
535
 *-------------------------------------------------------------------------
536
 */
537
const char *
538
H5PL__get_path(unsigned int idx)
539
0
{
540
0
    char *ret_value = NULL; /* Return value */
541
542
0
    FUNC_ENTER_PACKAGE
543
544
    /* Get the path at the requested index */
545
0
    if (idx >= H5PL_num_paths_g)
546
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADRANGE, NULL, "path index %u is out of range in table", idx);
547
548
0
    return H5PL_paths_g[idx];
549
0
done:
550
0
    FUNC_LEAVE_NOAPI(ret_value)
551
0
} /* end H5PL__get_path() */
552
553
/*-------------------------------------------------------------------------
554
 * Function:    H5PL__path_table_iterate
555
 *
556
 * Purpose:     Iterates over all the plugins in the plugin path table and
557
 *              calls the specified callback function on each plugin found.
558
 *
559
 * Return:      H5_ITER_CONT if all plugins are processed successfully
560
 *              H5_ITER_STOP if short-circuit success occurs while
561
 *                  processing plugins
562
 *              H5_ITER_ERROR if an error occurs while processing plugins
563
 *
564
 *-------------------------------------------------------------------------
565
 */
566
herr_t
567
H5PL__path_table_iterate(H5PL_iterate_type_t iter_type, H5PL_iterate_t iter_op, void *op_data)
568
78
{
569
78
    unsigned int u;
570
78
    herr_t       ret_value = H5_ITER_CONT;
571
572
78
    FUNC_ENTER_PACKAGE
573
574
234
    for (u = 0; (u < H5PL_num_paths_g) && (ret_value == H5_ITER_CONT); u++) {
575
156
        if ((ret_value =
576
156
                 H5PL__path_table_iterate_process_path(H5PL_paths_g[u], iter_type, iter_op, op_data)) < 0)
577
0
            HGOTO_ERROR(H5E_PLUGIN, H5E_BADITER, H5_ITER_ERROR,
578
156
                        "can't iterate over plugins in plugin path '%s'", H5PL_paths_g[u]);
579
156
    }
580
581
78
done:
582
78
    FUNC_LEAVE_NOAPI(ret_value)
583
78
} /* end H5PL__path_table_iterate() */
584
585
/*-------------------------------------------------------------------------
586
 * Function:    H5PL__path_table_iterate_process_path
587
 *
588
 * Purpose:     Iterates over all the plugins within a single plugin path
589
 *              entry in the plugin path table and calls the specified
590
 *              callback function on each plugin found. Two function
591
 *              definitions are for Unix and Windows.
592
 *
593
 * Return:      H5_ITER_CONT if all plugins are processed successfully
594
 *              H5_ITER_STOP if short-circuit success occurs while
595
 *                  processing plugins
596
 *              H5_ITER_ERROR if an error occurs while processing plugins
597
 *
598
 *-------------------------------------------------------------------------
599
 */
600
#ifndef H5_HAVE_WIN32_API
601
static herr_t
602
H5PL__path_table_iterate_process_path(const char *plugin_path, H5PL_iterate_type_t iter_type,
603
                                      H5PL_iterate_t iter_op, void *op_data)
604
156
{
605
156
    H5PL_type_t    plugin_type;
606
156
    const void    *plugin_info = NULL;
607
156
    bool           plugin_loaded;
608
156
    char          *path      = NULL;
609
156
    DIR           *dirp      = NULL; /* Directory stream */
610
156
    struct dirent *dp        = NULL; /* Directory entry */
611
156
    herr_t         ret_value = H5_ITER_CONT;
612
613
156
    FUNC_ENTER_PACKAGE
614
615
156
    assert(plugin_path);
616
156
    assert(iter_op);
617
618
    /* Open the directory - skip the path if the directory can't be opened */
619
156
    if (!(dirp = HDopendir(plugin_path)))
620
156
        HGOTO_DONE(H5_ITER_CONT);
621
622
    /* Iterate through all entries in the directory */
623
0
    while (NULL != (dp = HDreaddir(dirp))) {
624
        /* The library we are looking for should be called libxxx.so... on Unix
625
         * or libxxx.xxx.dylib on Mac.
626
         */
627
0
#ifndef __CYGWIN__
628
0
        if (!strncmp(dp->d_name, "lib", (size_t)3) &&
629
0
            (strstr(dp->d_name, ".so") || strstr(dp->d_name, ".dylib"))) {
630
#else
631
        if (!strncmp(dp->d_name, "cyg", (size_t)3) && strstr(dp->d_name, ".dll")) {
632
#endif
633
634
0
            bool      plugin_matches;
635
0
            h5_stat_t my_stat;
636
0
            size_t    len;
637
638
            /* Allocate & initialize the path name */
639
0
            len = strlen(plugin_path) + strlen(H5PL_PATH_SEPARATOR) + strlen(dp->d_name) + 1 /*\0*/ +
640
0
                  4; /* Extra "+4" to quiet GCC warning - 2019/07/05, QAK */
641
642
0
            if (NULL == (path = (char *)H5MM_calloc(len)))
643
0
                HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, H5_ITER_ERROR, "can't allocate memory for path");
644
645
0
            snprintf(path, len, "%s/%s", plugin_path, dp->d_name);
646
647
            /* Get info for directory entry */
648
0
            memset(&my_stat, 0, sizeof(h5_stat_t));
649
0
            if (HDstat(path, &my_stat) == -1)
650
0
                HGOTO_ERROR(H5E_FILE, H5E_CANTGET, H5_ITER_ERROR, "can't stat file %s -- error was: %s", path,
651
0
                            strerror(errno));
652
653
            /* If it is a directory, skip it */
654
0
            if (S_ISDIR(my_stat.st_mode))
655
0
                continue;
656
657
            /* Attempt to open the dynamic library */
658
0
            plugin_type   = H5PL_TYPE_ERROR;
659
0
            plugin_info   = NULL;
660
0
            plugin_loaded = false;
661
0
            if (H5PL__open(path, H5PL_TYPE_NONE, NULL, &plugin_loaded, &plugin_type, &plugin_info) < 0)
662
0
                HGOTO_ERROR(H5E_PLUGIN, H5E_CANTGET, H5_ITER_ERROR, "failed to open plugin '%s'", path);
663
664
            /* Determine if we should process this plugin */
665
0
            plugin_matches = (iter_type == H5PL_ITER_TYPE_ALL) ||
666
0
                             ((iter_type == H5PL_ITER_TYPE_FILTER) && (plugin_type == H5PL_TYPE_FILTER)) ||
667
0
                             ((iter_type == H5PL_ITER_TYPE_VOL) && (plugin_type == H5PL_TYPE_VOL)) ||
668
0
                             ((iter_type == H5PL_ITER_TYPE_VFD) && (plugin_type == H5PL_TYPE_VFD));
669
670
            /* If the plugin was successfully loaded, call supplied callback function on plugin */
671
0
            if (plugin_loaded && plugin_matches && (ret_value = iter_op(plugin_type, plugin_info, op_data)))
672
0
                break;
673
674
0
            path = (char *)H5MM_xfree(path);
675
0
        } /* end if */
676
0
    }     /* end while */
677
678
0
    if (ret_value < 0)
679
0
        HERROR(H5E_PLUGIN, H5E_CALLBACK, "callback operator function returned failure");
680
681
156
done:
682
156
    if (dirp)
683
0
        if (HDclosedir(dirp) < 0)
684
0
            HDONE_ERROR(H5E_FILE, H5E_CLOSEERROR, H5_ITER_ERROR, "can't close directory: %s",
685
156
                        strerror(errno));
686
687
156
    path = (char *)H5MM_xfree(path);
688
689
156
    FUNC_LEAVE_NOAPI(ret_value)
690
156
} /* end H5PL__path_table_iterate_process_path() */
691
#else  /* H5_HAVE_WIN32_API */
692
static herr_t
693
H5PL__path_table_iterate_process_path(const char *plugin_path, H5PL_iterate_type_t iter_type,
694
                                      H5PL_iterate_t iter_op, void *op_data)
695
{
696
    WIN32_FIND_DATAA fdFile;
697
    HANDLE           hFind = INVALID_HANDLE_VALUE;
698
    H5PL_type_t      plugin_type;
699
    const void      *plugin_info = NULL;
700
    bool             plugin_loaded;
701
    char            *path = NULL;
702
    char             service[2048];
703
    herr_t           ret_value = H5_ITER_CONT;
704
705
    FUNC_ENTER_PACKAGE
706
707
    /* Check args - Just assert on package functions */
708
    assert(plugin_path);
709
    assert(iter_op);
710
711
    /* Specify a file mask. *.* = We want everything! -
712
     * skip the path if the directory can't be opened */
713
    snprintf(service, sizeof(service), "%s\\*.dll", plugin_path);
714
    if ((hFind = FindFirstFileA(service, &fdFile)) == INVALID_HANDLE_VALUE)
715
        HGOTO_DONE(H5_ITER_CONT);
716
717
    /* Loop over all the files */
718
    do {
719
        /* Ignore '.' and '..' */
720
        if (strcmp(fdFile.cFileName, ".") != 0 && strcmp(fdFile.cFileName, "..") != 0) {
721
            bool   plugin_matches;
722
            size_t len;
723
724
            /* Allocate & initialize the path name */
725
            len = strlen(plugin_path) + strlen(H5PL_PATH_SEPARATOR) + strlen(fdFile.cFileName) + 1;
726
727
            if (NULL == (path = (char *)H5MM_calloc(len)))
728
                HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, H5_ITER_ERROR, "can't allocate memory for path");
729
730
            snprintf(path, len, "%s\\%s", plugin_path, fdFile.cFileName);
731
732
            /* Ignore directories */
733
            if (fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
734
                continue;
735
736
            /* Attempt to open the dynamic library */
737
            plugin_type   = H5PL_TYPE_ERROR;
738
            plugin_info   = NULL;
739
            plugin_loaded = false;
740
            if (H5PL__open(path, H5PL_TYPE_NONE, NULL, &plugin_loaded, &plugin_type, &plugin_info) < 0)
741
                HGOTO_ERROR(H5E_PLUGIN, H5E_CANTGET, H5_ITER_ERROR, "failed to open plugin '%s'", path);
742
743
            /* Determine if we should process this plugin */
744
            plugin_matches = (iter_type == H5PL_ITER_TYPE_ALL) ||
745
                             ((iter_type == H5PL_ITER_TYPE_FILTER) && (plugin_type == H5PL_TYPE_FILTER)) ||
746
                             ((iter_type == H5PL_ITER_TYPE_VOL) && (plugin_type == H5PL_TYPE_VOL)) ||
747
                             ((iter_type == H5PL_ITER_TYPE_VFD) && (plugin_type == H5PL_TYPE_VFD));
748
749
            /* If the plugin was successfully loaded, call supplied callback function on plugin */
750
            if (plugin_loaded && plugin_matches && (ret_value = iter_op(plugin_type, plugin_info, op_data)))
751
                break;
752
753
            path = (char *)H5MM_xfree(path);
754
        }
755
    } while (FindNextFileA(hFind, &fdFile));
756
757
    if (ret_value < 0)
758
        HERROR(H5E_PLUGIN, H5E_CALLBACK, "callback operator function returned failure");
759
760
done:
761
    if (hFind != INVALID_HANDLE_VALUE)
762
        FindClose(hFind);
763
764
    path = (char *)H5MM_xfree(path);
765
766
    FUNC_LEAVE_NOAPI(ret_value)
767
} /* end H5PL__path_table_iterate_process_path() */
768
#endif /* H5_HAVE_WIN32_API */
769
770
/*-------------------------------------------------------------------------
771
 * Function:    H5PL__find_plugin_in_path_table
772
 *
773
 * Purpose:     Attempts to find a matching plugin in the file system
774
 *              using the paths stored in the path table.
775
 *.
776
 *              The 'found' parameter will be set appropriately.
777
 *
778
 * Return:      SUCCEED/FAIL
779
 *
780
 *-------------------------------------------------------------------------
781
 */
782
herr_t
783
H5PL__find_plugin_in_path_table(const H5PL_search_params_t *search_params, bool *found,
784
                                const void **plugin_info)
785
0
{
786
0
    unsigned int u; /* iterator */
787
0
    herr_t       ret_value = SUCCEED;
788
789
0
    FUNC_ENTER_PACKAGE
790
791
    /* Check args - Just assert on package functions */
792
0
    assert(search_params);
793
0
    assert(found);
794
0
    assert(plugin_info);
795
796
    /* Initialize output parameters */
797
0
    *found       = false;
798
0
    *plugin_info = NULL;
799
800
    /* Loop over the paths in the table, checking for an appropriate plugin */
801
0
    for (u = 0; u < H5PL_num_paths_g; u++) {
802
803
        /* Search for the plugin in this path */
804
0
        if (H5PL__find_plugin_in_path(search_params, found, H5PL_paths_g[u], plugin_info) < 0)
805
0
            HERROR(H5E_PLUGIN, H5E_CANTGET, "search in path %s encountered an error", H5PL_paths_g[u]);
806
807
        /* Break out if found */
808
0
        if (*found) {
809
0
            if (!plugin_info)
810
0
                HGOTO_ERROR(H5E_PLUGIN, H5E_BADVALUE, FAIL, "plugin info should not be NULL");
811
0
            break;
812
0
        }
813
0
    }
814
815
0
done:
816
0
    FUNC_LEAVE_NOAPI(ret_value)
817
0
} /* end H5PL__find_plugin_in_path_table() */
818
819
/*-------------------------------------------------------------------------
820
 * Function:    H5PL__find_plugin_in_path
821
 *
822
 * Purpose:     Given a path, this function opens the directory and invokes
823
 *              another function to go through all files to find the right
824
 *              plugin library. Two function definitions are for Unix and
825
 *              Windows.
826
 *
827
 *              The found parameter will be set to true and the info
828
 *              parameter will be filled in on success.
829
 *
830
 * Return:      SUCCEED/FAIL
831
 *
832
 *-------------------------------------------------------------------------
833
 */
834
#ifndef H5_HAVE_WIN32_API
835
static herr_t
836
H5PL__find_plugin_in_path(const H5PL_search_params_t *search_params, bool *found, const char *dir,
837
                          const void **plugin_info)
838
0
{
839
0
    char          *path      = NULL;
840
0
    DIR           *dirp      = NULL; /* Directory stream */
841
0
    struct dirent *dp        = NULL; /* Directory entry */
842
0
    herr_t         ret_value = SUCCEED;
843
844
0
    FUNC_ENTER_PACKAGE
845
846
    /* Check args - Just assert on package functions */
847
0
    assert(search_params);
848
0
    assert(found);
849
0
    assert(dir);
850
0
    assert(plugin_info);
851
852
    /* Initialize the found parameter */
853
0
    *found = false;
854
855
    /* Open the directory */
856
0
    if (!(dirp = HDopendir(dir)))
857
0
        HGOTO_ERROR(H5E_PLUGIN, H5E_OPENERROR, FAIL, "can't open directory (%s). Please verify its existence",
858
0
                    dir);
859
860
    /* Iterate through all entries in the directory */
861
0
    while (NULL != (dp = HDreaddir(dirp))) {
862
863
        /* The library we are looking for should be called libxxx.so... on Unix
864
         * or libxxx.xxx.dylib on Mac.
865
         */
866
0
#ifndef __CYGWIN__
867
0
        if (!strncmp(dp->d_name, "lib", (size_t)3) &&
868
0
            (strstr(dp->d_name, ".so") || strstr(dp->d_name, ".dylib"))) {
869
#else
870
        if (!strncmp(dp->d_name, "cyg", (size_t)3) && strstr(dp->d_name, ".dll")) {
871
#endif
872
873
0
            h5_stat_t my_stat;
874
0
            size_t    len;
875
876
            /* Allocate & initialize the path name */
877
0
            len = strlen(dir) + strlen(H5PL_PATH_SEPARATOR) + strlen(dp->d_name) + 1 /*\0*/ +
878
0
                  4; /* Extra "+4" to quiet GCC warning - 2019/07/05, QAK */
879
880
0
            if (NULL == (path = (char *)H5MM_calloc(len)))
881
0
                HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't allocate memory for path");
882
883
0
            snprintf(path, len, "%s/%s", dir, dp->d_name);
884
885
            /* Get info for directory entry */
886
0
            memset(&my_stat, 0, sizeof(h5_stat_t));
887
0
            if (HDstat(path, &my_stat) == -1)
888
0
                HGOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "can't stat file %s -- error was: %s", path,
889
0
                            strerror(errno));
890
891
            /* If it is a directory, skip it */
892
0
            if (S_ISDIR(my_stat.st_mode)) {
893
0
                path = (char *)H5MM_xfree(path);
894
0
                continue;
895
0
            }
896
897
            /* attempt to open the dynamic library */
898
0
            if (H5PL__open(path, search_params->type, search_params->key, found, NULL, plugin_info) < 0)
899
0
                HGOTO_ERROR(H5E_PLUGIN, H5E_CANTGET, FAIL, "search in directory failed");
900
0
            if (*found)
901
0
                HGOTO_DONE(SUCCEED);
902
903
0
            path = (char *)H5MM_xfree(path);
904
0
        } /* end if */
905
0
    }     /* end while */
906
907
0
done:
908
0
    if (dirp)
909
0
        if (HDclosedir(dirp) < 0)
910
0
            HDONE_ERROR(H5E_FILE, H5E_CLOSEERROR, FAIL, "can't close directory: %s", strerror(errno));
911
912
0
    path = (char *)H5MM_xfree(path);
913
914
0
    FUNC_LEAVE_NOAPI(ret_value)
915
0
} /* end H5PL__find_plugin_in_path() */
916
#else  /* H5_HAVE_WIN32_API */
917
static herr_t
918
H5PL__find_plugin_in_path(const H5PL_search_params_t *search_params, bool *found, const char *dir,
919
                          const void **plugin_info)
920
{
921
    WIN32_FIND_DATAA fdFile;
922
    HANDLE           hFind = INVALID_HANDLE_VALUE;
923
    char            *path  = NULL;
924
    char             service[2048];
925
    herr_t           ret_value = SUCCEED;
926
927
    FUNC_ENTER_PACKAGE
928
929
    /* Check args - Just assert on package functions */
930
    assert(search_params);
931
    assert(found);
932
    assert(dir);
933
    assert(plugin_info);
934
935
    /* Initialize the found parameter */
936
    *found = false;
937
938
    /* Specify a file mask. *.* = We want everything! */
939
    snprintf(service, sizeof(service), "%s\\*.dll", dir);
940
    if ((hFind = FindFirstFileA(service, &fdFile)) == INVALID_HANDLE_VALUE)
941
        HGOTO_ERROR(H5E_PLUGIN, H5E_OPENERROR, FAIL, "can't open directory");
942
943
    /* Loop over all the files */
944
    do {
945
        /* Ignore '.' and '..' */
946
        if (strcmp(fdFile.cFileName, ".") != 0 && strcmp(fdFile.cFileName, "..") != 0) {
947
948
            /* XXX: Probably just continue here and move the code below over one tab */
949
950
            size_t len;
951
952
            /* Allocate & initialize the path name */
953
            len = strlen(dir) + strlen(H5PL_PATH_SEPARATOR) + strlen(fdFile.cFileName) + 1;
954
955
            if (NULL == (path = (char *)H5MM_calloc(len)))
956
                HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't allocate memory for path");
957
958
            snprintf(path, len, "%s\\%s", dir, fdFile.cFileName);
959
960
            /* Ignore directories */
961
            if (fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
962
                continue;
963
964
            /* attempt to open the dynamic library */
965
            if (H5PL__open(path, search_params->type, search_params->key, found, NULL, plugin_info) < 0)
966
                HGOTO_ERROR(H5E_PLUGIN, H5E_CANTGET, FAIL, "search in directory failed");
967
            if (*found)
968
                HGOTO_DONE(SUCCEED);
969
970
            path = (char *)H5MM_xfree(path);
971
        }
972
    } while (FindNextFileA(hFind, &fdFile));
973
974
done:
975
    if (hFind != INVALID_HANDLE_VALUE)
976
        FindClose(hFind);
977
    if (path)
978
        path = (char *)H5MM_xfree(path);
979
980
    FUNC_LEAVE_NOAPI(ret_value)
981
} /* end H5PL__find_plugin_in_path() */
982
#endif /* H5_HAVE_WIN32_API */