Coverage Report

Created: 2026-06-08 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hdf5/src/H5Oattribute.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:     H5Oattribute.c
16
 *
17
 * Purpose:     Object header attribute routines.
18
 *
19
 *-------------------------------------------------------------------------
20
 */
21
22
/****************/
23
/* Module Setup */
24
/****************/
25
26
#define H5A_FRIEND     /* Suppress error about including H5Apkg.h */
27
#include "H5Omodule.h" /* This source code file is part of the H5O module */
28
29
/***********/
30
/* Headers */
31
/***********/
32
#include "H5private.h"   /* Generic Functions                        */
33
#include "H5Apkg.h"      /* Attributes                               */
34
#include "H5Eprivate.h"  /* Error handling                           */
35
#include "H5MMprivate.h" /* Memory management                        */
36
#include "H5Opkg.h"      /* Object headers                           */
37
#include "H5SMprivate.h" /* Shared Object Header Messages            */
38
#include "H5Iprivate.h"  /* IDs                                      */
39
#include "H5Fprivate.h"  /* File                                     */
40
41
/****************/
42
/* Local Macros */
43
/****************/
44
45
/******************/
46
/* Local Typedefs */
47
/******************/
48
49
/* User data for iteration when converting attributes to dense storage */
50
typedef struct {
51
    H5F_t       *f;     /* Pointer to file for insertion */
52
    H5O_ainfo_t *ainfo; /* Attribute info struct */
53
} H5O_iter_cvt_t;
54
55
/* User data for iteration when opening an attribute */
56
typedef struct {
57
    /* down */
58
    const char *name; /* Name of attribute to open */
59
60
    /* up */
61
    H5A_t *attr; /* Attribute data to update object header with */
62
} H5O_iter_opn_t;
63
64
/* User data for iteration when updating an attribute */
65
typedef struct {
66
    /* down */
67
    H5F_t *f;    /* Pointer to file attribute is in */
68
    H5A_t *attr; /* Attribute data to update object header with */
69
70
    /* up */
71
    bool found; /* Whether the attribute was found */
72
} H5O_iter_wrt_t;
73
74
/* User data for iteration when renaming an attribute */
75
typedef struct {
76
    /* down */
77
    H5F_t      *f;        /* Pointer to file attribute is in */
78
    const char *old_name; /* Old name of attribute */
79
    const char *new_name; /* New name of attribute */
80
81
    /* up */
82
    bool found; /* Whether the attribute was found */
83
} H5O_iter_ren_t;
84
85
/* User data for iteration when removing an attribute */
86
typedef struct {
87
    /* down */
88
    H5F_t      *f;    /* Pointer to file attribute is in */
89
    const char *name; /* Name of attribute to open */
90
91
    /* up */
92
    bool found; /* Found attribute to delete */
93
} H5O_iter_rm_t;
94
95
/* User data for iteration when checking if an attribute exists */
96
typedef struct {
97
    /* down */
98
    const char *name; /* Name of attribute to open */
99
100
    /* up */
101
    bool *exists; /* Pointer to flag to indicate attribute exists */
102
} H5O_iter_xst_t;
103
104
/********************/
105
/* Package Typedefs */
106
/********************/
107
108
/********************/
109
/* Local Prototypes */
110
/********************/
111
static herr_t H5O__attr_to_dense_cb(H5O_t *oh, H5O_mesg_t *mesg, unsigned H5_ATTR_UNUSED sequence,
112
                                    void *_udata);
113
static htri_t H5O__attr_find_opened_attr(const H5O_loc_t *loc, H5A_t **attr, const char *name_to_open);
114
static herr_t H5O__attr_open_cb(H5O_t *oh, H5O_mesg_t *mesg, unsigned sequence, void *_udata);
115
static herr_t H5O__attr_open_by_idx_cb(const H5A_t *attr, void *_ret_attr);
116
static herr_t H5O__attr_write_cb(H5O_t *oh, H5O_mesg_t *mesg, unsigned H5_ATTR_UNUSED sequence, void *_udata);
117
static herr_t H5O__attr_rename_chk_cb(H5O_t H5_ATTR_UNUSED *oh, H5O_mesg_t *mesg,
118
                                      unsigned H5_ATTR_UNUSED sequence, void *_udata);
119
static herr_t H5O__attr_rename_mod_cb(H5O_t *oh, H5O_mesg_t *mesg, unsigned H5_ATTR_UNUSED sequence,
120
                                      void *_udata);
121
static herr_t H5O__attr_remove_update(const H5O_loc_t *loc, H5O_t *oh, H5O_ainfo_t *ainfo);
122
static herr_t H5O__attr_remove_cb(H5O_t *oh, H5O_mesg_t *mesg, unsigned H5_ATTR_UNUSED sequence,
123
                                  void *_udata);
124
static herr_t H5O__attr_exists_cb(H5O_t H5_ATTR_UNUSED *oh, H5O_mesg_t *mesg,
125
                                  unsigned H5_ATTR_UNUSED sequence, void *_udata);
126
127
/*********************/
128
/* Package Variables */
129
/*********************/
130
131
/*****************************/
132
/* Library Private Variables */
133
/*****************************/
134
135
/*******************/
136
/* Local Variables */
137
/*******************/
138
139
/*-------------------------------------------------------------------------
140
 * Function:    H5O__attr_to_dense_cb
141
 *
142
 * Purpose:     Object header iterator callback routine to convert compact
143
 *              attributes to dense attributes
144
 *
145
 * Return:      SUCCEED/FAIL
146
 *
147
 *-------------------------------------------------------------------------
148
 */
149
static herr_t
150
H5O__attr_to_dense_cb(H5O_t *oh, H5O_mesg_t *mesg /*in,out*/, unsigned H5_ATTR_UNUSED sequence,
151
                      void *_udata /*in,out*/)
152
0
{
153
0
    H5O_iter_cvt_t *udata     = (H5O_iter_cvt_t *)_udata; /* Operator user data */
154
0
    H5A_t          *attr      = (H5A_t *)mesg->native;    /* Pointer to attribute to insert */
155
0
    herr_t          ret_value = H5_ITER_CONT;             /* Return value */
156
157
0
    FUNC_ENTER_PACKAGE
158
159
    /* check args */
160
0
    assert(oh);
161
0
    assert(mesg);
162
0
    assert(udata);
163
0
    assert(udata->f);
164
0
    assert(udata->ainfo);
165
0
    assert(attr);
166
167
    /* Insert attribute into dense storage */
168
0
    if (H5A__dense_insert(udata->f, udata->ainfo, attr) < 0)
169
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTINSERT, H5_ITER_ERROR, "unable to add to dense storage");
170
171
    /* Convert message into a null message in the header */
172
    /* (don't delete attribute's space in the file though) */
173
0
    if (H5O__release_mesg(udata->f, oh, mesg, false) < 0)
174
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTDELETE, H5_ITER_ERROR, "unable to convert into null message");
175
176
    /* Indicate that the object header was modified */
177
0
    oh->mesgs_modified = H5O_MODIFY_CONDENSE;
178
179
0
done:
180
0
    FUNC_LEAVE_NOAPI(ret_value)
181
0
} /* end H5O__attr_to_dense_cb() */
182
183
/*-------------------------------------------------------------------------
184
 * Function:    H5O__attr_create
185
 *
186
 * Purpose:     Create a new attribute in the object header.
187
 *
188
 * Return:      SUCCEED/FAIL
189
 *
190
 *-------------------------------------------------------------------------
191
 */
192
herr_t
193
H5O__attr_create(const H5O_loc_t *loc, H5A_t *attr)
194
0
{
195
0
    H5O_t      *oh = NULL;           /* Pointer to actual object header */
196
0
    H5O_ainfo_t ainfo;               /* Attribute information for object */
197
0
    htri_t      shared_mesg;         /* Should this message be stored in the Shared Message table? */
198
0
    herr_t      ret_value = SUCCEED; /* Return value */
199
200
0
    FUNC_ENTER_PACKAGE
201
202
    /* Check arguments */
203
0
    assert(loc);
204
0
    assert(attr);
205
206
    /* Pin the object header */
207
0
    if (NULL == (oh = H5O_pin(loc)))
208
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTPIN, FAIL, "unable to pin object header");
209
210
    /* Check for creating attribute with unusual datatype */
211
0
    if (!(H5O_has_chksum(oh) || (H5F_RFIC_FLAGS(loc->file) & H5F_RFIC_UNUSUAL_NUM_UNUSED_NUMERIC_BITS)) &&
212
0
        H5T_is_numeric_with_unusual_unused_bits(attr->shared->dt))
213
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, FAIL,
214
0
                    "creating attribute with unusual datatype, see documentation for "
215
0
                    "H5Pset_relax_file_integrity_checks for details.");
216
217
    /* Check if this object already has attribute information */
