-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuse.c
134 lines (109 loc) · 2.58 KB
/
fuse.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
#define FUSE_USE_VERSION 26
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <fuse.h>
#include <ulockmgr.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <sys/time.h>
#include "vfs.h"
#include "message.h"
extern vfs_operations fat32fs_oper;
extern vfs_config fat32fs_config;
static void *fuse_init(void)
{
vfs_init(&fat32fs_oper, &fat32fs_config);
}
static int fuse_getattr(const char *path, stat *stbuf)
{
int res;
res = vfs_getattr(path, stbuf);
if (res == -1)
return -errno;
return 0;
}
static int fuse_opendir(const char *path, struct fuse_file_info *fi)
{
int fd;
if ((fd = vfs_opendir(path)) < 0)
{
MESSAGE_DEBUG("return:%d\n", fd);
return fd;
}
fi->fh = fd;
return 0;
}
static int fuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
dirent *de;
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
vfs_seekdir(fi->fh, offset);
while ((de = vfs_readdir(fi->fh)) != NULL) {
stat st;
memset(&st, 0, sizeof(st));
st.st_ino = de->d_ino;
st.st_mode = de->d_type << 12;
if (filler(buf, de->d_name, &st, 0))
break;
}
return 0;
}
static int fuse_releasedir(const char *path, struct fuse_file_info *fi)
{
vfs_closedir(fi->fh);
return 0;
}
static int fuse_open(const char *path, struct fuse_file_info *fi)
{
int fd;
if((fd = vfs_open(path, fi->flags, 0)) < 0)
return fd;
fi->fh = fd;
return 0;
}
static int fuse_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
int res;
if((res = vfs_lseek(fi->fh, offset, 0)) < 0)
return res;
if((res = vfs_read(fi->fh, buf, size)) < 0)
return res;
return res;
}
static int fuse_release(const char *path, struct fuse_file_info *fi)
{
if(!fi->fh)
return -1;
return vfs_close(fi->fh);
}
static struct fuse_operations fuse_oper = {
.init = fuse_init,
.getattr = fuse_getattr,
.opendir = fuse_opendir,
.readdir = fuse_readdir,
.releasedir = fuse_releasedir,
.open = fuse_open,
.read = fuse_read,
.release = fuse_release
};
int main(int argc, char *argv[])
{
/* struct fuse_file_info fi;
fuse_init();
fuse_open("/HOGE", &fi);
printf("%d\n", fi.fh);
char buf[256];
fuse_read("/HOGE", buf, 255, 0, &fi);
printf("%s\n", buf);
fuse_release("/HOGE", &fi);*/
return fuse_main(argc, argv, &fuse_oper, NULL);
}