-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfuseloop.c
330 lines (277 loc) · 8.31 KB
/
fuseloop.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* fuseloop.c - loopback via fuse
*
* See the LICENSE.txt file for license details.
*
* Mainly intended to allow creating of virtual disk images by giving
* specific offset/size access so that mke2fs can be run on less than the
* entire file.
*
* Once a virtual disk image has been partitioned and had the filesystems
* created, "mountlo" can be used to mount them.
*/
#define FUSE_USE_VERSION 26
#include <fuse.h>
#include <fuse_common.h>
#include <fuse_opt.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#define VERSION "1.0"
#define DBG(fmt, ...) do { \
fprintf(stderr, "debug: ");\
fprintf(stderr, fmt, __VA_ARGS__);\
fprintf(stderr, "\n"); }\
while (0)
/**
* Configuration options & state
*/
struct cfg{
char *src_file;
char *mount_file;
off_t offset_in_bytes;
off_t size_in_bytes;
int open_as_readonly;
int src_fd;
int debug;
};
#define LOCAL_OPT(opt, var, val) { opt, offsetof(struct cfg, var), val }
enum {
KEY_RO,
KEY_RW,
KEY_HELP,
KEY_VERSION,
};
static struct fuse_opt all_opts[] = {
LOCAL_OPT("-d", debug, 1),
LOCAL_OPT("-O %lu", offset_in_bytes, 0),
LOCAL_OPT("-S %lu", size_in_bytes, 0),
FUSE_OPT_KEY("-r", KEY_RO),
FUSE_OPT_KEY("-w", KEY_RW),
FUSE_OPT_KEY("-h", KEY_HELP),
FUSE_OPT_KEY("--help", KEY_HELP),
FUSE_OPT_KEY("-V", KEY_VERSION),
FUSE_OPT_KEY("--version", KEY_VERSION),
FUSE_OPT_END
};
static void syntax (const char *argv0)
{
fprintf (stderr,
"syntax: %s [options] <src_file> <mount_file>\n"
"options:\n"
" -h --help print help\n"
" -V --version print version\n"
" -O <bytes> src_file offset in bytes\n"
" -S <bytes> size in bytes available from offset\n"
" -r read only\n"
" -w read write (default)\n"
" -o <option(s)> mount options\n"
" -d enable debug output\n"
"\n",
argv0);
exit (1);
}
static int process_opts (void *data, const char *arg, int key, struct fuse_args *outargs)
{
struct cfg *conf = data;
switch (key)
{
case FUSE_OPT_KEY_NONOPT:
if (!conf->src_file)
{
conf->src_file = strdup (arg);
return 0;
}
else if (!conf->mount_file)
{
conf->mount_file = strdup (arg);
return 0;
}
break;
case FUSE_OPT_KEY_OPT:
// Keep -o options
return 1;
case KEY_RO:
conf->open_as_readonly = 1;
return fuse_opt_add_arg (outargs, "-oro");
case KEY_RW:
return fuse_opt_add_arg (outargs, "-orw");
case KEY_HELP:
syntax (outargs->argv[0]);
// does not return
case KEY_VERSION:
fprintf (stderr, "fuseloop %s\n", VERSION);
exit (0);
default:
break;
}
fprintf (stderr, "unknown option: '%s'\n", arg);
return -1;
}
#define ISROOT(path) (path[0] == '/' && path[1] == '\0')
static int fuseloop_open (const char *path, struct fuse_file_info *fi)
{
if (!ISROOT(path))
return -ENOENT;
return 0;
}
static int fuseloop_read (const char *path, char *buf, size_t size, off_t offs, struct fuse_file_info *fi)
{
int res = 0;
struct fuse_context *ctx = fuse_get_context ();
struct cfg *conf = ctx->private_data;
if (!ISROOT(path))
return -ENOENT;
// Truncate read request if outside of file bounds
if (conf->size_in_bytes >= 0 && (offs + size) > conf->size_in_bytes)
{
// size_t is unsigned, so make sure we don't by accident wrap it
if (offs > conf->size_in_bytes)
return -EINVAL; // Most suitable error code, I think?
size = conf->size_in_bytes - offs;
}
res = pread (conf->src_fd, buf, size, offs + conf->offset_in_bytes);
if (res == -1)
res = -errno;
return res;
}
static int fuseloop_write (const char *path, const char *buf, size_t size, off_t offs, struct fuse_file_info *fi)
{
int res = 0;
struct fuse_context *ctx = fuse_get_context ();
struct cfg *conf = ctx->private_data;
if (!ISROOT(path))
return -ENOENT;
if (conf->size_in_bytes >= 0 && (offs + size) > conf->size_in_bytes)
{
// size_t is unsigned, so make sure we don't by accident wrap it
if (offs > conf->size_in_bytes)
return -ENOSPC;
size = conf->size_in_bytes - offs;
}
res = pwrite (conf->src_fd, buf, size, offs + conf->offset_in_bytes);
if (res == -1)
res = -errno;
return res;
}
static int fuseloop_getattr (const char *path, struct stat *stbuf)
{
struct fuse_context *ctx = fuse_get_context ();
struct cfg *conf = ctx->private_data;
if (!ISROOT(path))
return -ENOENT;
if (fstat (conf->src_fd, stbuf) == -1)
return -errno;
stbuf->st_size = conf->size_in_bytes;
stbuf->st_mode = 0100600;
return 0;
}
static int fuseloop_fsync (const char *path, int unknown, struct fuse_file_info *fi)
{
int ret = 0;
struct fuse_context *ctx = fuse_get_context ();
struct cfg *conf = ctx->private_data;
if (!ISROOT(path))
return -ENOENT;
ret = fsync (conf->src_fd);
if (ret == -1)
ret = -errno;
return ret;
}
struct fuse_operations ops = {
.open = fuseloop_open,
.read = fuseloop_read,
.write = fuseloop_write,
.getattr = fuseloop_getattr,
.fsync = fuseloop_fsync,
};
enum ERR_CODES {
BAD_OPTS = 1,
FAILED_OPEN,
NO_SIZE,
FAILED_MOUNT,
FAILED_FUSENEW,
FAILED_SIGNALS,
FAILED_DAEMONIZE,
};
int main (int argc, char *argv[])
{
int open_mode = O_RDWR;
struct fuse_chan *chan = 0;
struct fuse *fs = 0;
struct cfg conf = { .size_in_bytes = -1, };
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
// FIXME - need to set -odirect_io ?
// Parse the options
if (fuse_opt_parse (&args, &conf, all_opts, process_opts) == -1)
return BAD_OPTS;
// Make sure we have at least srcfile + mountfile
if (!conf.src_file || !conf.mount_file)
syntax (argv[0]);
// Check if we need to open in read-only mode
if (conf.open_as_readonly)
open_mode = O_RDONLY;
// Show what we've got
if (conf.debug)
{
DBG("size set to: %ld", conf.size_in_bytes);
DBG("offset set to: %ld", conf.offset_in_bytes);
DBG("using open mode: %x", open_mode);
}
conf.src_fd = open (conf.src_file, open_mode);
if (conf.src_fd < 0)
{
perror ("Unable to open source file");
return FAILED_OPEN;
}
// Obtain the size, if necessary
if (conf.size_in_bytes == -1)
{
// Use actual file size
struct stat st;
if (fstat (conf.src_fd, &st) == -1)
{
perror ("Unable to obtain file size of source file");
return NO_SIZE;
}
DBG("looked up file size: %ld", st.st_size);
conf.size_in_bytes = st.st_size;
}
if (conf.debug)
DBG("file size is: %ld", conf.size_in_bytes);
// Mount a fuse on the specified file
chan = fuse_mount (conf.mount_file, &args);
if (!chan)
{
perror ("Unable to attach fuse to mount_file");
return FAILED_MOUNT;
}
// Register us as the handler of said fuse
fs = fuse_new (chan, &args, &ops, sizeof (ops), &conf);
if (!fs)
{
perror ("Failed to create fuse fs");
return FAILED_FUSENEW;
}
// Pop into the background unless we're debugging
if (fuse_daemonize (conf.debug) != 0)
{
perror ("Failed to daemonize process");
return FAILED_DAEMONIZE;
}
// Set the signal handlers (should this be done before daemonize?)
if (fuse_set_signal_handlers (fuse_get_session (fs)) != 0)
{
perror ("Unable to install signal handlers");
return FAILED_SIGNALS;
}
// Main loop, and cleanup
fuse_loop_mt (fs);
fuse_unmount (conf.mount_file, chan);
fuse_remove_signal_handlers (fuse_get_session (fs));
fuse_destroy (fs);
close (conf.src_fd);
return 0;
}