218
0
    if (oh->version > H5O_VERSION_1) {
219
0
        bool   new_ainfo = false; /* Flag to indicate that the attribute information is new */
220
0
        htri_t ainfo_exists;      /* Whether the attribute info was retrieved */
221
222
        /* Check for (& retrieve if available) attribute info */
223
0
        if ((ainfo_exists = H5A__get_ainfo(loc->file, oh, &ainfo)) < 0)
224
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't check for attribute info message");
225
0
        if (!ainfo_exists) {
226
            /* Initialize attribute information */
227
0
            ainfo.track_corder    = (bool)((oh->flags & H5O_HDR_ATTR_CRT_ORDER_TRACKED) ? true : false);
228
0
            ainfo.index_corder    = (bool)((oh->flags & H5O_HDR_ATTR_CRT_ORDER_INDEXED) ? true : false);
229
0
            ainfo.max_crt_idx     = 0;
230
0
            ainfo.corder_bt2_addr = HADDR_UNDEF;
231
0
            ainfo.nattrs          = 0;
232
0
            ainfo.fheap_addr      = HADDR_UNDEF;
233
0
            ainfo.name_bt2_addr   = HADDR_UNDEF;
234
235
            /* Set flag to add attribute information to object header */
236
0
            new_ainfo = true;
237
0
        } /* end if */
238
0
        else {
239
            /* Sanity check attribute info read in */
240
0
            assert(ainfo.nattrs > 0);
241
0
            assert(ainfo.track_corder == ((oh->flags & H5O_HDR_ATTR_CRT_ORDER_TRACKED) > 0));
242
0
            assert(ainfo.index_corder == ((oh->flags & H5O_HDR_ATTR_CRT_ORDER_INDEXED) > 0));
243
0
        } /* end else */
244
245
        /* Check if switching to "dense" attribute storage is possible */
246
0
        if (!H5_addr_defined(ainfo.fheap_addr)) {
247
0
            htri_t shareable;    /* Whether the attribute will be shared */
248
0
            size_t raw_size = 0; /* Raw size of message */
249
250
            /* Check for attribute being shareable */
251
0
            if ((shareable = H5SM_can_share(loc->file, NULL, NULL, H5O_ATTR_ID, attr)) < 0)
252
0
                HGOTO_ERROR(H5E_ATTR, H5E_BADMESG, FAIL, "can't determine attribute sharing status");
253
0
            else if (shareable == false) {
254
                /* Compute the size needed to encode the attribute */
255
0
                raw_size = (H5O_MSG_ATTR->raw_size)(loc->file, false, attr);
256
0
            } /* end if */
257
258
            /* Check for condititions for switching to "dense" attribute storage are met */
259
0
            if (ainfo.nattrs == oh->max_compact || (!shareable && raw_size >= H5O_MESG_MAX_SIZE)) {
260
0
                H5O_iter_cvt_t      udata; /* User data for callback */
261
0
                H5O_mesg_operator_t op;    /* Wrapper for operator */
262
263
                /* Create dense storage for attributes */
264
0
                if (H5A__dense_create(loc->file, &ainfo) < 0)
265
0
                    HGOTO_ERROR(H5E_OHDR, H5E_CANTINIT, FAIL,
266
0
                                "unable to create dense storage for attributes");
267
268
                /* Set up user data for callback */
269
0
                udata.f     = loc->file;
270
0
                udata.ainfo = &ainfo;
271
272
                /* Iterate over existing attributes, moving them to dense storage */
273
0
                op.op_type  = H5O_MESG_OP_LIB;
274
0
                op.u.lib_op = H5O__attr_to_dense_cb;
275
0
                if (H5O__msg_iterate_real(loc->file, oh, H5O_MSG_ATTR, &op, &udata) < 0)
276
0
                    HGOTO_ERROR(H5E_ATTR, H5E_CANTCONVERT, FAIL,
277
0
                                "error converting attributes to dense storage");
278
0
            } /* end if */
279
0
        }     /* end if */
280
281
        /* Increment attribute count on object */
282
0
        ainfo.nattrs++;
283
284
        /* Check whether we're tracking the creation index on attributes */
285
0
        if (ainfo.track_corder) {
286
            /* Check for attribute creation order index on the object wrapping around */
287
0
            if (ainfo.max_crt_idx == H5O_MAX_CRT_ORDER_IDX)
288
0
                HGOTO_ERROR(H5E_ATTR, H5E_CANTINC, FAIL, "attribute creation index can't be incremented");
289
290
            /* Set the creation order index on the attribute & incr. creation order index */
291
0
            attr->shared->crt_idx = ainfo.max_crt_idx++;
292
0
        } /* end if */
293
0
        else
294
            /* Set "bogus" creation index for attribute */
295
0
            attr->shared->crt_idx = H5O_MAX_CRT_ORDER_IDX;
296
297
        /* Add the attribute information message, if one is needed */
298
0
        if (new_ainfo) {
299
0
            if (H5O__msg_append_real(loc->file, oh, H5O_MSG_AINFO, H5O_MSG_FLAG_DONTSHARE, 0, &ainfo) < 0)
300
0
                HGOTO_ERROR(H5E_ATTR, H5E_CANTINSERT, FAIL, "unable to create new attribute info message");
301
0
        } /* end if */
302
        /* Otherwise, update existing message */
303
0
        else if (H5O__msg_write_real(loc->file, oh, H5O_MSG_AINFO, H5O_MSG_FLAG_DONTSHARE, 0, &ainfo) < 0)
304
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "unable to update attribute info message");
305
0
    } /* end if */
306
0
    else {
307
        /* Set "bogus" creation index for attribute */
308
0
        attr->shared->crt_idx = H5O_MAX_CRT_ORDER_IDX;
309
310
        /* Set attribute info value to get attribute into object header */
311
0
        ainfo.fheap_addr = HADDR_UNDEF;
312
0
    } /* end else */
313
314
    /* Check for storing attribute with dense storage */
315
0
    if (H5_addr_defined(ainfo.fheap_addr)) {
316
        /* Insert attribute into dense storage */
317
0
        if (H5A__dense_insert(loc->file, &ainfo, attr) < 0)
318
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTINSERT, FAIL, "unable to add to dense storage");
319
0
    } /* end if */
320
0
    else
321
        /* Append new message to object header */
322
0
        if (H5O__msg_append_real(loc->file, oh, H5O_MSG_ATTR, 0, 0, attr) < 0)
323
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTINSERT, FAIL, "unable to create new attribute in header");
324
325
    /* Increment reference count for shared attribute object for the
326
     * object handle created by the caller function H5A__create.  The count
327
     * for the cached object header has been incremented in the step above
328
     * (in H5O__msg_append_real).  The dense storage doesn't need a count.
329
     */
330
0
    attr->shared->nrefs += 1;
331
332
    /* Was new attribute shared? */
333
0
    if ((shared_mesg = H5O_msg_is_shared(H5O_ATTR_ID, attr)) > 0) {
334
0
        hsize_t attr_rc; /* Attribute's ref count in shared message storage */
335
336
        /* Retrieve ref count for shared attribute */
337
0
        if (H5SM_get_refcount(loc->file, H5O_ATTR_ID, &attr->sh_loc, &attr_rc) < 0)
338
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't retrieve shared message ref count");
339
340
        /* If this is not the first copy of the attribute in the shared message
341
         *      storage, decrement the reference count on any shared components
342
         *      of the attribute.  This is done because the shared message
343
         *      storage's "try delete" call doesn't call the message class's
344
         *      "delete" callback until the reference count drops to zero.
345
         *      However, attributes have already increased the reference
346
         *      count on shared components before passing the attribute
347
         *      to the shared message code to manage, causing an asymmetry
348
         *      in the reference counting for any shared components.
349
         *
350
         *      The alternate solution is to have the shared message's "try
351
         *      delete" code always call the message class's "delete" callback,
352
         *      even when the reference count is positive.  This can be done
353
         *      without an appreciable performance hit (by using H5HF_op() in
354
         *      the shared message comparison v2 B-tree callback), but it has
355
         *      the undesirable side-effect of leaving the reference count on
356
         *      the attribute's shared components artificially (and possibly
357
         *      misleadingly) high, because there's only one shared attribute
358
         *      referencing the shared components, not <refcount for the
359
         *      shared attribute> objects referencing the shared components.
360
         *
361
         *      *ick* -QAK, 2007/01/08
362
         */
363
0
        if (attr_rc > 1) {
364
0
            if (H5O__attr_delete(loc->file, oh, attr) < 0)
365
0
                HGOTO_ERROR(H5E_ATTR, H5E_CANTDELETE, FAIL, "unable to delete attribute");
366
0
        } /* end if */
367
0
    }     /* end if */
368
0
    else if (shared_mesg < 0)
369
0
        HGOTO_ERROR(H5E_ATTR, H5E_WRITEERROR, FAIL, "error determining if message should be shared");
370
371
    /* Update the modification time, if any */
372
0
    if (H5O_touch_oh(loc->file, oh, false) < 0)
373
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "unable to update time on object");
374
375
0
done:
376
0
    if (oh && H5O_unpin(oh) < 0)
377
0
        HDONE_ERROR(H5E_ATTR, H5E_CANTUNPIN, FAIL, "unable to unpin object header");
378
379
0
    FUNC_LEAVE_NOAPI(ret_value)
380
0
} /* end H5O__attr_create() */
381
382
/*-------------------------------------------------------------------------
383
 * Function:    H5O__attr_open_cb
384
 *
385
 * Purpose:     Object header iterator callback routine to open an
386
 *              attribute stored compactly.
387
 *
388
 * Return:      SUCCEED/FAIL
389
 *
390
 *-------------------------------------------------------------------------
391
 */
392
static herr_t
393
H5O__attr_open_cb(H5O_t *oh, H5O_mesg_t *mesg /*in,out*/, unsigned sequence, void *_udata /*in,out*/)
394
0
{
395
0
    H5O_iter_opn_t *udata     = (H5O_iter_opn_t *)_udata; /* Operator user data */
396
0
    herr_t          ret_value = H5_ITER_CONT;             /* Return value */
397
398
0
    FUNC_ENTER_PACKAGE
399
400
    /* check args */
401
0
    assert(oh);
402
0
    assert(mesg);
403
0
    assert(!udata->attr);
404
405
    /* Check for correct attribute message to modify */
406
0
    if (strcmp(((H5A_t *)mesg->native)->shared->name, udata->name) == 0) {
407
        /* Make a copy of the attribute to return */
408
0
        if (NULL == (udata->attr = H5A__copy(NULL, (H5A_t *)mesg->native)))
409
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTCOPY, H5_ITER_ERROR, "unable to copy attribute");
410
411
        /* Assign [somewhat arbitrary] creation order value, for older versions
412
         * of the format or if creation order is not tracked */
413
0
        if (oh->version == H5O_VERSION_1 || !(oh->flags & H5O_HDR_ATTR_CRT_ORDER_TRACKED))
414
0
            udata->attr->shared->crt_idx = sequence;
415
416
        /* Stop iterating */
417
0
        ret_value = H5_ITER_STOP;
418
0
    } /* end if */
419
420
0
done:
421
0
    FUNC_LEAVE_NOAPI(ret_value)
422
0
} /* end H5O__attr_open_cb() */
423
424
/*-------------------------------------------------------------------------
425
 * Function:    H5O__attr_open_by_name
426
 *
427
 * Purpose:     Open an existing attribute in an object header.
428
 *
429
 * Return:      SUCCEED/FAIL
430
 *
431
 *-------------------------------------------------------------------------
432
 */
433
H5A_t *
434
H5O__attr_open_by_name(const H5O_loc_t *loc, const char *name)
435
0
{
436
0
    H5O_t      *oh = NULL;               /* Pointer to actual object header */
437
0
    H5O_ainfo_t ainfo;                   /* Attribute information for object */
438
0
    H5A_t      *exist_attr      = NULL;  /* Existing opened attribute object */
439
0
    H5A_t      *opened_attr     = NULL;  /* Newly opened attribute object */
440
0
    htri_t      found_open_attr = false; /* Whether opened object is found */
441
0
    H5A_t      *ret_value       = NULL;  /* Return value */
442
443
0
    FUNC_ENTER_PACKAGE_TAG(loc->addr)
444
445
    /* Check arguments */
446
0
    assert(loc);
447
0
    assert(name);
448
449
    /* Protect the object header to iterate over */
450
0
    if (NULL == (oh = H5O_protect(loc, H5AC__READ_ONLY_FLAG, false)))
451
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTPROTECT, NULL, "unable to load object header");
452
453
    /* Check for attribute info stored */
454
0
    ainfo.fheap_addr = HADDR_UNDEF;
455
0
    if (oh->version > H5O_VERSION_1) {
456
        /* Check for (& retrieve if available) attribute info */
457
0
        if (H5A__get_ainfo(loc->file, oh, &ainfo) < 0)
458
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, NULL, "can't check for attribute info message");
459
0
    } /* end if */
460
461
    /* If found the attribute is already opened, make a copy of it to share the
462
     * object information.  If not, open attribute as a new object
463
     */
464
0
    if ((found_open_attr = H5O__attr_find_opened_attr(loc, &exist_attr, name)) < 0)
465
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, NULL, "failed in finding opened attribute");
466
0
    else if (found_open_attr == true) {
467
0
        if (NULL == (opened_attr = H5A__copy(NULL, exist_attr)))
468
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTCOPY, NULL, "can't copy existing attribute");
469
0
    } /* end else if */
