Coverage Report

Created: 2026-11-22 16:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hdf5/src/H5Gtraverse.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
 *
15
 * Created:   H5Gtraverse.c
16
 *
17
 * Purpose:   Functions for traversing group hierarchy
18
 *
19
 *-------------------------------------------------------------------------
20
 */
21
22
/****************/
23
/* Module Setup */
24
/****************/
25
26
#include "H5Gmodule.h" /* This source code file is part of the H5G module */
27
28
/***********/
29
/* Headers */
30
/***********/
31
#include "H5private.h"   /* Generic Functions                        */
32
#include "H5CXprivate.h" /* API Contexts                             */
33
#include "H5Dprivate.h"  /* Datasets                                 */
34
#include "H5Eprivate.h"  /* Error handling                           */
35
#include "H5Fprivate.h"  /* File access                              */
36
#include "H5Gpkg.h"      /* Groups                                   */
37
#include "H5Iprivate.h"  /* IDs                                      */
38
#include "H5Lprivate.h"  /* Links                                    */
39
#include "H5MMprivate.h" /* Memory management                        */
40
#include "H5Ppublic.h"   /* Property Lists                           */
41
#include "H5WBprivate.h" /* Wrapped Buffers                          */
42
43
/****************/
44
/* Local Macros */
45
/****************/
46
47
/******************/
48
/* Local Typedefs */
49
/******************/
50
51
/* User data for path traversal routine */
52
typedef struct {
53
    /* down */
54
    bool chk_exists; /* Flag to indicate we are checking if object exists */
55
56
    /* up */
57
    H5G_loc_t *obj_loc; /* Object location */
58
    bool       exists;  /* Indicate if object exists */
59
} H5G_trav_slink_t;
60
61
/********************/
62
/* Package Typedefs */
63
/********************/
64
65
/********************/
66
/* Local Prototypes */
67
/********************/
68
static herr_t H5G__traverse_slink_cb(H5G_loc_t *grp_loc, const char *name, const H5O_link_t *lnk,
69
                                     H5G_loc_t *obj_loc, void *_udata /*in,out*/,
70
                                     H5G_own_loc_t *own_loc /*out*/);
71
static herr_t H5G__traverse_ud(const H5G_loc_t *grp_loc, const H5O_link_t *lnk, H5G_loc_t *obj_loc /*in,out*/,
72
                               unsigned target, bool *obj_exists);
73
static herr_t H5G__traverse_slink(const H5G_loc_t *grp_loc, const H5O_link_t *lnk,
74
                                  H5G_loc_t *obj_loc /*in,out*/, unsigned target, bool *obj_exists);
75
static herr_t H5G__traverse_real(const H5G_loc_t *loc, const char *name, unsigned target, H5G_traverse_t op,
76
                                 void *op_data);
77
78
/*********************/
79
/* Package Variables */
80
/*********************/
81
82
/*****************************/
83
/* Library Private Variables */
84
/*****************************/
85
86
/*******************/
87
/* Local Variables */
88
/*******************/
89
90
/*-------------------------------------------------------------------------
91
 * Function:  H5G__traverse_slink_cb
92
 *
93
 * Purpose: Callback for soft link traversal.  This routine sets the
94
 *              correct information for the object location.
95
 *
96
 * Return:  Non-negative on success/Negative on failure
97
 *
98
 *-------------------------------------------------------------------------
99
 */
100
static herr_t
101
H5G__traverse_slink_cb(H5G_loc_t H5_ATTR_UNUSED *grp_loc, const char H5_ATTR_UNUSED *name,
102
                       const H5O_link_t H5_ATTR_UNUSED *lnk, H5G_loc_t *obj_loc, void *_udata /*in,out*/,
103
                       H5G_own_loc_t *own_loc /*out*/)
104
597
{
105
597
    H5G_trav_slink_t *udata     = (H5G_trav_slink_t *)_udata; /* User data passed in */
106
597
    herr_t            ret_value = SUCCEED;                    /* Return value */
107
108
597
    FUNC_ENTER_PACKAGE
109
110
    /* Check for dangling soft link */
111
597
    if (obj_loc == NULL) {
112
1
        if (udata->chk_exists)
113
0
            udata->exists = false;
114
1
        else
115
1
            HGOTO_ERROR(H5E_SYM, H5E_NOTFOUND, FAIL, "component not found");
116
1
    } /* end if */
117
596
    else {
118
        /* Copy new location information for resolved object */
119
596
        H5O_loc_copy_deep(udata->obj_loc->oloc, obj_loc->oloc);
120
121
        /* Indicate that the object exists */
122
596
        udata->exists = true;
123
596
    } /* end else */
124
125
597
done:
126
    /* Indicate that this callback didn't take ownership of the group *
127
     * location for the object */
128
597
    *own_loc = H5G_OWN_NONE;
129
130
597
    FUNC_LEAVE_NOAPI(ret_value)
131
597
} /* end H5G__traverse_slink_cb() */
132
133
/*-------------------------------------------------------------------------
134
 * Function:  H5G__traverse_ud
135
 *
136
 * Purpose: Callback for user-defined link traversal.  Sets up a
137
 *              location ID and passes it to the user traversal callback.
138
 *
139
 * Return:  Non-negative on success/Negative on failure
140
 *
141
 *-------------------------------------------------------------------------
142
 */
143
static herr_t
144
H5G__traverse_ud(const H5G_loc_t *grp_loc /*in,out*/, const H5O_link_t *lnk, H5G_loc_t *obj_loc /*in,out*/,
145
                 unsigned target, bool *obj_exists)
