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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by the Board of Trustees of the University of Illinois. *
* 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 files COPYING and Copyright.html. COPYING can be found at the root *
* of the source code distribution tree; Copyright.html can be found at the *
* root level of an installed copy of the electronic HDF5 document set and *
* is linked from the top-level documents page. It can also be found at *
* http://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html. If you do not have *
* access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#define H5A_PACKAGE /*prevent warning from including H5Apkg */
#define H5O_PACKAGE /*suppress error about including H5Opkg */
#define H5S_PACKAGE /*suppress error about including H5Spkg */
#include "H5private.h" /* Generic Functions */
#include "H5Apkg.h" /* Attributes */
#include "H5Dprivate.h" /* Datasets */
#include "H5Eprivate.h" /* Error handling */
#include "H5FLprivate.h" /* Free Lists */
#include "H5Gprivate.h" /* Groups */
#include "H5Iprivate.h" /* IDs */
#include "H5MMprivate.h" /* Memory management */
#include "H5Opkg.h" /* Object headers */
#include "H5Spkg.h" /* Dataspaces */
#include "H5SMprivate.h" /* Shared Object Header Messages */
/* User data for iteration when updating an attribute */
typedef struct {
/* down */
H5F_t *f; /* Pointer to file attribute is in */
hid_t dxpl_id; /* DXPL for operation */
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 */
hid_t dxpl_id; /* DXPL for operation */
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 iterating over attributes */
typedef struct {
/* down */
H5F_t *f; /* Pointer to file attribute is in */
hid_t dxpl_id; /* DXPL for operation */
hid_t loc_id; /* ID of object being iterated over */
unsigned skip; /* # of attributes to skip over */
H5A_operator_t op; /* Callback routine for each attribute */
void *op_data; /* User data for callback */
/* up */
unsigned count; /* Count of attributes examined */
} H5O_iter_itr_t;
/* PRIVATE PROTOTYPES */
static herr_t H5O_attr_encode(H5F_t *f, uint8_t *p, const void *mesg);
static void *H5O_attr_decode(H5F_t *f, hid_t dxpl_id, const uint8_t *p);
static void *H5O_attr_copy(const void *_mesg, void *_dest);
static size_t H5O_attr_size(const H5F_t *f, const void *_mesg);
static herr_t H5O_attr_reset(void *_mesg);
static herr_t H5O_attr_free(void *mesg);
static herr_t H5O_attr_delete(H5F_t *f, hid_t dxpl_id, const void *_mesg, hbool_t adj_link);
static herr_t H5O_attr_link(H5F_t *f, hid_t dxpl_id, const void *_mesg);
static herr_t H5O_attr_pre_copy_file(H5F_t *file_src, const H5O_msg_class_t *type,
const void *mesg_src, hbool_t *deleted, const H5O_copy_t *cpy_info, void *udata);
static void *H5O_attr_copy_file(H5F_t *file_src, const H5O_msg_class_t *mesg_type,
void *native_src, H5F_t *file_dst, hid_t dxpl_id, H5O_copy_t *cpy_info,
void *udata);
static herr_t H5O_attr_get_share(const void *_mesg, H5O_shared_t *sh);
static herr_t H5O_attr_set_share(void *_mesg, const H5O_shared_t *sh);
static htri_t H5O_attr_is_shared(const void *_mesg);
static herr_t H5O_attr_debug(H5F_t *f, hid_t dxpl_id, const void *_mesg,
FILE * stream, int indent, int fwidth);
/* This message derives from H5O message class */
const H5O_msg_class_t H5O_MSG_ATTR[1] = {{
H5O_ATTR_ID, /* message id number */
"attribute", /* message name for debugging */
sizeof(H5A_t), /* native message size */
H5O_attr_decode, /* decode message */
H5O_attr_encode, /* encode message */
H5O_attr_copy, /* copy the native value */
H5O_attr_size, /* size of raw message */
H5O_attr_reset, /* reset method */
H5O_attr_free, /* free method */
H5O_attr_delete, /* file delete method */
H5O_attr_link, /* link method */
H5O_attr_get_share, /* get share method */
H5O_attr_set_share, /* set share method */
H5O_attr_is_shared, /*is shared method */
H5O_attr_pre_copy_file, /* pre copy native value to file */
H5O_attr_copy_file, /* copy native value to file */
NULL, /* post copy native value to file */
H5O_attr_debug /* debug the message */
}};
/* This is the initial version, which does not have support for shared datatypes */
#define H5O_ATTR_VERSION_1 1
/* This version allows support for shared datatypes */
#define H5O_ATTR_VERSION_2 2
/* Add support for different character encodings of attribute names */
#define H5O_ATTR_VERSION_3 3
/* Add support for shared dataspaces */
#define H5O_ATTR_VERSION_4 4
/* The latest version of the format. Look through the 'encode'
* and 'size' callback for places to change when updating this. */
#define H5O_ATTR_VERSION_LATEST H5O_ATTR_VERSION_4
/* Flags for attribute flag encoding */
#define H5O_ATTR_FLAG_TYPE_SHARED 0x01
#define H5O_ATTR_FLAG_SPACE_SHARED 0x02
/* Declare external the free list for H5S_t's */
H5FL_EXTERN(H5S_t);
/* Declare external the free list for H5S_extent_t's */
H5FL_EXTERN(H5S_extent_t);
/*--------------------------------------------------------------------------
NAME
H5O_attr_decode
PURPOSE
Decode a attribute message and return a pointer to a memory struct
with the decoded information
USAGE
void *H5O_attr_decode(f, raw_size, p)
H5F_t *f; IN: pointer to the HDF5 file struct
size_t raw_size; IN: size of the raw information buffer
const uint8_t *p; IN: the raw information buffer
RETURNS
Pointer to the new message in native order on success, NULL on failure
DESCRIPTION
This function decodes the "raw" disk form of a attribute message
into a struct in memory native format. The struct is allocated within this
function using malloc() and is returned to the caller.
*
* Modifications:
* Robb Matzke, 17 Jul 1998
* Added padding for alignment.
*
* Robb Matzke, 20 Jul 1998
* Added a version number at the beginning.
*
* Raymond Lu, 8 April 2004
* Changed Dataspace operation on H5S_simple_t to H5S_extent_t.
*
* James Laird, 15 November 2005
* Added character encoding (version 3)
*
--------------------------------------------------------------------------*/
static void *
H5O_attr_decode(H5F_t *f, hid_t dxpl_id, const uint8_t *p)
{
H5A_t *attr = NULL;
H5S_extent_t *extent; /*extent dimensionality information */
size_t name_len; /*attribute name length */
int version; /*message version number*/
unsigned flags = 0; /* Attribute flags */
H5A_t *ret_value; /* Return value */
FUNC_ENTER_NOAPI_NOINIT(H5O_attr_decode)
/* check args */
HDassert(f);
HDassert(p);
if(NULL == (attr = H5FL_CALLOC(H5A_t)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")
/* Version number */
version = *p++;
if(version < H5O_ATTR_VERSION_1 || version > H5O_ATTR_VERSION_4)
HGOTO_ERROR(H5E_OHDR, H5E_CANTLOAD, NULL, "bad version number for attribute message")
/* Get the flags byte if we have a later version of the attribute */
if(version > H5O_ATTR_VERSION_1)
flags = *p++;
else
p++; /* Byte is unused when version<2 */
/*
* Decode the sizes of the parts of the attribute. The sizes stored in
* the file are exact but the parts are aligned on 8-byte boundaries.
*/
UINT16DECODE(p, name_len); /*including null*/
UINT16DECODE(p, attr->dt_size);
UINT16DECODE(p, attr->ds_size);
/*
* Decode the character encoding for the name for versions 3 or later,
* as well as some reserved bytes.
*/
if(version >= H5O_ATTR_VERSION_3)
attr->encoding = *p++;
/* Decode and store the name */
if(NULL == (attr->name = H5MM_strdup((const char *)p)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")
if(version < H5O_ATTR_VERSION_2)
p += H5O_ALIGN_OLD(name_len); /* advance the memory pointer */
else
p += name_len; /* advance the memory pointer */
/* decode the attribute datatype */
if(flags & H5O_ATTR_FLAG_TYPE_SHARED) {
H5O_shared_t *shared; /* Shared information */
/* Get the shared information */
if (NULL == (shared = (H5O_MSG_SHARED->decode)(f, dxpl_id, p)))
HGOTO_ERROR(H5E_OHDR, H5E_CANTDECODE, NULL, "unable to decode shared message")
/* Get the actual datatype information */
if((attr->dt = H5O_shared_read(f, dxpl_id, shared, H5O_MSG_DTYPE, NULL)) == NULL)
HGOTO_ERROR(H5E_ATTR, H5E_CANTDECODE, NULL, "can't decode attribute datatype")
/* Free the shared information */
H5O_msg_free_real(H5O_MSG_SHARED, shared);
} /* end if */
else {
if((attr->dt = (H5O_MSG_DTYPE->decode)(f, dxpl_id, p)) == NULL)
HGOTO_ERROR(H5E_ATTR, H5E_CANTDECODE, NULL, "can't decode attribute datatype")
} /* end else */
if(version < H5O_ATTR_VERSION_2)
p += H5O_ALIGN_OLD(attr->dt_size);
else
p += attr->dt_size;
/* decode the attribute dataspace. It can be shared in versions >= 4
* What's actually shared, though, is only the extent.
*/
if(NULL == (attr->ds = H5FL_CALLOC(H5S_t)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")
if (flags & H5O_ATTR_FLAG_SPACE_SHARED) {
H5O_shared_t *shared; /* Shared information */
/* Get the shared information */
if (NULL == (shared = (H5O_MSG_SHARED->decode) (f, dxpl_id, p)))
HGOTO_ERROR(H5E_OHDR, H5E_CANTDECODE, NULL, "unable to decode shared message")
/* Get the actual datatype information */
if((extent= H5O_shared_read(f, dxpl_id, shared, H5O_MSG_SDSPACE, NULL))==NULL)
HGOTO_ERROR(H5E_ATTR, H5E_CANTDECODE, NULL, "can't decode attribute dataspace")
/* Free the shared information */
H5O_msg_free_real(H5O_MSG_SHARED, shared);
} /* end if */
else {
if((extent = (H5O_MSG_SDSPACE->decode)(f, dxpl_id, p)) == NULL)
HGOTO_ERROR(H5E_ATTR, H5E_CANTDECODE, NULL, "can't decode attribute dataspace")
} /* end else */
/* Copy the extent information to the dataspace */
HDmemcpy(&(attr->ds->extent), extent, sizeof(H5S_extent_t));
/* Release temporary extent information */
H5FL_FREE(H5S_extent_t, extent);
/* Default to entire dataspace being selected */
if(H5S_select_all(attr->ds, 0) < 0)
HGOTO_ERROR(H5E_DATASPACE, H5E_CANTSET, NULL, "unable to set all selection")
if(version < H5O_ATTR_VERSION_2)
p += H5O_ALIGN_OLD(attr->ds_size);
else
p += attr->ds_size;
/* Compute the size of the data */
H5_ASSIGN_OVERFLOW(attr->data_size, H5S_GET_EXTENT_NPOINTS(attr->ds) * H5T_get_size(attr->dt), hsize_t, size_t);
/* Go get the data */
if(attr->data_size) {
if(NULL == (attr->data = H5FL_BLK_MALLOC(attr_buf, attr->data_size)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")
HDmemcpy(attr->data, p, attr->data_size);
} /* end if */
/* Indicate that the fill values aren't to be written out */
attr->initialized = 1;
/* Set return value */
ret_value = attr;
done:
FUNC_LEAVE_NOAPI(ret_value);
} /* end H5O_attr_decode() */
/*--------------------------------------------------------------------------
NAME
H5O_attr_encode
PURPOSE
Encode a simple attribute message
USAGE
herr_t H5O_attr_encode(f, raw_size, p, mesg)
H5F_t *f; IN: pointer to the HDF5 file struct
const uint8 *p; IN: the raw information buffer
const void *mesg; IN: Pointer to the simple datatype struct
RETURNS
Non-negative on success/Negative on failure
DESCRIPTION
This function encodes the native memory form of the attribute
message in the "raw" disk form.
*
* Modifications:
* Robb Matzke, 17 Jul 1998
* Added padding for alignment.
*
* Robb Matzke, 20 Jul 1998
* Added a version number at the beginning.
*
* Raymond Lu, 8 April 2004
* For data space, changed the operation on H5S_simple_t to
* H5S_extent_t
*
* James Laird, 15 November 2005
* Added character encoding (version 3)
*
--------------------------------------------------------------------------*/
static herr_t
H5O_attr_encode(H5F_t *f, uint8_t *p, const void *mesg)
{
const H5A_t *attr = (const H5A_t *) mesg;
size_t name_len; /* Attribute name length */
htri_t is_shared_ret; /* Return value from H5O_msg_is_shared */
unsigned version; /* Attribute version */
hbool_t type_shared; /* Flag to indicate that a shared datatype is used for this attribute */
hbool_t space_shared; /* Flag to indicate that a shared dataspace is used for this attribute */
H5O_shared_t sh_mesg; /* Shared message location */
hbool_t use_latest_format; /* Flag indicating the newest file format should be used */
unsigned flags=0; /* Attribute flags */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI_NOINIT(H5O_attr_encode)
/* check args */
HDassert(f);
HDassert(p);
HDassert(attr);
/* Get the file's 'use the latest version of the format' flag */
use_latest_format = H5F_USE_LATEST_FORMAT(f);
/* Check whether datatype and dataspace are shared */
if((is_shared_ret = H5O_msg_is_shared(H5O_DTYPE_ID, attr->dt)) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_BADMESG, FAIL, "can't determine if datatype is shared")
else if(is_shared_ret)
type_shared = TRUE;
else
type_shared = FALSE;
if((is_shared_ret = H5O_msg_is_shared(H5O_SDSPACE_ID, attr->ds)) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_BADMESG, FAIL, "can't determine if dataspace is shared")
else if(is_shared_ret)
space_shared = TRUE;
else
space_shared = FALSE;
/* Check which version to write out */
if(use_latest_format)
version = H5O_ATTR_VERSION_LATEST; /* Write out latest version of format */
else if(space_shared)
version = H5O_ATTR_VERSION_4; /* Write version with shared dataspaces */
else if(attr->encoding != H5T_CSET_ASCII)
version = H5O_ATTR_VERSION_3; /* Write version which includes the character encoding */
else if(type_shared)
version = H5O_ATTR_VERSION_2; /* Write out version with shared datatype */
else
version = H5O_ATTR_VERSION_1; /* Write out basic version */
/* Encode Version */
*p++ = version;
/* Set attribute flags if version >1 */
if(version > H5O_ATTR_VERSION_1)
{
flags = (type_shared ? H5O_ATTR_FLAG_TYPE_SHARED : 0 );
flags |= (space_shared ? H5O_ATTR_FLAG_SPACE_SHARED : 0);
*p++ = flags; /* Set flags for attribute */
}
else
*p++ = 0; /* Reserved, for version <2 */
/*
* Encode the lengths of the various parts of the attribute message. The
* encoded lengths are exact but we pad each part except the data to be a
* multiple of eight bytes (in the first version).
*/
name_len = HDstrlen(attr->name) + 1;
UINT16ENCODE(p, name_len);
UINT16ENCODE(p, attr->dt_size);
UINT16ENCODE(p, attr->ds_size);
/*
* Encode the character encoding used for the attribute's name
* Also add several "reserved" fields to pad to 16 bytes.
*/
/* JAMES: only do this if flag says to? */
if(version >= H5O_ATTR_VERSION_3)
*p++ = attr->encoding;
/* Write the name including null terminator */
HDmemcpy(p, attr->name, name_len);
if(version < H5O_ATTR_VERSION_2) {
/* Pad to the correct number of bytes */
HDmemset(p + name_len, 0, H5O_ALIGN_OLD(name_len) - name_len);
p += H5O_ALIGN_OLD(name_len);
} /* end if */
else
p += name_len;
/* encode the attribute datatype */
if(type_shared) {
/* Reset shared message information */
HDmemset(&sh_mesg, 0, sizeof(H5O_shared_t));
/* Get shared message information from datatype */
if((H5O_MSG_DTYPE->get_share)(attr->dt, &sh_mesg/*out*/) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTENCODE, FAIL, "can't encode shared attribute datatype")
/* Encode shared message information for datatype */
if((H5O_MSG_SHARED->encode)(f, p, &sh_mesg) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTENCODE, FAIL, "can't encode shared attribute datatype")
} /* end if */
else {
/* Encode datatype information */
if((H5O_MSG_DTYPE->encode)(f, p, attr->dt) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTENCODE, FAIL, "can't encode attribute datatype")
} /* end else */
if(version < H5O_ATTR_VERSION_2) {
HDmemset(p + attr->dt_size, 0, H5O_ALIGN_OLD(attr->dt_size) - attr->dt_size);
p += H5O_ALIGN_OLD(attr->dt_size);
} /* end if */
else
p += attr->dt_size;
/* encode the attribute dataspace */
if(space_shared) {
/* Reset shared message information */
HDmemset(&sh_mesg, 0, sizeof(H5O_shared_t));
/* Get shared message information from dataspace */
if((H5O_MSG_SDSPACE->get_share)(attr->ds, &sh_mesg/*out*/) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTENCODE, FAIL, "can't encode shared attribute dataspace")
/* Encode shared message information for dataspace */
if((H5O_MSG_SHARED->encode)(f, p, &sh_mesg) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTENCODE, FAIL, "can't encode shared attribute dataspace")
} /* end if */
else {
/* Encode non-shared dataspace information */
if((H5O_MSG_SDSPACE->encode)(f, p, &(attr->ds->extent)) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTENCODE, FAIL, "can't encode attribute dataspace")
} /* end else */
if(version < H5O_ATTR_VERSION_2) {
HDmemset(p + attr->ds_size, 0, H5O_ALIGN_OLD(attr->ds_size) - attr->ds_size);
p += H5O_ALIGN_OLD(attr->ds_size);
} /* end if */
else
p += attr->ds_size;
/* Store attribute data */
if(attr->data)
HDmemcpy(p, attr->data, attr->data_size);
else
HDmemset(p, 0, attr->data_size);
done:
FUNC_LEAVE_NOAPI(ret_value);
} /* end H5O_attr_encode() */
/*--------------------------------------------------------------------------
NAME
H5O_attr_copy
PURPOSE
Copies a message from MESG to DEST, allocating DEST if necessary.
USAGE
void *H5O_attr_copy(mesg, dest)
const void *mesg; IN: Pointer to the source attribute struct
const void *dest; IN: Pointer to the destination attribute struct
RETURNS
Pointer to DEST on success, NULL on failure
DESCRIPTION
This function copies a native (memory) attribute message,
allocating the destination structure if necessary.
--------------------------------------------------------------------------*/
static void *
H5O_attr_copy(const void *_src, void *_dst)
{
void *ret_value; /* Return value */
FUNC_ENTER_NOAPI_NOINIT(H5O_attr_copy)
/* check args */
HDassert(_src);
/* copy */
if(NULL == (ret_value = H5A_copy(_dst, _src)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, NULL, "can't copy attribute")
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_attr_copy() */
/*--------------------------------------------------------------------------
NAME
H5O_attr_size
PURPOSE
Return the raw message size in bytes
USAGE
size_t H5O_attr_size(f, mesg)
H5F_t *f; IN: pointer to the HDF5 file struct
const void *mesg; IN: Pointer to the source attribute struct
RETURNS
Size of message on success, 0 on failure
DESCRIPTION
This function returns the size of the raw attribute message on
success. (Not counting the message type or size fields, only the data
portion of the message). It doesn't take into account alignment.
*
* Modified:
* Robb Matzke, 17 Jul 1998
* Added padding between message parts for alignment.
--------------------------------------------------------------------------*/
static size_t
H5O_attr_size(const H5F_t *f, const void *_mesg)
{
const H5A_t *attr = (const H5A_t *)_mesg;
size_t name_len;
unsigned version; /* Attribute version */
hbool_t type_shared, space_shared; /* Flags to indicate that shared messages are used for this attribute */
hbool_t use_latest_format; /* Flag indicating the newest file format should be used */
size_t ret_value = 0;
FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5O_attr_size)
HDassert(attr);
name_len = HDstrlen(attr->name) + 1;
/* Get the file's 'use the latest version of the format' flag */
use_latest_format = H5F_USE_LATEST_FORMAT(f);
/* Check whether datatype and dataspace are shared */
if(H5O_msg_is_shared(H5O_DTYPE_ID, attr->dt) > 0)
type_shared = TRUE;
else
type_shared = FALSE;
if(H5O_msg_is_shared(H5O_SDSPACE_ID, attr->ds) > 0)
space_shared = TRUE;
else
space_shared = FALSE;
/* Check which version to write out */
if(use_latest_format)
version = H5O_ATTR_VERSION_LATEST; /* Write out latest version of format */
else if(space_shared)
version = H5O_ATTR_VERSION_4; /* Write version with shared dataspaces */
else if(attr->encoding != H5T_CSET_ASCII)
version = H5O_ATTR_VERSION_3; /* Write version which includes the character encoding */
else if(type_shared)
version = H5O_ATTR_VERSION_2; /* Write out version with shared datatype */
else
version = H5O_ATTR_VERSION_1; /* Write out basic version */
/* Common size information */
ret_value = 1 + /*version */
1 + /*reserved */
2 + /*name size inc. null */
2 + /*type size */
2; /*space size */
/* Version-specific size information */
if(version == H5O_ATTR_VERSION_1)
ret_value += H5O_ALIGN_OLD(name_len) + /*attribute name */
H5O_ALIGN_OLD(attr->dt_size) + /*data type */
H5O_ALIGN_OLD(attr->ds_size) + /*data space */
attr->data_size; /*the data itself */
else if(version == H5O_ATTR_VERSION_2)
ret_value += name_len + /*attribute name */
attr->dt_size + /*data type */
attr->ds_size + /*data space */
attr->data_size; /*the data itself */
else if(version == H5O_ATTR_VERSION_3 || version == H5O_ATTR_VERSION_4)
ret_value += 1 + /*character encoding */
name_len + /*attribute name */
attr->dt_size + /*data type */
attr->ds_size + /*data space */
attr->data_size; /*the data itself */
else
HDassert(0 && "Bad attribute version");
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_attr_size() */
/*-------------------------------------------------------------------------
* Function: H5O_attr_reset
*
* Purpose: Frees resources within a attribute message, but doesn't free
* the message itself.
*
* Return: Non-negative on success/Negative on failure
*
* Programmer: Robb Matzke
* Tuesday, December 9, 1997
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
static herr_t
H5O_attr_reset(void *_mesg)
{
H5A_t *attr = (H5A_t *)_mesg;
FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5O_attr_reset)
if(attr)
H5A_free(attr);
FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5O_attr_reset() */
/*-------------------------------------------------------------------------
* Function: H5O_attr_free
*
* Purpose: Free's the message
*
* Return: Non-negative on success/Negative on failure
*
* Programmer: Quincey Koziol
* Thursday, November 18, 2004
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
static herr_t
H5O_attr_free(void *mesg)
{
FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5O_attr_free)
HDassert(mesg);
H5FL_FREE(H5A_t, mesg);
FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5O_attr_free() */
/*-------------------------------------------------------------------------
* Function: H5O_attr_delete
*
* Purpose: Free file space referenced by message
*
* Return: Non-negative on success/Negative on failure
*
* Programmer: Quincey Koziol
* Friday, September 26, 2003
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
static herr_t
H5O_attr_delete(H5F_t *f, hid_t dxpl_id, const void *_mesg, hbool_t adj_link)
{
const H5A_t *attr = (const H5A_t *) _mesg;
htri_t tri_ret;
H5O_shared_t sh_mesg;
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI_NOINIT(H5O_attr_delete)
/* check args */
HDassert(f);
HDassert(attr);
/* Remove both the datatype and dataspace from the SOHM heap if they're
* shared there.
*/
if((tri_ret = H5O_msg_is_shared(H5O_DTYPE_ID, attr->dt)) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_BADMESG, FAIL, "can't tell if datatype is shared")
if(tri_ret > 0)
{
if(H5O_msg_get_share(H5O_DTYPE_ID, attr->dt, &sh_mesg) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_BADMESG, FAIL, "can't get shared message from datatype")
if(H5SM_try_delete(f, H5AC_dxpl_id, H5O_DTYPE_ID, &sh_mesg) < 0)
HGOTO_ERROR(H5E_SOHM, H5E_CANTREMOVE, FAIL, "can't remove datatype from SOHM heap")
}
if((tri_ret =H5O_msg_is_shared(H5O_SDSPACE_ID, attr->ds)) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_BADMESG, FAIL, "can't tell if dataspace is shared")
if(tri_ret > 0)
{
if(H5O_msg_get_share(H5O_SDSPACE_ID, attr->ds, &sh_mesg) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_BADMESG, FAIL, "can't get shared message from dataspace")
if(H5SM_try_delete(f, H5AC_dxpl_id, H5O_SDSPACE_ID, &sh_mesg) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_SOHM, FAIL, "can't remove dataspace from SOHM heap")
}
/* Check whether datatype is shared */
if(H5T_committed(attr->dt)) {
/* Decrement the reference count on the shared datatype, if requested */
if(adj_link)
if(H5T_link(attr->dt, -1, dxpl_id) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_LINKCOUNT, FAIL, "unable to adjust shared datatype link count")
} /* end if */
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_attr_delete() */
/*-------------------------------------------------------------------------
* Function: H5O_attr_link
*
* Purpose: Increment reference count on any objects referenced by
* message
*
* Return: Non-negative on success/Negative on failure
*
* Programmer: Quincey Koziol
* Friday, September 26, 2003
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
static herr_t
H5O_attr_link(H5F_t UNUSED *f, hid_t dxpl_id, const void *_mesg)
{
const H5A_t *attr = (const H5A_t *) _mesg;
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI_NOINIT(H5O_attr_link)
/* check args */
HDassert(f);
HDassert(attr);
/* Check whether datatype is shared */
if(H5T_committed(attr->dt)) {
/* Increment the reference count on the shared datatype */
if(H5T_link(attr->dt,1,dxpl_id) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_LINKCOUNT, FAIL, "unable to adjust shared datatype link count")
} /* end if */
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_attr_link() */
/*-------------------------------------------------------------------------
* Function: H5O_attr_pre_copy_file
*
* Purpose: Perform any necessary actions before copying message between
* files for attribute messages.
*
* Return: Success: Non-negative
*
* Failure: Negative
*
* Programmer: Quincey Koziol
* Monday, June 26, 2006
*
*-------------------------------------------------------------------------
*/
static herr_t
H5O_attr_pre_copy_file(H5F_t UNUSED *file_src, const H5O_msg_class_t UNUSED *type,
const void UNUSED *native_src, hbool_t *deleted, const H5O_copy_t *cpy_info,
void UNUSED *udata)
{
FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5O_attr_pre_copy_file)
/* check args */
HDassert(deleted);
HDassert(cpy_info);
/* If we are not copying attributes into the destination file, indicate
* that this message should be deleted.
*/
if(cpy_info->copy_without_attr)
*deleted = TRUE;
FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5O_attr_pre_copy_file() */
/*-------------------------------------------------------------------------
* Function: H5O_attr_copy_file
*
* Purpose: Copies a message from _MESG to _DEST in file
*
* Return: Success: Ptr to _DEST
*
* Failure: NULL
*
* Programmer: Quincey Koziol
* November 1, 2005
*
* Modifications: Peter Cao
* December 17, 2005: datatype conversion for variable length datatype
* August 1, 2006: fix reference datatype
*
*
*-------------------------------------------------------------------------
*/
static void *
H5O_attr_copy_file(H5F_t *file_src, const H5O_msg_class_t UNUSED *mesg_type,
void *native_src, H5F_t *file_dst, hid_t dxpl_id,
H5O_copy_t *cpy_info, void UNUSED *udata)
{
H5A_t *attr_src = (H5A_t *)native_src;
H5A_t *attr_dst = NULL;
/* for dataype conversion */
hid_t tid_src = -1; /* Datatype ID for source datatype */
hid_t tid_dst = -1; /* Datatype ID for destination datatype */
hid_t tid_mem = -1; /* Datatype ID for memory datatype */
void *buf = NULL; /* Buffer for copying data */
void *reclaim_buf = NULL; /* Buffer for reclaiming data */
hid_t buf_sid = -1; /* ID for buffer dataspace */
void *ret_value; /* Return value */
FUNC_ENTER_NOAPI_NOINIT(H5O_attr_copy_file)
/* check args */
HDassert(attr_src);
HDassert(file_dst);
HDassert(cpy_info);
HDassert(!cpy_info->copy_without_attr);
/* Allocate space for the destination message */
if(NULL == (attr_dst = H5FL_CALLOC(H5A_t)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")
/* Copy the top level of the attribute */
*attr_dst = *attr_src;
/* Don't have an opened group location for copy */
H5O_loc_reset(&(attr_dst->oloc));
H5G_name_reset(&(attr_dst->path));
attr_dst->obj_opened = 0;
/* Copy attribute's name */
attr_dst->name = H5MM_strdup(attr_src->name);
HDassert(attr_dst->name);
/* Copy attribute's datatype */
/* (Start destination datatype as transient, even if source is named) */
attr_dst->dt = H5T_copy(attr_src->dt, H5T_COPY_ALL);
HDassert(attr_dst->dt);
/* Set the location of the destination datatype */
if(H5T_set_loc(attr_dst->dt, file_dst, H5T_LOC_DISK) < 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "cannot mark datatype on disk")
/* Check for named datatype being copied */
if(H5T_committed(attr_src->dt)) {
H5O_loc_t *src_oloc; /* Pointer to source datatype's object location */
H5O_loc_t *dst_oloc; /* Pointer to dest. datatype's object location */
H5O_shared_t sh_mesg;
/* Get group entries for source & destination */
src_oloc = H5T_oloc(attr_src->dt);
HDassert(src_oloc);
dst_oloc = H5T_oloc(attr_dst->dt);
HDassert(dst_oloc);
/* Reset object location for new object */
H5O_loc_reset(dst_oloc);
dst_oloc->file = file_dst;
/* Copy the shared object from source to destination */
if(H5O_copy_header_map(src_oloc, dst_oloc, dxpl_id, cpy_info, FALSE) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_CANTCOPY, NULL, "unable to copy object")
/* Reset shared message information */
HDmemset(&sh_mesg, 0, sizeof(H5O_shared_t));
/* Get shared message information for datatype */
if(H5O_msg_get_share(H5O_DTYPE_ID, attr_src->dt, &sh_mesg/*out*/) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTOPENOBJ, NULL, "unable to get shared message")
/* Compute shared message size for datatype */
attr_dst->dt_size = H5O_msg_raw_size(file_dst, H5O_SHARED_ID, &sh_mesg);
} /* end if */
else
attr_dst->dt_size = H5O_msg_raw_size(file_dst, H5O_DTYPE_ID, attr_src->dt);
HDassert(attr_dst->dt_size > 0);
attr_dst->ds_size = H5S_raw_size(file_dst, attr_src->ds);
HDassert(attr_dst->ds_size > 0);
/* Copy the dataspace for the attribute */
attr_dst->ds = H5S_copy(attr_src->ds, FALSE);
HDassert(attr_dst->ds);
/* Compute the size of the data */
H5_ASSIGN_OVERFLOW(attr_dst->data_size, H5S_GET_EXTENT_NPOINTS(attr_dst->ds) * H5T_get_size(attr_dst->dt), hsize_t, size_t);
/* Copy (& convert) the data, if necessary */
if(attr_src->data) {
if(NULL == (attr_dst->data = H5FL_BLK_MALLOC(attr_buf, attr_dst->data_size)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")
/* Check if we need to convert data */
if(H5T_detect_class(attr_src->dt, H5T_VLEN) > 0) {
H5T_path_t *tpath_src_mem, *tpath_mem_dst; /* Datatype conversion paths */
H5T_t *dt_mem; /* Memory datatype */
size_t src_dt_size; /* Source datatype size */
size_t tmp_dt_size; /* Temp. datatype size */
size_t max_dt_size; /* Max atatype size */
H5S_t *buf_space; /* Dataspace describing buffer */
hsize_t buf_dim; /* Dimension for buffer */
size_t nelmts; /* Number of elements in buffer */
size_t buf_size; /* Size of copy buffer */
/* Create datatype ID for src datatype */
if((tid_src = H5I_register(H5I_DATATYPE, attr_src->dt)) < 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTREGISTER, NULL, "unable to register source file datatype")
/* create a memory copy of the variable-length datatype */
if(NULL == (dt_mem = H5T_copy(attr_src->dt, H5T_COPY_TRANSIENT)))
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "unable to copy")
if((tid_mem = H5I_register(H5I_DATATYPE, dt_mem)) < 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTREGISTER, NULL, "unable to register memory datatype")
/* create variable-length datatype at the destinaton file */
if((tid_dst = H5I_register(H5I_DATATYPE, attr_dst->dt)) < 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTREGISTER, NULL, "unable to register destination file datatype")
/* Set up the conversion functions */
if(NULL == (tpath_src_mem = H5T_path_find(attr_src->dt, dt_mem, NULL, NULL, dxpl_id, FALSE)))
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "unable to convert between src and mem datatypes")
if(NULL == (tpath_mem_dst = H5T_path_find(dt_mem, attr_dst->dt, NULL, NULL, dxpl_id, FALSE)))
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "unable to convert between mem and dst datatypes")
/* Determine largest datatype size */
if(0 == (src_dt_size = H5T_get_size(attr_src->dt)))
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "unable to determine datatype size")
if(0 == (tmp_dt_size = H5T_get_size(dt_mem)))
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "unable to determine datatype size")
max_dt_size = MAX(src_dt_size, tmp_dt_size);
if(0 == (tmp_dt_size = H5T_get_size(attr_dst->dt)))
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "unable to determine datatype size")
max_dt_size = MAX(max_dt_size, tmp_dt_size);
/* Set number of whole elements that fit in buffer */
if(0 == (nelmts = attr_src->data_size / src_dt_size))
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "element size too large")
/* Set up number of bytes to copy, and initial buffer size */
buf_size = nelmts * max_dt_size;
/* Create dataspace for number of elements in buffer */
buf_dim = nelmts;
/* Create the space and set the initial extent */
if(NULL == (buf_space = H5S_create_simple((unsigned)1, &buf_dim, NULL)))
HGOTO_ERROR(H5E_DATASPACE, H5E_CANTCREATE, NULL, "can't create simple dataspace")
/* Atomize */
if((buf_sid = H5I_register(H5I_DATASPACE, buf_space)) < 0) {
H5S_close(buf_space);
HGOTO_ERROR(H5E_ATOM, H5E_CANTREGISTER, NULL, "unable to register dataspace ID")
} /* end if */
/* Allocate memory for recclaim buf */
if(NULL == (reclaim_buf = H5FL_BLK_MALLOC(attr_buf, buf_size)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation NULLed for raw data chunk")
/* Allocate memory for copying the chunk */
if(NULL == (buf = H5FL_BLK_MALLOC(attr_buf, buf_size)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation NULLed for raw data chunk")
HDmemcpy(buf, attr_src->data, attr_src->data_size);
/* Convert from source file to memory */
if(H5T_convert(tpath_src_mem, tid_src, tid_mem, nelmts, (size_t)0, (size_t)0, buf, NULL, dxpl_id) < 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "datatype conversion NULLed")
HDmemcpy(reclaim_buf, buf, buf_size);
/* Convert from memory to destination file */
if(H5T_convert(tpath_mem_dst, tid_mem, tid_dst, nelmts, (size_t)0, (size_t)0, buf, NULL, dxpl_id) < 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "datatype conversion NULLed")
HDmemcpy(attr_dst->data, buf, attr_dst->data_size);
if(H5D_vlen_reclaim(tid_mem, buf_space, H5P_DATASET_XFER_DEFAULT, reclaim_buf) < 0)
HGOTO_ERROR(H5E_DATASET, H5E_BADITER, NULL, "unable to reclaim variable-length data")
} /* end if */
else if((H5T_get_class(attr_src->dt, FALSE) == H5T_REFERENCE) && (file_src != file_dst)) {
/* copy object pointed by reference. The current implementation does not deal with
nested reference such as reference in a compound structure */
/* Check for expanding references */
if(cpy_info->expand_ref) {
size_t ref_count;
/* Determine # of reference elements to copy */
ref_count = attr_dst->data_size / attr_dst->dt_size;
/* Copy objects referenced in source buffer to destination file and set destination elements */
if(H5O_copy_expand_ref(file_src, attr_src->data, dxpl_id,
file_dst, attr_dst->data, ref_count, H5T_get_ref_type(attr_src->dt), cpy_info) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTCOPY, NULL, "unable to copy reference attribute")
} /* end if */
else
/* Reset value to zero */
HDmemset(attr_dst->data, 0, attr_dst->data_size);
} /* end if */
else {
HDassert(attr_dst->data_size == attr_src->data_size);
HDmemcpy(attr_dst->data, attr_src->data, attr_src->data_size);
} /* end else */
} /* end if(attr_src->data) */
/* Indicate that the fill values aren't to be written out */
attr_dst->initialized = TRUE;
/* Set return value */
ret_value = attr_dst;
done:
if(buf_sid > 0)
if(H5I_dec_ref(buf_sid) < 0)
HDONE_ERROR(H5E_DATASET, H5E_CANTFREE, NULL, "Can't decrement temporary dataspace ID")
if(tid_src > 0)
/* Don't decrement ID, we want to keep underlying datatype */
if(H5I_remove(tid_src) == NULL)
HDONE_ERROR(H5E_DATASET, H5E_CANTFREE, NULL, "Can't decrement temporary datatype ID")
if(tid_dst > 0)
/* Don't decrement ID, we want to keep underlying datatype */
if(H5I_remove(tid_dst) == NULL)
HDONE_ERROR(H5E_DATASET, H5E_CANTFREE, NULL, "Can't decrement temporary datatype ID")
if(tid_mem > 0)
/* Decrement the memory datatype ID, it's transient */
if(H5I_dec_ref(tid_mem) < 0)
HDONE_ERROR(H5E_DATASET, H5E_CANTFREE, NULL, "Can't decrement temporary datatype ID")
if(buf)
H5FL_BLK_FREE(attr_buf, buf);
if(reclaim_buf)
H5FL_BLK_FREE(attr_buf, reclaim_buf);
/* Release destination attribute information on failure */
if(!ret_value)
if(attr_dst)
(void)H5A_free(attr_dst);
FUNC_LEAVE_NOAPI(ret_value)
} /* H5O_attr_copy_file() */
/*-------------------------------------------------------------------------
* Function: H5O_attr_get_share
*
* Purpose: Gets sharing information from the message
*
* Return: Non-negative on success/Negative on failure
*
* Programmer: James Laird
* Tuesday, October 17, 2006
*
*-------------------------------------------------------------------------
*/
static herr_t
H5O_attr_get_share(const void *_mesg, H5O_shared_t *sh /*out*/)
{
const H5A_t *mesg = (const H5A_t *)_mesg;
herr_t ret_value = SUCCEED;
FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5O_attr_get_share)
HDassert (mesg);
HDassert (sh);
if(NULL == H5O_msg_copy(H5O_SHARED_ID, &(mesg->sh_loc), sh))
ret_value = FAIL;
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_attr_get_share() */
/*-------------------------------------------------------------------------
* Function: H5O_attr_set_share
*
* Purpose: Sets sharing information for the message
*
* Return: Non-negative on success/Negative on failure
*
* Programmer: James Laird
* Tuesday, October 10, 2006
*
*-------------------------------------------------------------------------
*/
static herr_t
H5O_attr_set_share(void *_mesg/*in,out*/, const H5O_shared_t *sh)
{
H5A_t *mesg = (H5A_t *)_mesg;
herr_t ret_value = SUCCEED;
FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5O_attr_set_share)
HDassert (mesg);
HDassert (sh);
if(NULL == H5O_msg_copy(H5O_SHARED_ID, sh, &(mesg->sh_loc)))
ret_value = FAIL;
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_attr_set_share() */
/*-------------------------------------------------------------------------
* Function: H5O_attr_is_shared
*
* Purpose: Determines if this attribute is shared (committed or a SOHM)
* or not.
*
* Return: TRUE if attribute is shared
* FALSE if attribute is not shared
* Negative on failure
*
* Programmer: James Laird
* Tuesday, October 17, 2006
*
*-------------------------------------------------------------------------
*/
static htri_t
H5O_attr_is_shared(const void *_mesg)
{
const H5A_t *mesg = (const H5A_t *)_mesg;
htri_t ret_value;
FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5O_attr_is_shared)
HDassert(mesg);
/* Attributes can't currently be committed, but this should let the
* library read a "committed attribute" if we ever create one in
* the future.
*/
if(mesg->sh_loc.flags & (H5O_COMMITTED_FLAG | H5O_SHARED_IN_HEAP_FLAG))
ret_value = TRUE;
else
ret_value = FALSE;
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_attr_is_shared */
/*--------------------------------------------------------------------------
NAME
H5O_attr_debug
PURPOSE
Prints debugging information for an attribute message
USAGE
void *H5O_attr_debug(f, mesg, stream, indent, fwidth)
H5F_t *f; IN: pointer to the HDF5 file struct
const void *mesg; IN: Pointer to the source attribute struct
FILE *stream; IN: Pointer to the stream for output data
int indent; IN: Amount to indent information by
int fwidth; IN: Field width (?)
RETURNS
Non-negative on success/Negative on failure
DESCRIPTION
This function prints debugging output to the stream passed as a
parameter.
--------------------------------------------------------------------------*/
static herr_t
H5O_attr_debug(H5F_t *f, hid_t dxpl_id, const void *_mesg, FILE * stream, int indent,
int fwidth)
{
const H5A_t *mesg = (const H5A_t *)_mesg;
H5O_shared_t sh_mesg; /* Shared message information */
void *dt_mesg; /* Pointer to datatype message to dump */
herr_t (*debug)(H5F_t*, hid_t, const void*, FILE*, int, int)=NULL;
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI_NOINIT(H5O_attr_debug)
/* check args */
HDassert(f);
HDassert(stream);
HDassert(indent >= 0);
HDassert(fwidth >= 0);
fprintf(stream, "%*s%-*s \"%s\"\n", indent, "", fwidth,
"Name:",
mesg->name);
fprintf(stream, "%*s%-*s %u\n", indent, "", fwidth,
"Initialized:",
(unsigned int)mesg->initialized);
fprintf(stream, "%*s%-*s %u\n", indent, "", fwidth,
"Object opened:",
(unsigned int)mesg->obj_opened);
HDfprintf(stream, "%*s%-*s %a\n", indent, "", fwidth,
"Object:",
mesg->oloc.addr);
fprintf(stream, "%*sData type...\n", indent, "");
fprintf(stream, "%*s%-*s %lu\n", indent+3, "", MAX(0,fwidth-3),
"Size:",
(unsigned long)(mesg->dt_size));
fprintf (stream, "%*s%-*s %s\n", indent+3, "", MAX(0,fwidth-3),
"Shared:",
(H5T_committed(mesg->dt) ? "Yes" : "No")
);
if(H5T_committed(mesg->dt)) {
/* Reset shared message information */
HDmemset(&sh_mesg, 0, sizeof(H5O_shared_t));
/* Get shared message information from datatype */
if((H5O_MSG_DTYPE->get_share)(mesg->dt, &sh_mesg/*out*/) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTENCODE, FAIL, "can't retrieve shared message information")
debug = H5O_MSG_SHARED->debug;
dt_mesg = &sh_mesg;
} /* end if */
else {
debug = H5O_MSG_DTYPE->debug;
dt_mesg = mesg->dt;
} /* end else */
if(debug)
(debug)(f, dxpl_id, dt_mesg, stream, indent+3, MAX(0, fwidth-3));
else
fprintf(stream, "%*s<No info for this message>\n", indent + 6, "");
fprintf(stream, "%*sData space...\n", indent, "");
fprintf(stream, "%*s%-*s %lu\n", indent+3, "", MAX(0, fwidth - 3),
"Size:",
(unsigned long)(mesg->ds_size));
H5S_debug(f, dxpl_id, mesg->ds, stream, indent+3, MAX(0, fwidth - 3));
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_attr_debug() */
/*-------------------------------------------------------------------------
* Function: H5O_attr_write_cb
*
* Purpose: Object header iterator callback routine to update an
* attribute stored compactly.
*
* Return: Non-negative on success/Negative on failure
*
* Programmer: Quincey Koziol
* koziol@hdfgroup.org
* Dec 4 2006
*
*-------------------------------------------------------------------------
*/
static herr_t
H5O_attr_write_cb(H5O_t *oh, H5O_mesg_t *mesg/*in,out*/,
unsigned UNUSED sequence, unsigned *oh_flags_ptr, void *_udata/*in,out*/)
{
H5O_iter_wrt_t *udata = (H5O_iter_wrt_t *)_udata; /* Operator user data */
herr_t ret_value = H5_ITER_CONT; /* Return value */
FUNC_ENTER_NOAPI_NOINIT(H5O_attr_write_cb)
/* check args */
HDassert(oh);
HDassert(mesg);
HDassert(!udata->found);
/* Check for shared message */
if(mesg->flags & H5O_MSG_FLAG_SHARED) {
H5A_t shared_attr; /* Copy of shared attribute */
/*
* If the message is shared then then the native pointer points to an
* H5O_MSG_SHARED message. We use that information to look up the real
* message in the global heap or some other object header.
*/
if(NULL == H5O_shared_read(udata->f, udata->dxpl_id, mesg->native, H5O_MSG_ATTR, &shared_attr))
HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, H5_ITER_ERROR, "unable to read shared attribute")
/* Check for correct attribute message to modify */
if(HDstrcmp(shared_attr.name, udata->attr->name) == 0) {
htri_t shared_mesg; /* Whether the message should be shared */
/* 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(udata->f, udata->dxpl_id, H5O_ATTR_ID, udata->attr)) == 0)
HGOTO_ERROR(H5E_ATTR, H5E_BADMESG, H5_ITER_ERROR, "attribute changed sharing status")
else if(shared_mesg < 0)
HGOTO_ERROR(H5E_ATTR, H5E_BADMESG, H5_ITER_ERROR, "can't share attribute")
/* Remove the old attribut from the SOHM index */
if(H5SM_try_delete(udata->f, udata->dxpl_id, H5O_ATTR_ID, mesg->native) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTFREE, H5_ITER_ERROR, "unable to delete shared attribute in shared storage")
/* Extract shared message info from current attribute */
if(H5O_attr_get_share(udata->attr, mesg->native) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_BADMESG, H5_ITER_ERROR, "can't get shared info")
/* Indicate that we found the correct attribute */
udata->found = TRUE;
} /* end if */
/* Release copy of shared attribute */
H5O_attr_reset(&shared_attr);
} /* end if */
else {
/* Check for correct attribute message to modify */
if(HDstrcmp(((H5A_t *)mesg->native)->name, udata->attr->name) == 0) {
/* Allocate storage for the message's data, if necessary */
if(((H5A_t *)mesg->native)->data == NULL)
if(NULL == (((H5A_t *)mesg->native)->data = H5FL_BLK_MALLOC(attr_buf, udata->attr->data_size)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, H5_ITER_ERROR, "memory allocation failed")
/* Copy the data */
HDmemcpy(((H5A_t *)mesg->native)->data, udata->attr->data, udata->attr->data_size);
/* Indicate that we found the correct attribute */
udata->found = TRUE;
} /* end if */
} /* end else */
/* Set common info, if we found the correct attribute */
if(udata->found) {
/* Mark message as dirty */
mesg->dirty = TRUE;
/* Stop iterating */
ret_value = H5_ITER_STOP;
/* Indicate that the object header was modified */
*oh_flags_ptr |= H5AC__DIRTIED_FLAG;
} /* end if */
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_attr_write_cb() */
/*-------------------------------------------------------------------------
* Function: H5O_attr_write
*
* Purpose: Write a new value to an attribute.
*
* Return: Non-negative on success/Negative on failure
*
* Programmer: Quincey Koziol
* Monday, December 4, 2006
*
*-------------------------------------------------------------------------
*/
herr_t
H5O_attr_write(const H5O_loc_t *loc, hid_t dxpl_id, H5A_t *attr)
{
H5O_t *oh = NULL; /* Pointer to actual object header */
unsigned oh_flags = H5AC__NO_FLAGS_SET; /* Metadata cache flags for object header */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI_NOINIT(H5O_attr_write)
/* Check arguments */
HDassert(loc);
HDassert(attr);
/* Protect the object header to iterate over */
if(NULL == (oh = H5AC_protect(loc->file, dxpl_id, H5AC_OHDR, loc->addr, NULL, NULL, H5AC_WRITE)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTLOAD, FAIL, "unable to load object header")
/* Check for attributes stored densely */
if(H5F_addr_defined(oh->attr_fheap_addr)) {
/* Modify the attribute data in dense storage */
if(H5A_dense_write(loc->file, dxpl_id, oh, 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.dxpl_id = dxpl_id;
udata.attr = attr;
udata.found = FALSE;
/* Iterate over attributes, to locate correct one to update */
op.lib_op = H5O_attr_write_cb;
if(H5O_msg_iterate_real(loc->file, oh, H5O_MSG_ATTR, TRUE, op, &udata, dxpl_id, &oh_flags) < 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, dxpl_id, oh, FALSE, &oh_flags) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "unable to update time on object")
done:
if(oh && H5AC_unprotect(loc->file, dxpl_id, H5AC_OHDR, loc->addr, oh, oh_flags) < 0)
HDONE_ERROR(H5E_ATTR, H5E_PROTECT, FAIL, "unable to release object header")
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_attr_write */
/*-------------------------------------------------------------------------
* Function: H5O_attr_rename_dup_cb
*
* Purpose: Object header iterator callback routine to check for
* duplicate name during rename
*
* Return: Non-negative on success/Negative on failure
*
* Programmer: Quincey Koziol
* koziol@hdfgroup.org
* Dec 5 2006
*
*-------------------------------------------------------------------------
*/
static herr_t
H5O_attr_rename_dup_cb(H5O_t *oh, H5O_mesg_t *mesg/*in,out*/,
unsigned UNUSED sequence, unsigned UNUSED *oh_flags_ptr, 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_NOAPI_NOINIT(H5O_attr_rename_dup_cb)
/* check args */
HDassert(oh);
HDassert(mesg);
HDassert(!udata->found);
/* Check for shared message */
if(mesg->flags & H5O_MSG_FLAG_SHARED) {
H5A_t shared_attr; /* Copy of shared attribute */
/*
* If the message is shared then then the native pointer points to an
* H5O_MSG_SHARED message. We use that information to look up the real
* message in the global heap or some other object header.
*/
if(NULL == H5O_shared_read(udata->f, udata->dxpl_id, mesg->native, H5O_MSG_ATTR, &shared_attr))
HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, H5_ITER_ERROR, "unable to read shared attribute")
/* Check for existing attribute with new name */
if(HDstrcmp(shared_attr.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 */
/* Release copy of shared attribute */
H5O_attr_reset(&shared_attr);
} /* end if */
else {
/* Check for existing attribute with new name */
if(HDstrcmp(((H5A_t *)mesg->native)->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 */
} /* end else */
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_attr_rename_dup_cb() */
/*-------------------------------------------------------------------------
* Function: H5O_attr_rename_mod_cb
*
* Purpose: Object header iterator callback routine to change name of
* attribute during rename
*
* Return: Non-negative on success/Negative on failure
*
* Programmer: Quincey Koziol
* koziol@hdfgroup.org
* Dec 5 2006
*
*-------------------------------------------------------------------------
*/
static herr_t
H5O_attr_rename_mod_cb(H5O_t *oh, H5O_mesg_t *mesg/*in,out*/,
unsigned UNUSED sequence, unsigned *oh_flags_ptr, 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_NOAPI_NOINIT(H5O_attr_rename_mod_cb)
/* check args */
HDassert(oh);
HDassert(mesg);
HDassert(!udata->found);
/* Check for shared message */
if(mesg->flags & H5O_MSG_FLAG_SHARED) {
H5A_t shared_attr; /* Copy of shared attribute */
/*
* If the message is shared then then the native pointer points to an
* H5O_MSG_SHARED message. We use that information to look up the real
* message in the global heap or some other object header.
*/
if(NULL == H5O_shared_read(udata->f, udata->dxpl_id, mesg->native, H5O_MSG_ATTR, &shared_attr))
HGOTO_ERROR(H5E_ATTR, H5E_CANTINIT, H5_ITER_ERROR, "unable to read shared attribute")
/* Check for correct attribute message to modify */
if(HDstrcmp(shared_attr.name, udata->old_name) == 0) {
/* XXX: fix me */
HDfprintf(stderr, "%s: renaming a shared attribute not supported yet!\n", FUNC);
HGOTO_ERROR(H5E_ATTR, H5E_UNSUPPORTED, H5_ITER_ERROR, "renaming a shared attribute not supported yet")
} /* end if */
/* Release copy of shared attribute */
H5O_attr_reset(&shared_attr);
} /* end if */
else {
/* Find correct attribute message to rename */
if(HDstrcmp(((H5A_t *)mesg->native)->name, udata->old_name) == 0) {
/* Change the name for the attribute */
H5MM_xfree(((H5A_t *)mesg->native)->name);
((H5A_t *)mesg->native)->name = H5MM_xstrdup(udata->new_name);
/* Indicate that we found an existing attribute with the old name*/
udata->found = TRUE;
/* Mark message as dirty */
mesg->dirty = TRUE;
/* Stop iterating */
ret_value = H5_ITER_STOP;
/* Indicate that the object header was modified */
*oh_flags_ptr |= H5AC__DIRTIED_FLAG;
} /* end if */
} /* end else */
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_attr_rename_mod_cb() */
/*-------------------------------------------------------------------------
* Function: H5O_attr_rename
*
* Purpose: Rename an attribute.
*
* Return: Non-negative on success/Negative on failure
*
* Programmer: Quincey Koziol
* Tuesday, December 5, 2006
*
*-------------------------------------------------------------------------
*/
herr_t
H5O_attr_rename(const H5O_loc_t *loc, hid_t dxpl_id, const char *old_name, const char *new_name)
{
H5O_t *oh = NULL; /* Pointer to actual object header */
unsigned oh_flags = H5AC__NO_FLAGS_SET; /* Metadata cache flags for object header */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI_NOINIT(H5O_attr_rename)
/* Check arguments */
HDassert(loc);
HDassert(old_name);
HDassert(new_name);
/* Protect the object header to iterate over */
if(NULL == (oh = H5AC_protect(loc->file, dxpl_id, H5AC_OHDR, loc->addr, NULL, NULL, H5AC_WRITE)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTLOAD, FAIL, "unable to load object header")
/* Check for attributes stored densely */
if(H5F_addr_defined(oh->attr_fheap_addr)) {
/* XXX: fix me */
HDfprintf(stderr, "%s: renaming attributes in dense storage not supported yet!\n", FUNC);
HGOTO_ERROR(H5E_ATTR, H5E_UNSUPPORTED, FAIL, "renaming attributes in dense storage not supported yet")
} /* end if */
else {
H5O_iter_ren_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.dxpl_id = dxpl_id;
udata.old_name = old_name;
udata.new_name = new_name;
udata.found = FALSE;
/* Iterate over attributes, to check if "new name" exists already */
op.lib_op = H5O_attr_rename_dup_cb;
if(H5O_msg_iterate_real(loc->file, oh, H5O_MSG_ATTR, TRUE, op, &udata, dxpl_id, &oh_flags) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "error updating attribute")
/* If the new name was found, indicate an error */
if(udata.found)
HGOTO_ERROR(H5E_ATTR, H5E_EXISTS, FAIL, "attribute with new name already exists")
/* Iterate over attributes again, to actually rename attribute with old name */
op.lib_op = H5O_attr_rename_mod_cb;
if(H5O_msg_iterate_real(loc->file, oh, H5O_MSG_ATTR, TRUE, op, &udata, dxpl_id, &oh_flags) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "error updating attribute")
} /* end else */
/* Update the modification time, if any */
if(H5O_touch_oh(loc->file, dxpl_id, oh, FALSE, &oh_flags) < 0)
HGOTO_ERROR(H5E_ATTR, H5E_CANTUPDATE, FAIL, "unable to update time on object")
done:
if(oh && H5AC_unprotect(loc->file, dxpl_id, H5AC_OHDR, loc->addr, oh, oh_flags) < 0)
HDONE_ERROR(H5E_ATTR, H5E_PROTECT, FAIL, "unable to release object header")
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_attr_rename */
/*-------------------------------------------------------------------------
* Function: H5O_attr_iterate
*
* Purpose: Iterate over attributes for an object.
*
* Return: Non-negative on success/Negative on failure
*
* Programmer: Quincey Koziol
* Tuesday, December 5, 2006
*
*-------------------------------------------------------------------------
*/
herr_t
H5O_attr_iterate(hid_t loc_id, const H5O_loc_t *loc, hid_t dxpl_id,
unsigned skip, unsigned *last_attr, H5A_operator_t op, void *op_data)
{
H5O_t *oh = NULL; /* Pointer to actual object header */
haddr_t attr_fheap_addr; /* Address of fractal heap for dense attribute storage */
haddr_t name_bt2_addr; /* Address of v2 B-tree for name index on dense attribute storage */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI_NOINIT(H5O_attr_iterate)
/* Check arguments */
HDassert(loc);
HDassert(op);
/* Protect the object header to iterate over */
if(NULL == (oh = H5AC_protect(loc->file, dxpl_id, H5AC_OHDR, loc->addr, NULL, NULL, H5AC_READ)))
HGOTO_ERROR(H5E_ATTR, H5E_CANTLOAD, FAIL, "unable to load object header")
/* Retrieve the information about dense attribute storage */
if(oh->version > H5O_VERSION_1 && H5F_addr_defined(oh->attr_fheap_addr)) {
attr_fheap_addr = oh->attr_fheap_addr;
name_bt2_addr = oh->name_bt2_addr;
} /* end if */
else
attr_fheap_addr = name_bt2_addr = HADDR_UNDEF;
/* Release the object header */
if(H5AC_unprotect(loc->file, dxpl_id, H5AC_OHDR, loc->addr, oh, H5AC__NO_FLAGS_SET) < 0)
HDONE_ERROR(H5E_ATTR, H5E_PROTECT, FAIL, "unable to release object header")
oh = NULL;
/* Check for attributes stored densely */
if(H5F_addr_defined(attr_fheap_addr)) {
if((ret_value = H5A_dense_iterate(loc->file, dxpl_id, loc_id, attr_fheap_addr,
name_bt2_addr, skip, last_attr, op, op_data)) < 0)
HERROR(H5E_ATTR, H5E_BADITER, "error iterating over attributes");
} /* end if */
else {
unsigned idx; /* Current attribute to operate on */
/* Check for skipping over too many attributes */
if((int)skip < H5O_msg_count(loc, H5O_ATTR_ID, dxpl_id)) {
H5A_t found_attr; /* Copy of attribute for callback */
/* Read each attribute and call application's callback */
/* (this could be made more efficient by iterating over the
* attribute header messages with H5O_msg_iterate, but then
* the object header would be locked during the callback into
* the application code, causing problems if they attempt to
* do anything with the object the attribute is on - QAK)
*/
idx = skip;
while(H5O_msg_read(loc, H5O_ATTR_ID, (int)idx, &found_attr, dxpl_id) != NULL) {
/* Call application's callback */
idx++;
if((ret_value = (op)(loc_id, found_attr.name, op_data)) != 0) {
H5A_free(&found_attr);
break;
} /* end if */
H5A_free(&found_attr);
} /* end while */
/* Clear error stack from running off end of attributes */
if(ret_value == 0)
H5E_clear_stack(NULL);
} /* end if */
else
if(skip > 0)
HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid index specified")
/* Update last attribute looked at */
if(last_attr)
*last_attr = idx;
} /* end else */
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_attr_iterate */
|