-
Notifications
You must be signed in to change notification settings - Fork 2
/
ptfs.cc
261 lines (219 loc) · 7.88 KB
/
ptfs.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
#define FUSE_USE_VERSION 35
#include "ptfs.h"
#include <fcntl.h>
#include <string.h>
#include <string>
#include "relative_path.h"
#include "scoped_fd.h"
using std::string;
using std::unique_ptr;
namespace ptfs {
// For path based functions.
#define DECLARE_RELATIVE(path, relative_path) \
if (*path == 0) return -ENOENT; \
string relative_path(GetRelativePath(path));
// For FileHandle based functions.
#define USE_FILEHANDLE(fh, fi) \
FileHandle *fh = GetFileHandle(fi); \
if (fh == nullptr || fh->fd_get() == -1) return -EBADF;
PtfsHandler *GetContext() {
fuse_context *context = fuse_get_context();
return reinterpret_cast<PtfsHandler *>(context->private_data);
}
static FileHandle *GetFileHandle(struct fuse_file_info *fi) {
return reinterpret_cast<FileHandle *>(fi->fh);
}
static int fs_chmod(const char *path, mode_t mode, fuse_file_info *fi) {
DECLARE_RELATIVE(path, relative_path);
return GetContext()->Chmod(relative_path, mode);
}
static int fs_chown(const char *path, uid_t uid, gid_t gid,
fuse_file_info *fi) {
DECLARE_RELATIVE(path, relative_path);
return GetContext()->Chown(relative_path, uid, gid);
}
static int fs_truncate(const char *path, off_t size, fuse_file_info *fi) {
DECLARE_RELATIVE(path, relative_path);
return GetContext()->Truncate(relative_path, size);
}
static int fs_utimens(const char *path, const struct timespec ts[2],
fuse_file_info *) {
DECLARE_RELATIVE(path, relative_path);
return GetContext()->Utimens(relative_path, ts);
}
static int fs_mknod(const char *path, mode_t mode, dev_t rdev) {
DECLARE_RELATIVE(path, relative_path);
return GetContext()->Mknod(relative_path, mode, rdev);
}
static int fs_link(const char *from, const char *to) {
DECLARE_RELATIVE(from, from_s);
DECLARE_RELATIVE(to, to_s);
return GetContext()->Link(from_s, to_s);
}
static int fs_statfs(const char *path, struct statvfs *stbuf) {
return GetContext()->Statfs(stbuf);
}
static int fs_symlink(const char *from, const char *to) {
DECLARE_RELATIVE(to, to_s);
if (*from == 0) return -ENOENT;
return GetContext()->Symlink(from, to_s);
}
static int fs_getattr(const char *path, struct stat *stbuf,
fuse_file_info *fi) {
memset(stbuf, 0, sizeof(struct stat));
if (fi) {
USE_FILEHANDLE(fh, fi);
return GetContext()->GetAttr(*fh, stbuf);
} else {
DECLARE_RELATIVE(path, relative_path);
return GetContext()->GetAttr(relative_path, stbuf);
}
}
static int fs_opendir(const char *path, struct fuse_file_info *fi) {
if (*path == 0) return -ENOENT;
unique_ptr<string> relative_path(new string(GetRelativePath(path)));
fi->fh = reinterpret_cast<uint64_t>(relative_path.release());
return 0;
}
static int fs_releasedir(const char *, struct fuse_file_info *fi) {
if (fi->fh == 0) return -EBADF;
unique_ptr<string> auto_delete(reinterpret_cast<string *>(fi->fh));
return 0;
}
static int fs_readdir(const char *unused, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi,
fuse_readdir_flags) {
if (fi->fh == 0) return -ENOENT;
string *relative_path(reinterpret_cast<string *>(fi->fh));
return GetContext()->ReadDir(relative_path->c_str(), buf, filler, offset);
}
static int fs_open(const char *path, struct fuse_file_info *fi) {
DECLARE_RELATIVE(path, relative_path);
unique_ptr<FileHandle> fh(nullptr);
int ret = GetContext()->Open(relative_path, fi->flags, &fh);
if (fh.get() == nullptr) return -EBADF;
if (ret == 0) {
fi->fh = reinterpret_cast<uint64_t>(fh.release());
}
return ret;
}
static int fs_create(const char *path, mode_t mode, struct fuse_file_info *fi) {
DECLARE_RELATIVE(path, relative_path);
unique_ptr<FileHandle> fh(nullptr);
int ret = GetContext()->Create(relative_path, fi->flags, mode, &fh);
if (fh.get() == nullptr) return -EBADF;
if (ret == 0) {
fi->fh = reinterpret_cast<uint64_t>(fh.release());
}
return ret;
}
static int fs_release(const char *unused, struct fuse_file_info *fi) {
unique_ptr<FileHandle> fh(GetFileHandle(fi));
return GetContext()->Release(fi->flags, fh.get());
}
static int fs_read(const char *unused, char *target, size_t size, off_t offset,
struct fuse_file_info *fi) {
USE_FILEHANDLE(fh, fi);
return GetContext()->Read(*fh, target, size, offset);
}
static int fs_read_buf(const char *unused, struct fuse_bufvec **bufp,
size_t size, off_t offset, struct fuse_file_info *fi) {
USE_FILEHANDLE(fh, fi);
auto buf = *bufp = new struct fuse_bufvec;
*buf = FUSE_BUFVEC_INIT(size);
return GetContext()->ReadBuf(*fh, *buf, size, offset);
}
static int fs_write(const char *unused, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi) {
USE_FILEHANDLE(fh, fi);
return GetContext()->Write(*fh, buf, size, offset);
}
static int fs_readlink(const char *path, char *buf, size_t size) {
DECLARE_RELATIVE(path, relative_path);
return GetContext()->Readlink(relative_path, buf, size);
}
static int fs_mkdir(const char *path, mode_t mode) {
DECLARE_RELATIVE(path, relative_path);
return GetContext()->Mkdir(relative_path, mode);
}
static int fs_rmdir(const char *path) {
DECLARE_RELATIVE(path, relative_path);
return GetContext()->Rmdir(relative_path);
}
static int fs_unlink(const char *path) {
DECLARE_RELATIVE(path, relative_path);
return GetContext()->Unlink(relative_path);
}
static int fs_fsync(const char *unused, int isdatasync,
struct fuse_file_info *fi) {
USE_FILEHANDLE(fh, fi);
return GetContext()->Fsync(fh, isdatasync);
}
static int fs_fallocate(const char *unused, int mode, off_t offset,
off_t length, struct fuse_file_info *fi) {
USE_FILEHANDLE(fh, fi);
return GetContext()->Fallocate(fh, mode, offset, length);
}
static int fs_setxattr(const char *path, const char *name, const char *value,
size_t size, int flags) {
DECLARE_RELATIVE(path, relative_path);
return GetContext()->Setxattr(relative_path, name, value, size, flags);
}
static int fs_getxattr(const char *path, const char *name, char *value,
size_t size) {
DECLARE_RELATIVE(path, relative_path);
return GetContext()->Getxattr(relative_path, name, value, size);
}
static int fs_listxattr(const char *path, char *list, size_t size) {
DECLARE_RELATIVE(path, relative_path);
return GetContext()->Listxattr(relative_path, list, size);
}
static int fs_removexattr(const char *path, const char *name) {
DECLARE_RELATIVE(path, relative_path);
return GetContext()->Removexattr(relative_path, name);
}
static int fs_rename(const char *from, const char *to,
unsigned int rename_flags) {
DECLARE_RELATIVE(from, from_s);
DECLARE_RELATIVE(to, to_s);
return GetContext()->Rename(from_s, to_s, rename_flags);
}
static void fs_destroy(void *private_data) {
delete reinterpret_cast<PtfsHandler *>(private_data);
}
void FillFuseOperationsInternal(fuse_operations *o) {
#define DEFINE_HANDLER(n) o->n = &fs_##n
DEFINE_HANDLER(chmod);
DEFINE_HANDLER(chown);
DEFINE_HANDLER(create);
DEFINE_HANDLER(destroy);
DEFINE_HANDLER(fallocate);
DEFINE_HANDLER(fsync);
DEFINE_HANDLER(getattr);
DEFINE_HANDLER(getxattr);
// DEFINE_HANDLER(init); // initialized in ptfs.h
DEFINE_HANDLER(link);
DEFINE_HANDLER(listxattr);
DEFINE_HANDLER(mkdir);
DEFINE_HANDLER(mknod);
DEFINE_HANDLER(open);
DEFINE_HANDLER(opendir);
DEFINE_HANDLER(read);
DEFINE_HANDLER(read_buf);
DEFINE_HANDLER(readdir);
DEFINE_HANDLER(readlink);
DEFINE_HANDLER(release);
DEFINE_HANDLER(releasedir);
DEFINE_HANDLER(removexattr);
DEFINE_HANDLER(rename);
DEFINE_HANDLER(rmdir);
DEFINE_HANDLER(setxattr);
DEFINE_HANDLER(statfs);
DEFINE_HANDLER(symlink);
DEFINE_HANDLER(truncate);
DEFINE_HANDLER(unlink);
DEFINE_HANDLER(utimens);
DEFINE_HANDLER(write);
#undef DEFINE_HANDLER
}
} // namespace ptfs