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
57 static struct Source overlay;
60 static void print_source(struct Source *p) {
61 fprintf( stderr, "%p { %s, %ld, %ld, %ld, %d }\n",
62 p, p->filename, p->from, p->to, p->start, p-> fd );
67 static unsigned int c;
68 static int RANGE(int s,int n ) {
69 return ( s == n ) && *(range+c) == 0;
74 static void setup_overlay(char *filename) {
75 overlay.filename = filename;
76 overlay.fd = open( filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR );
77 if ( overlay.fd < 0 ) {
83 static int setup_sources(char **argv,int i,int n) {
84 sources.array = calloc( n, sizeof( struct Source ) );
85 if ( sources.array == 0 ) {
91 for ( ; j < n; i++, j++ ) {
93 struct Source *p = sources.array + j;
94 // Open the fragment file rw if possible, else ro
95 range = strrchr( argv[i], '/' ); // last '/'
96 p->filename = range? strndup( argv[i], range - argv[i] ) : argv[i];
97 p->fd = open( p->filename, O_RDWR );
101 p->fd = open( p->filename, O_RDONLY );
104 perror( p->filename );
105 return 1; // Error return
107 if ( stat( p->filename, &filestat ) ) {
108 perror( p->filename );
112 fprintf( stderr, "** %s opened read-only\n", p->filename );
115 p->to = filestat.st_size;
116 // Process any range variation
117 if ( range && *(++range) ) {
120 } else if ( RANGE( sscanf( range, "%d:%d%n", &a, &b, &c ), 2 )) {
121 p->from = ( a < 0 )? ( p->to + a ) : a;
122 p->to = ( b < 0 )? ( p->to + b ) : b;
123 } else if ( RANGE( sscanf( range, "%d+%d%n", &a, &b, &c ), 2 )) {
124 p->from = ( a < 0 )? ( p->to + a ) : a;
125 p->to = ( ( b < 0 )? p->to : p->from ) + b;
126 } else if ( RANGE( sscanf( range, "%d+%n", &a, &c ), 1 )) {
127 p->from = ( a < 0 )? ( p->to + a ) : a;
128 } else if ( RANGE( sscanf( range, ":%d%n", &b, &c ), 1 )) {
129 p->to = ( b < 0 )? ( p->to + b ) : b;
130 } else if ( RANGE( sscanf( range, "%d:%n", &a, &c ), 1 )) {
131 p->from = ( a < 0 )? ( p->to + a ) : a;
132 } else if ( RANGE( sscanf( range, "%d%n", &a, &c ), 1 )) {
138 } else if ( RANGE( sscanf( range, ":%n", &c), 0 ) ) {
141 fprintf( stderr, "** BAD RANGE: %s\n", argv[i] );
145 if ( ( filestat.st_mode & S_IFMT ) == S_IFCHR ) {
146 filestat.st_size = p->to; // Pretend size of character device
151 if ( p->to > filestat.st_size ) {
152 p->to = filestat.st_size;
154 if ( p->from >= p->to || p->from >= filestat.st_size ) {
155 fprintf( stderr, "** BAD RANGE: %s [%ld:%ld]\n",
156 argv[i], p->from, p->to );
159 p->start = sources.size; // the fusefile position of fragment
160 sources.size += p->to - p->from;
168 static int fusefile_getattr(const char *path,struct stat *stbuf) {
170 fprintf( stderr, "fusefile_getattr( %s )\n", path );
172 if ( strcmp( path, "/" ) != 0 ) {
176 fprintf( stderr, "getattr %ld\n", sources.size );
178 memset( stbuf, 0, sizeof( struct stat ) );
179 stbuf->st_mode = S_IFREG | 0644; // Hmmm
181 stbuf->st_size = sources.size;
182 stbuf->st_atime = times.atime;
183 stbuf->st_mtime = times.mtime;
184 stbuf->st_ctime = times.ctime;
185 stbuf->st_uid = getuid();
186 stbuf->st_gid = getgid();
190 static int fusefile_chmod(const char *path,mode_t m) {
192 fprintf( stderr, "fusefile_chmod( %s, %d )\n", path, m );
197 static int fusefile_open(const char *path,struct fuse_file_info *fi) {
199 fprintf( stderr, "fusefile_open( %s, %d )\n", path, fi->flags );
200 fprintf( stderr, "fixing( %d )\n", fi->flags | O_CLOEXEC );
202 if ( strcmp( path, "/" ) != 0 ) {
205 // set O-CLOEXEC for this opening?
206 times.atime = time( 0 );
210 static int find_source(off_t offset) {
212 int hi = sources.count;
213 if ( offset >= sources.size ) {
217 fprintf( stderr, "find_source( %ld )\n", offset );
219 while ( lo + 1 < hi ) {
220 int m = ( lo + hi ) / 2;
221 if ( offset < sources.array[ m ].start ) {
223 fprintf( stderr, " offset < [%d].start: %ld\n",
224 m, sources.array[ m ].start );
229 fprintf( stderr, " offset >= [%d].start: %ld\n",
230 m, sources.array[ m ].start );
236 fprintf( stderr, "found %d\n", lo );
241 #define OBUFSZ 1048576
242 static int overlay_merge(char *buf,off_t off,size_t size) {
243 static char obuf[ OBUFSZ ];
245 fprintf( stderr, "merge %ld %ld\n", off, size );
248 size_t n = size < OBUFSZ? size : OBUFSZ;
249 off_t ox = lseek( overlay.fd, off, SEEK_SET );
251 fprintf( stderr, " seek %ld %ld %ld\n", off, ox, n );
254 perror( overlay.filename );
260 n = read( overlay.fd, obuf, n );
262 fprintf( stderr, " got %ld\n", n );
265 perror( overlay.filename );
283 fprintf( stderr, "merged\n" );
288 // Read <size> bytes from <offset> in file
289 static int fusefile_read(const char *path, char *buf, size_t size,
290 off_t off, struct fuse_file_info *fi)
293 fprintf( stderr, "fusefile_read( %s )\n", path );
295 if( strcmp( path, "/" ) != 0 ) {
299 fprintf( stderr, "read %ld %ld\n", off, size );
301 size_t rr = 0; // total reading
304 fprintf( stderr, " find_source %ld %ld\n", off, size );
306 int i = find_source( off );
308 return ( off == sources.size )? rr : -ENOENT;
310 if ( sources.array[i].fd < 0 ) {
314 print_source( &sources.array[i] );
316 times.atime = time( 0 );
317 size_t b = off - sources.array[i].start + sources.array[i].from;
318 size_t n = sources.array[i].to - b;
323 fprintf( stderr, " seek fd=%d to %ld\n", sources.array[i].fd, b );
325 if ( lseek( sources.array[i].fd, b, SEEK_SET ) < 0 ) {
326 perror( sources.array[i].filename );
330 fprintf( stderr, " now read %ld from fd=%d\n",
331 n, sources.array[i].fd );
333 ssize_t r = read( sources.array[i].fd, buf + rr, n );
334 if ( overlay.filename ) {
335 int x = overlay_merge( buf + rr, off + rr, r );
341 fprintf( stderr, " got %ld bytes\n", r );
344 perror( sources.array[i].filename );
355 fprintf( stderr, " total reading %ld bytes\n", rr );
361 * Poll for IO readiness.
363 int fusefile_poll(const char *path, struct fuse_file_info *fi,
364 struct fuse_pollhandle *ph, unsigned *reventsp )
367 fprintf( stderr, "fusefile_poll( %s ) %p %d\n", path, ph, *reventsp );
369 if( strcmp( path, "/" ) != 0 ) {
373 return fuse_notify_poll( ph );
380 * Write a full block of data over the sources at the offset
382 static int write_block(off_t off,const char *buf,size_t size) {
384 fprintf( stderr, "write_block( %ld, ?, %ld )\n", off, size );
387 int index = find_source( off ); // index of source file
389 return -EIO; // past EOF
391 struct Source *source =
392 overlay.filename? &overlay : &sources.array[ index ];
393 off_t from = off - source->start + source->from;
394 off_t max = source->to - from;
395 if ( lseek( source->fd, from, SEEK_SET ) < 0 ) {
398 ssize_t todo = ( size < max )? size : max;
400 times.mtime = time( 0 );
401 ssize_t n = write( source->fd, buf, todo );
403 return -EIO; // Something wrong
414 static int fusefile_write_buf(const char *path, struct fuse_bufvec *buf,
415 off_t off, struct fuse_file_info *fi) {
417 fprintf( stderr, "fusefile_write_buf( %s )\n", path );
419 if ( strcmp( path, "/" ) != 0 ) {
425 for ( i = 0; i < buf->count; i++ ) {
426 struct fuse_buf *p = &buf->buf[i];
427 if ( p->flags & FUSE_BUF_IS_FD ) {
429 fprintf( stderr, "Content held in a file ... HELP!!\n" );
433 if ( write_block( off, (char*) p->mem, p->size ) < 0 ) {
439 fprintf( stderr, "fusefile_write_buf written %ld\n", size );
445 * Write a fragment at <off>. This overwrites files.
447 static int fusefile_write(const char *path, const char *buf, size_t size,
448 off_t off, struct fuse_file_info *fi)
451 fprintf( stderr, "fusefile_write( %s %ld )\n", path, size );
453 if ( strcmp( path, "/" ) != 0 ) {
457 if ( write_block( off, buf, size ) < 0 ) {
463 static void fusefile_destroy(void *data) {
464 char *mnt = (char*) data; // As passed to fuse_main
466 fprintf( stderr, "fusefile_destroy( %s )\n", mnt? mnt : "" );
473 static int fusefile_flush(const char *path, struct fuse_file_info *info) {
475 fprintf( stderr, "fusefile_flush( %s )\n", path );
477 if ( strcmp( path, "/" ) != 0 ) {
483 static int fusefile_release(const char *path, struct fuse_file_info *fi) {
485 fprintf( stderr, "fusefile_release( %s, %d )\n", path, fi->flags );
487 if ( strcmp( path, "/" ) != 0 ) {
493 static int fusefile_fsync(const char *path, int x, struct fuse_file_info *fi) {
495 fprintf( stderr, "fusefile_fsync( %s, %d )\n", path, x );
497 if ( strcmp( path, "/" ) != 0 ) {
506 static int fusefile_truncate(const char *path, off_t len) {
508 fprintf( stderr, "fusefile_truncate( %s, %ld )\n", path, len );
510 if ( strcmp( path, "/" ) != 0 ) {
516 void *fusefile_init(struct fuse_conn_info *fci) {
518 fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
520 // Disable asynchronous reading
522 fci->want &= ~FUSE_CAP_ASYNC_READ;
524 fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
529 static struct fuse_operations fusefile_oper = {
530 .getattr = fusefile_getattr,
531 .chmod = fusefile_chmod,
532 .open = fusefile_open,
533 .read = fusefile_read,
534 .poll = fusefile_poll,
535 .write = fusefile_write,
536 .write_buf = fusefile_write_buf,
537 .destroy = fusefile_destroy,
538 .flush = fusefile_flush,
539 .release = fusefile_release,
540 .fsync = fusefile_fsync,
541 .truncate = fusefile_truncate,
542 //.truncate = fusefile_truncate,
543 //.release = fusefile_release,
544 .init = fusefile_init,
547 static void usage() {
549 "Usage: fusefile [ <fuse options> ] <mount> <file/from-to> ... \n"
550 "Mounts a virtual, file that is a concatenation of file fragments\n"
552 fprintf( stderr, "%s", usage );
557 * Set up the arguments for the fuse_main call, adding our own.
558 * argv[argc] is the mount point argument
560 static int setup_argv(int argc,char ***argv) {
561 // note: (*argv)[ argc ] is the mount point argument
563 "-odefault_permissions",
566 #define OURSN ( sizeof( OURS ) / sizeof( char* ) )
567 int N = argc + OURSN;
568 // Allocate new arg array plus terminating null pointer
569 char **out = malloc( ( N + 1 ) * sizeof( char* ) );
571 for ( i = 0; i < argc; i++ ) {
572 out[ i ] = (*argv)[i];
573 //fprintf( stderr, " %s", out[ i ] );
575 for ( i = 0; i < OURSN; i++ ) {
576 out[ argc + i ] = OURS[i];
577 //fprintf( stderr, " %s", out[ i ] );
580 //fprintf( stderr, "\n" );
582 return N; // Don't include the terminating null pointer
586 * Mount a concatenation of files,
587 * [ <fuse options> ] <mount> <file/from-to> ...
589 int main(int argc, char *argv[])
599 for ( i = 1; i < argc; i++ ) {
600 if ( *argv[i] != '-' ) {
604 if ( i > argc - 2 ) { // At least mount point plus one source
608 mnt = argv[ i++ ]; // First non-option argument is the mount pount
609 char *overlaytag = "-overlay:";
610 int overlaytagsize = strlen( overlaytag );
611 if ( strncmp( argv[i], overlaytag, overlaytagsize ) == 0 ) {
612 // consume "-overlay:filename"
613 setup_overlay( argv[i++] + overlaytagsize ); // Need a writable file
618 if ( setup_sources( argv, i, argc-i ) ) {
621 overlay.to = sources.size; // Register total size.
622 if ( stat( mnt, &stbuf ) == -1 ) {
623 int fd = open( mnt, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR );
628 time_t now = time( 0 );
634 } else if ( ! S_ISREG( stbuf.st_mode ) ) {
635 fprintf( stderr, "mountpoint is not a regular file\n" );
638 times.atime = stbuf.st_atime;
639 times.mtime = stbuf.st_mtime;
640 times.ctime = stbuf.st_ctime;
644 int fd = open( mnt, O_RDWR, S_IRUSR | S_IWUSR );
649 if ( lseek( fd, sources.size, SEEK_SET ) < 0 ) {
653 fuseargc = setup_argv( fuseargc, &argv );
654 struct fuse_args args = FUSE_ARGS_INIT( fuseargc, argv );
655 if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {
658 fuse_opt_free_args( &args );
660 fprintf( stderr, "missing mountpoint parameter\n" );
663 return fuse_main( fuseargc, argv, &fusefile_oper, temporary? mnt : NULL );