470
0
    else {
471
        /* Check for attributes in dense storage */
472
0
        if (H5_addr_defined(ainfo.fheap_addr)) {
473
            /* Open attribute with dense storage */
474
0
            if (NULL == (opened_attr = H5A__dense_open(loc->file, &ainfo, name)))
475
0
                HGOTO_ERROR(H5E_ATTR, H5E_CANTOPENOBJ, NULL, "can't open attribute");
476
0
        } /* end if */
477
0
        else {
478
0
            H5O_iter_opn_t      udata; /* User data for callback */
479
0
            H5O_mesg_operator_t op;    /* Wrapper for operator */
480
481
            /* Set up user data for callback */
482
0
            udata.name = name;
483
0
            udata.attr = NULL;
484
485
            /* Iterate over attributes, to locate correct one to open */
486
0
            op.op_type  = H5O_MESG_OP_LIB;
487
0
            op.u.lib_op = H5O__attr_open_cb;
488
0
            if (H5O__msg_iterate_real(loc->file, oh, H5O_MSG_ATTR, &op, &udata) < 0)
489
0
                HGOTO_ERROR(H5E_ATTR, H5E_CANTOPENOBJ, NULL, "error updating attribute");
490
491
            /* Check that we found the attribute */
492
0
            if (!udata.attr)
493
0
                HGOTO_ERROR(H5E_ATTR, H5E_NOTFOUND, NULL, "can't locate attribute: '%s'", name);
494
495
            /* Get attribute opened from object header */
496
0
            assert(udata.attr);
497
0
            opened_attr = udata.attr;
498
0
        } /* end else */
499
500
        /* Mark datatype as being on disk now */
501
0
        if (H5T_set_loc(opened_attr->shared->dt, H5F_VOL_OBJ(loc->file), H5T_LOC_DISK) < 0)
502
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, NULL, "invalid datatype location");
503
0
    } /* end else */
504
505
    /* Set return value */
506
0
    ret_value = opened_attr;
507
508
0
done:
509
0
    if (oh && H5O_unprotect(loc, oh, H5AC__NO_FLAGS_SET) < 0)
510
0
        HDONE_ERROR(H5E_ATTR, H5E_CANTUNPROTECT, NULL, "unable to release object header");
511
512
    /* Release any resources, on error */
513
0
    if (NULL == ret_value && opened_attr)
514
0
        if (H5A__close(opened_attr) < 0)
515
0
            HDONE_ERROR(H5E_ATTR, H5E_CANTCLOSEOBJ, NULL, "can't close attribute");
516
517
0
    FUNC_LEAVE_NOAPI_TAG(ret_value)
518
0
} /* end H5O__attr_open_by_name() */
519
520
/*-------------------------------------------------------------------------
521
 * Function:    H5O__attr_open_by_idx_cb
522
 *
523
 * Purpose:     Callback routine opening an attribute by index
524
 *
525
 * Return:      SUCCEED/FAIL
526
 *
527
 *-------------------------------------------------------------------------
528
 */
529
static herr_t
530
H5O__attr_open_by_idx_cb(const H5A_t *attr, void *_ret_attr)
531
0
{
532
0
    H5A_t **ret_attr  = (H5A_t **)_ret_attr; /* 'User data' passed in */
533
0
    herr_t  ret_value = H5_ITER_STOP;        /* Return value */
534
535
0
    FUNC_ENTER_PACKAGE
536
537
    /* check arguments */
538
0
    assert(attr);
539
0
    assert(ret_attr);
540
541
    /* Copy attribute information.  Shared some attribute information. */
542
0
    if (NULL == (*ret_attr = H5A__copy(NULL, attr)))
543
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTCOPY, H5_ITER_ERROR, "can't copy attribute");
544
545
0
done:
546
0
    FUNC_LEAVE_NOAPI(ret_value)
547
0
} /* end H5O__attr_open_by_idx_cb() */
548
549
/*-------------------------------------------------------------------------
550
 * Function:    H5O__attr_open_by_idx
551
 *
552
 * Purpose:     Open an existing attribute in an object header according to
553
 *              an index.
554
 *
555
 * Return:      SUCCEED/FAIL
556
 *
557
 *-------------------------------------------------------------------------
558
 */
559
H5A_t *
560
H5O__attr_open_by_idx(const H5O_loc_t *loc, H5_index_t idx_type, H5_iter_order_t order, hsize_t n)
561
0
{
562
0
    H5A_attr_iter_op_t attr_op;                 /* Attribute operator */
563
0
    H5A_t             *exist_attr      = NULL;  /* Existing opened attribute object */
564
0
    H5A_t             *opened_attr     = NULL;  /* Newly opened attribute object */
565
0
    htri_t             found_open_attr = false; /* Whether opened object is found */
566
0
    H5A_t             *ret_value       = NULL;  /* Return value */
567
568
0
    FUNC_ENTER_PACKAGE
569
570
    /* Check arguments */
571
0
    assert(loc);
572
573
    /* Build attribute operator info */
574
0
    attr_op.op_type  = H5A_ATTR_OP_LIB;
575
0
    attr_op.u.lib_op = H5O__attr_open_by_idx_cb;
576
577
    /* Iterate over attributes to locate correct one */
578
0
    if (H5O_attr_iterate_real((hid_t)-1, loc, idx_type, order, n, NULL, &attr_op, &opened_attr) < 0)
579
0
        HGOTO_ERROR(H5E_ATTR, H5E_BADITER, NULL, "can't locate attribute");
580
581
    /* Find out whether it has already been opened.  If it has, close the object
582
     * and make a copy of the already opened object to share the object info.
583
     */
584
0
    if (opened_attr) {
585
0
        if ((found_open_attr = H5O__attr_find_opened_attr(loc, &exist_attr, opened_attr->shared->name)) < 0)
586
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, NULL, "failed in finding opened attribute");
587
588
        /* If found that the attribute is already opened, make a copy of it
589
         * and close the object just opened.
590
         */
591
0
        if (found_open_attr && exist_attr) {
592
0
            if (H5A__close(opened_attr) < 0)
593
0
                HGOTO_ERROR(H5E_ATTR, H5E_CANTCLOSEOBJ, NULL, "can't close attribute");
594
0
            if (NULL == (opened_attr = H5A__copy(NULL, exist_attr)))
595
0
                HGOTO_ERROR(H5E_ATTR, H5E_CANTCOPY, NULL, "can't copy existing attribute");
596
0
        }
597
0
        else {
598
            /* Mark datatype as being on disk now */
599
0
            if (H5T_set_loc(opened_attr->shared->dt, H5F_VOL_OBJ(loc->file), H5T_LOC_DISK) < 0)
600
0
                HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, NULL, "invalid datatype location");
601
0
        } /* end if */
602
0
    }     /* end if */
603
604
    /* Set return value */
605
0
    ret_value = opened_attr;
606
607
0
done:
608
    /* Release any resources, on error */
609
0
    if (NULL == ret_value && opened_attr)
610
0
        if (H5A__close(opened_attr) < 0)
611
0
            HDONE_ERROR(H5E_ATTR, H5E_CANTCLOSEOBJ, NULL, "can't close attribute");
612
613
0
    FUNC_LEAVE_NOAPI(ret_value)
614
0
} /* end H5O__attr_open_by_idx() */
615
616
/*-------------------------------------------------------------------------
617
 * Function:    H5O__attr_find_opened_attr
618
 *
619
 * Purpose:     Find out whether an attribute has been opened by giving
620
 *              the name.  Return the pointer to the object if found.
621
 *
622
 * Return:      true: found the already opened object
623
 *              false:  didn't find the opened object
624
 *              FAIL: function failed.
625
 *
626
 *-------------------------------------------------------------------------
627
 */
628
static htri_t
629
H5O__attr_find_opened_attr(const H5O_loc_t *loc, H5A_t **attr, const char *name_to_open)
630
0
{
631
0
    hid_t        *attr_id_list = NULL; /* List of IDs for opened attributes */
632
0
    unsigned long loc_fnum;            /* File serial # for object */
633
0
    size_t        num_open_attr;       /* Number of opened attributes */
634
0
    htri_t        ret_value = false;   /* Return value */
635
636
0
    FUNC_ENTER_PACKAGE
637
638
    /* Get file serial number for the location of attribute */
639
0
    if (H5F_get_fileno(loc->file, &loc_fnum) < 0)
640
0
        HGOTO_ERROR(H5E_ATTR, H5E_BADVALUE, FAIL, "can't get file serial number");
641
642
    /* Count all opened attributes */
643
0
    if (H5F_get_obj_count(loc->file, H5F_OBJ_ATTR | H5F_OBJ_LOCAL, false, &num_open_attr) < 0)
644
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't count opened attributes");
645
646
    /* Find out whether the attribute has been opened */
647
0
    if (num_open_attr) {
648
0
        size_t check_num_attr; /* Number of open attribute IDs */
649
0
        size_t u;              /* Local index variable */
650
651
        /* Allocate space for the attribute ID list */
652
0
        if (NULL == (attr_id_list = (hid_t *)H5MM_malloc(num_open_attr * sizeof(hid_t))))
653
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTALLOC, FAIL, "unable to allocate memory for attribute ID list");
654
655
        /* Retrieve the IDs of all opened attributes */
656
0
        if (H5F_get_obj_ids(loc->file, H5F_OBJ_ATTR | H5F_OBJ_LOCAL, num_open_attr, attr_id_list, false,
657
0
                            &check_num_attr) < 0)
658
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't get IDs of opened attributes");
659
0
        if (check_num_attr != num_open_attr)
660
0
            HGOTO_ERROR(H5E_ATTR, H5E_BADITER, FAIL, "open attribute count mismatch");
661
662
        /* Iterate over the attributes */
663
0
        for (u = 0; u < num_open_attr; u++) {
664
0
            unsigned long attr_fnum; /* Attributes file serial number */
665
666
            /* Get pointer to attribute */
667
0
            if (NULL == (*attr = (H5A_t *)H5VL_object_verify(attr_id_list[u], H5I_ATTR)))
668
0
                HGOTO_ERROR(H5E_ATTR, H5E_BADTYPE, FAIL, "not an attribute");
669
670
            /* Get file serial number for attribute */
671
0
            if (H5F_get_fileno((*attr)->oloc.file, &attr_fnum) < 0)
672
0
                HGOTO_ERROR(H5E_ATTR, H5E_BADVALUE, FAIL, "can't get file serial number");
673
674
            /* Verify whether it's the right object.  The attribute name, object
675
             *  address to which the attribute is attached, and file serial
676
             *  number should all match.
677
             */
678
0
            if (!strcmp(name_to_open, (*attr)->shared->name) && loc->addr == (*attr)->oloc.addr &&
679
0
                loc_fnum == attr_fnum) {
680
0
                ret_value = true;
681
0
                break;
682
0
            } /* end if */
683
0
        }     /* end for */
684
0
    }         /* end if */
685
686
0
done:
687
0
    if (attr_id_list)
688
0
        H5MM_free(attr_id_list);
689
690
0
    FUNC_LEAVE_NOAPI(ret_value)
691
0
} /* end H5O__attr_find_opened_attr() */
692
693
/*-------------------------------------------------------------------------
694
 * Function:    H5O__attr_update_shared
695
 *
696
 * Purpose:     Update a shared attribute.
697
 *
698
 * Return:      SUCCEED/FAIL
699
 *
700
 *-------------------------------------------------------------------------
701
 */
