forked from tihmstar/orbisFSTool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrbisFSFuse.cpp
More file actions
277 lines (239 loc) · 7.55 KB
/
OrbisFSFuse.cpp
File metadata and controls
277 lines (239 loc) · 7.55 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
//
// OrbisFSFuse.cpp
// orbisFSTool
//
// Created by tihmstar on 18.12.25.
//
#include "OrbisFSFuse.hpp"
#include "OrbisFSException.hpp"
#include <libgeneral/macros.h>
#ifdef HAVE_FUSE
# define FUSE_USE_VERSION 28
# include <fuse/fuse.h>
#endif
using namespace orbisFSTool;
#ifdef HAVE_FUSE
static int fs_getattr(const char *path, struct stat *stbuf) noexcept{
struct fuse_context *ctx = fuse_get_context();
OrbisFSImage *img = (OrbisFSImage*)ctx->private_data;
OrbisFSInode_t node = {};
memset(stbuf, 0, sizeof(*stbuf));
try {
node = img->getInodeForPath(path);
} catch(tihmstar::OrbisFSFileNotFound &e){
return -EEXIST;
} catch (tihmstar::exception &e) {
e.dump();
return -EFAULT;
}
stbuf->st_ino = node.inodeNum;
stbuf->st_mode = node.fileMode;
stbuf->st_size = node.filesize;
{
#ifdef __APPLE__
stbuf->st_birthtimespec.tv_sec = node.createDate;
stbuf->st_birthtimespec.tv_nsec = 0;
stbuf->st_mtimespec.tv_sec = node.modDate;
stbuf->st_mtimespec.tv_nsec = 0;
stbuf->st_ctimespec.tv_sec = node.createDate;
stbuf->st_ctimespec.tv_nsec = 0;
stbuf->st_atimespec.tv_sec = node.accessDate;
stbuf->st_atimespec.tv_nsec = 0;
#elif defined(__linux__)
stbuf->st_mtim.tv_sec = node.modDate;
stbuf->st_mtim.tv_nsec = 0;
stbuf->st_ctim.tv_sec = node.createDate;
stbuf->st_ctim.tv_nsec = 0;
stbuf->st_atim.tv_sec = node.accessDate;
stbuf->st_atim.tv_nsec = 0;
#else
#error unexpected platform!
#endif
}
stbuf->st_mode |= 05; //this isn't accurate, but we do want to read the files afterall, right?
return 0;
}
static int fs_readlink(const char *path, char *buf, size_t bufsize) noexcept{
/*
Currently not supported
*/
return -EFAULT;
}
static int fs_open(const char *path, struct fuse_file_info *fi) noexcept{
struct fuse_context *ctx = fuse_get_context();
OrbisFSImage *img = (OrbisFSImage*)ctx->private_data;
std::shared_ptr<OrbisFSFile> f;
try {
f = img->openFilAtPath(path);
} catch(tihmstar::OrbisFSFileNotFound &e){
return -EEXIST;
} catch (tihmstar::exception &e) {
e.dump();
return -EFAULT;
}
fi->fh = (uint64_t)new std::shared_ptr<OrbisFSFile>(f);
return 0;
}
static int fs_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) noexcept{
std::shared_ptr<OrbisFSFile> *f = (std::shared_ptr<OrbisFSFile> *)fi->fh;
try {
return (int)(*f)->pread(buf, size, offset);
} catch (tihmstar::exception &e) {
return -EFAULT;
}
}
static int fs_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) noexcept{
std::shared_ptr<OrbisFSFile> *f = (std::shared_ptr<OrbisFSFile> *)fi->fh;
try {
return (int)(*f)->pwrite(buf, size, offset);
} catch (tihmstar::exception &e) {
return -EFAULT;
}
}
static int fs_release(const char *path, struct fuse_file_info *fi) noexcept{
std::shared_ptr<OrbisFSFile> *f = (std::shared_ptr<OrbisFSFile> *)fi->fh; fi->fh = 0;
safeDelete(f);
return 0;
}
int fs_opendir(const char *path, struct fuse_file_info *fi) noexcept{
struct fuse_context *ctx = fuse_get_context();
OrbisFSImage *img = (OrbisFSImage*)ctx->private_data;
std::vector<std::pair<std::string, uint64_t>> *files = nullptr;
cleanup([&]{
safeDelete(files);
});
files = new std::vector<std::pair<std::string, uint64_t>>;
try {
auto ff = img->listFilesInFolder(path, true);
for (auto f : ff){
files->push_back({f.first.c_str(), f.second.inodeNum});
}
} catch(tihmstar::OrbisFSFileNotFound &e){
return -EEXIST;
} catch (tihmstar::exception &e) {
#ifdef DEBUG
e.dump();
#endif
return -EFAULT;
}
{
std::vector<std::pair<std::string, uint64_t>> *old = (std::vector<std::pair<std::string, uint64_t>>*)fi->fh; fi->fh = 0;
safeDelete(old);
}
fi->fh = (uint64_t)files; files = nullptr;
return 0;
}
int fs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t off, struct fuse_file_info *fi) noexcept{
std::vector<std::pair<std::string, uint64_t>> *tgt = (std::vector<std::pair<std::string, uint64_t>>*)fi->fh;
while (off < tgt->size()) {
auto e = tgt->at(off);
struct stat stbuf = {};
stbuf.st_ino = e.second;
off++;
if (filler(buf,e.first.c_str(), &stbuf, off)) break;
}
return 0;
}
int fs_releasedir(const char *path, struct fuse_file_info *fi) noexcept{
std::vector<std::pair<std::string, uint64_t>> *old = (std::vector<std::pair<std::string, uint64_t>>*)fi->fh; fi->fh = 0;
safeDelete(old);
return 0;
}
static const struct fuse_operations orbisimgFuse_ops = {
.getattr = fs_getattr,
.readlink = fs_readlink,
.open = fs_open,
.read = fs_read,
.write = fs_write,
.release = fs_release,
.opendir = fs_opendir,
.readdir = fs_readdir,
.releasedir = fs_releasedir,
};
#endif
#pragma mark OrbisFSFuse
OrbisFSFuse::OrbisFSFuse(std::shared_ptr<OrbisFSImage> img, const char *mountPath)
: _img(img)
, _fuse(NULL), _ch(NULL)
{
#ifndef HAVE_FUSE
reterror("Built without FUSE support!");
#else
retassure(mountPath, "Error no mount point specified!");
_mountpoint = mountPath;
int vers = fuse_version();
if (vers < FUSE_USE_VERSION) {
reterror("Fuse version too low, expected %d but got %d",FUSE_USE_VERSION,vers);
}
info("Got FUSE version %d",vers);
struct fuse_args args = {};
cleanup([&]{
fuse_opt_free_args(&args);
});
args = FUSE_ARGS_INIT(0, nullptr);
assure(!fuse_opt_add_arg(&args, "FIRST_ARG_IS_IGNORED"));
if (!_img->isWriteable()){
assure(!fuse_opt_add_arg(&args, "-r"));
}
{
std::string imgNameOpt = "fsname=OrbisFSFuse";
assure(!fuse_opt_add_arg(&args, "-o"));
assure(!fuse_opt_add_arg(&args, imgNameOpt.c_str()));
}
#if !defined(__linux__)
{
std::string imgNameOpt = "volname=OrbisFSFuse";
assure(!fuse_opt_add_arg(&args, "-o"));
assure(!fuse_opt_add_arg(&args, imgNameOpt.c_str()));
}
#endif //!defined(__linux__)
#ifdef __APPLE__
assure(!fuse_opt_add_arg(&args, "-o"));
if (!_img->isWriteable()){
assure(!fuse_opt_add_arg(&args, "rdonly,local"));
}else{
assure(!fuse_opt_add_arg(&args, "local"));
}
#endif
{
debug("Trying to mount at %s",_mountpoint.c_str());
retassure(_ch = fuse_mount(_mountpoint.c_str(), &args), "Failed to mount");
retassure(_fuse = fuse_new(_ch, &args, &orbisimgFuse_ops, sizeof(orbisimgFuse_ops), _img.get()), "Failed to create FUSE session");
}
info("Mounted at %s",_mountpoint.c_str());
#endif
}
OrbisFSFuse::~OrbisFSFuse(){
#ifdef HAVE_FUSE
if (_ch) {
fuse_unmount(_mountpoint.c_str(), _ch); _ch = NULL;
}
safeFreeCustom(_fuse, fuse_destroy);
#endif
}
#pragma mark OrbisFSFuse private
#pragma mark OrbisFSFuse public
void OrbisFSFuse::loopSession(){
#ifndef HAVE_FUSE
reterror("Built without FUSE support!");
#else
struct fuse_session *se = NULL;
cleanup([&]{
safeFreeCustom(se, fuse_remove_signal_handlers);
});
if ((se = fuse_get_session(_fuse))) {
if (fuse_set_signal_handlers(se)){
se = NULL;
error("Failed to set FUSE sighandlers");
}
}
fuse_loop_mt(_fuse);
#endif
}
void OrbisFSFuse::stopSession(){
#ifndef HAVE_FUSE
reterror("Built without FUSE support!");
#else
fuse_exit(_fuse);
#endif
}