146
0
{
147
0
    const H5L_class_t *link_class;     /* User-defined link class */
148
0
    hid_t              cb_return = -1; /* The ID the user-defined callback returned */
149
0
    H5G_loc_t          grp_loc_copy;
150
0
    H5G_name_t         grp_path_copy;
151
0
    H5O_loc_t          grp_oloc_copy;
152
0
    H5G_loc_t          new_loc; /* Group location for newly opened external object */
153
0
    H5G_t             *grp;
154
0
    hid_t              cur_grp   = (-1);
155
0
    herr_t             ret_value = SUCCEED; /* Return value */
156
157
0
    FUNC_ENTER_PACKAGE
158
159
    /* Sanity check */
160
0
    assert(grp_loc);
161
0
    assert(lnk);
162
0
    assert(lnk->type >= H5L_TYPE_UD_MIN);
163
0
    assert(obj_loc);
164
165
    /* Get the link class for this type of link. */
166
0
    if (NULL == (link_class = H5L_find_class(lnk->type)))
167
0
        HGOTO_ERROR(H5E_SYM, H5E_NOTREGISTERED, FAIL, "unable to get UD link class");
168
169
    /* Set up location for user-defined callback.  Use a copy of our current
170
     * grp_loc. */
171
0
    grp_loc_copy.path = &grp_path_copy;
172
0
    grp_loc_copy.oloc = &grp_oloc_copy;
173
0
    H5G_loc_reset(&grp_loc_copy);
174
0
    if (H5G_loc_copy(&grp_loc_copy, grp_loc, H5_COPY_DEEP) < 0)
175
0
        HGOTO_ERROR(H5E_SYM, H5E_CANTCOPY, FAIL, "unable to copy object location");
176
177
    /* Create a group ID to pass to the user-defined callback */
178
0
    if (NULL == (grp = H5G_open(&grp_loc_copy)))
179
0
        HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to open group");
180
0
    if ((cur_grp = H5VL_wrap_register(H5I_GROUP, grp, false)) < 0)
181
0
        HGOTO_ERROR(H5E_SYM, H5E_CANTREGISTER, FAIL, "unable to register group");
182
183
    /* Pause recording errors, if we are just checking for object's existence */
184
0
    if (target & H5G_TARGET_EXISTS)
185
0
        H5E_pause_stack();
186
187
        /* Invoke user-defined callback function */
188
#ifndef H5_NO_DEPRECATED_SYMBOLS
189
    /* (Backwardly compatible with v0 H5L_class_t traversal callback) */
190
    if (link_class->version == H5L_LINK_CLASS_T_VERS_0) {
191
        /* Prepare & restore library for user callback */
192
        H5_BEFORE_USER_CB(FAIL)
193
            {
194
                cb_return = (((const H5L_class_0_t *)link_class)->trav_func)(
195
                    lnk->name, cur_grp, lnk->u.ud.udata, lnk->u.ud.size, H5CX_get_lapl());
196
            }
197
        H5_AFTER_USER_CB(FAIL)
198
    }
199
    else {
200
        /* Prepare & restore library for user callback */
201
        H5_BEFORE_USER_CB(FAIL)
202
            {
203
                cb_return = (link_class->trav_func)(lnk->name, cur_grp, lnk->u.ud.udata, lnk->u.ud.size,
204
                                                    H5CX_get_lapl(), H5CX_get_dxpl());
205
            }
206
        H5_AFTER_USER_CB(FAIL)
207
    }
208
#else  /* H5_NO_DEPRECATED_SYMBOLS */
209
    /* Prepare & restore library for user callback */
210
0
    H5_BEFORE_USER_CB(FAIL)
211
0
        {
212
0
            cb_return = (link_class->trav_func)(lnk->name, cur_grp, lnk->u.ud.udata, lnk->u.ud.size,
213
0
                                                H5CX_get_lapl(), H5CX_get_dxpl());
214
0
        }
215
0
    H5_AFTER_USER_CB(FAIL)
216
0
#endif /* H5_NO_DEPRECATED_SYMBOLS */
217
218
    /* Resume recording errors, if we were just checking for object's existence */
219
0
    if (target & H5G_TARGET_EXISTS)
220
0
        H5E_resume_stack();
221
222
    /* Check for failing to locate the object */
223
0
    if (cb_return < 0) {
224
        /* Check if we just needed to know if the object exists */
225
0
        if (target & H5G_TARGET_EXISTS) {
226
            /* Indicate that the object doesn't exist */
227
0
            *obj_exists = false;
228
229
            /* Get out now */
230
0
            HGOTO_DONE(SUCCEED);
231
0
        } /* end if */
232
        /* else, we really needed to open the object */
233
0
        else
234
0
            HGOTO_ERROR(H5E_SYM, H5E_BADID, FAIL, "traversal callback returned invalid ID");
235
0
    } /* end if */
236
237
    /* Get the object location information from the ID the user callback returned */
238
0
    if (H5G_loc(cb_return, &new_loc) < 0)
239
0
        HGOTO_ERROR(H5E_SYM, H5E_BADVALUE, FAIL, "unable to get object location from ID");
240
241
    /* Release any previous location information for the object */
242
0
    H5G_loc_free(obj_loc);
243
244
    /* Copy new object's location information */
245
0
    H5G_loc_copy(obj_loc, &new_loc, H5_COPY_DEEP);
246
247
    /* Hold the file open until we free this object header (otherwise the
248
     * object location will be invalidated when the file closes).
249
     */
250
0
    if (H5O_loc_hold_file(obj_loc->oloc) < 0)
251
0
        HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to hold file open");
252
253
    /* We have a copy of the location and we're holding the file open.
254
     * Close the open ID the user passed back.
255
     */
256
0
    if (H5I_dec_ref(cb_return) < 0)
257
0
        HGOTO_ERROR(H5E_SYM, H5E_CANTRELEASE, FAIL, "unable to close ID from UD callback");
258
0
    cb_return = (hid_t)(-1);
259
260
0
done:
261
    /* Close location given to callback. */
262
0
    if (cur_grp > 0 && H5I_dec_ref(cur_grp) < 0)
263
0
        HDONE_ERROR(H5E_SYM, H5E_CANTRELEASE, FAIL, "unable to close ID for current location");
264
265
0
    if (ret_value < 0 && cb_return > 0 && H5I_dec_ref(cb_return) < 0)
266
0
        HDONE_ERROR(H5E_SYM, H5E_CANTRELEASE, FAIL, "unable to close ID from UD callback");
267
268
0
    FUNC_LEAVE_NOAPI(ret_value)
269
0
} /* end H5G__traverse_ud() */
270
271
/*-------------------------------------------------------------------------
272
 * Function:  H5G__traverse_slink
273
 *
274
 * Purpose: Traverses symbolic link.  The link head appears in the group
275
 *    whose entry is GRP_LOC and the link tail entry is OBJ_LOC.
276
 *
277
 * Return:  Success:  Non-negative, OBJ_LOC will contain information
278
 *        about the object to which the link points
279
 *
280
 *    Failure:  Negative
281
 *
282
 *-------------------------------------------------------------------------
283
 */