702
herr_t
703
H5O__attr_update_shared(H5F_t *f, H5O_t *oh, H5A_t *attr, H5O_shared_t *update_sh_mesg)
704
0
{
705
0
    H5O_shared_t sh_mesg;             /* Shared object header message */
706
0
    hsize_t      attr_rc;             /* Attribute's ref count in shared message storage */
707
0
    htri_t       shared_mesg;         /* Whether the message should be shared */
708
0
    herr_t       ret_value = SUCCEED; /* Return value */
709
710
0
    FUNC_ENTER_PACKAGE
711
712
    /* check args */
713
0
    assert(f);
714
0
    assert(attr);
715
716
    /* Extract shared message info from current attribute (for later use) */
717
0
    if (H5O_set_shared(&sh_mesg, &(attr->sh_loc)) < 0)
718
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTCOPY, FAIL, "can't get shared message");
719
720
    /* Reset existing sharing information */
721
0
    if (H5O_msg_reset_share(H5O_ATTR_ID, attr) < 0)
722
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTINIT, FAIL, "unable to reset attribute sharing");
723
724
    /* Store new version of message as a SOHM */
725
    /* (should always work, since we're not changing the size of the attribute) */
726
0
    if ((shared_mesg = H5SM_try_share(f, oh, 0, H5O_ATTR_ID, attr, NULL)) == 0)
727
0
        HGOTO_ERROR(H5E_ATTR, H5E_BADMESG, FAIL, "attribute changed sharing status");
728
0
    else if (shared_mesg < 0)
729
0
        HGOTO_ERROR(H5E_ATTR, H5E_BADMESG, FAIL, "can't share attribute");
730
731
    /* Retrieve shared message storage ref count for new shared attribute */
732
0
    if (H5SM_get_refcount(f, H5O_ATTR_ID, &attr->sh_loc, &attr_rc) < 0)
733
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't retrieve shared message ref count");
734
735
    /* If the newly shared attribute needs to share "ownership" of the shared
736
     *      components (ie. its reference count is 1), increment the reference
737
     *      count on any shared components of the attribute, so that they won't
738
     *      be removed from the file by the following "delete" operation on the
739
     *      original attribute shared message info.  (Essentially a "copy on
740
     *      write" operation).
741
     *
742
     *      *ick* -QAK, 2007/01/08
743
     */
744
0
    if (attr_rc == 1)
745
        /* Increment reference count on attribute components */
746
0
        if (H5O__attr_link(f, oh, attr) < 0)
747
0
            HGOTO_ERROR(H5E_ATTR, H5E_LINKCOUNT, FAIL, "unable to adjust attribute link count");
748
749
    /* Remove the old attribute from the SOHM storage */
750
0
    if (H5SM_delete(f, oh, &sh_mesg) < 0)
751
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTFREE, FAIL, "unable to delete shared attribute in shared storage");
752
753
    /* Extract updated shared message info from modified attribute, if requested */
754
0
    if (update_sh_mesg)
755
0
        if (H5O_set_shared(update_sh_mesg, &(attr->sh_loc)) < 0)
756
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTCOPY, FAIL, "can't get shared message");
757
758
0
done:
759
0
    FUNC_LEAVE_NOAPI(ret_value)
760
0
} /* end H5O__attr_update_shared() */
761
762
/*-------------------------------------------------------------------------
763
 * Function:    H5O__attr_write_cb
764
 *
765
 * Purpose:     Object header iterator callback routine to update an
766
 *              attribute stored compactly.
767
 *
768
 * Return:      SUCCEED/FAIL
769
 *
770
 *-------------------------------------------------------------------------
771
 */
772
static herr_t
773
H5O__attr_write_cb(H5O_t *oh, H5O_mesg_t *mesg /*in,out*/, unsigned H5_ATTR_UNUSED sequence,
774
                   void *_udata /*in,out*/)
775
0
{
776
0
    H5O_iter_wrt_t    *udata       = (H5O_iter_wrt_t *)_udata; /* Operator user data */
777
0
    H5O_chunk_proxy_t *chk_proxy   = NULL;                     /* Chunk that message is in */
778
0
    bool               chk_dirtied = false;                    /* Flag for unprotecting chunk */
779
0
    herr_t             ret_value   = H5_ITER_CONT;             /* Return value */
780
781
0
    FUNC_ENTER_PACKAGE
782
783
    /* check args */
784
0
    assert(oh);
785
0
    assert(mesg);
786
0
    assert(!udata->found);
787
788
    /* Check for correct attribute message to modify */
789
0
    if (0 == strcmp(((H5A_t *)mesg->native)->shared->name, udata->attr->shared->name)) {
790
        /* Protect chunk */
791
0
        if (NULL == (chk_proxy = H5O__chunk_protect(udata->f, oh, mesg->chunkno)))
792
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTPROTECT, H5_ITER_ERROR, "unable to load object header chunk");
793
794
        /* Because the attribute structure is shared now. The only situation that requires
795
         * copying the data is when the metadata cache evicts and reloads this attribute.
796
         * The shared attribute structure will be different in that situation. SLU-2010/7/29 */
797
0
        if (((H5A_t *)mesg->native)->shared != udata->attr->shared) {
798
            /* Sanity check */
799
0
            assert(((H5A_t *)mesg->native)->shared->data);
800
0
            assert(udata->attr->shared->data);
801
0
            assert(((H5A_t *)mesg->native)->shared->data != udata->attr->shared->data);
802
803
            /* (Needs to occur before updating the shared message, or the hash
804
             *      value on the old & new messages will be the same) */
805
0
            H5MM_memcpy(((H5A_t *)mesg->native)->shared->data, udata->attr->shared->data,
806
0
                        udata->attr->shared->data_size);
807
0
        } /* end if */
808
809
        /* Mark the message as modified */
810
0
        mesg->dirty = true;
811
0
        chk_dirtied = true;
812
813
        /* Release chunk */
814
0
        if (H5O__chunk_unprotect(udata->f, chk_proxy, chk_dirtied) < 0)
815
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTUNPROTECT, H5_ITER_ERROR,
816
0
                        "unable to unprotect object header chunk");
817
0
        chk_proxy = NULL;
818
819
        /* Update the shared attribute in the SOHM storage */
820
0
        if (mesg->flags & H5O_MSG_FLAG_SHARED)
821
0
            if (H5O__attr_update_shared(udata->f, oh, udata->attr, (H5O_shared_t *)mesg->native) < 0)
822
0
                HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, H5_ITER_ERROR,
823
0
                            "unable to update attribute in shared storage");
824
825
        /* Indicate that the object header was modified */
826
0
        oh->mesgs_modified = H5O_MODIFY;
827
828
        /* Indicate that the attribute was found */
829
0
        udata->found = true;
830
831
        /* Stop iterating */
832
0
        ret_value = H5_ITER_STOP;
833
0
    } /* end if */
834
835
0
done:
836
    /* Release chunk, if not already done */
837
0
    if (chk_proxy && H5O__chunk_unprotect(udata->f, chk_proxy, chk_dirtied) < 0)
838
0
        HDONE_ERROR(H5E_ATTR, H5E_CANTUNPROTECT, H5_ITER_ERROR, "unable to unprotect object header chunk");
839
840
0
    FUNC_LEAVE_NOAPI(ret_value)
841
0
} /* end H5O__attr_write_cb() */
842
843
/*-------------------------------------------------------------------------
844
 * Function:    H5O__attr_write
845
 *
846
 * Purpose:     Write a new value to an attribute.
847
 *
848
 * Return:      SUCCEED/FAIL
849
 *
850
 *-------------------------------------------------------------------------
851
 */
852
herr_t
853
H5O__attr_write(const H5O_loc_t *loc, H5A_t *attr)
854
0
{
855
0
    H5O_t      *oh = NULL;           /* Pointer to actual object header */
856
0
    H5O_ainfo_t ainfo;               /* Attribute information for object */
857
0
    herr_t      ret_value = SUCCEED; /* Return value */
858
859
0
    FUNC_ENTER_PACKAGE
860
861
    /* Check arguments */
862
0
    assert(loc);
863
0
    assert(attr);
864
865
    /* Pin the object header */
866
0
    if (NULL == (oh = H5O_pin(loc)))
867
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTPIN, FAIL, "unable to pin object header");
868
869
    /* Check for attribute info stored */
870
0
    ainfo.fheap_addr = HADDR_UNDEF;
871
0
    if (oh->version > H5O_VERSION_1) {
872
        /* Check for (& retrieve if available) attribute info */
873
0
        if (H5A__get_ainfo(loc->file, oh, &ainfo) < 0)
874
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't check for attribute info message");
875
0
    } /* end if */
876
877
    /* Check for attributes stored densely */
878
0
    if (H5_addr_defined(ainfo.fheap_addr)) {
879
        /* Modify the attribute data in dense storage */
880
0
        if (H5A__dense_write(loc->file, &ainfo, attr) < 0)
881
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "error updating attribute");
882
0
    } /* end if */
883
0
    else {
884
0
        H5O_iter_wrt_t      udata; /* User data for callback */
885
0
        H5O_mesg_operator_t op;    /* Wrapper for operator */
886
887
        /* Set up user data for callback */
888
0
        udata.f     = loc->file;
889
0
        udata.attr  = attr;
890
0
        udata.found = false;
891
892
        /* Iterate over attributes, to locate correct one to update */
893
0
        op.op_type  = H5O_MESG_OP_LIB;
894
0
        op.u.lib_op = H5O__attr_write_cb;
895
0
        if (H5O__msg_iterate_real(loc->file, oh, H5O_MSG_ATTR, &op, &udata) < 0)
896
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "error updating attribute");
897
898
        /* Check that we found the attribute */
899
0
        if (!udata.found)
900
0
            HGOTO_ERROR(H5E_ATTR, H5E_NOTFOUND, FAIL, "can't locate open attribute?");
901
0
    } /* end else */
902
903
    /* Update the modification time, if any */
904
0
    if (H5O_touch_oh(loc->file, oh, false) < 0)
905
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "unable to update time on object");
906
907
0
done:
908
0
    if (oh && H5O_unpin(oh) < 0)
909
0
        HDONE_ERROR(H5E_ATTR, H5E_CANTUNPIN, FAIL, "unable to unpin object header");
910
911
0
    FUNC_LEAVE_NOAPI(ret_value)
912
0
} /* end H5O__attr_write */
913
914
/*-------------------------------------------------------------------------
915
 * Function:    H5O__attr_rename_chk_cb
916
 *
917
 * Purpose:     Object header iterator callback routine to check for
918
 *              duplicate name during rename
919
 *
920
 * Return:      SUCCEED/FAIL
921
 *
922
 *-------------------------------------------------------------------------
923
 */
924
static herr_t
925
H5O__attr_rename_chk_cb(H5O_t H5_ATTR_UNUSED *oh, H5O_mesg_t *mesg /*in,out*/,
926
                        unsigned H5_ATTR_UNUSED sequence, void *_udata /*in,out*/)
