forked from HDFGroup/hdf5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
H5Oattribute.c
1902 lines (1610 loc) · 76.5 KB
/
H5Oattribute.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the COPYING file, which can be found at the root of the source code *
* distribution tree, or in https://www.hdfgroup.org/licenses. *
* If you do not have access to either file, you may request a copy from *
* help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*-------------------------------------------------------------------------
*
* Created: H5Oattribute.c
*
* Purpose: Object header attribute routines.
*
*-------------------------------------------------------------------------
*/
/****************/
/* Module Setup */
/****************/
#define H5A_FRIEND /* Suppress error about including H5Apkg.h */
#include "H5Omodule.h" /* This source code file is part of the H5O module */
/***********/
/* Headers */
/***********/
#include "H5private.h" /* Generic Functions */
#include "H5Apkg.h" /* Attributes */
#include "H5Eprivate.h" /* Error handling */
#include "H5MMprivate.h" /* Memory management */
#include "H5Opkg.h" /* Object headers */
#include "H5SMprivate.h" /* Shared Object Header Messages */
#include "H5Iprivate.h" /* IDs */
#include "H5Fprivate.h" /* File */
/****************/
/* Local Macros */
/****************/
/******************/
/* Local Typedefs */
/******************/
/* User data for iteration when converting attributes to dense storage */
typedef struct {
H5F_t *f; /* Pointer to file for insertion */
H5O_ainfo_t *ainfo; /* Attribute info struct */
} H5O_iter_cvt_t;
/* User data for iteration when opening an attribute */
typedef struct {
/* down */
const char *name; /* Name of attribute to open */
/* up */
H5A_t *attr; /* Attribute data to update object header with */
} H5O_iter_opn_t;
/* User data for iteration when updating an attribute */
typedef struct {
/* down */
H5F_t *f; /* Pointer to file attribute is in */
H5A_t *attr; /* Attribute data to update object header with */
/* up */
hbool_t found; /* Whether the attribute was found */
} H5O_iter_wrt_t;
/* User data for iteration when renaming an attribute */
typedef struct {
/* down */
H5F_t *f; /* Pointer to file attribute is in */
const char *old_name; /* Old name of attribute */
const char *new_name; /* New name of attribute */
/* up */
hbool_t found; /* Whether the attribute was found */
} H5O_iter_ren_t;
/* User data for iteration when removing an attribute */
typedef struct {
/* down */
H5F_t *f; /* Pointer to file attribute is in */
const char *name; /* Name of attribute to open */
/* up */
hbool_t found; /* Found attribute to delete */
} H5O_iter_rm_t;
/* User data for iteration when checking if an attribute exists */
typedef struct {
/* down */
const char *name; /* Name of attribute to open */
/* up */
hbool_t *exists; /* Pointer to flag to indicate attribute exists */
} H5O_iter_xst_t;
/********************/
/* Package Typedefs */
/********************/
/********************/
/* Local Prototypes */
/********************/
static herr_t H5O__attr_to_dense_cb(H5O_t *oh, H5O_mesg_t *mesg, unsigned H5_ATTR_UNUSED sequence,
unsigned *oh_modified, void *_udata);
static htri_t H5O__attr_find_opened_attr(const H5O_loc_t *loc, H5A_t **attr, const char *name_to_open);
static herr_t H5O__attr_open_cb(H5O_t *oh, H5O_mesg_t *mesg, unsigned sequence,
unsigned H5_ATTR_UNUSED *oh_modified, void *_udata);
static herr_t H5O__attr_open_by_idx_cb(const H5A_t *attr, void *_ret_attr);
static herr_t H5O__attr_write_cb(H5O_t *oh, H5O_mesg_t *mesg, unsigned H5_ATTR_UNUSED sequence,
unsigned *oh_modified, void *_udata);
static herr_t H5O__attr_rename_chk_cb(H5O_t H5_ATTR_UNUSED *oh, H5O_mesg_t *mesg,
unsigned H5_ATTR_UNUSED sequence, unsigned H5_ATTR_UNUSED *oh_modified,
void *_udata);
static herr_t H5O__attr_rename_mod_cb(H5O_t *oh, H5O_mesg_t *mesg, unsigned H5_ATTR_UNUSED sequence,
unsigned *oh_modified, void *_udata);
static herr_t H5O__attr_remove_update(const H5O_loc_t *loc, H5O_t *oh, H5O_ainfo_t *ainfo);
static herr_t H5O__attr_remove_cb(H5O_t *oh, H5O_mesg_t *mesg, unsigned H5_ATTR_UNUSED sequence,
unsigned *oh_modified, void *_udata);
static herr_t H5O__attr_exists_cb(H5O_t H5_ATTR_UNUSED *oh, H5O_mesg_t *mesg,
unsigned H5_ATTR_UNUSED sequence, unsigned H5_ATTR_UNUSED *oh_modified,
void *_udata);
/*********************/
/* Package Variables */
/*********************/
/*****************************/
/* Library Private Variables */
/*****************************/
/*******************/
/* Local Variables */
/*******************/
/*-------------------------------------------------------------------------
* Function: H5O__attr_to_dense_cb
*
* Purpose: Object header iterator callback routine to convert compact
* attributes to dense attributes
*
* Return: SUCCEED/FAIL
*
* Programmer: Quincey Koziol
* Dec 4 2006
*
*-------------------------------------------------------------------------
*/
static herr_t
H5O__attr_to_dense_cb(H5O_t *oh, H5O_mesg_t *mesg /*in,out*/, unsigned H5_ATTR_UNUSED sequence,
unsigned *oh_modified, void *_udata /*in,out*/)
{
H5O_iter_cvt_t *udata = (H5O_iter_cvt_t *)_udata; /* Operator user data */
H5A_t *attr = (H5A_t *)mesg->native; /* Pointer to attribute to insert */
herr_t ret_value = H5_ITER_CONT; /* Return value */
FUNC_ENTER_PACKAGE
/* check args */
HDassert(oh);
HDassert(mesg);
HDassert(udata);
HDassert(udata->f);
HDassert(udata->ainfo);
HDassert(attr);
/* Insert attribute into dense storage */
if (H5A__dense_insert(udata->f, udata->ainfo, attr) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_CANTINSERT, H5_ITER_ERROR, "unable to add to dense storage")
/* Convert message into a null message in the header */
/* (don't delete attribute's space in the file though) */
if (H5O__release_mesg(udata->f, oh, mesg, FALSE) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_CANTDELETE, H5_ITER_ERROR, "unable to convert into null message")
/* Indicate that the object header was modified */
*oh_modified = H5O_MODIFY_CONDENSE;
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O__attr_to_dense_cb() */
/*-------------------------------------------------------------------------
* Function: H5O__attr_create
*
* Purpose: Create a new attribute in the object header.
*
* Return: SUCCEED/FAIL
*
* Programmer: Quincey Koziol
* Friday, December 8, 2006
*
*-------------------------------------------------------------------------
*/
herr_t
H5O__attr_create(const H5O_loc_t *loc, H5A_t *attr)
{
H5O_t *oh = NULL; /* Pointer to actual object header */
H5O_ainfo_t ainfo; /* Attribute information for object */
htri_t shared_mesg; /* Should this message be stored in the Shared Message table? */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI_NOINIT
/* Check arguments */
HDassert(loc);
HDassert(attr);
/* Pin the object header */
if (NULL == (oh = H5O_pin(loc)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTPIN, FAIL, "unable to pin object header")
/* Check if this object already has attribute information */
if (oh->version > H5O_VERSION_1) {
hbool_t new_ainfo = FALSE; /* Flag to indicate that the attribute information is new */
htri_t ainfo_exists; /* Whether the attribute info was retrieved */
/* Check for (& retrieve if available) attribute info */
if ((ainfo_exists = H5A__get_ainfo(loc->file, oh, &ainfo)) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't check for attribute info message")
if (!ainfo_exists) {
/* Initialize attribute information */
ainfo.track_corder = (hbool_t)((oh->flags & H5O_HDR_ATTR_CRT_ORDER_TRACKED) ? TRUE : FALSE);
ainfo.index_corder = (hbool_t)((oh->flags & H5O_HDR_ATTR_CRT_ORDER_INDEXED) ? TRUE : FALSE);
ainfo.max_crt_idx = 0;
ainfo.corder_bt2_addr = HADDR_UNDEF;
ainfo.nattrs = 0;
ainfo.fheap_addr = HADDR_UNDEF;
ainfo.name_bt2_addr = HADDR_UNDEF;
/* Set flag to add attribute information to object header */
new_ainfo = TRUE;
} /* end if */
else {
/* Sanity check attribute info read in */
HDassert(ainfo.nattrs > 0);
HDassert(ainfo.track_corder == ((oh->flags & H5O_HDR_ATTR_CRT_ORDER_TRACKED) > 0));
HDassert(ainfo.index_corder == ((oh->flags & H5O_HDR_ATTR_CRT_ORDER_INDEXED) > 0));
} /* end else */
/* Check if switching to "dense" attribute storage is possible */
if (!H5F_addr_defined(ainfo.fheap_addr)) {
htri_t shareable; /* Whether the attribute will be shared */
size_t raw_size = 0; /* Raw size of message */
/* Check for attribute being shareable */
if ((shareable = H5SM_can_share(loc->file, NULL, NULL, H5O_ATTR_ID, attr)) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_BADMESG, FAIL, "can't determine attribute sharing status")
else if (shareable == FALSE) {
/* Compute the size needed to encode the attribute */
raw_size = (H5O_MSG_ATTR->raw_size)(loc->file, FALSE, attr);
} /* end if */
/* Check for condititions for switching to "dense" attribute storage are met */
if (ainfo.nattrs == oh->max_compact || (!shareable && raw_size >= H5O_MESG_MAX_SIZE)) {
H5O_iter_cvt_t udata; /* User data for callback */
H5O_mesg_operator_t op; /* Wrapper for operator */
/* Create dense storage for attributes */
if (H5A__dense_create(loc->file, &ainfo) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_CANTINIT, FAIL, "unable to create dense storage for attributes")
/* Set up user data for callback */
udata.f = loc->file;
udata.ainfo = &ainfo;
/* Iterate over existing attributes, moving them to dense storage */
op.op_type = H5O_MESG_OP_LIB;
op.u.lib_op = H5O__attr_to_dense_cb;
if (H5O__msg_iterate_real(loc->file, oh, H5O_MSG_ATTR, &op, &udata) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTCONVERT, FAIL,
"error converting attributes to dense storage")
} /* end if */
} /* end if */
/* Increment attribute count on object */
ainfo.nattrs++;
/* Check whether we're tracking the creation index on attributes */
if (ainfo.track_corder) {
/* Check for attribute creation order index on the object wrapping around */
if (ainfo.max_crt_idx == H5O_MAX_CRT_ORDER_IDX)
HGOTO_ERROR(H5E_ATTR, H5E_CANTINC, FAIL, "attribute creation index can't be incremented")
/* Set the creation order index on the attribute & incr. creation order index */
attr->shared->crt_idx = ainfo.max_crt_idx++;
} /* end if */
else
/* Set "bogus" creation index for attribute */
attr->shared->crt_idx = H5O_MAX_CRT_ORDER_IDX;
/* Add the attribute information message, if one is needed */
if (new_ainfo) {
if (H5O__msg_append_real(loc->file, oh, H5O_MSG_AINFO, H5O_MSG_FLAG_DONTSHARE, 0, &ainfo) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTINSERT, FAIL, "unable to create new attribute info message")
} /* end if */
/* Otherwise, update existing message */
else if (H5O__msg_write_real(loc->file, oh, H5O_MSG_AINFO, H5O_MSG_FLAG_DONTSHARE, 0, &ainfo) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "unable to update attribute info message")
} /* end if */
else {
/* Set "bogus" creation index for attribute */
attr->shared->crt_idx = H5O_MAX_CRT_ORDER_IDX;
/* Set attribute info value to get attribute into object header */
ainfo.fheap_addr = HADDR_UNDEF;
} /* end else */
/* Check for storing attribute with dense storage */
if (H5F_addr_defined(ainfo.fheap_addr)) {
/* Insert attribute into dense storage */
if (H5A__dense_insert(loc->file, &ainfo, attr) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTINSERT, FAIL, "unable to add to dense storage")
} /* end if */
else
/* Append new message to object header */
if (H5O__msg_append_real(loc->file, oh, H5O_MSG_ATTR, 0, 0, attr) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTINSERT, FAIL, "unable to create new attribute in header")
/* Increment reference count for shared attribute object for the
* object handle created by the caller function H5A__create. The count
* for the cached object header has been incremented in the step above
* (in H5O__msg_append_real). The dense storage doesn't need a count.
*/
attr->shared->nrefs += 1;
/* Was new attribute shared? */
if ((shared_mesg = H5O_msg_is_shared(H5O_ATTR_ID, attr)) > 0) {
hsize_t attr_rc; /* Attribute's ref count in shared message storage */
/* Retrieve ref count for shared attribute */
if (H5SM_get_refcount(loc->file, H5O_ATTR_ID, &attr->sh_loc, &attr_rc) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't retrieve shared message ref count")
/* If this is not the first copy of the attribute in the shared message
* storage, decrement the reference count on any shared components
* of the attribute. This is done because the shared message
* storage's "try delete" call doesn't call the message class's
* "delete" callback until the reference count drops to zero.
* However, attributes have already increased the reference
* count on shared components before passing the attribute
* to the shared message code to manage, causing an asymmetry
* in the reference counting for any shared components.
*
* The alternate solution is to have the shared message's "try
* delete" code always call the message class's "delete" callback,
* even when the reference count is positive. This can be done
* without an appreciable performance hit (by using H5HF_op() in
* the shared message comparison v2 B-tree callback), but it has
* the undesirable side-effect of leaving the reference count on
* the attribute's shared components artificially (and possibly
* misleadingly) high, because there's only one shared attribute
* referencing the shared components, not <refcount for the
* shared attribute> objects referencing the shared components.
*
* *ick* -QAK, 2007/01/08
*/
if (attr_rc > 1) {
if (H5O__attr_delete(loc->file, oh, attr) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTDELETE, FAIL, "unable to delete attribute")
} /* end if */
} /* end if */
else if (shared_mesg < 0)
HGOTO_ERROR(H5E_ATTR, H5E_WRITEERROR, FAIL, "error determining if message should be shared")
/* Update the modification time, if any */
if (H5O_touch_oh(loc->file, oh, FALSE) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "unable to update time on object")
done:
if (oh && H5O_unpin(oh) < 0)
HDONE_ERROR(H5E_ATTR, H5E_CANTUNPIN, FAIL, "unable to unpin object header")
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O__attr_create() */
/*-------------------------------------------------------------------------
* Function: H5O__attr_open_cb
*
* Purpose: Object header iterator callback routine to open an
* attribute stored compactly.
*
* Return: SUCCEED/FAIL
*
* Programmer: Quincey Koziol
* Dec 11 2006
*
*-------------------------------------------------------------------------
*/
static herr_t
H5O__attr_open_cb(H5O_t *oh, H5O_mesg_t *mesg /*in,out*/, unsigned sequence,
unsigned H5_ATTR_UNUSED *oh_modified, void *_udata /*in,out*/)
{
H5O_iter_opn_t *udata = (H5O_iter_opn_t *)_udata; /* Operator user data */
herr_t ret_value = H5_ITER_CONT; /* Return value */
FUNC_ENTER_PACKAGE
/* check args */
HDassert(oh);
HDassert(mesg);
HDassert(!udata->attr);
/* Check for correct attribute message to modify */
if (HDstrcmp(((H5A_t *)mesg->native)->shared->name, udata->name) == 0) {
/* Make a copy of the attribute to return */
if (NULL == (udata->attr = H5A__copy(NULL, (H5A_t *)mesg->native)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTCOPY, H5_ITER_ERROR, "unable to copy attribute")
/* Assign [somewhat arbitrary] creation order value, for older versions
* of the format or if creation order is not tracked */
if (oh->version == H5O_VERSION_1 || !(oh->flags & H5O_HDR_ATTR_CRT_ORDER_TRACKED))
udata->attr->shared->crt_idx = sequence;
/* Stop iterating */
ret_value = H5_ITER_STOP;
} /* end if */
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O__attr_open_cb() */
/*-------------------------------------------------------------------------
* Function: H5O__attr_open_by_name
*
* Purpose: Open an existing attribute in an object header.
*
* Return: SUCCEED/FAIL
*
* Programmer: Quincey Koziol
* Monday, December 11, 2006
*
*-------------------------------------------------------------------------
*/
H5A_t *
H5O__attr_open_by_name(const H5O_loc_t *loc, const char *name)
{
H5O_t *oh = NULL; /* Pointer to actual object header */
H5O_ainfo_t ainfo; /* Attribute information for object */
H5A_t *exist_attr = NULL; /* Existing opened attribute object */
H5A_t *opened_attr = NULL; /* Newly opened attribute object */
htri_t found_open_attr = FALSE; /* Whether opened object is found */
H5A_t *ret_value = NULL; /* Return value */
FUNC_ENTER_PACKAGE_TAG(loc->addr)
/* Check arguments */
HDassert(loc);
HDassert(name);
/* Protect the object header to iterate over */
if (NULL == (oh = H5O_protect(loc, H5AC__READ_ONLY_FLAG, FALSE)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTPROTECT, NULL, "unable to load object header")
/* Check for attribute info stored */
ainfo.fheap_addr = HADDR_UNDEF;
if (oh->version > H5O_VERSION_1) {
/* Check for (& retrieve if available) attribute info */
if (H5A__get_ainfo(loc->file, oh, &ainfo) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, NULL, "can't check for attribute info message")
} /* end if */
/* If found the attribute is already opened, make a copy of it to share the
* object information. If not, open attribute as a new object
*/
if ((found_open_attr = H5O__attr_find_opened_attr(loc, &exist_attr, name)) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, NULL, "failed in finding opened attribute")
else if (found_open_attr == TRUE) {
if (NULL == (opened_attr = H5A__copy(NULL, exist_attr)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTCOPY, NULL, "can't copy existing attribute")
} /* end else if */
else {
/* Check for attributes in dense storage */
if (H5F_addr_defined(ainfo.fheap_addr)) {
/* Open attribute with dense storage */
if (NULL == (opened_attr = H5A__dense_open(loc->file, &ainfo, name)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTOPENOBJ, NULL, "can't open attribute")
} /* end if */
else {
H5O_iter_opn_t udata; /* User data for callback */
H5O_mesg_operator_t op; /* Wrapper for operator */
/* Set up user data for callback */
udata.name = name;
udata.attr = NULL;
/* Iterate over attributes, to locate correct one to open */
op.op_type = H5O_MESG_OP_LIB;
op.u.lib_op = H5O__attr_open_cb;
if (H5O__msg_iterate_real(loc->file, oh, H5O_MSG_ATTR, &op, &udata) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTOPENOBJ, NULL, "error updating attribute")
/* Check that we found the attribute */
if (!udata.attr)
HGOTO_ERROR(H5E_ATTR, H5E_NOTFOUND, NULL, "can't locate attribute: '%s'", name)
/* Get attribute opened from object header */
HDassert(udata.attr);
opened_attr = udata.attr;
} /* end else */
/* Mark datatype as being on disk now */
if (H5T_set_loc(opened_attr->shared->dt, H5F_VOL_OBJ(loc->file), H5T_LOC_DISK) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, NULL, "invalid datatype location")
} /* end else */
/* Set return value */
ret_value = opened_attr;
done:
if (oh && H5O_unprotect(loc, oh, H5AC__NO_FLAGS_SET) < 0)
HDONE_ERROR(H5E_ATTR, H5E_CANTUNPROTECT, NULL, "unable to release object header")
/* Release any resources, on error */
if (NULL == ret_value && opened_attr)
if (H5A__close(opened_attr) < 0)
HDONE_ERROR(H5E_ATTR, H5E_CANTCLOSEOBJ, NULL, "can't close attribute")
FUNC_LEAVE_NOAPI_TAG(ret_value)
} /* end H5O__attr_open_by_name() */
/*-------------------------------------------------------------------------
* Function: H5O__attr_open_by_idx_cb
*
* Purpose: Callback routine opening an attribute by index
*
* Return: SUCCEED/FAIL
*
* Programmer: Quincey Koziol
* Dec 18 2006
*
*-------------------------------------------------------------------------
*/
static herr_t
H5O__attr_open_by_idx_cb(const H5A_t *attr, void *_ret_attr)
{
H5A_t **ret_attr = (H5A_t **)_ret_attr; /* 'User data' passed in */
herr_t ret_value = H5_ITER_STOP; /* Return value */
FUNC_ENTER_PACKAGE
/* check arguments */
HDassert(attr);
HDassert(ret_attr);
/* Copy attribute information. Shared some attribute information. */
if (NULL == (*ret_attr = H5A__copy(NULL, attr)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTCOPY, H5_ITER_ERROR, "can't copy attribute")
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O__attr_open_by_idx_cb() */
/*-------------------------------------------------------------------------
* Function: H5O__attr_open_by_idx
*
* Purpose: Open an existing attribute in an object header according to
* an index.
*
* Return: SUCCEED/FAIL
*
* Programmer: Quincey Koziol
* Monday, December 18, 2006
*
*-------------------------------------------------------------------------
*/
H5A_t *
H5O__attr_open_by_idx(const H5O_loc_t *loc, H5_index_t idx_type, H5_iter_order_t order, hsize_t n)
{
H5A_attr_iter_op_t attr_op; /* Attribute operator */
H5A_t *exist_attr = NULL; /* Existing opened attribute object */
H5A_t *opened_attr = NULL; /* Newly opened attribute object */
htri_t found_open_attr = FALSE; /* Whether opened object is found */
H5A_t *ret_value = NULL; /* Return value */
FUNC_ENTER_PACKAGE
/* Check arguments */
HDassert(loc);
/* Build attribute operator info */
attr_op.op_type = H5A_ATTR_OP_LIB;
attr_op.u.lib_op = H5O__attr_open_by_idx_cb;
/* Iterate over attributes to locate correct one */
if (H5O_attr_iterate_real((hid_t)-1, loc, idx_type, order, n, NULL, &attr_op, &opened_attr) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_BADITER, NULL, "can't locate attribute")
/* Find out whether it has already been opened. If it has, close the object
* and make a copy of the already opened object to share the object info.
*/
if (opened_attr) {
if ((found_open_attr = H5O__attr_find_opened_attr(loc, &exist_attr, opened_attr->shared->name)) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, NULL, "failed in finding opened attribute")
/* If found that the attribute is already opened, make a copy of it
* and close the object just opened.
*/
if (found_open_attr && exist_attr) {
if (H5A__close(opened_attr) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTCLOSEOBJ, NULL, "can't close attribute")
if (NULL == (opened_attr = H5A__copy(NULL, exist_attr)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTCOPY, NULL, "can't copy existing attribute")
}
else {
/* Mark datatype as being on disk now */
if (H5T_set_loc(opened_attr->shared->dt, H5F_VOL_OBJ(loc->file), H5T_LOC_DISK) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, NULL, "invalid datatype location")
} /* end if */
} /* end if */
/* Set return value */
ret_value = opened_attr;
done:
/* Release any resources, on error */
if (NULL == ret_value && opened_attr)
if (H5A__close(opened_attr) < 0)
HDONE_ERROR(H5E_ATTR, H5E_CANTCLOSEOBJ, NULL, "can't close attribute")
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O__attr_open_by_idx() */
/*-------------------------------------------------------------------------
* Function: H5O__attr_find_opened_attr
*
* Purpose: Find out whether an attribute has been opened by giving
* the name. Return the pointer to the object if found.
*
* Return: TRUE: found the already opened object
* FALSE: didn't find the opened object
* FAIL: function failed.
*
* Programmer: Raymond Lu
* 23 June 2008
*
*-------------------------------------------------------------------------
*/
static htri_t
H5O__attr_find_opened_attr(const H5O_loc_t *loc, H5A_t **attr, const char *name_to_open)
{
hid_t *attr_id_list = NULL; /* List of IDs for opened attributes */
unsigned long loc_fnum; /* File serial # for object */
size_t num_open_attr; /* Number of opened attributes */
htri_t ret_value = FALSE; /* Return value */
FUNC_ENTER_PACKAGE
/* Get file serial number for the location of attribute */
if (H5F_get_fileno(loc->file, &loc_fnum) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_BADVALUE, FAIL, "can't get file serial number")
/* Count all opened attributes */
if (H5F_get_obj_count(loc->file, H5F_OBJ_ATTR | H5F_OBJ_LOCAL, FALSE, &num_open_attr) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't count opened attributes")
/* Find out whether the attribute has been opened */
if (num_open_attr) {
size_t check_num_attr; /* Number of open attribute IDs */
size_t u; /* Local index variable */
/* Allocate space for the attribute ID list */
if (NULL == (attr_id_list = (hid_t *)H5MM_malloc(num_open_attr * sizeof(hid_t))))
HGOTO_ERROR(H5E_ATTR, H5E_CANTALLOC, FAIL, "unable to allocate memory for attribute ID list")
/* Retrieve the IDs of all opened attributes */
if (H5F_get_obj_ids(loc->file, H5F_OBJ_ATTR | H5F_OBJ_LOCAL, num_open_attr, attr_id_list, FALSE,
&check_num_attr) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't get IDs of opened attributes")
if (check_num_attr != num_open_attr)
HGOTO_ERROR(H5E_ATTR, H5E_BADITER, FAIL, "open attribute count mismatch")
/* Iterate over the attributes */
for (u = 0; u < num_open_attr; u++) {
unsigned long attr_fnum; /* Attributes file serial number */
/* Get pointer to attribute */
if (NULL == (*attr = (H5A_t *)H5VL_object_verify(attr_id_list[u], H5I_ATTR)))
HGOTO_ERROR(H5E_ATTR, H5E_BADTYPE, FAIL, "not an attribute")
/* Get file serial number for attribute */
if (H5F_get_fileno((*attr)->oloc.file, &attr_fnum) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_BADVALUE, FAIL, "can't get file serial number")
/* Verify whether it's the right object. The attribute name, object
* address to which the attribute is attached, and file serial
* number should all match.
*/
if (!HDstrcmp(name_to_open, (*attr)->shared->name) && loc->addr == (*attr)->oloc.addr &&
loc_fnum == attr_fnum) {
ret_value = TRUE;
break;
} /* end if */
} /* end for */
} /* end if */
done:
if (attr_id_list)
H5MM_free(attr_id_list);
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O__attr_find_opened_attr() */
/*-------------------------------------------------------------------------
* Function: H5O__attr_update_shared
*
* Purpose: Update a shared attribute.
*
* Return: SUCCEED/FAIL
*
* Programmer: Quincey Koziol
* Jan 2 2007
*
*-------------------------------------------------------------------------
*/
herr_t
H5O__attr_update_shared(H5F_t *f, H5O_t *oh, H5A_t *attr, H5O_shared_t *update_sh_mesg)
{
H5O_shared_t sh_mesg; /* Shared object header message */
hsize_t attr_rc; /* Attribute's ref count in shared message storage */
htri_t shared_mesg; /* Whether the message should be shared */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* check args */
HDassert(f);
HDassert(attr);
/* Extract shared message info from current attribute (for later use) */
if (H5O_set_shared(&sh_mesg, &(attr->sh_loc)) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTCOPY, FAIL, "can't get shared message")
/* Reset existing sharing information */
if (H5O_msg_reset_share(H5O_ATTR_ID, attr) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_CANTINIT, FAIL, "unable to reset attribute sharing")
/* Store new version of message as a SOHM */
/* (should always work, since we're not changing the size of the attribute) */
if ((shared_mesg = H5SM_try_share(f, oh, 0, H5O_ATTR_ID, attr, NULL)) == 0)
HGOTO_ERROR(H5E_ATTR, H5E_BADMESG, FAIL, "attribute changed sharing status")
else if (shared_mesg < 0)
HGOTO_ERROR(H5E_ATTR, H5E_BADMESG, FAIL, "can't share attribute")
/* Retrieve shared message storage ref count for new shared attribute */
if (H5SM_get_refcount(f, H5O_ATTR_ID, &attr->sh_loc, &attr_rc) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't retrieve shared message ref count")
/* If the newly shared attribute needs to share "ownership" of the shared
* components (ie. its reference count is 1), increment the reference
* count on any shared components of the attribute, so that they won't
* be removed from the file by the following "delete" operation on the
* original attribute shared message info. (Essentially a "copy on
* write" operation).
*
* *ick* -QAK, 2007/01/08
*/
if (attr_rc == 1)
/* Increment reference count on attribute components */
if (H5O__attr_link(f, oh, attr) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_LINKCOUNT, FAIL, "unable to adjust attribute link count")
/* Remove the old attribute from the SOHM storage */
if (H5SM_delete(f, oh, &sh_mesg) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTFREE, FAIL, "unable to delete shared attribute in shared storage")
/* Extract updated shared message info from modified attribute, if requested */
if (update_sh_mesg)
if (H5O_set_shared(update_sh_mesg, &(attr->sh_loc)) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTCOPY, FAIL, "can't get shared message")
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O__attr_update_shared() */
/*-------------------------------------------------------------------------
* Function: H5O__attr_write_cb
*
* Purpose: Object header iterator callback routine to update an
* attribute stored compactly.
*
* Return: SUCCEED/FAIL
*
* Programmer: Quincey Koziol
* Dec 4 2006
*
*-------------------------------------------------------------------------
*/
static herr_t
H5O__attr_write_cb(H5O_t *oh, H5O_mesg_t *mesg /*in,out*/, unsigned H5_ATTR_UNUSED sequence,
unsigned *oh_modified, void *_udata /*in,out*/)
{
H5O_iter_wrt_t *udata = (H5O_iter_wrt_t *)_udata; /* Operator user data */
H5O_chunk_proxy_t *chk_proxy = NULL; /* Chunk that message is in */
hbool_t chk_dirtied = FALSE; /* Flag for unprotecting chunk */
herr_t ret_value = H5_ITER_CONT; /* Return value */
FUNC_ENTER_PACKAGE
/* check args */
HDassert(oh);
HDassert(mesg);
HDassert(!udata->found);
/* Check for correct attribute message to modify */
if (0 == HDstrcmp(((H5A_t *)mesg->native)->shared->name, udata->attr->shared->name)) {
/* Protect chunk */
if (NULL == (chk_proxy = H5O__chunk_protect(udata->f, oh, mesg->chunkno)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTPROTECT, H5_ITER_ERROR, "unable to load object header chunk")
/* Because the attribute structure is shared now. The only situation that requires
* copying the data is when the metadata cache evicts and reloads this attribute.
* The shared attribute structure will be different in that situation. SLU-2010/7/29 */
if (((H5A_t *)mesg->native)->shared != udata->attr->shared) {
/* Sanity check */
HDassert(((H5A_t *)mesg->native)->shared->data);
HDassert(udata->attr->shared->data);
HDassert(((H5A_t *)mesg->native)->shared->data != udata->attr->shared->data);
/* (Needs to occur before updating the shared message, or the hash
* value on the old & new messages will be the same) */
H5MM_memcpy(((H5A_t *)mesg->native)->shared->data, udata->attr->shared->data,
udata->attr->shared->data_size);
} /* end if */
/* Mark the message as modified */
mesg->dirty = TRUE;
chk_dirtied = TRUE;
/* Release chunk */
if (H5O__chunk_unprotect(udata->f, chk_proxy, chk_dirtied) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTUNPROTECT, H5_ITER_ERROR, "unable to unprotect object header chunk")
chk_proxy = NULL;
/* Update the shared attribute in the SOHM storage */
if (mesg->flags & H5O_MSG_FLAG_SHARED)
if (H5O__attr_update_shared(udata->f, oh, udata->attr, (H5O_shared_t *)mesg->native) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, H5_ITER_ERROR,
"unable to update attribute in shared storage")
/* Indicate that the object header was modified */
*oh_modified = H5O_MODIFY;
/* Indicate that the attribute was found */
udata->found = TRUE;
/* Stop iterating */
ret_value = H5_ITER_STOP;
} /* end if */
done:
/* Release chunk, if not already done */
if (chk_proxy && H5O__chunk_unprotect(udata->f, chk_proxy, chk_dirtied) < 0)
HDONE_ERROR(H5E_ATTR, H5E_CANTUNPROTECT, H5_ITER_ERROR, "unable to unprotect object header chunk")
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O__attr_write_cb() */
/*-------------------------------------------------------------------------
* Function: H5O__attr_write
*
* Purpose: Write a new value to an attribute.
*
* Return: SUCCEED/FAIL
*
* Programmer: Quincey Koziol
* Monday, December 4, 2006
*
*-------------------------------------------------------------------------
*/
herr_t
H5O__attr_write(const H5O_loc_t *loc, H5A_t *attr)
{
H5O_t *oh = NULL; /* Pointer to actual object header */
H5O_ainfo_t ainfo; /* Attribute information for object */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Check arguments */
HDassert(loc);
HDassert(attr);
/* Pin the object header */
if (NULL == (oh = H5O_pin(loc)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTPIN, FAIL, "unable to pin object header")
/* Check for attribute info stored */
ainfo.fheap_addr = HADDR_UNDEF;
if (oh->version > H5O_VERSION_1) {
/* Check for (& retrieve if available) attribute info */
if (H5A__get_ainfo(loc->file, oh, &ainfo) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't check for attribute info message")
} /* end if */
/* Check for attributes stored densely */
if (H5F_addr_defined(ainfo.fheap_addr)) {
/* Modify the attribute data in dense storage */
if (H5A__dense_write(loc->file, &ainfo, attr) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "error updating attribute")
} /* end if */
else {
H5O_iter_wrt_t udata; /* User data for callback */
H5O_mesg_operator_t op; /* Wrapper for operator */
/* Set up user data for callback */
udata.f = loc->file;
udata.attr = attr;
udata.found = FALSE;
/* Iterate over attributes, to locate correct one to update */
op.op_type = H5O_MESG_OP_LIB;
op.u.lib_op = H5O__attr_write_cb;
if (H5O__msg_iterate_real(loc->file, oh, H5O_MSG_ATTR, &op, &udata) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "error updating attribute")
/* Check that we found the attribute */
if (!udata.found)
HGOTO_ERROR(H5E_ATTR, H5E_NOTFOUND, FAIL, "can't locate open attribute?")
} /* end else */
/* Update the modification time, if any */
if (H5O_touch_oh(loc->file, oh, FALSE) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "unable to update time on object")
done:
if (oh && H5O_unpin(oh) < 0)
HDONE_ERROR(H5E_ATTR, H5E_CANTUNPIN, FAIL, "unable to unpin object header")
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O__attr_write */
/*-------------------------------------------------------------------------
* Function: H5O__attr_rename_chk_cb
*
* Purpose: Object header iterator callback routine to check for
* duplicate name during rename
*
* Return: SUCCEED/FAIL
*
* Programmer: Quincey Koziol
* Dec 5 2006
*
*-------------------------------------------------------------------------
*/
static herr_t
H5O__attr_rename_chk_cb(H5O_t H5_ATTR_UNUSED *oh, H5O_mesg_t *mesg /*in,out*/,
unsigned H5_ATTR_UNUSED sequence, unsigned H5_ATTR_UNUSED *oh_modified,
void *_udata /*in,out*/)
{
H5O_iter_ren_t *udata = (H5O_iter_ren_t *)_udata; /* Operator user data */
herr_t ret_value = H5_ITER_CONT; /* Return value */
FUNC_ENTER_PACKAGE_NOERR
/* check args */
HDassert(oh);
HDassert(mesg);
HDassert(!udata->found);
/* Check for existing attribute with new name */
if (HDstrcmp(((H5A_t *)mesg->native)->shared->name, udata->new_name) == 0) {
/* Indicate that we found an existing attribute with the new name*/
udata->found = TRUE;
/* Stop iterating */
ret_value = H5_ITER_STOP;
} /* end if */
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O__attr_rename_chk_cb() */
/*-------------------------------------------------------------------------
* Function: H5O__attr_rename_mod_cb
*
* Purpose: Object header iterator callback routine to change name of
* attribute during rename
*
* Note: This routine doesn't currently allow an attribute to change
* its "shared" status, if the name change would cause a size
* difference that would put it into a different category.
* Something for later...
*
* Return: SUCCEED/FAIL
*
* Programmer: Quincey Koziol
* Dec 5 2006
*
*-------------------------------------------------------------------------
*/
static herr_t
H5O__attr_rename_mod_cb(H5O_t *oh, H5O_mesg_t *mesg /*in,out*/, unsigned H5_ATTR_UNUSED sequence,
unsigned *oh_modified, void *_udata /*in,out*/)