Coverage Report

Created: 2026-11-22 16:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hdf5/src/H5Doh.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
/* Module Setup */
15
/****************/
16
17
#include "H5Dmodule.h" /* This source code file is part of the H5D module */
18
#define H5O_FRIEND     /*suppress error about including H5Opkg    */
19
20
/***********/
21
/* Headers */
22
/***********/
23
#include "H5private.h"   /* Generic Functions     */
24
#include "H5CXprivate.h" /* API Contexts                         */
25
#include "H5Dpkg.h"      /* Datasets        */
26
#include "H5Eprivate.h"  /* Error handling        */
27
#include "H5FLprivate.h" /* Free lists                           */
28
#include "H5Iprivate.h"  /* IDs           */
29
#include "H5Opkg.h"      /* Object headers      */
30
#include "H5VLprivate.h" /* Virtual Object Layer                     */
31
32
/****************/
33
/* Local Macros */
34
/****************/
35
36
/******************/
37
/* Local Typedefs */
38
/******************/
39
40
/********************/
41
/* Local Prototypes */
42
/********************/
43
static void      *H5O__dset_get_copy_file_udata(void);
44
static void       H5O__dset_free_copy_file_udata(void *);
45
static htri_t     H5O__dset_isa(const H5O_t *loc);
46
static void      *H5O__dset_open(const H5G_loc_t *obj_loc, H5I_type_t *opened_type);
47
static void      *H5O__dset_create(H5F_t *f, void *_crt_info, H5G_loc_t *obj_loc);
48
static H5O_loc_t *H5O__dset_get_oloc(hid_t obj_id);
49
static herr_t     H5O__dset_bh_info(const H5O_loc_t *loc, H5O_t *oh, H5_ih_info_t *bh_info);
50
static herr_t     H5O__dset_flush(void *_obj_ptr);
51
52
/*********************/
53
/* Package Variables */
54
/*********************/
55
56
/*****************************/
57
/* Library Private Variables */
58
/*****************************/
59
60
/*******************/
61
/* Local Variables */
62
/*******************/
63
64
/* This message derives from H5O object class */
65
const H5O_obj_class_t H5O_OBJ_DATASET[1] = {{
66
    H5O_TYPE_DATASET,               /* object type      */
67
    "dataset",                      /* object name, for debugging */
68
    H5O__dset_get_copy_file_udata,  /* get 'copy file' user data  */
69
    H5O__dset_free_copy_file_udata, /* free 'copy file' user data */
70
    H5O__dset_isa,                  /* "isa" message    */
71
    H5O__dset_open,                 /* open an object of this class */
72
    H5O__dset_create,               /* create an object of this class */
73
    H5O__dset_get_oloc,             /* get an object header location for an object */
74
    H5O__dset_bh_info,              /* get the index & heap info for an object */
75
    H5O__dset_flush                 /* flush an opened object of this class */
76
}};
77
78
/* Declare a free list to manage the H5D_copy_file_ud_t struct */
79
H5FL_DEFINE(H5D_copy_file_ud_t);
80
81
/*-------------------------------------------------------------------------
82
 * Function:  H5O__dset_get_copy_file_udata
83
 *
84
 * Purpose: Allocates the user data needed for copying a dataset's
85
 *    object header from file to file.
86
 *
87
 * Return:  Success:  Non-NULL pointer to user data
88
 *
89
 *    Failure:  NULL
90
 *
91
 *-------------------------------------------------------------------------
92
 */
93
static void *
94
H5O__dset_get_copy_file_udata(void)
95
0
{
96
0
    void *ret_value = NULL; /* Return value */
97
98
0
    FUNC_ENTER_PACKAGE
99
100
    /* Allocate space for the 'copy file' user data for copying datasets */
101
0
    if (NULL == (ret_value = H5FL_CALLOC(H5D_copy_file_ud_t)))
102
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed");
103
104
0
done:
105
0
    FUNC_LEAVE_NOAPI(ret_value)
106
0
} /* end H5O__dset_get_copy_file_udata() */
107
108
/*-------------------------------------------------------------------------
109
 * Function:  H5O__dset_free_copy_file_udata
110
 *
111
 * Purpose: Release the user data needed for copying a dataset's
112
 *    object header from file to file.
113
 *
114
 * Return:  <none>
115
 *
116
 *-------------------------------------------------------------------------
117
 */