927
0
{
928
0
    H5O_iter_ren_t *udata     = (H5O_iter_ren_t *)_udata; /* Operator user data */
929
0
    herr_t          ret_value = H5_ITER_CONT;             /* Return value */
930
931
0
    FUNC_ENTER_PACKAGE_NOERR
932
933
    /* check args */
934
0
    assert(oh);
935
0
    assert(mesg);
936
0
    assert(!udata->found);
937
938
    /* Check for existing attribute with new name */
939
0
    if (strcmp(((H5A_t *)mesg->native)->shared->name, udata->new_name) == 0) {
940
        /* Indicate that we found an existing attribute with the new name*/
941
0
        udata->found = true;
942
943
        /* Stop iterating */
944
0
        ret_value = H5_ITER_STOP;
945
0
    } /* end if */
946
947
0
    FUNC_LEAVE_NOAPI(ret_value)
948
0
} /* end H5O__attr_rename_chk_cb() */
949
950
/*-------------------------------------------------------------------------
951
 * Function:    H5O__attr_rename_mod_cb
952
 *
953
 * Purpose:     Object header iterator callback routine to change name of
954
 *              attribute during rename
955
 *
956
 * Note:        This routine doesn't currently allow an attribute to change
957
 *              its "shared" status, if the name change would cause a size
958
 *              difference that would put it into a different category.
959
 *              Something for later...
960
 *
961
 * Return:      SUCCEED/FAIL
962
 *
963
 *-------------------------------------------------------------------------
964
 */
965
static herr_t
966
H5O__attr_rename_mod_cb(H5O_t *oh, H5O_mesg_t *mesg /*in,out*/, unsigned H5_ATTR_UNUSED sequence,
967
                        void *_udata /*in,out*/)
968
0
{
969
0
    H5O_iter_ren_t    *udata       = (H5O_iter_ren_t *)_udata; /* Operator user data */
970
0
    H5O_chunk_proxy_t *chk_proxy   = NULL;                     /* Chunk that message is in */
971
0
    bool               chk_dirtied = false;                    /* Flag for unprotecting chunk */
972
0
    herr_t             ret_value   = H5_ITER_CONT;             /* Return value */
973
974
0
    FUNC_ENTER_PACKAGE
975
976
    /* check args */
977
0
    assert(oh);
978
0
    assert(mesg);
979
0
    assert(!udata->found);
980
981
    /* Find correct attribute message to rename */
982
0
    if (strcmp(((H5A_t *)mesg->native)->shared->name, udata->old_name) == 0) {
983
0
        unsigned old_version = ((H5A_t *)mesg->native)->shared->version; /* Old version of the attribute */
984
985
        /* Protect chunk */
986
0
        if (NULL == (chk_proxy = H5O__chunk_protect(udata->f, oh, mesg->chunkno)))
987
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTPROTECT, H5_ITER_ERROR, "unable to load object header chunk");
988
989
        /* Change the name for the attribute */
990
0
        H5MM_xfree(((H5A_t *)mesg->native)->shared->name);
991
0
        ((H5A_t *)mesg->native)->shared->name = H5MM_xstrdup(udata->new_name);
992
993
        /* Recompute the version to encode the attribute with */
994
0
        if (H5A__set_version(udata->f, ((H5A_t *)mesg->native)) < 0)
995
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTSET, H5_ITER_ERROR, "unable to update attribute version");
996
997
        /* Mark the message as modified */
998
0
        mesg->dirty = true;
999
0
        chk_dirtied = true;
1000
1001
        /* Release chunk */
1002
0
        if (H5O__chunk_unprotect(udata->f, chk_proxy, chk_dirtied) < 0)
1003
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTUNPROTECT, H5_ITER_ERROR,
1004
0
                        "unable to unprotect object header chunk");
1005
0
        chk_proxy = NULL;
1006
1007
        /* Check for shared message */
1008
0
        if (mesg->flags & H5O_MSG_FLAG_SHARED) {
1009
            /* Update the shared attribute in the SOHM storage */
1010
0
            if (H5O__attr_update_shared(udata->f, oh, (H5A_t *)mesg->native, NULL) < 0)
1011
0
                HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, H5_ITER_ERROR,
1012
0
                            "unable to update attribute in shared storage");
1013
0
        } /* end if */
1014
0
        else {
1015
            /* Sanity check */
1016
0
            assert(H5O_msg_is_shared(H5O_ATTR_ID, (H5A_t *)mesg->native) == false);
1017
1018
            /* Check for attribute message changing size */
1019
0
            if (strlen(udata->new_name) != strlen(udata->old_name) ||
1020
0
                old_version != ((H5A_t *)mesg->native)->shared->version) {
1021
0
                H5A_t *attr; /* Attribute to re-add */
1022
1023
                /* Take ownership of the message's native info (the attribute)
1024
                 *      so any shared objects in the file aren't adjusted (and
1025
                 *      possibly deleted) when the message is released.
1026
                 */
1027
                /* (We do this more complicated sequence of actions because the
1028
                 *      simpler solution of adding the modified attribute first
1029
                 *      and then deleting the old message can re-allocate the
1030
                 *      list of messages during the "add the modified attribute"
1031
                 *      step, invalidating the message pointer we have here - QAK)
1032
                 */
1033
0
                attr         = (H5A_t *)mesg->native;
1034
0
                mesg->native = NULL;
1035
1036
                /* Delete old attribute */
1037
                /* (doesn't decrement the link count on shared components because
1038
                 *      the "native" pointer has been reset)
1039
                 */
1040
0
                if (H5O__release_mesg(udata->f, oh, mesg, false) < 0)
1041
0
                    HGOTO_ERROR(H5E_ATTR, H5E_CANTDELETE, H5_ITER_ERROR,
1042
0
                                "unable to release previous attribute");
1043
1044
0
                oh->mesgs_modified = H5O_MODIFY_CONDENSE;
1045
1046
                /* Append renamed attribute to object header */
1047
                /* (Don't let it become shared) */
1048
0
                if (H5O__msg_append_real(udata->f, oh, H5O_MSG_ATTR, (mesg->flags | H5O_MSG_FLAG_DONTSHARE),
1049
0
                                         0, attr) < 0)
1050
0
                    HGOTO_ERROR(H5E_ATTR, H5E_CANTINSERT, H5_ITER_ERROR,
1051
0
                                "unable to relocate renamed attribute in header");
1052
1053
                /* Sanity check */
1054
0
                assert(H5O_msg_is_shared(H5O_ATTR_ID, attr) == false);
1055
1056
                /* Close the local copy of the attribute */
1057
0
                H5A__close(attr);
1058
0
            } /* end if */
1059
0
        }     /* end else */
1060
1061
        /* Indicate that the object header was modified */
1062
0
        oh->mesgs_modified |= H5O_MODIFY;
1063
1064
        /* Indicate that we found an existing attribute with the old name */
1065
0
        udata->found = true;
1066
1067
        /* Stop iterating */
1068
0
        ret_value = H5_ITER_STOP;
1069
0
    } /* end if */
1070
1071
0
done:
1072
    /* Release chunk, if not already done */
1073
0
    if (chk_proxy && H5O__chunk_unprotect(udata->f, chk_proxy, chk_dirtied) < 0)
1074
0
        HDONE_ERROR(H5E_ATTR, H5E_CANTUNPROTECT, H5_ITER_ERROR, "unable to unprotect object header chunk");
1075
1076
0
    FUNC_LEAVE_NOAPI(ret_value)
1077
0
} /* end H5O__attr_rename_mod_cb() */
1078
1079
/*-------------------------------------------------------------------------
1080
 * Function:    H5O__attr_rename
1081
 *
1082
 * Purpose:     Rename an attribute.
1083
 *
1084
 * Return:      SUCCEED/FAIL
1085
 *
1086
 *-------------------------------------------------------------------------
1087
 */
1088
herr_t
1089
H5O__attr_rename(const H5O_loc_t *loc, const char *old_name, const char *new_name)
1090
0
{
1091
0
    H5O_t      *oh = NULL;           /* Pointer to actual object header */
1092
0
    H5O_ainfo_t ainfo;               /* Attribute information for object */
1093
0
    herr_t      ret_value = SUCCEED; /* Return value */
1094
1095
0
    FUNC_ENTER_PACKAGE_TAG(loc->addr)
1096
1097
    /* Check arguments */
1098
0
    assert(loc);
1099
0
    assert(old_name);
1100
0
    assert(new_name);
1101
1102
    /* Pin the object header */
1103
0
    if (NULL == (oh = H5O_pin(loc)))
1104
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTPIN, FAIL, "unable to pin object header");
1105
1106
    /* Check for attribute info stored */
1107
0
    ainfo.fheap_addr = HADDR_UNDEF;
1108
0
    if (oh->version > H5O_VERSION_1) {
1109
        /* Check for (& retrieve if available) attribute info */
1110
0
        if (H5A__get_ainfo(loc->file, oh, &ainfo) < 0)
1111
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't check for attribute info message");
1112
0
    } /* end if */
1113
1114
    /* Check for attributes stored densely */
1115
0
    if (H5_addr_defined(ainfo.fheap_addr)) {
1116
        /* Rename the attribute data in dense storage */
1117
0
        if (H5A__dense_rename(loc->file, &ainfo, old_name, new_name) < 0)
1118
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "error updating attribute");
1119
0
    } /* end if */
1120
0
    else {
1121
0
        H5O_iter_ren_t      udata; /* User data for callback */
1122
0
        H5O_mesg_operator_t op;    /* Wrapper for operator */
1123
1124
        /* Set up user data for callback */
1125
0
        udata.f        = loc->file;
1126
0
        udata.old_name = old_name;
1127
0
        udata.new_name = new_name;
1128
0
        udata.found    = false;
1129
1130
        /* Iterate over attributes, to check if "new name" exists already */
1131
0
        op.op_type  = H5O_MESG_OP_LIB;
1132
0
        op.u.lib_op = H5O__attr_rename_chk_cb;
1133
0
        if (H5O__msg_iterate_real(loc->file, oh, H5O_MSG_ATTR, &op, &udata) < 0)
1134
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "error updating attribute");
1135
1136
        /* If the new name was found, indicate an error */
1137
0
        if (udata.found)
1138
0
            HGOTO_ERROR(H5E_ATTR, H5E_EXISTS, FAIL, "attribute with new name already exists");
1139
1140
        /* Iterate over attributes again, to actually rename attribute with old name */
1141
0
        op.op_type  = H5O_MESG_OP_LIB;
1142
0
        op.u.lib_op = H5O__attr_rename_mod_cb;
1143
0
        if (H5O__msg_iterate_real(loc->file, oh, H5O_MSG_ATTR, &op, &udata) < 0)
1144
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "error updating attribute");
1145
1146
        /* Check that we found the attribute to rename */
1147
0
        if (!udata.found)
1148
0
            HGOTO_ERROR(H5E_ATTR, H5E_NOTFOUND, FAIL, "can't locate attribute with old name");
1149
0
    } /* end else */
1150
1151
    /* Update the modification time, if any */
1152
0
    if (H5O_touch_oh(loc->file, oh, false) < 0)
1153
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "unable to update time on object");
1154
1155
0
done:
1156
0
    if (oh && H5O_unpin(oh) < 0)
1157
0
        HDONE_ERROR(H5E_ATTR, H5E_CANTUNPIN, FAIL, "unable to unpin object header");
1158
1159
0
    FUNC_LEAVE_NOAPI_TAG(ret_value)
1160
0
} /* end H5O__attr_rename() */
1161
1162
/*-------------------------------------------------------------------------
1163
 * Function:    H5O_attr_iterate_real
1164
 *
1165
 * Purpose:     Internal routine to iterate over attributes for an object.
1166
 *
1167
 * Return:      SUCCEED/FAIL
1168
 *
1169
 *-------------------------------------------------------------------------
1170
 */
1171
herr_t
1172
H5O_attr_iterate_real(hid_t loc_id, const H5O_loc_t *loc, H5_index_t idx_type, H5_iter_order_t order,
1173
                      hsize_t skip, hsize_t *last_attr, const H5A_attr_iter_op_t *attr_op, void *op_data)
