-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexamplefs.cc
250 lines (208 loc) · 7.2 KB
/
examplefs.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
#include "examplefs.hh"
ExampleFS* ExampleFS::_instance = NULL;
#define RETURN_ERRNO(x) (x) == 0 ? 0 : -errno
ExampleFS* ExampleFS::Instance() {
if(_instance == NULL) {
_instance = new ExampleFS();
}
return _instance;
}
ExampleFS::ExampleFS() {
}
ExampleFS::~ExampleFS() {
}
void ExampleFS::AbsPath(char dest[PATH_MAX], const char *path) {
strcpy(dest, _root);
strncat(dest, path, PATH_MAX);
//printf("translated path: %s to %s\n", path, dest);
}
void ExampleFS::setRootDir(const char *path) {
printf("setting FS root to: %s\n", path);
_root = path;
}
int ExampleFS::Getattr(const char *path, struct stat *statbuf) {
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
printf("getattr(%s)\n", fullPath);
return RETURN_ERRNO(lstat(fullPath, statbuf));
}
int ExampleFS::Readlink(const char *path, char *link, size_t size) {
printf("readlink(path=%s, link=%s, size=%d)\n", path, link, (int)size);
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
return RETURN_ERRNO(readlink(fullPath, link, size));
}
int ExampleFS::Mknod(const char *path, mode_t mode, dev_t dev) {
printf("mknod(path=%s, mode=%d)\n", path, mode);
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
//handles creating FIFOs, regular files, etc...
return RETURN_ERRNO(mknod(fullPath, mode, dev));
}
int ExampleFS::Mkdir(const char *path, mode_t mode) {
printf("**mkdir(path=%s, mode=%d)\n", path, (int)mode);
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
return RETURN_ERRNO(mkdir(fullPath, mode));
}
int ExampleFS::Unlink(const char *path) {
printf("unlink(path=%s\n)", path);
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
return RETURN_ERRNO(unlink(fullPath));
}
int ExampleFS::Rmdir(const char *path) {
printf("rmkdir(path=%s\n)", path);
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
return RETURN_ERRNO(rmdir(fullPath));
}
int ExampleFS::Symlink(const char *path, const char *link) {
printf("symlink(path=%s, link=%s)\n", path, link);
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
return RETURN_ERRNO(symlink(fullPath, link));
}
int ExampleFS::Rename(const char *path, const char *newpath) {
printf("rename(path=%s, newPath=%s)\n", path, newpath);
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
return RETURN_ERRNO(rename(fullPath, newpath));
}
int ExampleFS::Link(const char *path, const char *newpath) {
printf("link(path=%s, newPath=%s)\n", path, newpath);
char fullPath[PATH_MAX];
char fullNewPath[PATH_MAX];
AbsPath(fullPath, path);
AbsPath(fullNewPath, newpath);
return RETURN_ERRNO(link(fullPath, fullNewPath));
}
int ExampleFS::Chmod(const char *path, mode_t mode) {
printf("chmod(path=%s, mode=%d)\n", path, mode);
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
return RETURN_ERRNO(chmod(fullPath, mode));
}
int ExampleFS::Chown(const char *path, uid_t uid, gid_t gid) {
printf("chown(path=%s, uid=%d, gid=%d)\n", path, (int)uid, (int)gid);
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
return RETURN_ERRNO(chown(fullPath, uid, gid));
}
int ExampleFS::Truncate(const char *path, off_t newSize) {
printf("truncate(path=%s, newSize=%d\n", path, (int)newSize);
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
return RETURN_ERRNO(truncate(fullPath, newSize));
}
int ExampleFS::Utime(const char *path, struct utimbuf *ubuf) {
printf("utime(path=%s)\n", path);
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
return RETURN_ERRNO(utime(fullPath, ubuf));
}
int ExampleFS::Open(const char *path, struct fuse_file_info *fileInfo) {
printf("open(path=%s)\n", path);
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
fileInfo->fh = open(fullPath, fileInfo->flags);
return 0;
}
int ExampleFS::Read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fileInfo) {
printf("read(path=%s, size=%d, offset=%d)\n", path, (int)size, (int)offset);
return RETURN_ERRNO(pread(fileInfo->fh, buf, size, offset));
}
int ExampleFS::Write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fileInfo) {
printf("write(path=%s, size=%d, offset=%d)\n", path, (int)size, (int)offset);
return RETURN_ERRNO(pwrite(fileInfo->fh, buf, size, offset));
}
int ExampleFS::Statfs(const char *path, struct statvfs *statInfo) {
printf("statfs(path=%s)\n", path);
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
return RETURN_ERRNO(statvfs(fullPath, statInfo));
}
int ExampleFS::Flush(const char *path, struct fuse_file_info *fileInfo) {
printf("flush(path=%s)\n", path);
//noop because we don't maintain our own buffers
return 0;
}
int ExampleFS::Release(const char *path, struct fuse_file_info *fileInfo) {
printf("release(path=%s)\n", path);
return 0;
}
int ExampleFS::Fsync(const char *path, int datasync, struct fuse_file_info *fi) {
printf("fsync(path=%s, datasync=%d\n", path, datasync);
if(datasync) {
//sync data only
return RETURN_ERRNO(fdatasync(fi->fh));
} else {
//sync data + file metadata
return RETURN_ERRNO(fsync(fi->fh));
}
}
int ExampleFS::Setxattr(const char *path, const char *name, const char *value, size_t size, int flags) {
printf("setxattr(path=%s, name=%s, value=%s, size=%d, flags=%d\n",
path, name, value, (int)size, flags);
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
return RETURN_ERRNO(lsetxattr(fullPath, name, value, size, flags));
}
int ExampleFS::Getxattr(const char *path, const char *name, char *value, size_t size) {
printf("getxattr(path=%s, name=%s, size=%d\n", path, name, (int)size);
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
return RETURN_ERRNO(getxattr(fullPath, name, value, size));
}
int ExampleFS::Listxattr(const char *path, char *list, size_t size) {
printf("listxattr(path=%s, size=%d)\n", path, (int)size);
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
return RETURN_ERRNO(llistxattr(fullPath, list, size));
}
int ExampleFS::Removexattr(const char *path, const char *name) {
printf("removexattry(path=%s, name=%s)\n", path, name);
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
return RETURN_ERRNO(lremovexattr(fullPath, name));
}
int ExampleFS::Opendir(const char *path, struct fuse_file_info *fileInfo) {
printf("opendir(path=%s)\n", path);
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
DIR *dir = opendir(fullPath);
fileInfo->fh = (uint64_t)dir;
return NULL == dir ? -errno : 0;
}
int ExampleFS::Readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fileInfo) {
printf("readdir(path=%s, offset=%d)\n", path, (int)offset);
DIR *dir = (DIR*)fileInfo->fh;
struct dirent *de = readdir(dir);
if(NULL == de) {
return -errno;
} else {
do {
if(filler(buf, de->d_name, NULL, 0) != 0) {
return -ENOMEM;
}
} while(NULL != (de = readdir(dir)));
}
return 0;
}
int ExampleFS::Releasedir(const char *path, struct fuse_file_info *fileInfo) {
printf("releasedir(path=%s)\n", path);
closedir((DIR*)fileInfo->fh);
return 0;
}
int ExampleFS::Fsyncdir(const char *path, int datasync, struct fuse_file_info *fileInfo) {
return 0;
}
int ExampleFS::Init(struct fuse_conn_info *conn) {
return 0;
}
int ExampleFS::Truncate(const char *path, off_t offset, struct fuse_file_info *fileInfo) {
printf("truncate(path=%s, offset=%d)\n", path, (int)offset);
char fullPath[PATH_MAX];
AbsPath(fullPath, path);
return RETURN_ERRNO(ftruncate(fileInfo->fh, offset));
}