118
static void
119
H5O__dset_free_copy_file_udata(void *_udata)
120
0
{
121
0
    H5D_copy_file_ud_t *udata = (H5D_copy_file_ud_t *)_udata;
122
123
0
    FUNC_ENTER_PACKAGE_NOERR
124
125
    /* Sanity check */
126
0
    assert(udata);
127
128
    /* Release copy of dataset's dataspace extent, if it was set */
129
0
    if (udata->src_space_extent)
130
0
        H5O_msg_free(H5O_SDSPACE_ID, udata->src_space_extent);
131
132
    /* Release copy of dataset's datatype, if it was set */
133
0
    if (udata->src_dtype)
134
0
        H5T_close_real(udata->src_dtype);
135
136
    /* Release copy of dataset's filter pipeline, if it was set */
137
0
    if (udata->common.src_pline)
138
0
        H5O_msg_free(H5O_PLINE_ID, udata->common.src_pline);
139
140
    /* Release space for 'copy file' user data */
141
0
    udata = H5FL_FREE(H5D_copy_file_ud_t, udata);
142
143
0
    FUNC_LEAVE_NOAPI_VOID
144
0
} /* end H5O__dset_free_copy_file_udata() */
145
146
/*-------------------------------------------------------------------------
147
 * Function:  H5O__dset_isa
148
 *
149
 * Purpose: Determines if an object has the requisite messages for being
150
 *    a dataset.
151
 *
152
 * Return:  Success:  true if the required dataset messages are
153
 *        present; false otherwise.
154
 *
155
 *    Failure:  FAIL if the existence of certain messages
156
 *        cannot be determined.
157
 *
158
 *-------------------------------------------------------------------------
159
 */
160
static htri_t
161
H5O__dset_isa(const H5O_t *oh)
162
18.5k
{
163
18.5k
    htri_t exists;           /* Flag if header message of interest exists */
164
18.5k
    htri_t ret_value = true; /* Return value */
165
166
18.5k
    FUNC_ENTER_PACKAGE
167
168
18.5k
    assert(oh);
169
170
    /* Datatype */
171
18.5k
    if ((exists = H5O_msg_exists_oh(oh, H5O_DTYPE_ID)) < 0)
172
0
        HGOTO_ERROR(H5E_DATASET, H5E_CANTINIT, FAIL, "unable to read object header");
173
18.5k
    else if (!exists)
174
17
        HGOTO_DONE(false);
175
176
    /* Layout */
177
18.5k
    if ((exists = H5O_msg_exists_oh(oh, H5O_SDSPACE_ID)) < 0)
178
0
        HGOTO_ERROR(H5E_DATASET, H5E_CANTINIT, FAIL, "unable to read object header");
179
18.5k
    else if (!exists)
180
44
        HGOTO_DONE(false);
181
182
18.5k
done:
183
18.5k
    FUNC_LEAVE_NOAPI(ret_value)
184
18.5k
} /* end H5O__dset_isa() */
185
186
/*-------------------------------------------------------------------------
187
 * Function:  H5O__dset_open
188
 *
189
 * Purpose: Open a dataset at a particular location
190
 *
191
 * Return:  Success:  Open object identifier
192
 *    Failure:  Negative
193
 *
194
 *-------------------------------------------------------------------------
195
 */