1174
0
{
1175
0
    H5O_t           *oh = NULL;                /* Pointer to actual object header */
1176
0
    H5O_ainfo_t      ainfo;                    /* Attribute information for object */
1177
0
    H5A_attr_table_t atable    = {0, 0, NULL}; /* Table of attributes */
1178
0
    herr_t           ret_value = FAIL;         /* Return value */
1179
1180
0
    FUNC_ENTER_NOAPI_NOINIT_TAG(loc->addr)
1181
1182
    /* Check arguments */
1183
0
    assert(loc);
1184
0
    assert(loc->file);
1185
0
    assert(H5_addr_defined(loc->addr));
1186
0
    assert(attr_op);
1187
1188
    /* Protect the object header to iterate over */
1189
0
    if (NULL == (oh = H5O_protect(loc, H5AC__READ_ONLY_FLAG, false)))
1190
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTPROTECT, FAIL, "unable to load object header");
1191
1192
    /* Check for attribute info stored */
1193
0
    ainfo.fheap_addr = HADDR_UNDEF;
1194
0
    if (oh->version > H5O_VERSION_1) {
1195
        /* Check for (& retrieve if available) attribute info */
1196
0
        if (H5A__get_ainfo(loc->file, oh, &ainfo) < 0)
1197
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't check for attribute info message");
1198
0
    } /* end if */
1199
1200
    /* Check for attributes stored densely */
1201
0
    if (H5_addr_defined(ainfo.fheap_addr)) {
1202
        /* Check for skipping too many attributes */
1203
0
        if (skip > 0 && skip >= ainfo.nattrs)
1204
0
            HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid index specified");
1205
1206
        /* Release the object header */
1207
0
        if (H5O_unprotect(loc, oh, H5AC__NO_FLAGS_SET) < 0)
1208
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTUNPROTECT, FAIL, "unable to release object header");
1209
0
        oh = NULL;
1210
1211
        /* Iterate over attributes in dense storage */
1212
0
        if ((ret_value = H5A__dense_iterate(loc->file, loc_id, &ainfo, idx_type, order, skip, last_attr,
1213
0
                                            attr_op, op_data)) < 0)
1214
0
            HERROR(H5E_ATTR, H5E_BADITER, "error iterating over attributes");
1215
0
    } /* end if */
1216
0
    else {
1217
        /* Build table of attributes for compact storage */
1218
0
        if (H5A__compact_build_table(loc->file, oh, idx_type, order, &atable) < 0)
1219
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, FAIL, "error building attribute table");
1220
1221
        /* Release the object header */
1222
0
        if (H5O_unprotect(loc, oh, H5AC__NO_FLAGS_SET) < 0)
1223
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTUNPROTECT, FAIL, "unable to release object header");
1224
0
        oh = NULL;
1225
1226
        /* Check for skipping too many attributes */
1227
0
        if (skip > 0 && skip >= atable.num_attrs)
1228
0
            HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid index specified");
1229
1230
        /* Iterate over attributes in table */
1231
0
        if ((ret_value = H5A__attr_iterate_table(&atable, skip, last_attr, loc_id, attr_op, op_data)) < 0)
1232
0
            HERROR(H5E_ATTR, H5E_BADITER, "iteration operator failed");
1233
0
    } /* end else */
1234
1235
0
done:
1236
    /* Release resources */
1237
0
    if (oh && H5O_unprotect(loc, oh, H5AC__NO_FLAGS_SET) < 0)
1238
0
        HDONE_ERROR(H5E_ATTR, H5E_CANTUNPROTECT, FAIL, "unable to release object header");
1239
0
    if (atable.attrs && H5A__attr_release_table(&atable) < 0)
1240
0
        HDONE_ERROR(H5E_ATTR, H5E_CANTFREE, FAIL, "unable to release attribute table");
1241
1242
0
    FUNC_LEAVE_NOAPI_TAG(ret_value)
1243
0
} /* end H5O_attr_iterate_real() */
1244
1245
/*-------------------------------------------------------------------------
1246
 * Function:    H5O__attr_iterate
1247
 *
1248
 * Purpose:     Iterate over attributes for an object.
1249
 *
1250
 * Return:      SUCCEED/FAIL
1251
 *
1252
 *-------------------------------------------------------------------------
1253
 */
1254
herr_t
1255
H5O__attr_iterate(hid_t loc_id, H5_index_t idx_type, H5_iter_order_t order, hsize_t skip, hsize_t *last_attr,
1256
                  const H5A_attr_iter_op_t *attr_op, void *op_data)
1257
0
{
1258
0
    H5G_loc_t loc;              /* Object location */
1259
0
    herr_t    ret_value = FAIL; /* Return value */
1260
1261
0
    FUNC_ENTER_PACKAGE
1262
1263
    /* Check arguments */
1264
0
    assert(attr_op);
1265
1266
    /* Look up location for location ID */
1267
0
    if (H5G_loc(loc_id, &loc) < 0)
1268
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a location");
1269
1270
    /* Iterate over attributes to locate correct one */
1271
0
    if ((ret_value =
1272
0
             H5O_attr_iterate_real(loc_id, loc.oloc, idx_type, order, skip, last_attr, attr_op, op_data)) < 0)
1273
0
        HERROR(H5E_ATTR, H5E_BADITER, "error iterating over attributes");
1274
1275
0
done:
1276
0
    FUNC_LEAVE_NOAPI(ret_value)
1277
0
} /* end H5O__attr_iterate() */
1278
1279
/*-------------------------------------------------------------------------
1280
 * Function:    H5O__attr_remove_update
1281
 *
1282
 * Purpose:     Check for reverting from dense to compact attribute storage
1283
 *
1284
 *              When converting storage from dense to compact, if found
1285
 *              the attribute is already opened, use the opened message
1286
 *              to insert.  If not, still use the message in the attribute
1287
 *              table. This will guarantee that the attribute message is
1288
 *              shared between the object in metadata cache and the opened
1289
 *              object.
1290
 *
1291
 * Return:      SUCCEED/FAIL
1292
 *-------------------------------------------------------------------------
1293
 */
1294
static herr_t
1295
H5O__attr_remove_update(const H5O_loc_t *loc, H5O_t *oh, H5O_ainfo_t *ainfo)
1296
0
{
1297
0
    H5A_attr_table_t atable    = {0, 0, NULL}; /* Table of attributes */
1298
0
    herr_t           ret_value = SUCCEED;      /* Return value */
1299
1300
0
    FUNC_ENTER_PACKAGE
1301
1302
    /* Check arguments */
1303
0
    assert(loc);
1304
0
    assert(oh);
1305
0
    assert(ainfo);
1306
1307
    /* Decrement the number of attributes on the object */
1308
0
    ainfo->nattrs--;
1309
1310
    /* Check for shifting from dense storage back to compact storage */
1311
0
    if (H5_addr_defined(ainfo->fheap_addr) && ainfo->nattrs < oh->min_dense) {
1312
0
        bool   can_convert = true; /* Whether converting to attribute messages is possible */
1313
0
        size_t u;                  /* Local index */
1314
1315
        /* Build the table of attributes for this object */
1316
0
        if (H5A__dense_build_table(loc->file, ainfo, H5_INDEX_NAME, H5_ITER_NATIVE, &atable) < 0)
1317
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, FAIL, "error building attribute table");
1318
1319
        /* Inspect attributes in table for ones that can't be converted back
1320
         * into attribute message form (currently only attributes which
1321
         * can't fit into an object header message)
1322
         */
1323
0
        for (u = 0; u < ainfo->nattrs; u++)
1324
0
            if (H5O_msg_size_oh(loc->file, oh, H5O_ATTR_ID, (atable.attrs[u]), (size_t)0) >=
1325
0
                H5O_MESG_MAX_SIZE) {
1326
0
                can_convert = false;
1327
0
                break;
1328
0
            } /* end if */
1329
1330
        /* If ok, insert attributes as object header messages */
1331
0
        if (can_convert) {
1332
0
            H5A_t *exist_attr      = NULL;
1333
0
            htri_t found_open_attr = false;
1334
1335
            /* Iterate over attributes, to put them into header */
1336
0
            for (u = 0; u < ainfo->nattrs; u++) {
1337
0
                htri_t shared_mesg; /* Should this message be stored in the Shared Message table? */
1338
1339
                /* Check if attribute is shared */
1340
0
                if ((shared_mesg = H5O_msg_is_shared(H5O_ATTR_ID, (atable.attrs[u]))) < 0)
1341
0
                    HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "error determining if message is shared");
1342
0
                else if (shared_mesg == 0) {
1343
                    /* Increment reference count on attribute components */
1344
                    /* (so that they aren't deleted when the dense attribute storage is deleted) */
1345
0
                    if (H5O__attr_link(loc->file, oh, (atable.attrs[u])) < 0)
1346
0
                        HGOTO_ERROR(H5E_ATTR, H5E_LINKCOUNT, FAIL, "unable to adjust attribute link count");
1347
0
                } /* end if */
1348
0
                else {
1349
                    /* Reset 'shared' status, so attribute will be shared again */
1350
0
                    (atable.attrs[u])->sh_loc.type = H5O_SHARE_TYPE_UNSHARED;
1351
0
                } /* end else */
1352
1353
                /* Insert attribute message into object header (Will increment
1354
                   reference count on shared attributes) */
1355
                /* Find out whether the attribute has been opened */
1356
0
                if ((found_open_attr =
1357
0
                         H5O__attr_find_opened_attr(loc, &exist_attr, (atable.attrs[u])->shared->name)) < 0)
1358
0
                    HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "failed in finding opened attribute");
1359
1360
                /* If found the attribute is already opened, use the opened message to insert.
1361
                   If not, still use the message in the attribute table. */
1362
0
                if (found_open_attr && exist_attr) {
1363
0
                    if (H5O__msg_append_real(loc->file, oh, H5O_MSG_ATTR, 0, 0, exist_attr) < 0)
1364
0
                        HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, FAIL, "can't create message");
1365
1366
0
                } /* end if */
1367
0
                else if (H5O__msg_append_real(loc->file, oh, H5O_MSG_ATTR, 0, 0, (atable.attrs[u])) < 0)
1368
0
                    HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, FAIL, "can't create message");
1369
0
            } /* end for */
1370
1371
            /* Remove the dense storage */
1372
0
            if (H5A__dense_delete(loc->file, ainfo) < 0)
1373
0
                HGOTO_ERROR(H5E_ATTR, H5E_CANTDELETE, FAIL, "unable to delete dense attribute storage");
1374
0
        } /* end if */
1375
0
    }     /* end if */
1376
1377
    /* Update the message after removing the attribute */
1378
    /* This is particularly needed when removing the last attribute that is
1379
     * accessed via fractal heap/v2 B-tree (HDFFV-9277)
1380
     */
1381
0
    if (H5O__msg_write_real(loc->file, oh, H5O_MSG_AINFO, H5O_MSG_FLAG_DONTSHARE, 0, ainfo) < 0)
1382
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "unable to update attribute info message");
1383
1384
    /* Check if we have deleted all the attributes and the attribute info
1385
     *  message should be deleted itself.
1386
     */
