-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfilesystem.c
More file actions
248 lines (200 loc) · 5.36 KB
/
filesystem.c
File metadata and controls
248 lines (200 loc) · 5.36 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
#ifndef USE_FUSE
#include <string.h>
/* Dummy version of vfile creators, so callers don't have to conditionalize. */
void fs_add_chunk (char *name, void *data, size_t size, int writable)
{
}
#else
/*** FUSE made easy. ***/
#define FUSE_USE_VERSION 26
#define __USE_XOPEN
#define __need_timespec
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <pthread.h>
#include <fuse.h>
#include "filesystem.h"
static char fs_mountpoint[PATH_MAX];
static struct fuse *fuse;
static struct fuse_chan *ch;
static int fs_mounted = 0;
struct vfile {
const char *name;
enum { chunk } type;
struct vfile *next;
int writable;
void *data;
size_t data_size;
};
struct vfile *vfiles = NULL;
void fs_add_chunk (const char *name, void *data, size_t size, int writable)
{
struct vfile *vf = malloc(sizeof(struct vfile));
assert(vf);
vf->name = name;
vf->type = chunk;
vf->writable = (writable!=0);
vf->data = data;
vf->data_size = size;
vf->next = vfiles;
vfiles = vf;
}
static struct vfile *find_vfile (const char *name)
{
struct vfile *vf = vfiles;
if (name[0] == '/') name++;
while ((vf != NULL) && strcmp(name, vf->name)) vf = vf->next;
return vf;
}
static int fs_getattr (const char *path, struct stat *stbuf)
{
struct vfile *vf = find_vfile(path);
memset(stbuf, 0, sizeof(struct stat));
if(strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
return 0;
} else if (vf) {
stbuf->st_mode = S_IFREG | (vf->writable? 0666 : 0444);
stbuf->st_nlink = 1;
stbuf->st_size = vf->data_size;
return 0;
} else return -ENOENT;
}
static int fs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
struct vfile *vf = vfiles;
if(strcmp(path, "/") != 0) return -ENOENT;
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
while (vf) {
filler(buf, vf->name, NULL, 0);
vf = vf->next;
}
return 0;
}
static int fs_open(const char *path, struct fuse_file_info *fi)
{
struct vfile *vf = find_vfile(path);
if (!vf) return -ENOENT;
if ((!vf->writable) && ((fi->flags & 3) != O_RDONLY)) return -EACCES;
return 0;
}
static int fs_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
struct vfile *vf = find_vfile(path);
void *data;
size_t len;
if (!vf) return -ENOENT;
switch (vf->type) {
case chunk:
data = vf->data;
len = vf->data_size;
break;
default: assert(0);
}
if (offset < len) {
if (offset + size > len) size = len - offset;
memcpy(buf, data + offset, size);
} else size = 0;
return size;
}
static int fs_truncate(const char *path, off_t offset)
{
struct vfile *vf = find_vfile(path);
if (!vf) return -ENOENT;
switch (vf->type) {
case chunk:
if (offset != vf->data_size) return EPERM;
break;
default: assert(0);
}
return 0;
}
static int fs_write (const char *path, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi)
{
struct vfile *vf = find_vfile(path);
if (!vf) return -ENOENT;
if (!vf->writable) return -EPERM;
/* printf("fs_write: %s offset %X size %X\n", path, (int)offset, (int)size); */
switch (vf->type) {
case chunk:
{
if (offset >= vf->data_size) return -EFBIG;
if (size > vf->data_size) return -EFBIG;
if (offset + size > vf->data_size) return -EFBIG;
memcpy(vf->data + offset, buf, size);
} break;
default: assert(0);
}
return size;
}
static struct fuse_operations fs_ops = {
.getattr = fs_getattr,
.readdir = fs_readdir,
.open = fs_open,
.read = fs_read,
.write = fs_write,
.truncate = fs_truncate
};
static void *run_fs_thread (void *arg)
{
fuse_loop(fuse);
return NULL;
}
/* Mount the NES filesystem */
int fs_mount (char *mountpoint)
{
pthread_t thread;
if (fs_mounted) return 0;
mkdir(mountpoint, 0700);
if (strlen(mountpoint) + 1 >= sizeof(fs_mountpoint)) return 0;
strcpy(fs_mountpoint, mountpoint);
ch = fuse_mount(mountpoint, NULL);
if (!ch) {
printf("fuse_mount failed!\n");
return 0;
}
fuse = fuse_new(ch, NULL, &fs_ops, sizeof(fs_ops), NULL);
if (!fuse) {
printf("fuse_new failed!\n");
fuse_unmount(mountpoint, NULL);
return 0;
}
fuse_set_signal_handlers(fuse_get_session(fuse));
fs_mounted = 1;
atexit(fs_unmount);
if (pthread_create(&thread, NULL, run_fs_thread, NULL)) {
printf("Error creating FUSE loop thread.\n");
fs_unmount();
return 0;
}
printf("Mounted NES virtual filesystem at %s\n", fs_mountpoint);
return 1;
}
/* Unmount the NES filesystem */
void fs_unmount (void)
{
if (!fs_mounted) return;
fuse_remove_signal_handlers(fuse_get_session(fuse));
fuse_unmount(fs_mountpoint, ch);
fuse_destroy(fuse);
}
/* Returns nonzero if the NES filesystem is mounted, otherwise returns zero. */
int fs_is_mounted (void)
{
return fs_mounted;
}
#endif