-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnfile_link.c
250 lines (213 loc) · 7.73 KB
/
nfile_link.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Copyright (C) 2021, Northwestern University
* See COPYRIGHT notice in top-level directory.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* strdup() */
#include <unistd.h> /* getopt() */
#include <assert.h> /* assert() */
#include <errno.h>
#include <hdf5.h>
static int verbose;
#define HANDLE_ERROR(msg,name) { \
printf("Error at line %d: func %s on %s\n",__LINE__,msg,name); \
err_exit = -1; \
goto fn_exit; \
}
/* parameters to be passed to the call back function */
typedef struct {
char in_fname[1024];
hid_t out_fd; /* output file ID */
herr_t err;
} op_data;
/*----< metadata() >---------------------------------------------------------*/
/* call back function used in H5Ovisit() */
static
herr_t metadata(hid_t loc_id, /* object ID */
const char *name, /* object name */
const H5O_info_t *info, /* object metadata */
void *operator_data) /* data passed from caller */
{
int err_exit=0;
herr_t err=0;
op_data *it_op = (op_data*)operator_data;
it_op->err = 0;
if (info->type == H5O_TYPE_GROUP) {
/* Skip root group */
if (!strcmp(name, ".")) return 0;
if (verbose) printf("Create external link for GROUP %s\n",name);
err = H5Lcreate_external(it_op->in_fname, name, it_op->out_fd, name,
H5P_DEFAULT, H5P_DEFAULT);
if (err < 0) HANDLE_ERROR("H5Lcreate_external", name)
}
/* ignore other object types */
fn_exit:
it_op->err = err_exit;
return (err_exit == 0) ? 0 : 1;
/* return a positive value causes the visit iterator to
* immediately return that positive value, indicating
* short-circuit success.
*/
}
/*----< check_h5_objects() >-------------------------------------------------*/
static
int check_h5_objects(const char *filename, hid_t fid)
{
ssize_t num_objs;
size_t ii, howmany;
H5I_type_t ot;
hid_t *objs;
char obj_name[1024];
num_objs = H5Fget_obj_count(fid, H5F_OBJ_ALL);
if (num_objs <= 0) return num_objs;
/* ignore FILE object */
if (num_objs > 1) printf("%zd object(s) open\n", num_objs);
objs = (hid_t*) malloc(num_objs * sizeof(hid_t));
howmany = H5Fget_obj_ids(fid, H5F_OBJ_ALL, num_objs, objs);
if (howmany > 1) printf("open objects: %zd\n", howmany);
for (ii=0; ii<howmany; ii++) {
char *type_name="";
ot = H5Iget_type(objs[ii]);
if (ot == H5I_FILE) continue; /* type_name = "H5I_FILE"; */
else if (ot == H5I_GROUP) type_name = "H5I_GROUP";
else if (ot == H5I_DATATYPE) type_name = "H5I_DATATYPE";
else if (ot == H5I_DATASPACE) type_name = "H5I_DATASPACE";
else if (ot == H5I_DATASET) type_name = "H5I_DATASET";
else if (ot == H5I_ATTR) type_name = "H5I_ATTR";
H5Iget_name(objs[ii], obj_name, 1024);
printf("%s %4zd: type %s, name %s\n",
filename, ii, type_name, obj_name);
}
free(objs);
return howmany;
}
/*----< usage() >------------------------------------------------------------*/
static void
usage(char *progname)
{
#define USAGE "\
[-h] print this command usage message\n\
[-v] verbose mode (default: off)\n\
-i file name of input text file (required)\n\
-o file name of output HDF5 file (required)\n\n\
This utility program creates a master file and adds groups of all input\n\
files as external links, The group names must be all distinct. \n\
*ph5concat version _PH5CONCAT_VERSION_ of _PH5CONCAT_RELEASE_DATE_\n"
printf("Usage: %s [-h|-v] -i input_file -o output_file\n%s\n", progname, USAGE);
}
/*----< main() >-------------------------------------------------------------*/
int main(int argc, char **argv)
{
FILE *fd;
int i, c, err_exit=0, num_files;
char *infname=NULL, *outfname=NULL, line[1024], **infile_names;
herr_t err;
op_data it_op;
verbose = 0; /* default is quiet */
/* command-line arguments */
while ((c = getopt(argc, argv, "hvi:o:")) != -1)
switch(c) {
case 'v': verbose = 1;
break;
case 'h': usage(argv[0]);
return 0;
case 'i': infname = strdup(optarg);
break;
case 'o': outfname = strdup(optarg);
break;
default : usage(argv[0]);
return 1;
}
if (infname == NULL) { /* input file name is mandatory */
printf("input file is missing\n");
usage(argv[0]);
exit(1);
}
if (outfname == NULL) { /* output file name is mandatory */
printf("output file is missing\n");
usage(argv[0]);
exit(1);
}
if (verbose) {
printf("input file: %s\n", infname);
printf("output file: %s\n", outfname);
}
memset(&it_op, 0, sizeof(op_data));
fd = fopen(infname, "r");
if (fd == NULL) {
printf("Error: open fails on file %s (%s)\n",infname,strerror(errno));
exit(1);
}
/* count number of input files */
num_files = 0;
while (fgets(line, 1024, fd)) {
if (strlen(line) == 0)
continue; /* skip empty lines */
if (line[0] == '#')
continue; /* skip comment line (start with #) */
num_files++;
}
if (verbose) printf("Number of input files = %d\n",num_files);
infile_names = (char**) malloc(num_files * sizeof(char*));
infile_names[0] = (char*) malloc(num_files * 1024);
for (i=1; i<num_files; i++)
infile_names[i] = infile_names[i-1] + 1024;
/* read input file names */
rewind(fd);
i = 0;
while (fgets(line, 1024, fd)) {
char *tail;
if (strlen(line) == 0)
continue; /* skip empty lines */
if (line[0] == '#')
continue; /* skip comment line (start with #) */
/* remove blanks at tail. Note fgets stores newline to the buffer */
tail = line + strlen(line) - 1;
while (*tail == ' ' || *tail == '\t' || *tail == '\n') tail--;
tail[1] = '\0';
/* save file name to in_list */
strcpy(infile_names[i], line);
i++;
}
assert(i == num_files);
fclose(fd);
/* create output file */
it_op.out_fd = H5Fcreate(outfname, H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
if (it_op.out_fd < 0)
HANDLE_ERROR("H5Fcreate in exclusive mode ", outfname)
for (i=0; i<num_files; i++) {
char *fname = infile_names[i];
if (verbose) printf("open file %s\n",fname);
/* open input file in read-only mode */
hid_t in_fd = H5Fopen(fname, H5F_ACC_RDONLY, H5P_DEFAULT);
if (in_fd < 0) HANDLE_ERROR("Can't open input file", fname)
strcpy(it_op.in_fname, fname);
/* Iterate all objects in the input file i */
#if defined HAS_H5OVISIT3 && HAS_H5OVISIT3
err = H5Ovisit3(in_fd, H5_INDEX_CRT_ORDER, H5_ITER_NATIVE, metadata,
&it_op, H5O_INFO_ALL);
if (err < 0) HANDLE_ERROR("H5Ovisit3", fname)
#else
err = H5Ovisit(in_fd, H5_INDEX_CRT_ORDER, H5_ITER_NATIVE, metadata,
&it_op);
if (err < 0) HANDLE_ERROR("H5Ovisit", fname)
#endif
if (it_op.err < 0) HANDLE_ERROR("H5Ovisit", fname)
check_h5_objects(fname, in_fd);
err = H5Fclose(in_fd);
if (err < 0) HANDLE_ERROR("H5Fclose ", fname)
}
check_h5_objects(outfname, it_op.out_fd);
err = H5Fclose(it_op.out_fd);
if (err < 0) HANDLE_ERROR("H5Fclose ",outfname)
fn_exit:
free(infile_names[0]);
free(infile_names);
if (infname != NULL) free(infname);
if (outfname != NULL) free(outfname);
return (err_exit != 0);
}