forked from autch/piemu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfusepfi.c
311 lines (258 loc) · 6.05 KB
/
fusepfi.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/** @file fusepfi.c
* mounting PFI files using FUSE: Filesystem in Userspace
*
* @author: autch
*/
/*
FUSE: Filesystem in Userspace
Copyright (C) 2001-2007 Miklos Szeredi <[email protected]>
This program can be distributed under the terms of the GNU GPL.
See the file COPYING.
gcc -Wall `pkg-config fuse --cflags --libs` fusexmp.c -o fusexmp
*/
#define FUSE_USE_VERSION 26
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef linux
/* For pread()/pwrite() */
#define _XOPEN_SOURCE 500
#endif
#include <errno.h>
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
#include "pffs.h"
#include "pfi.h"
static int fusepfi_access(const char *path, int mask)
{
/*
if(strcmp(path, "/") == 0
|| strcmp(path, ".") == 0
|| strcmp(path, "..") == 0)
{
return access(path, mask);
}
*/
return 0;
}
static int fusepfi_getattr(const char *path, struct stat *stbuf)
{
struct fuse_context* ctx = fuse_get_context();
PFI* pPfi = ctx->private_data;
DIRECTORY* pDir;
char* pp = (char*)path;
int ret;
if(strcmp(path, "/") == 0
|| strcmp(path, ".") == 0
|| strcmp(path, "..") == 0)
{
ret = lstat(path, stbuf);
if(ret == -1)
return -errno;
else
return 0;
}
if(*pp == '/') pp++;
pDir = PFFSFindFile(pPfi, pp);
if(pDir)
{
stbuf->st_nlink = 1;
stbuf->st_mode = S_IFREG | 0660;
stbuf->st_size = pDir->size;
stbuf->st_uid = ctx->uid;
stbuf->st_gid = ctx->gid;
}
else
{
ret = lstat(path, stbuf);
if(ret == -1)
return -errno;
}
return 0;
}
static int fusepfi_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
struct fuse_context* ctx = fuse_get_context();
PFI* pPfi = ctx->private_data;
DIRECTORY* pDir;
int i, dirs;
struct stat st;
dirs = PFFSDirCount(pPfi);
for(i = 0; i < dirs; i++)
{
pDir = PFFSDir(pPfi, i);
st.st_nlink = 1;
st.st_size = pDir->size;
st.st_mode = S_IFREG | 0660;
st.st_uid = ctx->uid;
st.st_gid = ctx->gid;
filler(buf, (const char*)pDir->name, NULL, 0);
}
return 0;
}
static int fusepfi_open(const char *path, struct fuse_file_info *fi)
{
struct fuse_context* ctx = fuse_get_context();
PFI* pPfi = ctx->private_data;
DIRECTORY* pDir;
const char* pPath;
if((fi->flags & (O_WRONLY | O_RDWR)) != 0)
{
return -EACCES;
}
pPath = strrchr(path, '/');
if(pPath != NULL) pPath++; else pPath = path;
pDir = PFFSFindFile(pPfi, (char*)pPath);
if(pDir == NULL)
{
return -ENOENT;
}
fi->fh = (uint64_t)pDir;
fi->nonseekable = 1;
return 0;
}
static int fusepfi_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
struct fuse_context* ctx = fuse_get_context();
PFI* pPfi = ctx->private_data;
DIRECTORY* pDir;
pDir = (DIRECTORY*)fi->fh;
if(pDir == NULL)
{
return -ENOENT;
}
int nFileSize = pDir->size;
int nBytesToSkip = offset;
int nSector = pDir->chain;
while(nBytesToSkip > 4096 && nFileSize > 0 && nSector != FAT_END) {
nSector = pPfi->msb->fat[nSector].chain;
nBytesToSkip -= 4096;
nFileSize -= 4096;
}
// オフセットまで飛ばすうちにファイル終了
if(nFileSize <= 0 || nSector == FAT_END) return 0;
int nBytesToRead, nBytesRead = 0;
int nBytesToCopy = size;
while(nFileSize > 0 && nSector != FAT_END)
{
nBytesToRead = (nFileSize > 4096) ? 4096 : nFileSize;
if(nBytesToCopy < nBytesToRead) nBytesToRead = nBytesToCopy;
memcpy(buf + nBytesToSkip, PFFSSectorToPointer(pPfi, nSector), nBytesToRead - nBytesToSkip);
nBytesToSkip = 0;
nBytesRead += nBytesToRead - nBytesToSkip;
buf += nBytesToRead;
nFileSize -= nBytesToRead - nBytesToSkip;
nSector = pPfi->msb->fat[nSector].chain;
}
return nBytesRead;
}
static int fusepfi_write(const char *path, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi)
{
int fd;
int res;
(void) fi;
fd = open(path, O_WRONLY);
if (fd == -1)
return -errno;
res = pwrite(fd, buf, size, offset);
if (res == -1)
res = -errno;
close(fd);
return res;
}
static int fusepfi_release(const char *path, struct fuse_file_info *fi)
{
/* Just a stub. This method is optional and can safely be left
unimplemented */
fi->fh = 0;
return 0;
}
static int fusepfi_fsync(const char *path, int isdatasync,
struct fuse_file_info *fi)
{
/* Just a stub. This method is optional and can safely be left
unimplemented */
(void) path;
(void) isdatasync;
(void) fi;
return 0;
}
void* fusepfi_init(struct fuse_conn_info* conn)
{
PFI* pPfi = malloc(sizeof(PFI));
char* filename;
filename = fuse_get_context()->private_data;
PFIInit(pPfi);
PFIOpen(pPfi, filename);
return pPfi;
}
void fusepfi_destroy(void* data)
{
PFI* pPfi = data;
PFIClose(pPfi);
PFIExit(pPfi);
free(pPfi);
}
struct fusepfi_param
{
const char* filename;
int is_help;
};
static struct fuse_operations fusepfi_oper = {
// .access = fusepfi_access,
.getattr = fusepfi_getattr,
.readdir = fusepfi_readdir,
.open = fusepfi_open,
.read = fusepfi_read,
.release = fusepfi_release,
// .fsync = fusepfi_fsync,
.init = fusepfi_init,
.destroy = fusepfi_destroy,
};
struct fuse_opt fusepfi_opts[] =
{
FUSE_OPT_KEY("-h", 0),
FUSE_OPT_KEY("--help", 0),
FUSE_OPT_END
};
static int fusepfi_process_arg(void *data, const char *arg, int key,
struct fuse_args *outargs)
{
struct fusepfi_param *param = data;
switch(key)
{
case FUSE_OPT_KEY_NONOPT:
if(!param->filename)
{
param->filename = arg;
return 0;
}
return 1;
case 0:
param->is_help = 1;
return fuse_opt_add_arg(outargs, "-ho");
default:
return 1;
}
}
int main(int argc, char *argv[])
{
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
struct fusepfi_param param = { NULL, 0 };
if(fuse_opt_parse(&args, ¶m, fusepfi_opts, fusepfi_process_arg))
{
fprintf(stderr, "Failed to parse options\n");
return 1;
}
return fuse_main(args.argc, args.argv, &fusepfi_oper, param.filename);
}