-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathh5filewrapper.cpp
144 lines (123 loc) · 4.03 KB
/
h5filewrapper.cpp
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
#include "h5filewrapper.h"
hid_t
H5FSRoot::OpenRoot(const std::string&& path){
if (!H5Fis_hdf5(path.c_str()))
assert(false);
m_rootid = H5Fopen(path.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
if (m_rootid < 0)
assert(false);
return m_rootid;
}
herr_t
H5FSRoot::CloseRoot(){
if (m_rootid >= 0)
return H5Fclose(m_rootid);
return 0;
}
H5FSRoot&
H5FSRoot::GetRootObject(){
static std::unique_ptr<H5FSRoot> inst{new H5FSRoot()};
return *inst;
}
hid_t
H5FSRoot::GetRootHandle() const{
return m_rootid;
}
hsize_t
H5FSRoot::DsetSize(const std::string&& path){
assert(m_rootid >= 0);
hid_t dataset = H5Dopen(m_rootid, path.c_str(), H5P_DEFAULT);
if (dataset < 0)
return 0;
hid_t datatype = H5Dget_type(dataset);
hid_t dataspace = H5Dget_space(dataset);
size_t type_size = H5Tget_size(datatype);
size_t num_elems = H5Sget_simple_extent_npoints(dataspace);
H5Sclose(dataspace);
H5Dclose(dataset);
return num_elems * type_size;
}
herr_t
H5FSRoot::FillDir(const std::string&& path, std::vector<std::string>& entries){
H5G_info_t group_info;
assert(m_rootid >= 0);
if(H5Gget_info_by_name(m_rootid, path.c_str(), &group_info, H5P_DEFAULT) < 0)
return -ENOENT;
entries.emplace_back(".");
entries.emplace_back("..");
for (hsize_t i = 0; i < group_info.nlinks; ++i) {
char name[128];
herr_t err = H5Lget_name_by_idx(m_rootid, path.c_str(),
H5_INDEX_NAME, H5_ITER_INC, i, name, 128, H5P_DEFAULT);
if (err < 0)
return err;
entries.emplace_back(std::string(name));
}
return 0;
}
herr_t
H5FSRoot::GetAttr(const std::string&& path, struct stat *stbuf){
memset(stbuf, 0, sizeof(struct stat));
H5O_info_t obj_info;
assert(m_rootid >= 0);
if(H5Oget_info_by_name(m_rootid, path.c_str(), &obj_info, H5O_INFO_ALL, H5P_DEFAULT) < 0)
return -ENOENT;
stbuf->st_ctim.tv_sec = obj_info.ctime;
stbuf->st_atim.tv_sec = obj_info.atime;
stbuf->st_mtim.tv_sec = obj_info.mtime;
if(obj_info.type == H5O_TYPE_GROUP) {
stbuf->st_mode = S_IFDIR | 0555;
H5G_info_t group_info;
H5Gget_info_by_name(m_rootid, path.c_str(), &group_info, H5P_DEFAULT);
stbuf->st_nlink = 2 + group_info.nlinks;
stbuf->st_size = group_info.nlinks;
}
else if (obj_info.type == H5O_TYPE_DATASET) {
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_size = DsetSize(std::move(path));
}
else {
stbuf->st_mode = S_IFCHR | 0000;
stbuf->st_size = 0;
}
return 0;
}
herr_t
H5FSRoot::Open(const std::string&& path, struct fuse_file_info *fi){
assert(m_rootid >= 0);
if((fi->flags & 3) != O_RDONLY)
return -EACCES;
H5O_info_t obj_info;
if(H5Oget_info_by_name(m_rootid, path.c_str(), &obj_info, H5P_DEFAULT, H5P_DEFAULT) < 0)
return -ENOENT;
fi->fh = H5Dopen(m_rootid, path.c_str(), H5P_DEFAULT);
return 0;
}
hsize_t
H5FSRoot::Read(const std::string&& path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi){
(void) fi;
assert(m_rootid >= 0);
hid_t dataset = H5Dopen(m_rootid, path.c_str(), H5P_DEFAULT);
hid_t datatype = H5Dget_type(dataset);
size_t buf_size = DsetSize(path.c_str());
char *hdf5_buf = new char[buf_size];
H5Dread(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, hdf5_buf);
size_t copy_size = buf_size - offset < size ? buf_size - offset : size;
memcpy(buf, hdf5_buf+offset, copy_size);
delete[] hdf5_buf;
H5Dclose(dataset);
return copy_size;
}
herr_t
H5FSRoot::Unlink(const std::string&& path){
H5O_info_t obj_info;
assert(m_rootid >= 0);
if(H5Oget_info_by_name(m_rootid, path.c_str(), &obj_info, H5P_DEFAULT, H5P_DEFAULT) < 0)
return -ENOENT;
return H5Ldelete(m_rootid, path.c_str(), H5P_DEFAULT);
}
herr_t
H5FSRoot::Mkgrp(const std::string&& path, mode_t mode){
assert(m_rootid >= 0);
return (H5Gcreate(m_rootid, path.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT) >= 0) ? 0 : EACCES;
}