-
-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathH5Aint.c
More file actions
2804 lines (2331 loc) · 103 KB
/
H5Aint.c
File metadata and controls
2804 lines (2331 loc) · 103 KB
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 LICENSE 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: H5Aint.c
*
* Purpose: Internal routines for managing attributes.
*
*-------------------------------------------------------------------------
*/
/****************/
/* Module Setup */
/****************/
#include "H5Amodule.h" /* This source code file is part of the H5A module */
#define H5O_FRIEND /*suppress error about including H5Opkg */
/***********/
/* Headers */
/***********/
#include "H5private.h" /* Generic Functions */
#include "H5Apkg.h" /* Attributes */
#include "H5Eprivate.h" /* Error handling */
#include "H5FLprivate.h" /* Free Lists */
#include "H5Iprivate.h" /* IDs */
#include "H5MMprivate.h" /* Memory management */
#include "H5Opkg.h" /* Object headers */
#include "H5SMprivate.h" /* Shared Object Header Messages */
#include "H5VLprivate.h" /* Virtual Object Layer */
/****************/
/* Local Macros */
/****************/
/******************/
/* Local Typedefs */
/******************/
/* Data exchange structure to use when building table of compact attributes for an object */
typedef struct {
H5F_t *f; /* Pointer to file that fractal heap is in */
H5A_attr_table_t *atable; /* Pointer to attribute table to build */
bool bogus_crt_idx; /* Whether bogus creation index values need to be set */
} H5A_compact_bt_ud_t;
/* Data exchange structure to use when copying an attribute from _SRC to _DST */
typedef struct {
const H5O_ainfo_t *ainfo; /* dense information */
H5F_t *file; /* file */
bool *recompute_size; /* Flag to indicate if size changed */
H5O_copy_t *cpy_info; /* Information on copying options */
const H5O_loc_t *oloc_src;
H5O_loc_t *oloc_dst;
} H5A_dense_file_cp_ud_t;
/********************/
/* Package Typedefs */
/********************/
/********************/
/* Local Prototypes */
/********************/
static herr_t H5A__close_cb(void *attr_vol_obj, void **request);
static herr_t H5A__compact_build_table_cb(H5O_t *oh, H5O_mesg_t *mesg /*in,out*/, unsigned sequence,
void *_udata /*in,out*/);
static herr_t H5A__dense_build_table_cb(const H5A_t *attr, void *_udata);
static int H5A__attr_cmp_name_inc(const void *attr1, const void *attr2);
static int H5A__attr_cmp_name_dec(const void *attr1, const void *attr2);
static int H5A__attr_cmp_corder_inc(const void *attr1, const void *attr2);
static int H5A__attr_cmp_corder_dec(const void *attr1, const void *attr2);
static herr_t H5A__attr_sort_table(H5A_attr_table_t *atable, H5_index_t idx_type, H5_iter_order_t order);
static herr_t H5A__iterate_common(hid_t loc_id, H5_index_t idx_type, H5_iter_order_t order, hsize_t *idx,
H5A_attr_iter_op_t *attr_op, void *op_data);
/*********************/
/* Package Variables */
/*********************/
/* Package initialization variable */
bool H5_PKG_INIT_VAR = false;
/* Format version bounds for attribute */
const unsigned H5O_attr_ver_bounds[] = {
H5O_ATTR_VERSION_1, /* H5F_LIBVER_EARLIEST */
H5O_ATTR_VERSION_3, /* H5F_LIBVER_V18 */
H5O_ATTR_VERSION_3, /* H5F_LIBVER_V110 */
H5O_ATTR_VERSION_3, /* H5F_LIBVER_V112 */
H5O_ATTR_VERSION_3, /* H5F_LIBVER_V114 */
H5O_ATTR_VERSION_3, /* H5F_LIBVER_V200 */
H5O_ATTR_VERSION_LATEST /* H5F_LIBVER_LATEST */
};
/*****************************/
/* Library Private Variables */
/*****************************/
/*******************/
/* Local Variables */
/*******************/
/* Declare the free lists of H5A_t */
H5FL_DEFINE(H5A_t);
/* Declare the free lists for H5A_shared_t's */
H5FL_DEFINE(H5A_shared_t);
/* Declare a free list to manage blocks of type conversion data */
H5FL_BLK_DEFINE(attr_buf);
typedef H5A_t *H5A_t_ptr;
H5FL_SEQ_DEFINE_STATIC(H5A_t_ptr);
/* Attribute ID class */
static const H5I_class_t H5I_ATTR_CLS[1] = {{
H5I_ATTR, /* ID class value */
0, /* Class flags */
0, /* # of reserved IDs for class */
H5A__close_cb /* Callback routine for closing objects of this class */
}};
/* Flag indicating "top" of interface has been initialized */
static bool H5A_top_package_initialize_s = false;
/*-------------------------------------------------------------------------
* Function: H5A_init
*
* Purpose: Initialize the interface from some other layer.
*
* Return: Success: non-negative
*
* Failure: negative
*-------------------------------------------------------------------------
*/
herr_t
H5A_init(void)
{
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI(FAIL)
/* FUNC_ENTER() does all the work */
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5A_init() */
/*--------------------------------------------------------------------------
NAME
H5A__init_package -- Initialize interface-specific information
USAGE
herr_t H5A__init_package()
RETURNS
Non-negative on success/Negative on failure
DESCRIPTION
Initializes any interface-specific data or routines.
--------------------------------------------------------------------------*/
herr_t
H5A__init_package(void)
{
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/*
* Create attribute ID type.
*/
if (H5I_register_type(H5I_ATTR_CLS) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, FAIL, "unable to initialize interface");
/* Mark "top" of interface as initialized, too */
H5A_top_package_initialize_s = true;
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5A__init_package() */
/*--------------------------------------------------------------------------
NAME
H5A_top_term_package
PURPOSE
Terminate various H5A objects
USAGE
void H5A_top_term_package()
RETURNS
DESCRIPTION
Release IDs for the atom group, deferring full interface shutdown
until later (in H5A_term_package).
GLOBAL VARIABLES
COMMENTS, BUGS, ASSUMPTIONS
Can't report errors...
EXAMPLES
REVISION LOG
--------------------------------------------------------------------------*/
int
H5A_top_term_package(void)
{
int n = 0;
FUNC_ENTER_NOAPI_NOINIT_NOERR
if (H5A_top_package_initialize_s) {
if (H5I_nmembers(H5I_ATTR) > 0) {
(void)H5I_clear_type(H5I_ATTR, false, false);
n++; /*H5I*/
} /* end if */
/* Mark closed */
if (0 == n)
H5A_top_package_initialize_s = false;
} /* end if */
FUNC_LEAVE_NOAPI(n)
} /* H5A_top_term_package() */
/*--------------------------------------------------------------------------
NAME
H5A_term_package
PURPOSE
Terminate various H5A objects
USAGE
void H5A_term_package()
RETURNS
DESCRIPTION
Release any other resources allocated.
GLOBAL VARIABLES
COMMENTS, BUGS, ASSUMPTIONS
Can't report errors...
Finishes shutting down the interface, after H5A_top_term_package()
is called
EXAMPLES
REVISION LOG
--------------------------------------------------------------------------*/
int
H5A_term_package(void)
{
int n = 0;
FUNC_ENTER_NOAPI_NOINIT_NOERR
if (H5_PKG_INIT_VAR) {
/* Sanity checks */
assert(0 == H5I_nmembers(H5I_ATTR));
assert(false == H5A_top_package_initialize_s);
/* Destroy the attribute object id group */
n += (H5I_dec_type_ref(H5I_ATTR) > 0);
/* Mark closed */
if (0 == n)
H5_PKG_INIT_VAR = false;
} /* end if */
FUNC_LEAVE_NOAPI(n)
} /* H5A_term_package() */
/*-------------------------------------------------------------------------
* Function: H5A__create
*
* Purpose: This is the guts of creating an attribute.
*
* Return: Attribute structure on success, NULL on Failure.
*
*-------------------------------------------------------------------------
*/
H5A_t *
H5A__create(const H5G_loc_t *loc, const char *attr_name, const H5T_t *type, const H5S_t *space, hid_t acpl_id)
{
H5A_t *attr = NULL; /* Attribute created */
hssize_t snelmts; /* elements in attribute */
size_t nelmts; /* elements in attribute */
bool exists; /* Whether attribute exists */
H5A_t *ret_value = NULL; /* Return value */
FUNC_ENTER_PACKAGE_TAG(loc->oloc->addr)
/* Check args */
assert(loc);
assert(attr_name);
assert(type);
assert(space);
/* Check for existing attribute with same name */
/* (technically, the "attribute create" operation will fail for a duplicated
* name, but it's going to be hard to unwind all the special cases on
* failure, so just check first, for now - QAK)
*/
exists = false;
if (H5O__attr_exists(loc->oloc, attr_name, &exists) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_NOTFOUND, NULL, "error checking attributes");
if (exists)
HGOTO_ERROR(H5E_ATTR, H5E_ALREADYEXISTS, NULL, "attribute already exists");
/* Check if the dataspace has an extent set (or is NULL) */
if (!(H5S_has_extent(space)))
HGOTO_ERROR(H5E_ATTR, H5E_BADVALUE, NULL, "dataspace extent has not been set");
/* Check if the datatype is "sensible" for use in a dataset */
if (H5T_is_sensible(type) != true)
HGOTO_ERROR(H5E_ATTR, H5E_BADTYPE, NULL, "datatype is not sensible");
/* Build the attribute information */
if (NULL == (attr = H5FL_CALLOC(H5A_t)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTALLOC, NULL, "memory allocation failed for attribute info");
if (NULL == (attr->shared = H5FL_CALLOC(H5A_shared_t)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTALLOC, NULL, "can't allocate shared attr structure");
/* If the creation property list is H5P_ATTRIBUTE_CREATE_DEFAULT, use the default character encoding */
assert(acpl_id != H5P_DEFAULT);
if (acpl_id == H5P_ATTRIBUTE_CREATE_DEFAULT)
attr->shared->encoding = H5F_DEFAULT_CSET;
else {
H5P_genplist_t *ac_plist; /* ACPL Property list */
/* Get a local copy of the attribute creation property list */
if (NULL == (ac_plist = (H5P_genplist_t *)H5I_object(acpl_id)))
HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a property list");
if (H5P_get(ac_plist, H5P_STRCRT_CHAR_ENCODING_NAME, &(attr->shared->encoding)) < 0)
HGOTO_ERROR(H5E_PLIST, H5E_CANTGET, NULL, "can't get character encoding flag");
} /* end else */
/* Copy the attribute name */
attr->shared->name = H5MM_xstrdup(attr_name);
/* Copy datatype */
if (NULL == (attr->shared->dt = H5T_copy(type, H5T_COPY_ALL)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, NULL, "can't get shared datatype info");
/* Convert a datatype (if committed) to a transient type if the committed datatype's file
location is different from the file location where the attribute will be created */
if (H5T_convert_committed_datatype(attr->shared->dt, loc->oloc->file) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, NULL, "can't get shared datatype info");
/* Mark datatype as being on disk now */
if (H5T_set_loc(attr->shared->dt, H5F_VOL_OBJ(loc->oloc->file), H5T_LOC_DISK) < 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "invalid datatype location");
/* Set the version for datatype */
if (H5T_set_version(loc->oloc->file, attr->shared->dt) < 0)
HGOTO_ERROR(H5E_DATASET, H5E_CANTSET, NULL, "can't set version of datatype");
/* Copy the dataspace for the attribute */
attr->shared->ds = H5S_copy(space, false, true);
/* Set the version for dataspace */
if (H5S_set_version(loc->oloc->file, attr->shared->ds) < 0)
HGOTO_ERROR(H5E_DATASET, H5E_CANTSET, NULL, "can't set version of dataspace");
/* Copy the object header information */
if (H5O_loc_copy_deep(&(attr->oloc), loc->oloc) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTOPENOBJ, NULL, "unable to copy entry");
/* Deep copy of the group hierarchy path */
if (H5G_name_copy(&(attr->path), loc->path, H5_COPY_DEEP) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTCOPY, NULL, "unable to copy path");
/* Check if any of the pieces should be (or are already) shared in the
* SOHM table
*/
if (H5SM_try_share(attr->oloc.file, NULL, 0, H5O_DTYPE_ID, attr->shared->dt, NULL) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_BADMESG, NULL, "trying to share datatype failed");
if (H5SM_try_share(attr->oloc.file, NULL, 0, H5O_SDSPACE_ID, attr->shared->ds, NULL) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_BADMESG, NULL, "trying to share dataspace failed");
/* Check whether datatype is committed & increment ref count
* (to maintain ref. count incr/decr similarity with "shared message"
* type of datatype sharing)
*/
if (H5T_is_named(attr->shared->dt))
/* Increment the reference count on the shared datatype */
if (H5T_link(attr->shared->dt, 1) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_LINKCOUNT, NULL, "unable to adjust shared datatype link count");
/* Compute the size of pieces on disk. This is either the size of the
* datatype and dataspace messages themselves, or the size of the "shared"
* messages if either or both of them are shared.
*/
attr->shared->dt_size = H5O_msg_raw_size(attr->oloc.file, H5O_DTYPE_ID, false, attr->shared->dt);
attr->shared->ds_size = H5O_msg_raw_size(attr->oloc.file, H5O_SDSPACE_ID, false, attr->shared->ds);
/* Get # of elements for attribute's dataspace */
if ((snelmts = H5S_GET_EXTENT_NPOINTS(attr->shared->ds)) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTCOUNT, NULL, "dataspace is invalid");
H5_CHECKED_ASSIGN(nelmts, size_t, snelmts, hssize_t);
assert(attr->shared->dt_size > 0);
assert(attr->shared->ds_size > 0);
attr->shared->data_size = nelmts * H5T_GET_SIZE(attr->shared->dt);
/* Hold the symbol table entry (and file) open */
if (H5O_open(&(attr->oloc)) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTOPENOBJ, NULL, "unable to open");
attr->obj_opened = true;
/* Set the version to encode the attribute with */
if (H5A__set_version(attr->oloc.file, attr) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTSET, NULL, "unable to update attribute version");
/* Insert the attribute into the object header */
if (H5O__attr_create(&(attr->oloc), attr) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTINSERT, NULL, "unable to create attribute in object header");
/* Set return value */
ret_value = attr;
done:
/* Cleanup on failure */
if (NULL == ret_value && attr && H5A__close(attr))
HDONE_ERROR(H5E_ATTR, H5E_CANTFREE, NULL, "can't close attribute");
FUNC_LEAVE_NOAPI_TAG(ret_value)
} /* H5A__create() */
/*-------------------------------------------------------------------------
* Function: H5A__create_by_name
*
* Purpose: Create an attribute on object, according to it's name
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
H5A_t *
H5A__create_by_name(const H5G_loc_t *loc, const char *obj_name, const char *attr_name, const H5T_t *type,
const H5S_t *space, hid_t acpl_id)
{
H5G_loc_t obj_loc; /* Location used to open group */
H5G_name_t obj_path; /* Opened object group hier. path */
H5O_loc_t obj_oloc; /* Opened object object location */
bool loc_found = false; /* Entry at 'obj_name' found */
H5A_t *attr = NULL; /* Attribute from object header */
H5A_t *ret_value = NULL; /* Return value */
FUNC_ENTER_PACKAGE
/* check args */
assert(loc);
assert(obj_name);
assert(attr_name);
/* Set up opened group location to fill in */
obj_loc.oloc = &obj_oloc;
obj_loc.path = &obj_path;
H5G_loc_reset(&obj_loc);
/* Find the object's location */
if (H5G_loc_find(loc, obj_name, &obj_loc /*out*/) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_NOTFOUND, NULL, "object not found");
loc_found = true;
/* Go do the real work for attaching the attribute to the object */
if (NULL == (attr = H5A__create(&obj_loc, attr_name, type, space, acpl_id)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, NULL, "unable to create attribute");
/* Set return value */
ret_value = attr;
done:
/* Release resources */
if (loc_found && H5G_loc_free(&obj_loc) < 0)
HDONE_ERROR(H5E_ATTR, H5E_CANTRELEASE, NULL, "can't free location");
/* Cleanup on failure */
if (ret_value == NULL)
if (attr && H5A__close(attr) < 0)
HDONE_ERROR(H5E_ATTR, H5E_CANTFREE, NULL, "can't close attribute");
FUNC_LEAVE_NOAPI(ret_value)
} /* H5A__create_by_name() */
/*-------------------------------------------------------------------------
* Function: H5A__open_common
*
* Purpose: Finishes initializing an attributes the open
*
* Usage:
* herr_t H5A__open_common(loc, name)
* const H5G_loc_t *loc; IN: Pointer to group location for object
* H5A_t *attr; IN/OUT: Pointer to attribute to initialize
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
static herr_t
H5A__open_common(const H5G_loc_t *loc, H5A_t *attr)
{
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* check args */
assert(loc);
assert(attr);
#if defined(H5_USING_MEMCHECKER) || !defined(NDEBUG)
/* Clear object location */
if (H5O_loc_reset(&(attr->oloc)) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTOPENOBJ, FAIL, "unable to reset location");
#endif /* H5_USING_MEMCHECKER */
/* Free any previous group hier. path */
if (H5G_name_free(&(attr->path)) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTRELEASE, FAIL, "can't release group hier. path");
/* Deep copy of the symbol table entry */
if (H5O_loc_copy_deep(&(attr->oloc), loc->oloc) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTOPENOBJ, FAIL, "unable to copy entry");
/* Deep copy of the group hier. path */
if (H5G_name_copy(&(attr->path), loc->path, H5_COPY_DEEP) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTCOPY, FAIL, "unable to copy entry");
/* Hold the symbol table entry (and file) open */
if (H5O_open(&(attr->oloc)) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTOPENOBJ, FAIL, "unable to open");
attr->obj_opened = true;
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* H5A__open_common() */
/*-------------------------------------------------------------------------
* Function: H5A__open
*
* Purpose: Open an attribute in an object header
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
H5A_t *
H5A__open(const H5G_loc_t *loc, const char *attr_name)
{
H5A_t *attr = NULL; /* Attribute from object header */
H5A_t *ret_value = NULL; /* Return value */
FUNC_ENTER_PACKAGE
/* check args */
assert(loc);
assert(attr_name);
/* Read in attribute from object header */
if (NULL == (attr = H5O__attr_open_by_name(loc->oloc, attr_name)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTOPENOBJ, NULL,
"unable to load attribute info from object header for attribute: '%s'", attr_name);
/* Finish initializing attribute */
if (H5A__open_common(loc, attr) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, NULL, "unable to initialize attribute");
/* Set return value */
ret_value = attr;
done:
/* Cleanup on failure */
if (ret_value == NULL)
if (attr && H5A__close(attr) < 0)
HDONE_ERROR(H5E_ATTR, H5E_CANTFREE, NULL, "can't close attribute");
FUNC_LEAVE_NOAPI(ret_value)
} /* H5A__open() */
/*-------------------------------------------------------------------------
* Function: H5A__open_by_idx
*
* Purpose: Open an attribute according to its index order
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
H5A_t *
H5A__open_by_idx(const H5G_loc_t *loc, const char *obj_name, H5_index_t idx_type, H5_iter_order_t order,
hsize_t n)
{
H5G_loc_t obj_loc; /* Location used to open group */
H5G_name_t obj_path; /* Opened object group hier. path */
H5O_loc_t obj_oloc; /* Opened object object location */
bool loc_found = false; /* Entry at 'obj_name' found */
H5A_t *attr = NULL; /* Attribute from object header */
H5A_t *ret_value = NULL; /* Return value */
FUNC_ENTER_PACKAGE
/* check args */
assert(loc);
assert(obj_name);
/* Set up opened group location to fill in */
obj_loc.oloc = &obj_oloc;
obj_loc.path = &obj_path;
H5G_loc_reset(&obj_loc);
/* Find the object's location */
if (H5G_loc_find(loc, obj_name, &obj_loc /*out*/) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_NOTFOUND, NULL, "object not found");
loc_found = true;
/* Read in attribute from object header */
if (NULL == (attr = H5O__attr_open_by_idx(obj_loc.oloc, idx_type, order, n)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTOPENOBJ, NULL, "unable to load attribute info from object header");
/* Finish initializing attribute */
if (H5A__open_common(&obj_loc, attr) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, NULL, "unable to initialize attribute");
/* Set return value */
ret_value = attr;
done:
/* Release resources */
if (loc_found && H5G_loc_free(&obj_loc) < 0)
HDONE_ERROR(H5E_ATTR, H5E_CANTRELEASE, NULL, "can't free location");
/* Cleanup on failure */
if (ret_value == NULL)
if (attr && H5A__close(attr) < 0)
HDONE_ERROR(H5E_ATTR, H5E_CANTFREE, NULL, "can't close attribute");
FUNC_LEAVE_NOAPI(ret_value)
} /* H5A__open_by_idx() */
/*-------------------------------------------------------------------------
* Function: H5A__open_by_name
*
* Purpose: Open an attribute in an object header, according to it's name
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
H5A_t *
H5A__open_by_name(const H5G_loc_t *loc, const char *obj_name, const char *attr_name)
{
H5G_loc_t obj_loc; /* Location used to open group */
H5G_name_t obj_path; /* Opened object group hier. path */
H5O_loc_t obj_oloc; /* Opened object object location */
bool loc_found = false; /* Entry at 'obj_name' found */
H5A_t *attr = NULL; /* Attribute from object header */
H5A_t *ret_value = NULL; /* Return value */
FUNC_ENTER_PACKAGE
/* check args */
assert(loc);
assert(obj_name);
assert(attr_name);
/* Set up opened group location to fill in */
obj_loc.oloc = &obj_oloc;
obj_loc.path = &obj_path;
H5G_loc_reset(&obj_loc);
/* Find the object's location */
if (H5G_loc_find(loc, obj_name, &obj_loc /*out*/) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_NOTFOUND, NULL, "object not found");
loc_found = true;
/* Read in attribute from object header */
if (NULL == (attr = H5O__attr_open_by_name(obj_loc.oloc, attr_name)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, NULL, "unable to load attribute info from object header");
/* Finish initializing attribute */
if (H5A__open_common(loc, attr) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, NULL, "unable to initialize attribute");
/* Set return value */
ret_value = attr;
done:
/* Release resources */
if (loc_found && H5G_loc_free(&obj_loc) < 0)
HDONE_ERROR(H5E_ATTR, H5E_CANTRELEASE, NULL, "can't free location");
/* Cleanup on failure */
if (ret_value == NULL)
if (attr && H5A__close(attr) < 0)
HDONE_ERROR(H5E_ATTR, H5E_CANTFREE, NULL, "can't close attribute");
FUNC_LEAVE_NOAPI(ret_value)
} /* H5A__open_by_name() */
/*--------------------------------------------------------------------------
NAME
H5A__read
PURPOSE
Actually read in data from an attribute
USAGE
herr_t H5A__read (attr, mem_type, buf)
H5A_t *attr; IN: Attribute to read
const H5T_t *mem_type; IN: Memory datatype of buffer
void *buf; IN: Buffer for data to read
RETURNS
Non-negative on success/Negative on failure
DESCRIPTION
This function reads a complete attribute from disk.
--------------------------------------------------------------------------*/
herr_t
H5A__read(const H5A_t *attr, const H5T_t *mem_type, void *buf)
{
uint8_t *tconv_buf = NULL; /* datatype conv buffer*/
uint8_t *bkg_buf = NULL; /* background buffer */
hssize_t snelmts; /* elements in attribute */
size_t nelmts; /* elements in attribute*/
H5T_path_t *tpath = NULL; /* type conversion info */
size_t src_type_size; /* size of source type */
size_t dst_type_size; /* size of destination type */
size_t buf_size; /* desired buffer size */
herr_t ret_value = SUCCEED;
FUNC_ENTER_PACKAGE_TAG(attr->oloc.addr)
assert(attr);
assert(mem_type);
assert(buf);
/* Patch the top level file pointer in attr->shared->dt->shared->u.vlen.f if needed */
if (H5T_patch_vlen_file(attr->shared->dt, H5F_VOL_OBJ(attr->oloc.file)) < 0)
HGOTO_ERROR(H5E_DATASET, H5E_CANTOPENOBJ, FAIL, "can't patch VL datatype file pointer");
/* Create buffer for data to store on disk */
if ((snelmts = H5S_GET_EXTENT_NPOINTS(attr->shared->ds)) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTCOUNT, FAIL, "dataspace is invalid");
H5_CHECKED_ASSIGN(nelmts, size_t, snelmts, hssize_t);
if (nelmts > 0) {
/* Get the memory and file datatype sizes */
src_type_size = H5T_GET_SIZE(attr->shared->dt);
dst_type_size = H5T_GET_SIZE(mem_type);
/* Check if the attribute has any data yet, if not, fill with zeroes */
if (attr->obj_opened && !attr->shared->data)
memset(buf, 0, (dst_type_size * nelmts));
else { /* Attribute exists and has a value */
/* Convert memory buffer into disk buffer */
/* Set up type conversion function */
if (NULL == (tpath = H5T_path_find(attr->shared->dt, mem_type)))
HGOTO_ERROR(H5E_ATTR, H5E_UNSUPPORTED, FAIL,
"unable to convert between src and dst datatypes");
/* Check for type conversion required */
if (!H5T_path_noop(tpath)) {
H5T_bkg_t need_bkg; /* Background buffer type */
/* Get the maximum buffer size needed and allocate it */
buf_size = nelmts * MAX(src_type_size, dst_type_size);
if (NULL == (tconv_buf = H5FL_BLK_MALLOC(attr_buf, buf_size)))
HGOTO_ERROR(H5E_ATTR, H5E_NOSPACE, FAIL, "memory allocation failed");
/* Copy the attribute data into the buffer for conversion */
H5MM_memcpy(tconv_buf, attr->shared->data, (src_type_size * nelmts));
/* Check if we need a background buffer */
need_bkg = H5T_path_bkg(tpath);
if (need_bkg) {
/* Allocate background buffer */
if (NULL == (bkg_buf = H5FL_BLK_CALLOC(attr_buf, buf_size)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTALLOC, FAIL, "memory allocation failed");
/* Copy the application buffer into the background buffer if necessary */
if (need_bkg == H5T_BKG_YES) {
assert(buf_size >= (dst_type_size * nelmts));
H5MM_memcpy(bkg_buf, buf, dst_type_size * nelmts);
}
}
/* Perform datatype conversion. */
if (H5T_convert(tpath, attr->shared->dt, mem_type, nelmts, (size_t)0, (size_t)0, tconv_buf,
bkg_buf) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTCONVERT, FAIL, "datatype conversion failed");
/* Copy the converted data into the user's buffer */
H5MM_memcpy(buf, tconv_buf, (dst_type_size * nelmts));
} /* end if */
/* No type conversion necessary */
else {
assert(dst_type_size == src_type_size);
/* Copy the attribute data into the user's buffer */
H5MM_memcpy(buf, attr->shared->data, (dst_type_size * nelmts));
} /* end else */
} /* end else */
} /* end if */
done:
/* Release resources */
if (tconv_buf)
tconv_buf = H5FL_BLK_FREE(attr_buf, tconv_buf);
if (bkg_buf)
bkg_buf = H5FL_BLK_FREE(attr_buf, bkg_buf);
FUNC_LEAVE_NOAPI_TAG(ret_value)
} /* H5A__read() */
/*--------------------------------------------------------------------------
NAME
H5A__write
PURPOSE
Actually write out data to an attribute
USAGE
herr_t H5A__write (attr, mem_type, buf)
H5A_t *attr; IN: Attribute to write
const H5T_t *mem_type; IN: Memory datatype of buffer
const void *buf; IN: Buffer of data to write
RETURNS
Non-negative on success/Negative on failure
DESCRIPTION
This function writes a complete attribute to disk.
--------------------------------------------------------------------------*/
herr_t
H5A__write(H5A_t *attr, const H5T_t *mem_type, const void *buf)
{
uint8_t *tconv_buf = NULL; /* datatype conv buffer */
uint8_t *bkg_buf = NULL; /* temp conversion buffer */
hssize_t snelmts; /* elements in attribute */
size_t nelmts; /* elements in attribute */
H5T_path_t *tpath = NULL; /* conversion information*/
size_t src_type_size; /* size of source type */
size_t dst_type_size; /* size of destination type*/
size_t buf_size; /* desired buffer size */
herr_t ret_value = SUCCEED;
FUNC_ENTER_PACKAGE_TAG(attr->oloc.addr)
assert(attr);
assert(mem_type);
assert(buf);
/* Patch the top level file pointer in attr->shared->dt->shared->u.vlen.f if needed */
if (H5T_patch_vlen_file(attr->shared->dt, H5F_VOL_OBJ(attr->oloc.file)) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, FAIL, "can't patch VL datatype file pointer");
/* Get # of elements for attribute's dataspace */
if ((snelmts = H5S_GET_EXTENT_NPOINTS(attr->shared->ds)) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTCOUNT, FAIL, "dataspace is invalid");
H5_CHECKED_ASSIGN(nelmts, size_t, snelmts, hssize_t);
/* If there's actually data elements for the attribute, make a copy of the data passed in */
if (nelmts > 0) {
/* Get the memory and file datatype sizes */
src_type_size = H5T_GET_SIZE(mem_type);
dst_type_size = H5T_GET_SIZE(attr->shared->dt);
/* Convert memory buffer into disk buffer */
/* Set up type conversion function */
if (NULL == (tpath = H5T_path_find(mem_type, attr->shared->dt)))
HGOTO_ERROR(H5E_ATTR, H5E_UNSUPPORTED, FAIL, "unable to convert between src and dst datatypes");
/* Check for type conversion required */
if (!H5T_path_noop(tpath)) {
H5T_bkg_t need_bkg; /* Background buffer type */
/* Get the maximum buffer size needed and allocate it */
buf_size = nelmts * MAX(src_type_size, dst_type_size);
if (NULL == (tconv_buf = H5FL_BLK_MALLOC(attr_buf, buf_size)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTALLOC, FAIL, "memory allocation failed");
/* Copy the user's data into the buffer for conversion */
H5MM_memcpy(tconv_buf, buf, (src_type_size * nelmts));
/* Check if we need a background buffer */
if (H5T_detect_class(attr->shared->dt, H5T_VLEN, false))
need_bkg = H5T_BKG_YES;
else
need_bkg = H5T_path_bkg(tpath);
if (need_bkg) {
/* Use the existing attribute data buffer, if present, as the background buffer,
* otherwise allocate one. Note we don't need to track which one it is since both
* use the "attr_buf" free list block. */
if (attr->shared->data) {
bkg_buf = attr->shared->data;
attr->shared->data = NULL;
/* Clear background buffer if it's not supposed to be initialized with file
* contents */
if (need_bkg == H5T_BKG_TEMP)
memset(bkg_buf, 0, dst_type_size * nelmts);
}
else if (NULL == (bkg_buf = H5FL_BLK_CALLOC(attr_buf, buf_size)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTALLOC, FAIL, "memory allocation failed");
}
/* Perform datatype conversion */
if (H5T_convert(tpath, mem_type, attr->shared->dt, nelmts, (size_t)0, (size_t)0, tconv_buf,
bkg_buf) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTCONVERT, FAIL, "datatype conversion failed");
/* Free the previous attribute data buffer, if there is one */
if (attr->shared->data)
attr->shared->data = H5FL_BLK_FREE(attr_buf, attr->shared->data);
/* Set the pointer to the attribute data to the converted information */
attr->shared->data = tconv_buf;
tconv_buf = NULL;
} /* end if */
/* No type conversion necessary */
else {
assert(dst_type_size == src_type_size);
/* Allocate the attribute buffer, if there isn't one */
if (attr->shared->data == NULL)
if (NULL == (attr->shared->data = H5FL_BLK_MALLOC(attr_buf, dst_type_size * nelmts)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed");
/* Copy the attribute data into the attribute data buffer */
H5MM_memcpy(attr->shared->data, buf, (dst_type_size * nelmts));
} /* end else */
/* Modify the attribute in the object header */
if (H5O__attr_write(&(attr->oloc), attr) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, FAIL, "unable to modify attribute");
} /* end if */
done:
/* Release resources */
if (tconv_buf)
tconv_buf = H5FL_BLK_FREE(attr_buf, tconv_buf);
if (bkg_buf)
bkg_buf = H5FL_BLK_FREE(attr_buf, bkg_buf);
FUNC_LEAVE_NOAPI_TAG(ret_value)
} /* H5A__write() */
/*--------------------------------------------------------------------------
NAME
H5A__get_name
PURPOSE
Gets a copy of the name for an attribute
RETURNS
Non-negative on success/Negative on failure
DESCRIPTION
This function retrieves the name of an attribute for an attribute ID.
Up to 'buf_size' characters are stored in 'buf' followed by a '\0' string
terminator. If the name of the attribute is longer than 'buf_size'-1,
the string terminator is stored in the last position of the buffer to
properly terminate the string.
This function returns the length of the attribute's name (which may be
longer than 'buf_size') in the 'attr_name_len' parameter.
--------------------------------------------------------------------------*/
herr_t
H5A__get_name(H5A_t *attr, size_t buf_size, char *buf, size_t *attr_name_len)
{
size_t copy_len, nbytes;
FUNC_ENTER_PACKAGE_NOERR
/* Sanity checks */
assert(attr);
assert(attr_name_len);
/* Get the real attribute length */
nbytes = strlen(attr->shared->name);
/* Compute the string length which will fit into the user's buffer */
copy_len = MIN(buf_size - 1, nbytes);
/* Copy all/some of the name */
if (buf && copy_len > 0) {
H5MM_memcpy(buf, attr->shared->name, copy_len);
/* Terminate the string */
buf[copy_len] = '\0';
} /* end if */
/* Set actual attribute name length */
*attr_name_len = nbytes;
FUNC_LEAVE_NOAPI(SUCCEED)
} /* H5A__get_name() */
/*-------------------------------------------------------------------------
* Function: H5A_get_space
*
* Purpose: Returns dataspace of the attribute.
*
* Return: Success: A valid ID for the dataspace of an attribute
*
* Failure: H5I_INVALID_ID
*
*-------------------------------------------------------------------------