1387
0
    if (ainfo->nattrs == 0) {
1388
0
        if (H5O__msg_remove_real(loc->file, oh, H5O_MSG_AINFO, H5O_ALL, NULL, NULL, true) < 0)
1389
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTDELETE, FAIL, "unable to delete attribute info");
1390
0
    } /* end if */
1391
1392
0
done:
1393
    /* Release resources */
1394
0
    if (atable.attrs && H5A__attr_release_table(&atable) < 0)
1395
0
        HDONE_ERROR(H5E_ATTR, H5E_CANTFREE, FAIL, "unable to release attribute table");
1396
1397
0
    FUNC_LEAVE_NOAPI(ret_value)
1398
0
} /* end H5O__attr_remove_update() */
1399
1400
/*-------------------------------------------------------------------------
1401
 * Function:    H5O__attr_remove_cb
1402
 *
1403
 * Purpose:     Object header iterator callback routine to remove an
1404
 *              attribute stored compactly.
1405
 *
1406
 * Return:      SUCCEED/FAIL
1407
 *
1408
 *-------------------------------------------------------------------------
1409
 */
1410
static herr_t
1411
H5O__attr_remove_cb(H5O_t *oh, H5O_mesg_t *mesg /*in,out*/, unsigned H5_ATTR_UNUSED sequence,
1412
                    void *_udata /*in,out*/)
1413
0
{
1414
0
    H5O_iter_rm_t *udata     = (H5O_iter_rm_t *)_udata; /* Operator user data */
1415
0
    herr_t         ret_value = H5_ITER_CONT;            /* Return value */
1416
1417
0
    FUNC_ENTER_PACKAGE
1418
1419
    /* check args */
1420
0
    assert(oh);
1421
0
    assert(mesg);
1422
0
    assert(!udata->found);
1423
1424
    /* Check for correct attribute message to modify */
1425
0
    if (strcmp(((H5A_t *)mesg->native)->shared->name, udata->name) == 0) {
1426
        /* Convert message into a null message (i.e. delete it) */
1427
0
        if (H5O__release_mesg(udata->f, oh, mesg, true) < 0)
1428
0
            HGOTO_ERROR(H5E_OHDR, H5E_CANTDELETE, H5_ITER_ERROR, "unable to convert into null message");
1429
1430
        /* Indicate that the object header was modified */
1431
0
        oh->mesgs_modified = H5O_MODIFY_CONDENSE;
1432
1433
        /* Indicate that this message is the attribute to be deleted */
1434
0
        udata->found = true;
1435
1436
        /* Stop iterating */
1437
0
        ret_value = H5_ITER_STOP;
1438
0
    } /* end if */
1439
1440
0
done:
1441
0
    FUNC_LEAVE_NOAPI(ret_value)
1442
0
} /* end H5O__attr_remove_cb() */
1443
1444
/*-------------------------------------------------------------------------
1445
 * Function:    H5O__attr_remove
1446
 *
1447
 * Purpose:     Delete an attribute on an object.
1448
 *
1449
 * Return:      SUCCEED/FAIL
1450
 *
1451
 *-------------------------------------------------------------------------
1452
 */
1453
herr_t
1454
H5O__attr_remove(const H5O_loc_t *loc, const char *name)
1455
0
{
1456
0
    H5O_t      *oh = NULL;              /* Pointer to actual object header */
1457
0
    H5O_ainfo_t ainfo;                  /* Attribute information for object */
1458
0
    htri_t      ainfo_exists = false;   /* Whether the attribute info exists in the file */
1459
0
    herr_t      ret_value    = SUCCEED; /* Return value */
1460
1461
0
    FUNC_ENTER_PACKAGE_TAG(loc->addr)
1462
1463
    /* Check arguments */
1464
0
    assert(loc);
1465
0
    assert(name);
1466
1467
    /* Pin the object header */
1468
0
    if (NULL == (oh = H5O_pin(loc)))
1469
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTPIN, FAIL, "unable to pin object header");
1470
1471
    /* Check for attribute info stored */
1472
0
    ainfo.fheap_addr = HADDR_UNDEF;
1473
0
    if (oh->version > H5O_VERSION_1) {
1474
        /* Check for (& retrieve if available) attribute info */
1475
0
        if ((ainfo_exists = H5A__get_ainfo(loc->file, oh, &ainfo)) < 0)
1476
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't check for attribute info message");
1477
0
    } /* end if */
1478
1479
    /* Check for attributes stored densely */
1480
0
    if (H5_addr_defined(ainfo.fheap_addr)) {
1481
        /* Delete attribute from dense storage */
1482
0
        if (H5A__dense_remove(loc->file, &ainfo, name) < 0)
1483
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTDELETE, FAIL, "unable to delete attribute in dense storage");
1484
0
    } /* end if */
1485
0
    else {
1486
0
        H5O_iter_rm_t       udata; /* User data for callback */
1487
0
        H5O_mesg_operator_t op;    /* Wrapper for operator */
1488
1489
        /* Set up user data for callback */
1490
0
        udata.f     = loc->file;
1491
0
        udata.name  = name;
1492
0
        udata.found = false;
1493
1494
        /* Iterate over attributes, to locate correct one to delete */
1495
0
        op.op_type  = H5O_MESG_OP_LIB;
1496
0
        op.u.lib_op = H5O__attr_remove_cb;
1497
0
        if (H5O__msg_iterate_real(loc->file, oh, H5O_MSG_ATTR, &op, &udata) < 0)
1498
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTDELETE, FAIL, "error deleting attribute");
1499
1500
        /* Check that we found the attribute */
1501
0
        if (!udata.found)
1502
0
            HGOTO_ERROR(H5E_ATTR, H5E_NOTFOUND, FAIL, "can't locate attribute");
1503
0
    } /* end else */
1504
1505
    /* Update the attribute information after removing an attribute */
1506
0
    if (ainfo_exists)
1507
0
        if (H5O__attr_remove_update(loc, oh, &ainfo) < 0)
1508
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "unable to update attribute info");
1509
1510
    /* Update the modification time, if any */
1511
0
    if (H5O_touch_oh(loc->file, oh, false) < 0)
1512
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "unable to update time on object");
1513
1514
0
done:
1515
0
    if (oh && H5O_unpin(oh) < 0)
1516
0
        HDONE_ERROR(H5E_ATTR, H5E_CANTUNPIN, FAIL, "unable to unpin object header");
1517
1518
0
    FUNC_LEAVE_NOAPI_TAG(ret_value)
1519
0
} /* end H5O__attr_remove() */
1520
1521
/*-------------------------------------------------------------------------
1522
 * Function:    H5O__attr_remove_by_idx
1523
 *
1524
 * Purpose:     Delete an attribute on an object, according to an order within
1525
 *              an index.
1526
 *
1527
 * Return:      SUCCEED/FAIL
1528
 *
1529
 *-------------------------------------------------------------------------
1530
 */
1531
herr_t
1532
H5O__attr_remove_by_idx(const H5O_loc_t *loc, H5_index_t idx_type, H5_iter_order_t order, hsize_t n)
1533
0
{
1534
0
    H5O_t           *oh = NULL;                   /* Pointer to actual object header */
1535
0
    H5O_ainfo_t      ainfo;                       /* Attribute information for object */
1536
0
    htri_t           ainfo_exists = false;        /* Whether the attribute info exists in the file */
1537
0
    H5A_attr_table_t atable       = {0, 0, NULL}; /* Table of attributes */
1538
0
    herr_t           ret_value    = SUCCEED;      /* Return value */
1539
1540
0
    FUNC_ENTER_PACKAGE_TAG(loc->addr)
1541
1542
    /* Check arguments */
1543
0
    assert(loc);
1544
1545
    /* Pin the object header */
1546
0
    if (NULL == (oh = H5O_pin(loc)))
1547
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTPIN, FAIL, "unable to pin object header");
1548
1549
    /* Check for attribute info stored */
1550
0
    ainfo.fheap_addr = HADDR_UNDEF;
1551
0
    if (oh->version > H5O_VERSION_1) {
1552
        /* Check for (& retrieve if available) attribute info */
1553
0
        if ((ainfo_exists = H5A__get_ainfo(loc->file, oh, &ainfo)) < 0)
1554
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't check for attribute info message");
1555
0
    } /* end if */
1556
1557
    /* Check for attributes stored densely */
1558
0
    if (H5_addr_defined(ainfo.fheap_addr)) {
1559
        /* Delete attribute from dense storage */
1560
0
        if (H5A__dense_remove_by_idx(loc->file, &ainfo, idx_type, order, n) < 0)
1561
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTDELETE, FAIL, "unable to delete attribute in dense storage");
1562
0
    } /* end if */
1563
0
    else {
1564
0
        H5O_iter_rm_t       udata; /* User data for callback */
1565
0
        H5O_mesg_operator_t op;    /* Wrapper for operator */
1566
1567
        /* Build table of attributes for compact storage */
1568
0
        if (H5A__compact_build_table(loc->file, oh, idx_type, order, &atable) < 0)
1569
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, FAIL, "error building attribute table");
1570
1571
        /* Check for skipping too many attributes */
1572
0
        if (n >= atable.num_attrs)
1573
0
            HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid index specified");
1574
1575
        /* Set up user data for callback, to remove the attribute by name */
1576
0
        udata.f     = loc->file;
1577
0
        udata.name  = ((atable.attrs[n])->shared)->name;
1578
0
        udata.found = false;
1579
1580
        /* Iterate over attributes, to locate correct one to delete */
1581
0
        op.op_type  = H5O_MESG_OP_LIB;
1582
0
        op.u.lib_op = H5O__attr_remove_cb;
1583
0
        if (H5O__msg_iterate_real(loc->file, oh, H5O_MSG_ATTR, &op, &udata) < 0)
1584
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTDELETE, FAIL, "error deleting attribute");
1585
1586
        /* Check that we found the attribute */
1587
0
        if (!udata.found)
1588
0
            HGOTO_ERROR(H5E_ATTR, H5E_NOTFOUND, FAIL, "can't locate attribute");
1589
0
    } /* end else */
1590
1591
    /* Update the attribute information after removing an attribute */
1592
0
    if (ainfo_exists)
1593
0
        if (H5O__attr_remove_update(loc, oh, &ainfo) < 0)
1594
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "unable to update attribute info");
1595
1596
    /* Update the modification time, if any */
1597
0
    if (H5O_touch_oh(loc->file, oh, false) < 0)
1598
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "unable to update time on object");
1599
1600
0
done:
1601
0
    if (oh && H5O_unpin(oh) < 0)
1602
0
        HDONE_ERROR(H5E_ATTR, H5E_CANTUNPIN, FAIL, "unable to unpin object header");
1603
0
    if (atable.attrs && H5A__attr_release_table(&atable) < 0)
1604
0
        HDONE_ERROR(H5E_ATTR, H5E_CANTFREE, FAIL, "unable to release attribute table");
1605
1606
0
    FUNC_LEAVE_NOAPI_TAG(ret_value)
1607
0
} /* end H5O__attr_remove_by_idx() */
1608
1609
/*-------------------------------------------------------------------------
1610
 * Function:    H5O__attr_count_real
1611
 *
1612
 * Purpose:     Determine the # of attributes on an object
1613
 *
1614
 * Return:      SUCCEED/FAIL
1615
 *
1616
 *-------------------------------------------------------------------------
1617
 */