284
static herr_t
285
H5G__traverse_slink(const H5G_loc_t *grp_loc, const H5O_link_t *lnk, H5G_loc_t *obj_loc /*in,out*/,
286
                    unsigned target, bool *obj_exists)
287
597
{
288
597
    H5G_trav_slink_t udata;                     /* User data to pass to link traversal callback */
289
597
    H5G_name_t       tmp_obj_path;              /* Temporary copy of object's path */
290
597
    bool             tmp_obj_path_set = false;  /* Flag to indicate that tmp object path is initialized */
291
597
    H5O_loc_t        tmp_grp_oloc;              /* Temporary copy of group entry */
292
597
    H5G_name_t       tmp_grp_path;              /* Temporary copy of group's path */
293
597
    H5G_loc_t        tmp_grp_loc;               /* Temporary copy of group's location */
294
597
    bool             tmp_grp_loc_set = false;   /* Flag to indicate that tmp group location is initialized */
295
597
    herr_t           ret_value       = SUCCEED; /* Return value */
296
297
597
    FUNC_ENTER_PACKAGE
298
299
    /* Sanity check */
300
597
    assert(grp_loc);
301
597
    assert(lnk);
302
597
    assert(lnk->type == H5L_TYPE_SOFT);
303
304
    /* Set up temporary location */
305
597
    tmp_grp_loc.oloc = &tmp_grp_oloc;
306
597
    tmp_grp_loc.path = &tmp_grp_path;
307
308
    /* Portably initialize the temporary objects */
309
597
    H5G_loc_reset(&tmp_grp_loc);
310
597
    H5G_name_reset(&tmp_obj_path);
311
312
    /* Clone the group location, so we can track the names properly */
313
    /* ("tracking the names properly" means to ignore the effects of the
314
     *  link traversal on the object's & group's paths - QAK)
315
     */
316
597
    H5G_loc_copy(&tmp_grp_loc, grp_loc, H5_COPY_DEEP);
317
597
    tmp_grp_loc_set = true;
318
319
    /* Hold the object's group hier. path to restore later */
320
    /* (Part of "tracking the names properly") */
321
597
    H5G_name_copy(&tmp_obj_path, obj_loc->path, H5_COPY_SHALLOW);
322
597
    tmp_obj_path_set = true;
323
324
    /* Set up user data for traversal callback */
325
597
    udata.chk_exists = (target & H5G_TARGET_EXISTS) ? true : false;
326
597
    udata.exists     = false;
327
597
    udata.obj_loc    = obj_loc;
328
329
    /* Traverse the link */
330
597
    if (H5G__traverse_real(&tmp_grp_loc, lnk->u.soft.name, target, H5G__traverse_slink_cb, &udata) < 0)
331
1
        HGOTO_ERROR(H5E_SYM, H5E_NOTFOUND, FAIL, "unable to follow symbolic link");
332
333
    /* Pass back information about whether the object exists */
334
596
    *obj_exists = udata.exists;
335
336
597
done:
337
    /* Restore object's group hier. path */
338
597
    if (tmp_obj_path_set) {
339
597
        H5G_name_free(obj_loc->path);
340
597
        H5G_name_copy(obj_loc->path, &tmp_obj_path, H5_COPY_SHALLOW);
341
597
    } /* end if */
342
343
    /* Release cloned copy of group location */
344
597
    if (tmp_grp_loc_set)
345
597
        H5G_loc_free(&tmp_grp_loc);
346
347
597
    FUNC_LEAVE_NOAPI(ret_value)
348
597
} /* end H5G__traverse_slink() */
349
350
/*-------------------------------------------------------------------------
351
 * Function:  H5G__traverse_special
352
 *
353
 * Purpose: Handle traversing special link situations
354
 *
355
 * Return:  Non-negative on success/Negative on failure
356
 *
357
 *-------------------------------------------------------------------------
358
 */
359
herr_t
360
H5G__traverse_special(const H5G_loc_t *grp_loc, const H5O_link_t *lnk, unsigned target, bool last_comp,
361
                      H5G_loc_t *obj_loc, bool *obj_exists)