196
static void *
197
H5O__dset_open(const H5G_loc_t *obj_loc, H5I_type_t *opened_type)
198
745
{
199
745
    H5D_t *dset = NULL;      /* Dataset opened */
200
745
    hid_t  dapl_id;          /* dapl to use to open this dataset */
201
745
    void  *ret_value = NULL; /* Return value */
202
203
745
    FUNC_ENTER_PACKAGE
204
205
745
    assert(obj_loc);
206
207
745
    *opened_type = H5I_DATASET;
208
209
    /* Get the LAPL (which is a superclass of DAPLs) from the API context, but
210
     * if it's the default link access property list or a custom link access
211
     * property list but not a dataset access property list, use the default
212
     * dataset access property list instead.  (Since LAPLs don't have the
213
     * additional properties that DAPLs have)
214
     */
215
745
    dapl_id = H5CX_get_lapl();
216
745
    if (dapl_id == H5P_LINK_ACCESS_DEFAULT)
217
0
        dapl_id = H5P_DATASET_ACCESS_DEFAULT;
218
745
    else {
219
745
        htri_t is_lapl, is_dapl; /* Class of LAPL from API context */
220
221
        /* Check class of LAPL from API context */
222
745
        if ((is_lapl = H5P_isa_class(dapl_id, H5P_LINK_ACCESS)) < 0)
223
0
            HGOTO_ERROR(H5E_DATASET, H5E_CANTGET, NULL, "unable to get LAPL status");
224
745
        if ((is_dapl = H5P_isa_class(dapl_id, H5P_DATASET_ACCESS)) < 0)
225
0
            HGOTO_ERROR(H5E_DATASET, H5E_CANTGET, NULL, "unable to get DAPL status");
226
227
        /* Switch to default DAPL if not an actual DAPL in the API context */
228
745
        if (!is_dapl && is_lapl)
229
0
            dapl_id = H5P_DATASET_ACCESS_DEFAULT;
230
745
    } /* end else */
231
232
    /* Open the dataset */
233
745
    if (NULL == (dset = H5D_open(obj_loc, dapl_id)))
234
32
        HGOTO_ERROR(H5E_DATASET, H5E_CANTOPENOBJ, NULL, "unable to open dataset");
235
236
713
    ret_value = (void *)dset;
237
238
745
done:
239
745
    if (NULL == ret_value)
240
32
        if (dset && H5D_close(dset) < 0)
241
0
            HDONE_ERROR(H5E_DATASET, H5E_CLOSEERROR, NULL, "unable to release dataset");
242
243
745
    FUNC_LEAVE_NOAPI(ret_value)
244
745
} /* end H5O__dset_open() */
245
246
/*-------------------------------------------------------------------------
247
 * Function:  H5O__dset_create
248
 *
249
 * Purpose: Create a dataset in a file
250
 *
251
 * Return:  Success:  Pointer to the dataset data structure
252
 *    Failure:  NULL
253
 *
254
 *-------------------------------------------------------------------------
255
 */
256
static void *
257
H5O__dset_create(H5F_t *f, void *_crt_info, H5G_loc_t *obj_loc)
258
0
{
259
0
    H5D_obj_create_t *crt_info  = (H5D_obj_create_t *)_crt_info; /* Dataset creation parameters */
260
0
    H5D_t            *dset      = NULL;                          /* New dataset created */
261
0
    void             *ret_value = NULL;                          /* Return value */
262
263
0
    FUNC_ENTER_PACKAGE
264
265
    /* Sanity checks */
266
0
    assert(f);
267
0
    assert(crt_info);
268
0
    assert(obj_loc);
269
270
    /* Create the dataset */
271
0
    if (NULL ==
272
0
        (dset = H5D__create(f, crt_info->type_id, crt_info->space, crt_info->dcpl_id, crt_info->dapl_id)))
273
0
        HGOTO_ERROR(H5E_DATASET, H5E_CANTINIT, NULL, "unable to create dataset");
274
275
    /* Set up the new dataset's location */
276
0
    if (NULL == (obj_loc->oloc = H5D_oloc(dset)))
277
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "unable to get object location of dataset");
278
0
    if (NULL == (obj_loc->path = H5D_nameof(dset)))