1618
herr_t
1619
H5O__attr_count_real(H5F_t *f, H5O_t *oh, hsize_t *nattrs)
1620
0
{
1621
0
    herr_t ret_value = SUCCEED; /* Return value */
1622
1623
0
    FUNC_ENTER_PACKAGE
1624
1625
    /* Check arguments */
1626
0
    assert(f);
1627
0
    assert(oh);
1628
0
    assert(nattrs);
1629
1630
    /* Check for attributes stored densely */
1631
0
    if (oh->version > H5O_VERSION_1) {
1632
0
        htri_t      ainfo_exists = false; /* Whether the attribute info exists in the file */
1633
0
        H5O_ainfo_t ainfo;                /* Attribute information for object */
1634
1635
        /* Attempt to get the attribute information from the object header */
1636
0
        if ((ainfo_exists = H5A__get_ainfo(f, oh, &ainfo)) < 0)
1637
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't check for attribute info message");
1638
0
        else if (ainfo_exists > 0)
1639
0
            *nattrs = ainfo.nattrs;
1640
0
        else
1641
0
            *nattrs = 0;
1642
0
    } /* end if */
1643
0
    else {
1644
0
        hsize_t  attr_count; /* Number of attributes found */
1645
0
        unsigned u;          /* Local index variable */
1646
1647
        /* Loop over all messages, counting the attributes */
1648
0
        attr_count = 0;
1649
0
        for (u = 0; u < oh->nmesgs; u++)
1650
0
            if (oh->mesg[u].type == H5O_MSG_ATTR)
1651
0
                attr_count++;
1652
0
        *nattrs = attr_count;
1653
0
    } /* end else */
1654
1655
0
done:
1656
0
    FUNC_LEAVE_NOAPI(ret_value)
1657
0
} /* end H5O__attr_count_real */
1658
1659
/*-------------------------------------------------------------------------
1660
 * Function:    H5O__attr_exists_cb
1661
 *
1662
 * Purpose:     Object header iterator callback routine to check for an
1663
 *              attribute stored compactly, by name.
1664
 *
1665
 * Return:      SUCCEED/FAIL
1666
 *
1667
 *-------------------------------------------------------------------------
1668
 */
1669
static herr_t
1670
H5O__attr_exists_cb(H5O_t H5_ATTR_UNUSED *oh, H5O_mesg_t *mesg /*in,out*/, unsigned H5_ATTR_UNUSED sequence,
1671
                    void *_udata /*in,out*/)
1672
0
{
1673
0
    H5O_iter_xst_t *udata     = (H5O_iter_xst_t *)_udata; /* Operator user data */
1674
0
    herr_t          ret_value = H5_ITER_CONT;             /* Return value */
1675
1676
0
    FUNC_ENTER_PACKAGE_NOERR
1677
1678
    /* check args */
1679
0
    assert(mesg);
1680
0
    assert(udata->exists && !*udata->exists);
1681
1682
    /* Check for correct attribute message */
1683
0
    if (strcmp(((H5A_t *)mesg->native)->shared->name, udata->name) == 0) {
1684
        /* Indicate that this message is the attribute sought */
1685
0
        *udata->exists = true;
1686
1687
        /* Stop iterating */
1688
0
        ret_value = H5_ITER_STOP;
1689
0
    } /* end if */
1690
1691
0
    FUNC_LEAVE_NOAPI(ret_value)
1692
0
} /* end H5O__attr_exists_cb() */
1693
1694
/*-------------------------------------------------------------------------
1695
 * Function:    H5O__attr_exists
1696
 *
1697
 * Purpose:     Determine if an attribute with a particular name exists on an object
1698
 *
1699
 * Return:      SUCCEED/FAIL
1700
 *
1701
 *-------------------------------------------------------------------------
1702
 */
1703
herr_t
1704
H5O__attr_exists(const H5O_loc_t *loc, const char *name, bool *attr_exists)
1705
0
{
1706
0
    H5O_t      *oh = NULL;           /* Pointer to actual object header */
1707
0
    H5O_ainfo_t ainfo;               /* Attribute information for object */
1708
0
    herr_t      ret_value = SUCCEED; /* Return value */
1709
1710
0
    FUNC_ENTER_PACKAGE_TAG(loc->addr)
1711
1712
    /* Check arguments */
1713
0
    assert(loc);
1714
0
    assert(name);
1715
0
    assert(attr_exists);
1716
1717
    /* Protect the object header to iterate over */
1718
0
    if (NULL == (oh = H5O_protect(loc, H5AC__READ_ONLY_FLAG, false)))
1719
0
        HGOTO_ERROR(H5E_ATTR, H5E_CANTPROTECT, FAIL, "unable to load object header");
1720
1721
    /* Check for attribute info stored */
1722
0
    ainfo.fheap_addr = HADDR_UNDEF;
1723
0
    if (oh->version > H5O_VERSION_1) {
1724
        /* Check for (& retrieve if available) attribute info */
1725
0
        if (H5A__get_ainfo(loc->file, oh, &ainfo) < 0)
1726
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't check for attribute info message");
1727
0
    } /* end if */
1728
1729
    /* Check for attributes stored densely */
1730
0
    if (H5_addr_defined(ainfo.fheap_addr)) {
1731
        /* Check if attribute exists in dense storage */
1732
0
        if (H5A__dense_exists(loc->file, &ainfo, name, attr_exists) < 0)
1733
0
            HGOTO_ERROR(H5E_ATTR, H5E_BADITER, FAIL, "error checking for existence of attribute");
1734
0
    } /* end if */
1735
0
    else {
1736
0
        H5O_iter_xst_t      udata; /* User data for callback */
1737
0
        H5O_mesg_operator_t op;    /* Wrapper for operator */
1738
1739
        /* Set up user data for callback */
1740
0
        udata.name   = name;
1741
0
        udata.exists = attr_exists;
1742
1743
        /* Iterate over existing attributes, checking for attribute with same name */
1744
0
        op.op_type  = H5O_MESG_OP_LIB;
1745
0
        op.u.lib_op = H5O__attr_exists_cb;
1746
0
        if (H5O__msg_iterate_real(loc->file, oh, H5O_MSG_ATTR, &op, &udata) < 0)
1747
0
            HGOTO_ERROR(H5E_ATTR, H5E_BADITER, FAIL, "error checking for existence of attribute");
1748
0
    } /* end else */
1749
1750
0
done:
1751
0
    if (oh && H5O_unprotect(loc, oh, H5AC__NO_FLAGS_SET) < 0)
1752
0
        HDONE_ERROR(H5E_ATTR, H5E_CANTUNPROTECT, FAIL, "unable to release object header");
1753
1754
0
    FUNC_LEAVE_NOAPI_TAG(ret_value)
1755
0
} /* end H5O__attr_exists() */
1756
1757
/*-------------------------------------------------------------------------
1758
 * Function:    H5O__attr_bh_info
1759
 *
1760
 * Purpose:     For 1.8 attribute, returns storage amount for btree and fractal heap
1761
 *
1762
 * Return:      SUCCEED/FAIL
1763
 *
1764
 *-------------------------------------------------------------------------
1765
 */
1766
herr_t
1767
H5O__attr_bh_info(H5F_t *f, H5O_t *oh, H5_ih_info_t *bh_info)
1768
0
{
1769
0
    H5HF_t *fheap      = NULL;    /* Fractal heap handle */
1770
0
    H5B2_t *bt2_name   = NULL;    /* v2 B-tree handle for name index */
1771
0
    H5B2_t *bt2_corder = NULL;    /* v2 B-tree handle for creation order index */
1772
0
    herr_t  ret_value  = SUCCEED; /* Return value */
1773
1774
0
    FUNC_ENTER_PACKAGE
1775
1776
0
    assert(f);
1777
0
    assert(oh);
1778
0
    assert(bh_info);
1779
1780
    /* Attributes are only stored in fractal heap & indexed w/v2 B-tree in later versions */
1781
0
    if (oh->version > H5O_VERSION_1) {
1782
0
        H5O_ainfo_t ainfo;                /* Attribute information for object */
1783
0
        htri_t      ainfo_exists = false; /* Whether the attribute info exists in the file */
1784
1785
        /* Check for (& retrieve if available) attribute info */
1786
0
        if ((ainfo_exists = H5A__get_ainfo(f, oh, &ainfo)) < 0)
1787
0
            HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't check for attribute info message");
1788
0
        else if (ainfo_exists > 0) {
1789
            /* Check if name index available */
1790
0
            if (H5_addr_defined(ainfo.name_bt2_addr)) {
1791
                /* Open the name index v2 B-tree */
1792
0
                if (NULL == (bt2_name = H5B2_open(f, ainfo.name_bt2_addr, NULL)))
1793
0
                    HGOTO_ERROR(H5E_ATTR, H5E_CANTOPENOBJ, FAIL, "unable to open v2 B-tree for name index");
1794
1795
                /* Get name index B-tree size */
1796
0
                if (H5B2_size(bt2_name, &(bh_info->index_size)) < 0)
1797
0
                    HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't retrieve B-tree storage info");
1798
0
            } /* end if */
1799
1800
            /* Check if creation order index available */
1801
0
            if (H5_addr_defined(ainfo.corder_bt2_addr)) {
1802
                /* Open the creation order index v2 B-tree */
1803
0
                if (NULL == (bt2_corder = H5B2_open(f, ainfo.corder_bt2_addr, NULL)))
1804
0
                    HGOTO_ERROR(H5E_ATTR, H5E_CANTOPENOBJ, FAIL,
1805
0
                                "unable to open v2 B-tree for creation order index");
1806
1807
                /* Get creation order index B-tree size */
1808
0
                if (H5B2_size(bt2_corder, &(bh_info->index_size)) < 0)
1809
0
                    HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't retrieve B-tree storage info");
1810
0
            } /* end if */
1811
1812
            /* Get storage size of fractal heap, if it's used */
1813
0
            if (H5_addr_defined(ainfo.fheap_addr)) {
1814
                /* Open the fractal heap for attributes */
1815
0
                if (NULL == (fheap = H5HF_open(f, ainfo.fheap_addr)))
1816
0
                    HGOTO_ERROR(H5E_ATTR, H5E_CANTOPENOBJ, FAIL, "unable to open fractal heap");
1817
1818
                /* Get heap storage size */
1819
0
                if (H5HF_size(fheap, &(bh_info->heap_size)) < 0)
1820
0
                    HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't retrieve B-tree storage info");
1821
0
            } /* end if */
1822
0
        }     /* end else */
1823
0
    }         /* end if */
1824
1825
0
done:
1826
    /* Release resources */
1827
0
    if (fheap && H5HF_close(fheap) < 0)
1828
0
        HDONE_ERROR(H5E_ATTR, H5E_CANTCLOSEOBJ, FAIL, "can't close fractal heap");
1829
0
    if (bt2_name && H5B2_close(bt2_name) < 0)
1830
0
        HDONE_ERROR(H5E_ATTR, H5E_CANTCLOSEOBJ, FAIL, "can't close v2 B-tree for name index");
1831
0
    if (bt2_corder && H5B2_close(bt2_corder) < 0)
1832
0
        HDONE_ERROR(H5E_ATTR, H5E_CANTCLOSEOBJ, FAIL, "can't close v2 B-tree for creation order index");
1833
1834
0
    FUNC_LEAVE_NOAPI(ret_value)
1835
0
} /* H5O__attr_bh_info() */