362
22.1k
{
363
22.1k
    size_t nlinks;              /* # of soft / UD links left to traverse */
364
22.1k
    herr_t ret_value = SUCCEED; /* Return value */
365
366
22.1k
    FUNC_ENTER_PACKAGE
367
368
    /* Sanity check */
369
22.1k
    assert(grp_loc);
370
22.1k
    assert(lnk);
371
22.1k
    assert(obj_loc);
372
373
    /* If we found a symbolic link then we should follow it.  But if this
374
     * is the last component of the name and the H5G_TARGET_SLINK bit of
375
     * TARGET is set then we don't follow it.
376
     */
377
22.1k
    if (H5L_TYPE_SOFT == lnk->type && (0 == (target & H5G_TARGET_SLINK) || !last_comp)) {
378
379
        /* Get the # of soft / UD links left to traverse */
380
597
        if (H5CX_get_nlinks(&nlinks) < 0)
381
0
            HGOTO_ERROR(H5E_LINK, H5E_CANTGET, FAIL, "unable to retrieve # of soft / UD links to traverse");
382
383
        /* Decrement # of links and range check */
384
597
        if ((nlinks)-- <= 0)
385
0
            HGOTO_ERROR(H5E_LINK, H5E_NLINKS, FAIL, "too many links");
386
387
        /* Update the # of links in the API context */
388
597
        if (H5CX_set_nlinks(nlinks) < 0)
389
0
            HGOTO_ERROR(H5E_LINK, H5E_CANTSET, FAIL, "can't update # of soft / UD links to traverse");
390
391
        /* Traverse soft link */
392
597
        if (H5G__traverse_slink(grp_loc, lnk, obj_loc, (target & H5G_TARGET_EXISTS), obj_exists) < 0)
393
1
            HGOTO_ERROR(H5E_LINK, H5E_TRAVERSE, FAIL, "symbolic link traversal failed");
394
597
    } /* end if */
395
396
    /*
397
     * If we found a user-defined link then we should follow it.  But if this
398
     * is the last component of the name and the H5G_TARGET_UDLINK bit of
399
     * TARGET is set then we don't follow it.
400
     */
401
22.1k
    if (lnk->type >= H5L_TYPE_UD_MIN && (0 == (target & H5G_TARGET_UDLINK) || !last_comp)) {
402
403
        /* Get the # of soft / UD links left to traverse */
404
0
        if (H5CX_get_nlinks(&nlinks) < 0)
405
0
            HGOTO_ERROR(H5E_LINK, H5E_CANTGET, FAIL, "unable to retrieve # of soft / UD links to traverse");
406
407
        /* Decrement # of links and range check */
408
0
        if ((nlinks)-- <= 0)
409
0
            HGOTO_ERROR(H5E_LINK, H5E_NLINKS, FAIL, "too many links");
410
411
        /* Update the # of links in the API context */
412
0
        if (H5CX_set_nlinks(nlinks) < 0)
413
0
            HGOTO_ERROR(H5E_LINK, H5E_CANTSET, FAIL, "can't update # of soft / UD links to traverse");
414
415
        /* Traverse user-defined link */
416
0
        if (H5G__traverse_ud(grp_loc, lnk, obj_loc, (target & H5G_TARGET_EXISTS), obj_exists) < 0)
417
0
            HGOTO_ERROR(H5E_LINK, H5E_TRAVERSE, FAIL, "user-defined link traversal failed");
418
0
    } /* end if */
419
420
    /*
421
     * Resolve mount points to the mounted group.  Do not do this step if
422
     * the H5G_TARGET_MOUNT bit of TARGET is set and this is the last
423
     * component of the name.
424
     *
425
     * (If this link is a hard link, try to perform mount point traversal)
426
     *
427
     * (Note that the soft and external link traversal above can change
428
     *  the status of the object (into a hard link), so don't use an 'else'
429
     *  statement here. -QAK)
430
     */
431
22.1k
    if (H5_addr_defined(obj_loc->oloc->addr) && (0 == (target & H5G_TARGET_MOUNT) || !last_comp)) {
432
22.1k
        if (H5F_traverse_mount(obj_loc->oloc /*in,out*/) < 0)
433
0
            HGOTO_ERROR(H5E_SYM, H5E_NOTFOUND, FAIL, "mount point traversal failed");
434
22.1k
    } /* end if */
435
436
    /* If the grp_loc is the only thing holding an external file open
437
     * and obj_loc is in the same file, obj_loc should also hold the
438
     * file open so that closing the grp_loc doesn't close the file.
439
     */
440
22.1k
    if (grp_loc->oloc->holding_file && grp_loc->oloc->file == obj_loc->oloc->file)
441
0
        if (H5O_loc_hold_file(obj_loc->oloc) < 0)
442
0
            HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to hold file open");
443
444
22.1k
done:
445
22.1k
    FUNC_LEAVE_NOAPI(ret_value)
446
22.1k
} /* end H5G__traverse_special() */
447
448
/*-------------------------------------------------------------------------
449
 * Function:  H5G__traverse_real
450
 *
451
 * Purpose: Internal version of path traversal routine
452
 *
453
 * Return:  Success:  Non-negative if name can be fully resolved.
454
 *
455
 *    Failure:  Negative if the name could not be fully
456
 *        resolved.
457
 *
458
 *-------------------------------------------------------------------------
459
 */
