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
|
/* -*- c-file-style: "stroustrup" -*- */
#include "hdf5.h"
#include <stdio.h>
#include <stdlib.h>
#define H5P_DEFAULTx2 H5P_DEFAULT, H5P_DEFAULT
int
main(void)
{
int ret_val = EXIT_SUCCESS;
//! <!-- [create] -->
{
__label__ fail_file;
hid_t file, group;
char src_path[] = "/a/few/groups";
if ((file = H5Fcreate("o1.h5", H5F_ACC_TRUNC, H5P_DEFAULTx2)) == H5I_INVALID_HID) {
ret_val = EXIT_FAILURE;
goto fail_file;
}
// create a few groups
{
__label__ fail_group, fail_lcpl;
hid_t lcpl;
if ((lcpl = H5Pcreate(H5P_LINK_CREATE)) == H5I_INVALID_HID) {
ret_val = EXIT_FAILURE;
goto fail_lcpl;
}
if (H5Pset_create_intermediate_group(lcpl, 1) < 0) {
ret_val = EXIT_FAILURE;
goto fail_group;
}
if ((group = H5Gcreate(file, src_path, lcpl, H5P_DEFAULTx2)) == H5I_INVALID_HID) {
ret_val = EXIT_FAILURE;
goto fail_group;
}
H5Gclose(group);
fail_group:
H5Pclose(lcpl);
fail_lcpl:;
}
// create a copy
if (H5Ocopy(file, ".", file, "copy of", H5P_DEFAULTx2) < 0) {
ret_val = EXIT_FAILURE;
}
H5Fclose(file);
fail_file:;
}
//! <!-- [create] -->
//! <!-- [read] -->
{
__label__ fail_info, fail_file;
hid_t file;
char path[] = "/a/few/groups";
H5O_info2_t info;
if ((file = H5Fopen("o1.h5", H5F_ACC_RDONLY, H5P_DEFAULT)) == H5I_INVALID_HID) {
ret_val = EXIT_FAILURE;
goto fail_file;
}
// retrieve information about the object
if (H5Oget_info_by_name(file, path, &info, H5O_INFO_BASIC | H5O_INFO_NUM_ATTRS, H5P_DEFAULT) < 0) {
ret_val = EXIT_FAILURE;
goto fail_info;
}
// determine the object type
switch (info.type) {
case H5O_TYPE_GROUP:
printf("HDF5 group\n");
break;
case H5O_TYPE_DATASET:
printf("HDF5 dataset\n");
break;
case H5O_TYPE_NAMED_DATATYPE:
printf("HDF5 datatype\n");
break;
default:
printf("UFO?\n");
break;
}
// print basic information
printf("Reference count: %u\n", info.rc);
printf("Attribute count: %lld\n", info.num_attrs);
fail_info:
H5Fclose(file);
fail_file:;
}
//! <!-- [read] -->
//! <!-- [update] -->
{
__label__ fail_obj, fail_incr, fail_file;
hid_t file, obj;
char path[] = "/a/few/groups";
H5O_info2_t info;
if ((file = H5Fopen("o1.h5", H5F_ACC_RDWR, H5P_DEFAULT)) == H5I_INVALID_HID) {
ret_val = EXIT_FAILURE;
goto fail_file;
}
// open an object by path name
if ((obj = H5Oopen(file, path, H5P_DEFAULT)) == H5I_INVALID_HID) {
ret_val = EXIT_FAILURE;
goto fail_obj;
}
// bump its reference count (by 1)
if (H5Oincr_refcount(obj) < 0) {
ret_val = EXIT_FAILURE;
goto fail_incr;
}
// confirm the new reference count
if (H5Oget_info(obj, &info, H5O_INFO_BASIC) < 0) {
ret_val = EXIT_FAILURE;
goto fail_incr;
}
printf("Reference count: %u\n", info.rc);
fail_incr:
H5Oclose(obj);
fail_obj:
H5Fclose(file);
fail_file:;
}
//! <!-- [update] -->
//! <!-- [delete] -->
{
__label__ fail_obj, fail_delete, fail_file;
hid_t file, obj;
char path[] = "/a/few/groups";
H5O_info2_t info;
if ((file = H5Fopen("o1.h5", H5F_ACC_RDWR, H5P_DEFAULT)) == H5I_INVALID_HID) {
ret_val = EXIT_FAILURE;
goto fail_file;
}
// open an object by path name
if ((obj = H5Oopen(file, path, H5P_DEFAULT)) == H5I_INVALID_HID) {
ret_val = EXIT_FAILURE;
goto fail_obj;
}
// render it inaccessible from the root group by deleting the one and
// only link to it; this drops the reference count by 1
if (H5Ldelete(file, path, H5P_DEFAULT) < 0) {
ret_val = EXIT_FAILURE;
goto fail_delete;
}
// confirm the new reference count
if (H5Oget_info(obj, &info, H5O_INFO_BASIC) < 0) {
ret_val = EXIT_FAILURE;
goto fail_delete;
}
printf("Reference count: %u\n", info.rc);
// if we close the file at this point, we'd be creating a tombstone,
// an object that cannot be reached and that cannot be reclaimed by the
// freespace manager; decrement the reference count to zero to prevent that
if (H5Idec_ref(obj) < 0) {
ret_val = EXIT_FAILURE;
goto fail_delete;
}
else
// attempting to close the object would be like a double H5Oclose and fail
goto fail_obj;
fail_delete:
H5Oclose(obj);
fail_obj:
H5Fclose(file);
fail_file:;
}
//! <!-- [delete] -->
return ret_val;
}
|