279
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "unable to get path of dataset");
280
281
    /* Set the return value */
282
0
    ret_value = dset;
283
284
0
done:
285
0
    if (ret_value == NULL)
286
0
        if (dset && H5D_close(dset) < 0)
287
0
            HDONE_ERROR(H5E_DATASET, H5E_CLOSEERROR, NULL, "unable to release dataset");
288
289
0
    FUNC_LEAVE_NOAPI(ret_value)
290
0
} /* end H5O__dset_create() */
291
292
/*-------------------------------------------------------------------------
293
 * Function:  H5O__dset_get_oloc
294
 *
295
 * Purpose: Retrieve the object header location for an open object
296
 *
297
 * Return:  Success:  Pointer to object header location
298
 *    Failure:  NULL
299
 *
300
 *-------------------------------------------------------------------------
301
 */
302
static H5O_loc_t *
303
H5O__dset_get_oloc(hid_t obj_id)
304
0
{
305
0
    H5D_t     *dset;             /* Dataset opened */
306
0
    H5O_loc_t *ret_value = NULL; /* Return value */
307
308
0
    FUNC_ENTER_PACKAGE
309
310
    /* Get the dataset */
311
0
    if (NULL == (dset = (H5D_t *)H5VL_object(obj_id)))
312
0
        HGOTO_ERROR(H5E_OHDR, H5E_BADID, NULL, "couldn't get object from ID");
313
314
    /* Get the dataset's object header location */
315
0
    if (NULL == (ret_value = H5D_oloc(dset)))
316
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, NULL, "unable to get object location from object");
317
318
0
done:
319
0
    FUNC_LEAVE_NOAPI(ret_value)
320
0
} /* end H5O__dset_get_oloc() */
321
322
/*-------------------------------------------------------------------------
323
 * Function:    H5O__dset_bh_info
324
 *
325
 * Purpose:     Returns the amount of btree storage that is used for chunked
326
 *              dataset.
327
 *
328
 * Return:      Success:        non-negative
329
 *              Failure:        negative
330
 *
331
 *-------------------------------------------------------------------------
332
 */
333
static herr_t
334
H5O__dset_bh_info(const H5O_loc_t *loc, H5O_t *oh, H5_ih_info_t *bh_info)
335
0
{
336
0
    H5O_layout_t layout;              /* Data storage layout message */
337
0
    H5O_efl_t    efl;                 /* External File List message */
338
0
    bool         layout_read = false; /* Whether the layout message was read */
339
0
    bool         efl_read    = false; /* Whether the external file list message was read */
340
0
    htri_t       exists;              /* Flag if header message of interest exists */
341
0
    herr_t       ret_value = SUCCEED; /* Return value */
342
343
0
    FUNC_ENTER_PACKAGE
344
345
    /* Sanity check */
346
0
    assert(loc);
347
0
    assert(loc->file);
348
0
    assert(H5_addr_defined(loc->addr));
349
0
    assert(oh);
350
0
    assert(bh_info);
351
352
    /* Get the layout message from the object header */
353
0
    if (NULL == H5O_msg_read_oh(loc->file, oh, H5O_LAYOUT_ID, &layout))
354
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, FAIL, "can't find layout message");
355
0
    layout_read = true;
356
357
    /* Check for chunked dataset storage */
358
0
    if (layout.type == H5D_CHUNKED && H5D__chunk_is_space_alloc(&layout.storage)) {
359
        /* Get size of chunk index */
360
0
        if (H5D__chunk_bh_info(loc, oh, &layout, &(bh_info->index_size)) < 0)
361
0
            HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, FAIL, "can't determine chunked dataset btree info");
362
0
    } /* end if */