460
static herr_t
461
H5G__traverse_real(const H5G_loc_t *_loc, const char *name, unsigned target, H5G_traverse_t op, void *op_data)
462
59.6k
{
463
59.6k
    H5G_loc_t     loc;                    /* Location of start object     */
464
59.6k
    H5O_loc_t     grp_oloc;               /* Object loc. for current group */
465
59.6k
    H5G_name_t    grp_path;               /* Path for current group */
466
59.6k
    H5G_loc_t     grp_loc;                /* Location of group            */
467
59.6k
    H5O_loc_t     obj_oloc;               /* Object found     */
468
59.6k
    H5G_name_t    obj_path;               /* Path for object found  */
469
59.6k
    H5G_loc_t     obj_loc;                /* Location of object           */
470
59.6k
    size_t        nchars;                 /* component name length  */
471
59.6k
    H5O_link_t    lnk;                    /* Link information for object  */
472
59.6k
    bool          link_valid    = false;  /* Flag to indicate that the link information is valid */
473
59.6k
    bool          obj_loc_valid = false;  /* Flag to indicate that the object location is valid */
474
59.6k
    H5G_own_loc_t own_loc = H5G_OWN_NONE; /* Enum to indicate whether callback took ownership of locations*/
475
59.6k
    bool          group_copy = false;     /* Flag to indicate that the group entry is copied */
476
59.6k
    char          comp_buf[1024];         /* Temporary buffer for path components */
477
59.6k
    char         *comp;                   /* Pointer to buffer for path components */
478
59.6k
    H5WB_t       *wb        = NULL;       /* Wrapped buffer for temporary buffer */
479
59.6k
    bool          last_comp = false; /* Flag to indicate that a component is the last component in the name */
480
59.6k
    herr_t        ret_value = SUCCEED; /* Return value */
481
482
59.6k
    FUNC_ENTER_PACKAGE
483
484
    /* Check parameters */
485
59.6k
    assert(_loc);
486
59.6k
    assert(name);
487
59.6k
    assert(op);
488
489
    /*
490
     * Where does the searching start?  For absolute names it starts at the
491
     * root of the file; for relative names it starts at CWG.
492
     */
493
    /* Check if we need to get the root group's entry */
494
59.6k
    if ('/' == *name) {
495
3
        H5G_t *root_grp; /* Temporary pointer to root group of file */
496
497
        /* Look up root group for starting location */
498
3
        root_grp = H5G_rootof(_loc->oloc->file);
499
3
        assert(root_grp);
500
501
        /* Set the location entry to the root group's info */
502
3
        loc.oloc = &(root_grp->oloc);
503
3
        loc.path = &(root_grp->path);
504
3
    } /* end if */
505
59.6k
    else {
506
59.6k
        loc.oloc = _loc->oloc;
507
59.6k
        loc.path = _loc->path;
508
59.6k
    } /* end else */
509
510
    /* Set up group & object locations */
511
59.6k
    grp_loc.oloc = &grp_oloc;
512
59.6k
    grp_loc.path = &grp_path;
513
59.6k
    obj_loc.oloc = &obj_oloc;
514
59.6k
    obj_loc.path = &obj_path;
515
516
#if defined(H5_USING_MEMCHECKER) || !defined(NDEBUG)
517
    /* Clear group location */
518
    if (H5G_loc_reset(&grp_loc) < 0)
519
        HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to reset location");
520
#endif /* H5_USING_MEMCHECKER */
521
522
    /* Deep copy of the starting location to group location */
523
59.6k
    if (H5G_loc_copy(&grp_loc, &loc, H5_COPY_DEEP) < 0)
524
0
        HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to copy location");
525
59.6k
    group_copy = true;
526
527
    /* Clear object location */
528
59.6k
    if (H5G_loc_reset(&obj_loc) < 0)
529
0
        HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to reset location");
530
531
    /* Wrap the local buffer for serialized header info */
532
59.6k
    if (NULL == (wb = H5WB_wrap(comp_buf, sizeof(comp_buf))))
533
0
        HGOTO_ERROR(H5E_SYM, H5E_CANTINIT, FAIL, "can't wrap buffer");
534
535
    /* Get a pointer to a buffer that's large enough  */
536
59.6k
    if (NULL == (comp = (char *)H5WB_actual(wb, (strlen(name) + 1))))
537
0
        HGOTO_ERROR(H5E_SYM, H5E_NOSPACE, FAIL, "can't get actual buffer");
538
539
    /* Traverse the path */
540
96.2k
    while ((name = H5G__component(name, &nchars)) && *name) {
541
59.0k
        const char *s;             /* Temporary string pointer */
542
59.0k
        bool        lookup_status; /* Status from object lookup */
543
59.0k
        bool        obj_exists;    /* Whether the object exists */
544
545
        /*
546
         * Copy the component name into a null-terminated buffer so
547
         * we can pass it down to the other symbol table functions.
548
         */
549
59.0k
        H5MM_memcpy(comp, name, nchars);
550
59.0k
        comp[nchars] = '\0';
551
552
        /*
553
         * The special name `.' is a no-op.
554
         */
555
59.0k
        if ('.' == comp[0] && !comp[1]) {
556
36.6k
            name += nchars;
557
36.6k
            continue;
558
36.6k
        } /* end if */
559
560
        /* Check if this is the last component of the name */
561
22.4k
        if (!((s = H5G__component(name + nchars, NULL)) && *s))
562
22.4k
            last_comp = true;
563
564
        /* If there's valid information in the link, reset it */
565
22.4k
        if (link_valid) {
566
0
            H5O_msg_reset(H5O_LINK_ID, &lnk);
567
0
            link_valid = false;
568
0
        } /* end if */
569
570
        /* Get information for object in current group */
571
22.4k
        lookup_status = false;
572
22.4k
        if (H5G__obj_lookup(grp_loc.oloc, comp, &lookup_status, &lnk /*out*/) < 0)
573
160
            HGOTO_ERROR(H5E_SYM, H5E_NOTFOUND, FAIL, "can't look up component");
574
22.2k
        obj_exists = false;
575
576
        /* If the lookup was OK, build object location and traverse special links, etc. */
577
22.2k
        if (lookup_status) {
578
            /* Sanity check link and indicate it's valid */
579
22.1k
            assert(lnk.type >= H5L_TYPE_HARD);
580
22.1k
            assert(!strcmp(comp, lnk.name));
581
22.1k
            link_valid = true;
582
583
            /* Build object location from the link */
584
22.1k
            if (H5G__link_to_loc(&grp_loc, &lnk, &obj_loc) < 0)
585
0
                HGOTO_ERROR(H5E_SYM, H5E_CANTINIT, FAIL, "cannot initialize object location");
586
22.1k
            obj_loc_valid = true;
587
588
            /* Assume object exists */
589
22.1k
            obj_exists = true;
590
591
            /* Perform any special traversals that the link needs */
592
            /* (soft links, user-defined links, file mounting, etc.) */
593
22.1k
            if (H5G__traverse_special(&grp_loc, &lnk, target, last_comp, &obj_loc, &obj_exists) < 0)
594
1
                HGOTO_ERROR(H5E_LINK, H5E_TRAVERSE, FAIL, "special link traversal failed");
595
22.1k
        } /* end if */
596
597
        /* Check for last component in name provided */
598
22.2k
        if (last_comp) {
599
22.2k
            H5O_link_t *cb_lnk; /* Pointer to link info for callback */
600
22.2k
            H5G_loc_t  *cb_loc; /* Pointer to object location for callback */
601
602
            /* Set callback parameters appropriately, based on link being found */
603
22.2k
            if (lookup_status) {
604
22.1k
                cb_lnk = &lnk;
605
22.1k
                if (obj_exists)
606
22.1k
                    cb_loc = &obj_loc;
607
0
                else
608
0
                    cb_loc = NULL;
609
22.1k
            } /* end if */
610
89
            else {
611
89
                assert(!obj_loc_valid);
612
89
                cb_lnk = NULL;
613
89
                cb_loc = NULL;
614
89
            } /* end else */
615
616
            /* Call 'operator' routine */
617
22.2k
            if ((op)(&grp_loc, comp, cb_lnk, cb_loc, op_data, &own_loc) < 0)
618
486
                HGOTO_ERROR(H5E_SYM, H5E_CALLBACK, FAIL, "traversal operator failed");
619
620
21.8k
            HGOTO_DONE(SUCCEED);
621
21.8k
        } /* end if */
622
623
        /* Handle lookup failures now */
624
0
        if (!lookup_status) {
625
            /* If an intermediate group doesn't exist & flag is set, create the group */
626
0
            if (target & H5G_CRT_INTMD_GROUP) {
627
0
                const H5O_ginfo_t  def_ginfo = H5G_CRT_GROUP_INFO_DEF; /* Default group info settings */
628
0
                const H5O_linfo_t  def_linfo = H5G_CRT_LINK_INFO_DEF;  /* Default link info settings */
629
0
                const H5O_pline_t  def_pline = H5O_CRT_PIPELINE_DEF;   /* Default filter pipeline settings */
630
0
                H5O_ginfo_t        par_ginfo; /* Group info settings for parent group */
631
0
                H5O_linfo_t        par_linfo; /* Link info settings for parent group */
632
0
                H5O_pline_t        par_pline; /* Filter pipeline settings for parent group */
633
0
                H5O_linfo_t        tmp_linfo; /* Temporary link info settings */
634
0
                htri_t             exists;    /* Whether a group or link info message exists */
635
0
                const H5O_ginfo_t *ginfo;     /* Group info settings for new group */
636
0
                const H5O_linfo_t *linfo;     /* Link info settings for new group */
637
0
                const H5O_pline_t *pline;     /* Filter pipeline settings for new group */
638
0
                H5G_obj_create_t   gcrt_info; /* Group creation info */
639
0
                H5O_obj_create_t  *ocrt_info; /* Object creation info in op_data */
640
641
                /* Check for the parent group having a group info message */
642
                /* (OK if not found) */
643
0
                if ((exists = H5O_msg_exists(grp_loc.oloc, H5O_GINFO_ID)) < 0)
644
0
                    HGOTO_ERROR(H5E_SYM, H5E_CANTGET, FAIL, "unable to read object header");
645
0
                if (exists) {
646
                    /* Get the group info for parent group */
647
0
                    if (NULL == H5O_msg_read(grp_loc.oloc, H5O_GINFO_ID, &par_ginfo))
648
0
                        HGOTO_ERROR(H5E_SYM, H5E_CANTGET, FAIL, "group info message not present");
649
650
                    /* Use parent group info settings */
651
0
                    ginfo = &par_ginfo;
652
0
                } /* end if */
653
0
                else
654
                    /* Use default group info settings */
655
0
                    ginfo = &def_ginfo;
656
657
                /* Check for the parent group having a link info message */
658
                /* (OK if not found) */
659
                /* Get the link info for parent group */
660
0
                if ((exists = H5G__obj_get_linfo(grp_loc.oloc, &par_linfo)) < 0)
661
0
                    HGOTO_ERROR(H5E_SYM, H5E_CANTGET, FAIL, "unable to read object header");
662
0
                if (exists) {
663
                    /* Only keep the creation order information from the parent
664
                     *  group's link info
665
                     */
666
0
                    H5MM_memcpy(&tmp_linfo, &def_linfo, sizeof(H5O_linfo_t));
667
0
                    tmp_linfo.track_corder = par_linfo.track_corder;
668
0
                    tmp_linfo.index_corder = par_linfo.index_corder;
669
0
                    linfo                  = &tmp_linfo;
670
0
                } /* end if */
671
0
                else
672
                    /* Use default link info settings */
673
0
                    linfo = &def_linfo;
674
675
                /* Check for the parent group having a filter pipeline message */
676
                /* (OK if not found) */
677
0
                if ((exists = H5O_msg_exists(grp_loc.oloc, H5O_PLINE_ID)) < 0)
678
0
                    HGOTO_ERROR(H5E_SYM, H5E_CANTGET, FAIL, "unable to read object header");
679
0
                if (exists) {
680
                    /* Get the filter pipeline for parent group */
681
0
                    if (NULL == H5O_msg_read(grp_loc.oloc, H5O_PLINE_ID, &par_pline))
682
0
                        HGOTO_ERROR(H5E_SYM, H5E_CANTGET, FAIL, "filter pipeline message not present");
683
684
                    /* Use parent filter pipeline settings */
685
0
                    pline = &par_pline;
686
0
                } /* end if */
687
0
                else
688
                    /* Use default filter pipeline settings */
689
0
                    pline = &def_pline;
690
691
                /* Create the intermediate group */
692
0
                gcrt_info.gcpl_id = H5P_GROUP_CREATE_DEFAULT;
693
                /* Propagate the object creation properties when creating intermedidate groups */
694
0
                if ((target & H5G_CRT_OBJ) && (ocrt_info = H5L_OCRT_INFO(op_data)) != NULL) {
695
0
                    if (ocrt_info->obj_type == H5O_TYPE_GROUP)
696
0
                        gcrt_info.gcpl_id = H5G_OBJ_ID(ocrt_info->crt_info);
697
0
                    else if (ocrt_info->obj_type == H5O_TYPE_DATASET)
698
0
                        gcrt_info.gcpl_id = H5D_OBJ_ID(ocrt_info->crt_info);
699
0
                }
700
701
0
                gcrt_info.cache_type = H5G_NOTHING_CACHED;
702
0
                memset(&gcrt_info.cache, 0, sizeof(gcrt_info.cache));
703
0
                if (H5G__obj_create_real(grp_oloc.file, ginfo, linfo, pline, &gcrt_info,
704
0
                                         obj_loc.oloc /*out*/) < 0)
705
0
                    HGOTO_ERROR(H5E_SYM, H5E_CANTINIT, FAIL, "unable to create group entry");
706
707
                /* Insert new group into current group's symbol table */
708
0
                if (H5G__loc_insert(&grp_loc, comp, &obj_loc, H5O_TYPE_GROUP, &gcrt_info) < 0)
709
0
                    HGOTO_ERROR(H5E_SYM, H5E_CANTINSERT, FAIL, "unable to insert intermediate group");
710
711
                /* Decrement refcount on intermediate group's object header in memory */
712
0
                if (H5O_dec_rc_by_loc(obj_loc.oloc) < 0)
713
0
                    HGOTO_ERROR(H5E_SYM, H5E_CANTDEC, FAIL,
714
0
                                "unable to decrement refcount on newly created object");
715
716
                /* Close new group */
717
0
                if (H5O_close(obj_loc.oloc, NULL) < 0)
718
0
                    HGOTO_ERROR(H5E_SYM, H5E_CANTINIT, FAIL, "unable to close");
719
720
                /* If the parent group was holding the file open, the
721
                 * newly-created group should, as well.
722
                 */
723
0
                if (grp_loc.oloc->holding_file)
724
0
                    if (H5O_loc_hold_file(obj_loc.oloc) < 0)
725
0
                        HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to hold file open");
726
727
                /* Reset any non-default object header messages */
728
0
                H5_WARN_CAST_AWAY_CONST_OFF
729
0
                if (ginfo != &def_ginfo)
730
0
                    if (H5O_msg_reset(H5O_GINFO_ID, (void *)ginfo) < 0)
731
0
                        HGOTO_ERROR(H5E_SYM, H5E_CANTRELEASE, FAIL, "unable to reset group info message");
732
0
                if (linfo != &def_linfo)
733
0
                    if (H5O_msg_reset(H5O_LINFO_ID, (void *)linfo) < 0)
734
0
                        HGOTO_ERROR(H5E_SYM, H5E_CANTRELEASE, FAIL, "unable to reset link info message");
735
0
                if (pline != &def_pline)
736
0
                    if (H5O_msg_reset(H5O_PLINE_ID, (void *)pline) < 0)
737
0
                        HGOTO_ERROR(H5E_SYM, H5E_CANTRELEASE, FAIL, "unable to reset I/O pipeline message");
738
0
                H5_WARN_CAST_AWAY_CONST_ON
739
0
            } /* end if */
740
0
            else
741
0
                HGOTO_ERROR(H5E_SYM, H5E_NOTFOUND, FAIL, "component not found");
742
0
        } /* end if */
743
744
        /*
745
         * Advance to the next component of the path.
746
         */
747
748
        /* Transfer "ownership" of the object's information to the group object */
749
0
        H5G_loc_free(&grp_loc);
750
0
        H5G_loc_copy(&grp_loc, &obj_loc, H5_COPY_SHALLOW);
751
0
        H5G_loc_reset(&obj_loc);
752
0
        obj_loc_valid = false;
753
754
        /* Advance to next component in string */
755
0
        name += nchars;
756
0
    } /* end while */
757
758
    /* Call 'operator' routine */
759
    /* If we've fallen through to here, the name must be something like just '.'
760
     * and we should issue the callback on that. -QAK
761
     * Since we don't have a group location or a link to the object we pass in
762
     * NULL.
763
     */
764
59.6k
    assert(group_copy);
765
37.2k
    if ((op)(NULL, ".", NULL, &grp_loc, op_data, &own_loc) < 0)
766
0
        HGOTO_ERROR(H5E_SYM, H5E_CANTNEXT, FAIL, "traversal operator failed");
767
768
    /* If the callback took ownership of the object location, it actually has
769
     * ownership of grp_loc.  It shouldn't have tried to take ownership of
770
     * the "group location", which was NULL. */
771
37.2k
    assert(!(own_loc & H5G_OWN_GRP_LOC));
772
37.2k
    if (own_loc & H5G_OWN_OBJ_LOC)
773
36.5k
        own_loc |= H5G_OWN_GRP_LOC;
774
775
59.6k
done:
776
    /* If there's been an error, the callback doesn't really get ownership of
777
     * any location and we should close them both */
778
59.6k
    if (ret_value < 0)
779
647
        own_loc = H5G_OWN_NONE;
780
781
    /* Free all open locations.  This also closes any open external files. */
782
59.6k
    if (obj_loc_valid && !(own_loc & H5G_OWN_OBJ_LOC))
783
11.8k
        H5G_loc_free(&obj_loc);
784
59.6k
    if (group_copy && !(own_loc & H5G_OWN_GRP_LOC))
785
23.1k
        H5G_loc_free(&grp_loc);
786
787
    /* If there's valid information in the link, reset it */
788
59.6k
    if (link_valid)
789
22.1k
        if (H5O_msg_reset(H5O_LINK_ID, &lnk) < 0)
790
0
            HDONE_ERROR(H5E_SYM, H5E_CANTRELEASE, FAIL, "unable to reset link message");
791
792
    /* Release temporary component buffer */
793
59.6k
    if (wb && H5WB_unwrap(wb) < 0)
794
0
        HDONE_ERROR(H5E_SYM, H5E_CANTRELEASE, FAIL, "can't release wrapped buffer");
795
796
59.6k
    FUNC_LEAVE_NOAPI(ret_value)
797
59.6k
} /* end H5G__traverse_real() */
798
799
/*-------------------------------------------------------------------------
800
 * Function:  H5G_traverse
801
 *
802
 * Purpose: Traverse a path from a location & perform an operation when
803
 *              the last component of the name is reached.
804
 *
805
 * Return:  Success:  Non-negative if path can be fully traversed.
806
 *    Failure:  Negative if the path could not be fully
807
 *        traversed.
808
 *
809
 *-------------------------------------------------------------------------
810
 */
