-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcowmount.c
More file actions
266 lines (239 loc) · 6.56 KB
/
cowmount.c
File metadata and controls
266 lines (239 loc) · 6.56 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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <dirent.h>
#include <bsd/string.h>
#include <assert.h>
#define FUSE_USE_VERSION 26
#include <fuse.h>
#include "storage.h"
#include "slist.h"
#include "util.h"
// implementation for: man 2 access
// Checks if a file exists.
int
nufs_access(const char *path, int mask)
{
int rv = 0;
printf("access(%s, %04o) -> %d\n", path, mask, rv);
return rv;
}
// implementation for: man 2 stat
// gets an object's attributes (type, permissions, size, etc)
int
nufs_getattr(const char *path, struct stat *st)
{
int rv = storage_stat(path, st);
printf("getattr(%s) -> (%d) {mode: %04o, size: %ld}\n", path, rv, st->st_mode, st->st_size);
return rv;
}
// implementation for: man 2 readdir
// lists the contents of a directory
int
nufs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
struct stat st;
char item_path[128];
int rv;
rv = storage_stat(path, &st);
assert(rv == 0);
filler(buf, ".", &st, 0);
filler(buf, "..", &st, 0);
slist* items = storage_list(path);
for (slist* xs = items; xs != 0; xs = xs->next) {
printf("+ looking at path: '%s'\n", xs->data);
if (streq(path, "/")) {
item_path[0] = '/';
strlcpy(item_path + 1, xs->data, 127);
} else {
strlcpy(item_path, path, 128);
item_path[strlen(path)] = '/';
strlcpy(item_path + strlen(path) + 1, xs->data, (127 - strlen(path)));
}
rv = storage_stat(item_path, &st);
assert(rv == 0);
filler(buf, xs->data, &st, 0);
}
s_free(items);
printf("readdir(%s) -> %d\n", path, rv);
return 0;
}
// mknod makes a filesystem object like a file or directory
// called for: man 2 open, man 2 link
int
nufs_mknod(const char *path, mode_t mode, dev_t rdev)
{
//printf("MKNOD %s\n", path);
int rv = storage_mknod(path, mode);
printf("mknod(%s, %04o) -> %d\n", path, mode, rv);
return rv;
}
// most of the following callbacks implement
// another system call; see section 2 of the manual
int
nufs_mkdir(const char *path, mode_t mode)
{
//printf("MKDIR %s\n", path);
mode = 040000 + mode;
int rv = storage_mknod(path, mode);
printf("mkdir(%s %04o) %d -> %d\n", path, mode, mode, rv);
return rv;
}
int
nufs_unlink(const char *path)
{
int rv = storage_unlink(path);
printf("unlink(%s) -> %d\n", path, rv);
return rv;
}
int
nufs_link(const char *from, const char *to)
{
int rv = storage_link(from, to);
printf("link(%s => %s) -> %d\n", from, to, rv);
return rv;
}
int
nufs_rmdir(const char *path)
{
struct stat st;
int rv = storage_stat(path, &st);
if (rv < 0) {
return rv;
}
if (st.st_mode < 040000 || st.st_mode >= 050000) {
return -ENOTDIR;
}
if (st.st_size > 0 || streq(path, "..")) {
return -ENOTEMPTY;
}
rv = storage_unlink(path);
printf("rmdir(%s) -> %d\n", path, rv);
return rv;
}
// implements: man 2 rename
// called to move a file within the same filesystem
int
nufs_rename(const char *from, const char *to)
{
int rv = storage_rename(from, to);
printf("rename(%s => %s) -> %d\n", from, to, rv);
return rv;
}
int
nufs_chmod(const char *path, mode_t mode)
{
int rv = storage_chmod(path, mode);
printf("chmod(%s, %04o) -> %d\n", path, mode, rv);
return rv;
}
int
nufs_truncate(const char *path, off_t size)
{
int rv = storage_truncate(path, size);
printf("truncate(%s, %ld bytes) -> %d\n", path, size, rv);
return rv;
}
// this is called on open, but doesn't need to do much
// since FUSE doesn't assume you maintain state for
// open files.
int
nufs_open(const char *path, struct fuse_file_info *fi)
{
int rv = nufs_access(path, 0);
printf("open(%s) -> %d\n", path, rv);
return rv;
}
// Actually read data
int
nufs_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{
int rv = storage_read(path, buf, size, offset);
printf("read(%s, %ld bytes, @+%ld) -> %d\n", path, size, offset, rv);
return rv;
}
// Actually write data
int
nufs_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{
int rv = storage_write(path, buf, size, offset);
printf("write(%s, %ld bytes, @+%ld) -> %d\n", path, size, offset, rv);
return rv;
}
// Update the timestamps on a file or directory.
int
nufs_utimens(const char* path, const struct timespec ts[2])
{
int rv = storage_set_time(path, ts);
printf("utimens(%s, [%ld, %ld; %ld %ld]) -> %d\n",
path, ts[0].tv_sec, ts[0].tv_nsec, ts[1].tv_sec, ts[1].tv_nsec, rv);
return rv;
}
// Extended operations
int
nufs_ioctl(const char* path, int cmd, void* arg, struct fuse_file_info* fi,
unsigned int flags, void* data)
{
int rv = -1;
printf("ioctl(%s, %d, %d, ...) -> %d\n", path, cmd, flags, rv);
return rv;
}
int nufs_symlink(const char* to, const char* from) {
int rv = storage_mknod(from, 0120777);
if (rv < 0) {
printf("symlink(%s, %s) -> %d\n", to, from, rv);
return rv;
}
rv = storage_write(from, to, strlen(to) + 1, 0);
if (rv < 0) {
printf("symlink(%s, %s) -> %d\n", to, from, rv);
return rv;
}
printf("symlink(%s, %s) -> 0\n", to, from);
return 0;
}
int nufs_readlink(const char* path, char* buf, size_t size) {
int rv = storage_read(path, buf, size, 0);
if (rv < 0) {
printf("readlink(%s, %s, %d) -> %d\n", path, buf, size, rv);
return rv;
}
printf("readlink(%s, %s, %d) -> 0\n", path, buf, size);
return 0;
}
void
nufs_init_ops(struct fuse_operations* ops)
{
memset(ops, 0, sizeof(struct fuse_operations));
ops->access = nufs_access;
ops->getattr = nufs_getattr;
ops->readdir = nufs_readdir;
ops->mknod = nufs_mknod;
ops->mkdir = nufs_mkdir;
ops->link = nufs_link;
ops->unlink = nufs_unlink;
ops->rmdir = nufs_rmdir;
ops->rename = nufs_rename;
ops->chmod = nufs_chmod;
ops->truncate = nufs_truncate;
ops->open = nufs_open;
ops->read = nufs_read;
ops->write = nufs_write;
ops->utimens = nufs_utimens;
ops->ioctl = nufs_ioctl;
ops->symlink = nufs_symlink;
ops->readlink = nufs_readlink;
};
struct fuse_operations nufs_ops;
int
main(int argc, char *argv[])
{
assert(argc > 2 && argc < 6);
storage_init(argv[--argc], 0);
nufs_init_ops(&nufs_ops);
return fuse_main(argc, argv, &nufs_ops, NULL);
}