2 fusefile - overlay a file path with a concatenation of parts of
3 other files, read only.
5 Copyright (C) 2019 Ralph Ronnquist
7 This program is free software: you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation, either version 3 of the
10 License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see
19 <http://www.gnu.org/licenses/>.
21 This source was inspired by the "null.c" example of the libfuse
22 sources, which is distributed under GPL2, and copyright (C)
23 2001-2007 Miklos Szeredi <miklos@szeredi.hu>.
26 #define FUSE_USE_VERSION 33
29 #include <fuse/fuse_lowlevel.h>
41 ssize_t start; // starting position in concatenated file
58 static void print_source(struct Source *p) {
59 fprintf( stderr, "%p { %s, %ld, %ld, %ld, %d }\n",
60 p, p->filename, p->from, p->to, p->start, p-> fd );
64 // Scan the source specification, and return the length of the
65 // inclusion. "filename/from,to"
70 static size_t scan_source(char *in,struct Source *p) {
75 // scan for last '/' and last '-'
76 for ( ; i >= 0; i-- ) {
86 fprintf( stderr, "m=%d s=%d\n", m, s );
88 // Copy the filename, and set from and to
89 p->filename = strndup( in, ( s < 0 )? e : s );
91 if ( stat( p->filename, & buf ) ) {
92 perror( p->filename );
95 p->from = ( s < 0 )? 0 : atol( in+s+1 );
100 fprintf( stderr, "p->from=%ld\n", p->from );
102 p->to = ( m < 0 )? buf.st_size : atol( in+m+1 );
103 if ( p->from > p->to || p->to > buf.st_size ) {
109 static int setup_sources(char **argv,int i,int n) {
110 sources.array = calloc( n, sizeof( struct Source ) );
111 if ( sources.array == 0 ) {
117 for ( ; j < n; i++, j++ ) {
118 struct Source *p = sources.array + j;
119 if ( scan_source( argv[i], p ) ) {
120 // should free everything malloc-ed
123 p->start = sources.size;
124 sources.size += p->to - p->from;
125 p->fd = open( p->filename, O_RDWR );
127 fprintf( stderr, "** %s opened read-only\n", p->filename );
128 p->fd = open( p->filename, O_RDONLY );
131 perror( p->filename );
141 static int fusefile_getattr(const char *path,struct stat *stbuf) {
143 fprintf( stderr, "fusefile_getattr( %s )\n", path );
145 if ( strcmp( path, "/" ) != 0 ) {
149 fprintf( stderr, "getattr %ld\n", sources.size );
151 memset( stbuf, 0, sizeof( struct stat ) );
152 stbuf->st_mode = S_IFREG | 0644; // Hmmm
154 stbuf->st_size = sources.size;
155 stbuf->st_atime = times.atime;
156 stbuf->st_mtime = times.mtime;
157 stbuf->st_ctime = times.ctime;
158 stbuf->st_uid = getuid();
159 stbuf->st_gid = getgid();
163 static int fusefile_chmod(const char *path,mode_t m) {
165 fprintf( stderr, "fusefile_chmod( %s, %d )\n", path, m );
170 static int fusefile_open(const char *path,struct fuse_file_info *fi) {
172 fprintf( stderr, "fusefile_open( %s, %d )\n", path, fi->flags );
173 fprintf( stderr, "fixing( %d )\n", fi->flags | O_CLOEXEC );
175 if ( strcmp( path, "/" ) != 0 ) {
178 // set O-CLOEXEC for this opening?
179 times.atime = time( 0 );
183 static int find_source(off_t offset) {
185 int hi = sources.count;
186 if ( offset >= sources.size ) {
189 while ( lo + 1 < hi ) {
190 int m = ( lo + hi ) / 2;
191 if ( offset < sources.array[ m ].start ) {
200 // Read <size> bytes from <offset> in file
201 static int fusefile_read(const char *path, char *buf, size_t size,
202 off_t off, struct fuse_file_info *fi)
205 fprintf( stderr, "fusefile_read( %s )\n", path );
207 if( strcmp( path, "/" ) != 0 ) {
211 fprintf( stderr, "read %ld %ld\n", off, size );
216 fprintf( stderr, "find_source %ld %ld\n", off, size );
218 int i = find_source( off );
220 return ( off == sources.size )? rr : -ENOENT;
222 if ( sources.array[i].fd < 0 ) {
226 print_source( &sources.array[i] );
228 times.atime = time( 0 );
229 size_t b = off - sources.array[i].start + sources.array[i].from;
230 size_t n = sources.array[i].to - b;
234 if ( lseek( sources.array[i].fd, b, SEEK_SET ) < 0 ) {
235 perror( sources.array[i].filename );
239 fprintf( stderr, "get %ld bytes at %ld\n", n, rr );
241 ssize_t r = read( sources.array[i].fd, buf + rr, n );
243 fprintf( stderr, "got %ld bytes\n", r );
246 perror( sources.array[i].filename );
260 * Poll for IO readiness.
262 int fusefile_poll(const char *path, struct fuse_file_info *fi,
263 struct fuse_pollhandle *ph, unsigned *reventsp )
266 fprintf( stderr, "fusefile_poll( %s ) %p %d\n", path, ph, *reventsp );
268 if( strcmp( path, "/" ) != 0 ) {
272 return fuse_notify_poll( ph );
279 * Write a full block of data over the sources at the offset
281 static int write_block(off_t off,const char *buf,size_t size) {
283 fprintf( stderr, "write_block( %ld, ?, %ld )\n", off, size );
286 int index = find_source( off ); // index of source file
288 return -EIO; // past EOF
290 struct Source *source = &sources.array[ index ];
291 off_t from = off - source->start + source->from;
292 off_t max = source->to - from;
293 if ( lseek( source->fd, from, SEEK_SET ) < 0 ) {
296 ssize_t todo = ( size < max )? size : max;
298 times.mtime = time( 0 );
299 ssize_t n = write( source->fd, buf, todo );
301 return -EIO; // Something wrong
312 static int fusefile_write_buf(const char *path, struct fuse_bufvec *buf,
313 off_t off, struct fuse_file_info *fi) {
315 fprintf( stderr, "fusefile_write_buf( %s )\n", path );
317 if ( strcmp( path, "/" ) != 0 ) {
323 for ( i = 0; i < buf->count; i++ ) {
324 struct fuse_buf *p = &buf->buf[i];
325 if ( p->flags & FUSE_BUF_IS_FD ) {
327 fprintf( stderr, "Content held in a file ... HELP!!\n" );
331 if ( write_block( off, (char*) p->mem, p->size ) < 0 ) {
337 fprintf( stderr, "fusefile_write_buf written %ld\n", size );
343 * Write a fragment at <off>. This overwrites files.
345 static int fusefile_write(const char *path, const char *buf, size_t size,
346 off_t off, struct fuse_file_info *fi)
349 fprintf( stderr, "fusefile_write( %s %ld )\n", path, size );
351 if ( strcmp( path, "/" ) != 0 ) {
355 if ( write_block( off, buf, size ) < 0 ) {
361 static void fusefile_destroy(void *data) {
362 char *mnt = (char*) data; // As passed to fuse_main
364 fprintf( stderr, "fusefile_destroy( %s )\n", mnt? mnt : "" );
371 static int fusefile_flush(const char *path, struct fuse_file_info *info) {
373 fprintf( stderr, "fusefile_flush( %s )\n", path );
375 if ( strcmp( path, "/" ) != 0 ) {
381 static int fusefile_release(const char *path, struct fuse_file_info *fi) {
383 fprintf( stderr, "fusefile_release( %s, %d )\n", path, fi->flags );
385 if ( strcmp( path, "/" ) != 0 ) {
391 static int fusefile_fsync(const char *path, int x, struct fuse_file_info *fi) {
393 fprintf( stderr, "fusefile_fsync( %s, %d )\n", path, x );
395 if ( strcmp( path, "/" ) != 0 ) {
404 static int fusefile_truncate(const char *path, off_t len) {
406 fprintf( stderr, "fusefile_truncate( %s, %ld )\n", path, len );
408 if ( strcmp( path, "/" ) != 0 ) {
414 void *fusefile_init(struct fuse_conn_info *fci) {
416 fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
418 // Disable asynchronous reading
420 fci->want &= ~FUSE_CAP_ASYNC_READ;
422 fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
427 static struct fuse_operations fusefile_oper = {
428 .getattr = fusefile_getattr,
429 .chmod = fusefile_chmod,
430 .open = fusefile_open,
431 .read = fusefile_read,
432 .poll = fusefile_poll,
433 .write = fusefile_write,
434 .write_buf = fusefile_write_buf,
435 .destroy = fusefile_destroy,
436 .flush = fusefile_flush,
437 .release = fusefile_release,
438 .fsync = fusefile_fsync,
439 .truncate = fusefile_truncate,
440 //.truncate = fusefile_truncate,
441 //.release = fusefile_release,
442 .init = fusefile_init,
445 static void usage() {
447 "Usage: fusefile [ <fuse options> ] <mount> <file/from-to> ... \n"
448 "Mounts a virtual, read-only file that is a concatenation of file fragments\n"
450 fprintf( stderr, "%s", usage );
455 * Set up the arguments for the fuse_main call, adding our own.
456 * argv[argc] is the mount point argument
458 static int setup_argv(int argc,char ***argv) {
459 // note: (*argv)[ argc ] is the mount point argument
461 "-odefault_permissions",
464 #define OURSN ( sizeof( OURS ) / sizeof( char* ) )
465 int N = argc + OURSN;
466 // Allocate new arg array plus terminating null pointer
467 char **out = malloc( ( N + 1 ) * sizeof( char* ) );
469 for ( i = 0; i < argc; i++ ) {
470 out[ i ] = (*argv)[i];
471 //fprintf( stderr, " %s", out[ i ] );
473 for ( i = 0; i < OURSN; i++ ) {
474 out[ argc + i ] = OURS[i];
475 //fprintf( stderr, " %s", out[ i ] );
478 //fprintf( stderr, "\n" );
480 return N; // Don't include the terminating null pointer
484 * Mount a concatenation of files,
485 * [ <fuse options> ] <mount> <file/from-to> ...
487 int main(int argc, char *argv[])
497 for ( i = 1; i < argc; i++ ) {
498 if ( *argv[i] != '-' ) {
502 if ( i > argc - 2 ) { // At least mount point plus one source
506 mnt = argv[ i++ ]; // First non-option argument is the mount pount
507 if ( setup_sources( argv, i, argc-i ) ) {
510 if ( stat( mnt, &stbuf ) == -1 ) {
511 int fd = open( mnt, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR );
516 time_t now = time( 0 );
522 } else if ( ! S_ISREG( stbuf.st_mode ) ) {
523 fprintf( stderr, "mountpoint is not a regular file\n" );
526 times.atime = stbuf.st_atime;
527 times.mtime = stbuf.st_mtime;
528 times.ctime = stbuf.st_ctime;
531 fuseargc = setup_argv( fuseargc, &argv );
532 struct fuse_args args = FUSE_ARGS_INIT( fuseargc, argv );
533 if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {
536 fuse_opt_free_args( &args );
538 fprintf( stderr, "missing mountpoint parameter\n" );
541 return fuse_main( fuseargc, argv, &fusefile_oper, temporary? mnt : NULL );