811
herr_t
812
H5G_traverse(const H5G_loc_t *loc, const char *name, unsigned target, H5G_traverse_t op, void *op_data)
813
59.0k
{
814
59.0k
    size_t orig_nlinks;         /* Original value for # of soft / UD links able to traverse */
815
59.0k
    herr_t ret_value = SUCCEED; /* Return value */
816
817
59.0k
    FUNC_ENTER_NOAPI(FAIL)
818
819
    /* Check args */
820
59.0k
    if (!name || !*name)
821
0
        HGOTO_ERROR(H5E_SYM, H5E_NOTFOUND, FAIL, "no name given");
822
59.0k
    if (!loc)
823
0
        HGOTO_ERROR(H5E_SYM, H5E_NOTFOUND, FAIL, "no starting location");
824
59.0k
    if (!op)
825
0
        HGOTO_ERROR(H5E_SYM, H5E_NOTFOUND, FAIL, "no operation provided");
826
827
    /* Retrieve the original # of soft / UD links that are able to be traversed
828
     * (So that multiple calls to H5G_traverse don't incorrectly look
829
     *  like they've traversed too many.  Nested calls, like in H5L__move(),
830
     *  may need their own mechanism to set & reset the # of links to traverse)
831
     */
832
59.0k
    if (H5CX_get_nlinks(&orig_nlinks) < 0)
833
0
        HGOTO_ERROR(H5E_SYM, H5E_CANTGET, FAIL, "unable to retrieve # of soft / UD links to traverse");
834
835
    /* Set up invalid tag. This is a precautionary step only. Setting an invalid
836
     * tag here will ensure that no metadata accessed while doing the traversal
837
     * is given an improper tag, unless another one is specifically set up
838
     * first. This will ensure we're not accidentally tagging something we
839
     * shouldn't be during the traversal. Note that for best tagging assertion
840
     * coverage, setting H5C_DO_TAGGING_SANITY_CHECKS is advised.
841
     */
842
59.0k
    H5_BEGIN_TAG(H5AC__INVALID_TAG)
843
844
    /* Go perform "real" traversal */
845
59.0k
    if (H5G__traverse_real(loc, name, target, op, op_data) < 0)
846
646
        HGOTO_ERROR_TAG(H5E_SYM, H5E_NOTFOUND, FAIL, "internal path traversal failed");
847
848
    /* Reset tag after traversal */
849
58.4k
    H5_END_TAG
850
851
    /* Reset the # of soft / UD links that can be traversed */
852
58.4k
    if (H5CX_set_nlinks(orig_nlinks) < 0)
853
0
        HGOTO_ERROR(H5E_SYM, H5E_CANTSET, FAIL, "can't reset # of soft / UD links to traverse");
854
855
59.0k
done:
856
59.0k
    FUNC_LEAVE_NOAPI(ret_value)
857
59.0k
} /* end H5G_traverse() */