This repository has been archived by the owner on Nov 18, 2019. It is now read-only.
/
fusehammer.c
171 lines (134 loc) · 3.22 KB
/
fusehammer.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
#define FUSE_USE_VERSION 26
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h> /* struct stat */
#include <dirent.h> /* struct dirent */
#include "hammerread.h"
static struct hfs hfs;
static int hammer_getattr(const char *path, struct stat *stbuf)
{
memset(stbuf, 0, sizeof(struct stat));
ino_t ino = hlookup(&hfs, path);
if(ino == (ino_t)-1) {
return -ENOENT;
}
if(hstat(&hfs, ino, stbuf)) {
return -ENOENT;
}
return 0;
}
static int hammer_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
(void) offset;
(void) fi;
ino_t ino = hlookup(&hfs, path);
if(ino == (ino_t)-1) {
return -ENOENT;
}
struct stat st;
if(hstat(&hfs, ino, &st)) {
return -ENOENT;
}
if(!S_ISDIR(st.st_mode)) {
return -ENOENT;
}
if(offset == 0) {
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
}
struct dirent de;
while(hreaddir(&hfs, ino, &offset, &de) == 0) {
if(filler(buf, de.d_name, NULL, 0)) {
return 0;
}
}
return 0;
}
static int hammer_open(const char *path, struct fuse_file_info *fi)
{
ino_t ino = hlookup(&hfs, path);
if(ino == (ino_t)-1) {
return -ENOENT;
}
struct stat st;
if(hstat(&hfs, ino, &st)) {
return -ENOENT;
}
if((fi->flags & 3) != O_RDONLY)
return -EACCES;
return 0;
}
static int hammer_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
(void) fi;
ino_t ino = hlookup(&hfs, path);
if(ino == (ino_t)-1) {
return -ENOENT;
}
struct stat st;
if(hstat(&hfs, ino, &st)) {
return -ENOENT;
}
if(S_ISDIR(st.st_mode)) {
return -EISDIR;
}
if (offset < st.st_size) {
if (offset + size > st.st_size)
size = st.st_size - offset;
hreadf(&hfs, ino, offset, size, buf);
} else
size = 0;
return size;
}
static int hammer_readlink(const char *path, char *buf, size_t size)
{
int res;
ino_t ino = hlookup(&hfs, path);
if(ino == (ino_t)-1) {
return -ENOENT;
}
res = hreadlink(&hfs, ino, buf, size);
if(res < 0)
return -EINVAL;
return res;
}
static void usage(char* argv[])
{
printf("HAMMER filesystem for FUSE (readonly)\n");
printf("Usage: %s <dev> <mount_point> [<FUSE library options>]\n", argv[0]);
}
static struct fuse_operations hammer_oper = {
.getattr = hammer_getattr,
.readdir = hammer_readdir,
.open = hammer_open,
.read = hammer_read,
.readlink = hammer_readlink,
};
int main(int argc, char *argv[])
{
int ret;
if(argc < 3) {
usage(argv);
exit(EINVAL);
}
hfs.fd = open(argv[1], O_RDONLY);
if (hfs.fd == -1) {
fprintf(stderr, "unable to open %s", argv[1]);
exit(EINVAL);
}
if (hinit(&hfs) == -1) {
fprintf(stderr, "invalid hammerfs");
exit(EINVAL);
}
argc--;
argv++;
ret = fuse_main(argc, argv, &hammer_oper, NULL);
hclose(&hfs);
return ret;
}