-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount.cc
269 lines (216 loc) · 9.86 KB
/
mount.cc
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
#include <filesystem>
#include <fuse.h>
#include <glog/logging.h>
#include "mount.h"
#include "inode.h"
#include "video.h"
#include "nativevideo.h"
namespace vfs {
static Mount &instance() {
return *static_cast<Mount*>(fuse_get_context()->private_data);
}
Mount::Mount(std::string path, std::string mount)
: path_(std::move(path)),
mount_(std::move(mount)),
operations_{std::make_unique<fuse_operations>()},
root_(std::make_unique<RootDirectory>(*this)) {
root_->emplace_child(std::make_unique<Directory>("dogs", *root_, 00777));
auto &cats = root_->emplace_child(std::make_unique<Video>("cats", *this, 00777));
cats.emplace_child(std::make_unique<NativeVideo>("/home/bhaynes/fireworks.hevc", cats, 00777));
//root_->children().emplace_back(std::make_unique<Directory>("dogs", *root_, 00777));
//root_->children().emplace_back(std::make_unique<Video>("cats", *this, 00777));
//auto& cats = dynamic_cast<Video&>(*root_->children().back());
//cats.children().emplace_back(std::make_unique<NativeVideo>("/home/bhaynes/fireworks.hevc", cats, 00777));
//, std::vector<std::filesystem::path>{{"/home/bhaynes/foo.mp4"}}));
operations_->getattr =[](const char *path, struct stat *stat) { return instance().getattr(path, stat); };
operations_->readdir = [](const char *path, void *buf, fuse_fill_dir_t dir, off_t offset, struct fuse_file_info* fi) { return instance().readdir(path, buf, dir, offset, *fi); };
operations_->mkdir = [](const char *path, mode_t mode) { return instance().mkdir(path, mode); };
operations_->mknod = [](const char *path, mode_t mode, dev_t rdev) { return instance().mknod(path, mode, rdev); };
//operations_->mknod = [](const char *path, mode_t mode, struct fuse_file_info* fi) { return instance().mknod(path, mode, *fi); };
operations_->create = [](const char *path, mode_t mode, struct fuse_file_info* fi) { return instance().create(path, mode, *fi); };
operations_->unlink = [](const char *path) { return instance().unlink(path); };
operations_->open = [](const char *path, struct fuse_file_info* fi) { return instance().open(path, *fi); };
operations_->read = [](const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info* fi) { return instance().read(path, buf, size, offset, *fi); };
operations_->write = [](const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info* fi) { return instance().write(path, buf, size, offset, *fi); };
operations_->truncate = [](const char *path, off_t newsize) { return instance().truncate(path, newsize); };
operations_->flush = [](const char *path, struct fuse_file_info* fi) { return instance().flush(path, *fi); };
operations_->readlink = [](const char *path, char *buf, size_t size) { return instance().readlink(path, buf, size); };
operations_->ioctl = [](const char *path, int cmd, void *arg, struct fuse_file_info* fi, unsigned int flags, void *data) { return instance().ioctl(path, cmd, arg, *fi, flags, data); };
}
Mount::~Mount() = default;
template<typename T=Inode>
T &find(std::filesystem::path::iterator ¤t, const std::filesystem::path::iterator &end, Directory &node) {
auto child = node.find_child(*current);
if(current == end && dynamic_cast<T*>(&node) != nullptr)
return dynamic_cast<T&>(node);
else if(!child.has_value())
throw std::runtime_error("ENOENT");
else if(++current != end)
return find<T>(current, end, dynamic_cast<Directory&>(child.value().get()));
else if(dynamic_cast<T*>(&child.value().get()) != nullptr)
return dynamic_cast<T&>(child.value().get());
else
throw std::runtime_error("ENOENT");
}
/*
Inode &find(std::filesystem::path::iterator ¤t, const std::filesystem::path::iterator &end, Directory &node) {
auto child = node.find_child(*current);
if(current == end)
return node;
else if(!child.has_value())
throw std::runtime_error("ENOENT");
else if(++current != end)
return find(current, end, dynamic_cast<Directory&>(child.value().get()));
else
return child.value();
}
*/
template<typename T=Inode>
int find(const std::filesystem::path &path, Directory &root, T** result) {
try {
*result = &find<T>(++path.begin(), path.end(), root);
return 0;
} catch(std::runtime_error&) {
result = nullptr;
return ENOENT;
}
}
Inode &Mount::find(const std::filesystem::path &path) {
return vfs::find(++path.begin(), path.end(), root());
}
int Mount::getattr(const std::filesystem::path &path, struct stat *stat) {
LOG(INFO) << "getattr: " << path;
int result;
Inode* node;
if((result = vfs::find(path, root(), &node)) == 0)
return node->getattr(stat);
else
return -result;
}
int Mount::readdir(const std::filesystem::path &path, void *buf, vfs::fuse_fill_dir_t filler, off_t offset, struct fuse_file_info& fi) {
Directory *directory;
int result;
LOG(INFO) << "readdir " << path;
if((result = vfs::find<Directory>(path, root(), &directory)) != 0)
return -result;
else
//else if(node->type() == Inode::directory && (directory = dynamic_cast<Directory*>(node)) != nullptr)
return directory->readdir(buf, filler, offset, fi);
//else
// return -ENOENT;
}
int Mount::mkdir(const std::filesystem::path &path, mode_t mode) {
int result;
File* file;
LOG(INFO) << "create: " << path;
if((result = vfs::find<File>(path, root(), &file)) == 0)
return EEXIST;
else if(result == ENOENT) {
root_->emplace_child(std::make_unique<Video>(path.filename(), *this, mode));
return 0;
} else
return -result;
}
int Mount::mknod(const std::filesystem::path &path, mode_t mode, dev_t rdev) {
return -EACCES;
}
//int Mount::mknod(const std::filesystem::path &path, mode_t mode, struct fuse_file_info& fi) {
// return EACCES;
//}
int Mount::create(const std::filesystem::path &path, mode_t mode, struct fuse_file_info& info) {
int result;
File* file;
LOG(INFO) << "create: " << path;
if((result = vfs::find<File>(path, root(), &file)) == 0)
return EEXIST;
else if(result == ENOENT) {
//auto &video =
//mode |= S_IFDIR;
//auto &video = root_->emplace_child(std::make_unique<Video>(path.filename(), *this, 00777));
//video.write(info);
return -ENOENT;
} else
return -result;
return -EACCES;
}
int Mount::unlink(const std::filesystem::path &path) {
return -1;
}
int Mount::open(const std::filesystem::path &path, struct fuse_file_info& info) {
int result;
File* file;
LOG(INFO) << "open: " << path;
/*if(info.flags & (O_RDWR | O_WRONLY | O_APPEND))
return EACCES;
else */if((result = vfs::find<File>(path, root(), &file)) == 0)
return file->open(info);
else
return -result;
}
int Mount::read(const std::filesystem::path &path, char *buffer, size_t size, off_t offset, struct fuse_file_info& info) {
int result;
File* node;
LOG(INFO) << "read: " << path;
File *file = reinterpret_cast<File*>(info.fh);
return file->read(path, buffer, size, offset, info);
/*if((result = vfs::find(path, root(), &node)) == 0)
return dynamic_cast<File*>(node)->open(info);
else
return -result;
if(offset < 5) {
memcpy(buf, "A", 2);
return 1;
}
return 0;*/
}
int Mount::write(const std::filesystem::path &path, const char *buffer, size_t size, off_t offset, struct fuse_file_info& info) {
int result;
File* file;
LOG(INFO) << "truncate: " << path;
/*if(newsize != 0)
return -EACCES;
else*/ if((result = vfs::find<File>(path, root(), &file)) == 0)
return file->write(buffer, size, offset, info);
else
return -result;
}
int Mount::flush(const std::filesystem::path &path, struct fuse_file_info &info) {
int result;
File* file;
LOG(INFO) << "flush: " << path;
if((result = vfs::find<File>(path, root(), &file)) == 0)
return file->flush(info);
else
return -result;
}
int Mount::truncate(const std::filesystem::path &path, off_t newsize) {
int result;
File* file;
LOG(INFO) << "truncate: " << path;
/*if(newsize != 0)
return -EACCES;
else*/ if((result = vfs::find<File>(path, root(), &file)) == 0)
return file->truncate(newsize);
else
return -result;
}
int Mount::readlink(const std::filesystem::path &path, char *buffer, size_t size) {
int result;
Inode* node;
LOG(INFO) << "readlink: " << path;
if((result = vfs::find(path, root(), &node)) == 0)
return dynamic_cast<Link*>(node)->readlink(buffer, size);
else
return -result;
}
int Mount::ioctl(const std::filesystem::path &path, int cmd, void *arg, struct fuse_file_info& fi, unsigned int flags, void *data) {
return -ENOTTY;
}
int Mount::run() {
char *argv[] {const_cast<char*>(path_.c_str()),
const_cast<char*>(mount_.c_str()),
"-obig_writes",
"-f"};
return fuse_main(std::size(argv), argv, &*operations_, this);
}
}