-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuse_wrap_old.c
187 lines (145 loc) · 3.59 KB
/
fuse_wrap_old.c
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
#define FUSE_USE_VERSION 30
#include <fuse.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <sys/time.h>
#include "disk_emu.h"
#include "sfs_api.h"
static int fuse_getattr(const char *path, struct stat *stbuf)
{
int res = 0;
int size;
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
} else if((size = sfs_getfilesize(path)) != -1) {
stbuf->st_mode = S_IFREG | 0666;
stbuf->st_nlink = 1;
stbuf->st_size = size;
} else
res = -ENOENT;
return res;
}
static int fuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
char file_name[MAXFILENAME];
if (strcmp(path, "/") != 0)
return -ENOENT;
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
while(sfs_getnextfilename(file_name)) {
filler(buf, &file_name[1], NULL, 0);
}
return 0;
}
static int fuse_unlink(const char *path)
{
int res;
char filename[MAXFILENAME];
strcpy(filename, path);
res = sfs_remove(filename);
if (res == -1)
return -errno;
return 0;
}
static int fuse_open(const char *path, struct fuse_file_info *fi)
{
int res;
char filename[MAXFILENAME];
strcpy(filename, path);
res = sfs_fopen(filename);
if (res == -1)
return -errno;
sfs_fclose(res);
return 0;
}
static int fuse_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
int fd;
int res;
char filename[MAXFILENAME];
strcpy(filename, path);
fd = sfs_fopen(filename);
if (fd == -1)
return -errno;
if(sfs_fseek(fd, offset) == -1)
return -errno;
res = sfs_fread(fd, buf, size);
if (res == -1)
return -errno;
sfs_fclose(fd);
return res;
}
static int fuse_write(const char *path, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi)
{
int fd;
int res;
char filename[MAXFILENAME];
strcpy(filename, path);
fd = sfs_fopen(filename);
if (fd == -1)
return -errno;
if(sfs_fseek(fd, offset) == -1)
return -errno;
res = sfs_fwrite(fd, buf, size);
if (res == -1)
return -errno;
sfs_fclose(fd);
return res;
}
static int fuse_truncate(const char *path, off_t size)
{
char filename[MAXFILENAME];
int fd;
strcpy(filename, path);
fd = sfs_remove(filename);
if (fd == -1)
return -errno;
fd = sfs_fopen(filename);
sfs_fclose(fd);
return 0;
}
static int fuse_access(const char *path, int mask)
{
return 0;
}
static int fuse_mknod(const char *path, mode_t mode, dev_t rdev)
{
return 0;
}
static int fuse_create (const char *path, mode_t mode, struct fuse_file_info *fp)
{
char filename[MAXFILENAME];
int fd;
strcpy(filename, path);
fd = sfs_fopen(filename);
sfs_fclose(fd);
return 0;
}
static struct fuse_operations xmp_oper = {
.getattr = fuse_getattr,
.readdir = fuse_readdir,
.mknod = fuse_mknod,
.unlink = fuse_unlink,
.truncate = fuse_truncate,
.open = fuse_open,
.read = fuse_read,
.write = fuse_write,
.access = fuse_access,
.create = fuse_create,
};
int main(int argc, char *argv[])
{
mksfs(0);
return fuse_main(argc, argv, &xmp_oper, NULL);
}