363
0
    else if (layout.type == H5D_VIRTUAL && (layout.storage.u.virt.serial_list_hobjid.addr != HADDR_UNDEF)) {
364
0
        size_t virtual_heap_size;
365
366
        /* Get size of global heap object for virtual dataset */
367
0
        if (H5HG_get_obj_size(loc->file, &(layout.storage.u.virt.serial_list_hobjid), &virtual_heap_size) < 0)
368
0
            HGOTO_ERROR(H5E_DATASET, H5E_CANTGET, FAIL,
369
0
                        "can't get global heap size for virtual dataset mapping");
370
371
        /* Return heap size */
372
0
        bh_info->heap_size = (hsize_t)virtual_heap_size;
373
0
    } /* end if */
374
375
    /* Check for External File List message in the object header */
376
0
    if ((exists = H5O_msg_exists_oh(oh, H5O_EFL_ID)) < 0)
377
0
        HGOTO_ERROR(H5E_OHDR, H5E_NOTFOUND, FAIL, "unable to check for EFL message");
378
379
0
    if (exists && H5D__efl_is_space_alloc(&layout.storage)) {
380
        /* Start with clean EFL info */
381
0
        memset(&efl, 0, sizeof(efl));
382
383
        /* Get External File List message from the object header */
384
0
        if (NULL == H5O_msg_read_oh(loc->file, oh, H5O_EFL_ID, &efl))
385
0
            HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, FAIL, "can't find EFL message");
386
0
        efl_read = true;
387
388
        /* Get size of local heap for EFL message's file list */
389
0
        if (H5D__efl_bh_info(loc->file, &efl, &(bh_info->heap_size)) < 0)
390
0
            HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, FAIL, "can't determine EFL heap info");
391
0
    } /* end if */
392
393
0
done:
394
    /* Free messages, if they've been read in */
395
0
    if (layout_read && H5O_msg_reset(H5O_LAYOUT_ID, &layout) < 0)
396
0
        HDONE_ERROR(H5E_DATASET, H5E_CANTRESET, FAIL, "unable to reset data storage layout message");
397
0
    if (efl_read && H5O_msg_reset(H5O_EFL_ID, &efl) < 0)
398
0
        HDONE_ERROR(H5E_DATASET, H5E_CANTRESET, FAIL, "unable to reset external file list message");
399
400
0
    FUNC_LEAVE_NOAPI(ret_value)
401
0
} /* end H5O__dset_bh_info() */
402
403
/*-------------------------------------------------------------------------
404
 * Function:    H5O__dset_flush
405
 *
406
 * Purpose:     To flush any dataset information cached in memory
407
 *
408
 * Return:      Success:        non-negative
409
 *              Failure:        negative
410
 *
411
 *-------------------------------------------------------------------------
412
 */
413
static herr_t
414
H5O__dset_flush(void *_obj_ptr)
415
0
{
416
0
    H5D_t     *dset = (H5D_t *)_obj_ptr; /* Pointer to dataset object */
417
0
    H5O_type_t obj_type;                 /* Type of object at location */
418
0
    herr_t     ret_value = SUCCEED;      /* Return value */
419
420
0
    FUNC_ENTER_PACKAGE
421
422
0
    assert(dset);
423
0
    assert(&dset->oloc);
424
425
    /* Check that the object found is the correct type */
426
0
    if (H5O_obj_type(&dset->oloc, &obj_type) < 0)
427
0
        HGOTO_ERROR(H5E_DATASET, H5E_CANTGET, FAIL, "can't get object type");
428
0
    if (obj_type != H5O_TYPE_DATASET)
429
0
        HGOTO_ERROR(H5E_DATASET, H5E_BADTYPE, FAIL, "not a dataset");
430
431
0
    if (H5D__flush_real(dset) < 0)
432
0
        HDONE_ERROR(H5E_DATASET, H5E_WRITEERROR, FAIL, "unable to flush cached dataset info");
433
434
0
done:
435
0
    FUNC_LEAVE_NOAPI(ret_value)
436
0
} /* end H5O__dset_flush() */