2 fusefile - overlay a file path with a concatenation of parts of
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>
46 ssize_t start; // starting position in concatenated file
76 * Find the nearest overlay.table region below pos. Returns the index,
77 * or -1 if there is none, i.e. pos < overlay.table[0].
79 static ssize_t overlay_prior_fragment(off_t pos) {
80 size_t lo = 0, hi = overlay.count;
82 size_t m = ( lo + hi ) / 2;
84 return overlay.table[m].beg <= pos? m : -1;
86 if ( overlay.table[m].beg <= pos ) {
96 * Save the entry count for overlay.table as 64-bit integer
97 * immediately following the overlay content at the index
98 * corresponding to the fused file size.
100 static void overlay_save_count() {
101 lseek( overlay.source.fd, overlay.source.to, SEEK_SET );
102 size_t size = sizeof( overlay.count );
103 char *p = (char *) &overlay.count ;
105 size_t n = write( overlay.source.fd, p, size );
107 perror( overlay.source.filename );
113 if ( overlay.source.dirty++ > 1000 ) {
114 fsync( overlay.source.fd );
115 overlay.source.dirty = 0;
120 * Update the on-disk cache of overlay.table between the given
121 * indexes. The table is laid out immediately following the table
122 * count with each region saved as two 64-bit unsigned integers.
124 static void overlay_save_table(size_t lo,size_t hi) {
125 char *p = (char *) &overlay.table[ lo ];
126 size_t pos = overlay.source.to + sizeof( overlay.count ) +
127 lo * sizeof( struct Region );
128 size_t size = ( hi - lo ) * sizeof( struct Region );
129 if ( pos != lseek( overlay.source.fd, pos, SEEK_SET ) ) {
130 fprintf( stderr, "%s: seek error\n", overlay.source.filename );
134 size_t n = write( overlay.source.fd, p, size );
136 perror( overlay.source.filename );
142 if ( overlay.source.dirty++ > 1000 ) {
143 fsync( overlay.source.fd );
144 overlay.source.dirty = 0;
149 * Insert a new region at index p, with previous portion [p,count]
150 * moved up to make space.
152 static void overlay_insert(size_t p,off_t beg,off_t end) {
154 // Grow the table if needed
155 if ( overlay.count >= overlay.limit ) {
156 overlay.limit = overlay.count + 10;
157 bytes = overlay.limit * sizeof( struct Region );
158 overlay.table = overlay.table?
159 realloc( overlay.table, bytes ) : malloc( bytes );
161 bytes = ( overlay.count++ - p ) * sizeof( struct Region );
163 memmove( (char*) &overlay.table[ p+1 ],
164 (char*) &overlay.table[ p ],
167 overlay.table[ p ].beg = beg;
168 overlay.table[ p ].end = end;
169 overlay_save_count();
173 * Delete the region entry at p by moving the portion [p+1,count]
176 static void overlay_delete(size_t p) {
177 size_t bytes = ( --overlay.count - p ) * sizeof( struct Region );
179 memmove( (char*) &overlay.table[ p ],
180 (char*) &overlay.table[ p+1 ],
186 * Mark the given region as updated, i.e. written to the overlay. The
187 * mark region may attach to prior marked regions or be a new,
188 * separate region. If attaching, it causes the prior regions to
189 * expand and the table adjusted by deleting any regions that become
190 * fully contained in other regions.
192 static void overlay_mark(off_t beg,off_t end) {
194 fprintf( stderr, "overlay_mark( %ld, %ld )\n", beg, end );
198 ssize_t p = overlay_prior_fragment( beg );
199 // p is the nearest region below or at beg (or -1)
200 if ( p >= 0 && beg <= overlay.table[p].end ) {
201 // p overlaps mark region
202 if ( end <= overlay.table[p].end ) {
203 // region p covers mark region already
205 fprintf( stderr, "overlay covering ( %ld %ld )\n",
206 overlay.table[p].beg, overlay.table[p].end );
210 // the new mark region extends region p
211 overlay.table[p].end = end;
213 while ( q < overlay.count &&
214 overlay.table[q].beg <= overlay.table[p].end ) {
215 // Extended region merges with subsequent region
216 if ( overlay.table[p].end < overlay.table[q].end ) {
217 overlay.table[p].end = overlay.table[q].end;
223 overlay_save_count();
226 overlay_save_table( p, q );
228 fprintf( stderr, "overlay expand ( %ld %ld ) deleted %d\n",
229 overlay.table[p].beg, overlay.table[p].end, deleted );
233 // The prior region p does not expand into new mark region
234 p++; // subsequent region
235 if ( p >= overlay.count || end < overlay.table[p].beg ) {
236 // New mark region is a separate region at p
237 overlay_insert( p, beg, end );
239 fprintf( stderr, "overlay new ( %ld %ld )\n",
240 overlay.table[p].beg, overlay.table[p].end );
242 overlay_save_table( p, overlay.count );
245 // New marks start before and overlap with region p => change p
246 // and handle any subsequent regions being covered
247 overlay.table[p].beg = beg;
249 if ( overlay.table[p].end < end ) {
250 overlay.table[p].end = end;
251 while ( q < overlay.count &&
252 overlay.table[q].beg <= overlay.table[p].end ) {
253 if ( overlay.table[p].end < overlay.table[q].end ) {
254 overlay.table[p].end = overlay.table[q].end;
260 overlay_save_count();
264 overlay_save_table( p, q );
266 fprintf( stderr, "overlay before ( %ld %ld ) deleted %d\n",
267 overlay.table[p].beg, overlay.table[p].end, deleted );
271 static void setup_overlay(char *filename) {
272 overlay.source.filename = filename;
273 overlay.source.fd = open( filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR );
274 if ( overlay.source.fd < 0 ) {
281 static void print_source(struct Source *p) {
282 fprintf( stderr, "%p { %s, %ld, %ld, %ld, %d }\n",
283 p, p->filename, p->from, p->to, p->start, p-> fd );
288 static unsigned int c;
289 static int RANGE(int s,int n ) {
290 return ( s == n ) && *(range+c) == 0;
293 static int setup_sources(char **argv,int i,int n) {
294 sources.array = calloc( n, sizeof( struct Source ) );
295 if ( sources.array == 0 ) {
301 for ( ; j < n; i++, j++ ) {
302 struct stat filestat;
303 struct Source *p = sources.array + j;
304 // Open the fragment file rw if possible, else ro
305 range = strrchr( argv[i], '/' ); // last '/'
306 p->filename = range? strndup( argv[i], range - argv[i] ) : argv[i];
307 p->fd = open( p->filename, O_RDWR );
311 p->fd = open( p->filename, O_RDONLY );
314 perror( p->filename );
315 return 1; // Error return
317 if ( stat( p->filename, &filestat ) ) {
318 perror( p->filename );
322 fprintf( stderr, "** %s opened read-only\n", p->filename );
325 p->to = filestat.st_size;
326 // Process any range variation
327 if ( range && *(++range) ) {
330 } else if ( RANGE( sscanf( range, "%d:%d%n", &a, &b, &c ), 2 )) {
331 p->from = ( a < 0 )? ( p->to + a ) : a;
332 p->to = ( b < 0 )? ( p->to + b ) : b;
333 } else if ( RANGE( sscanf( range, "%d+%d%n", &a, &b, &c ), 2 )) {
334 p->from = ( a < 0 )? ( p->to + a ) : a;
335 p->to = ( ( b < 0 )? p->to : p->from ) + b;
336 } else if ( RANGE( sscanf( range, "%d+%n", &a, &c ), 1 )) {
337 p->from = ( a < 0 )? ( p->to + a ) : a;
338 } else if ( RANGE( sscanf( range, ":%d%n", &b, &c ), 1 )) {
339 p->to = ( b < 0 )? ( p->to + b ) : b;
340 } else if ( RANGE( sscanf( range, "%d:%n", &a, &c ), 1 )) {
341 p->from = ( a < 0 )? ( p->to + a ) : a;
342 } else if ( RANGE( sscanf( range, "%d%n", &a, &c ), 1 )) {
348 } else if ( RANGE( sscanf( range, ":%n", &c), 0 ) ) {
351 fprintf( stderr, "** BAD RANGE: %s\n", argv[i] );
355 if ( ( filestat.st_mode & S_IFMT ) == S_IFCHR ) {
356 filestat.st_size = p->to; // Pretend size of character device
361 if ( p->to > filestat.st_size ) {
362 p->to = filestat.st_size;
364 if ( p->from >= p->to || p->from >= filestat.st_size ) {
365 fprintf( stderr, "** BAD RANGE: %s [%ld:%ld]\n",
366 argv[i], p->from, p->to );
369 p->start = sources.size; // the fusefile position of fragment
370 sources.size += p->to - p->from;
378 static int fusefile_getattr(const char *path,struct stat *stbuf) {
380 fprintf( stderr, "fusefile_getattr( %s )\n", path );
382 if ( strcmp( path, "/" ) != 0 ) {
386 fprintf( stderr, "getattr %ld\n", sources.size );
388 memset( stbuf, 0, sizeof( struct stat ) );
389 stbuf->st_mode = S_IFREG | 0644; // Hmmm
391 stbuf->st_size = sources.size;
392 stbuf->st_atime = times.atime;
393 stbuf->st_mtime = times.mtime;
394 stbuf->st_ctime = times.ctime;
395 stbuf->st_uid = getuid();
396 stbuf->st_gid = getgid();
400 static int fusefile_chmod(const char *path,mode_t m) {
402 fprintf( stderr, "fusefile_chmod( %s, %d )\n", path, m );
407 static int fusefile_open(const char *path,struct fuse_file_info *fi) {
409 fprintf( stderr, "fusefile_open( %s, %d )\n", path, fi->flags );
410 fprintf( stderr, "fixing( %d )\n", fi->flags | O_CLOEXEC );
412 if ( strcmp( path, "/" ) != 0 ) {
415 // set O-CLOEXEC for this opening?
416 times.atime = time( 0 );
420 static int find_source(off_t offset) {
422 int hi = sources.count;
423 if ( offset >= sources.size ) {
427 fprintf( stderr, "find_source( %ld )\n", offset );
429 while ( lo + 1 < hi ) {
430 int m = ( lo + hi ) / 2;
431 if ( offset < sources.array[ m ].start ) {
433 fprintf( stderr, " offset < [%d].start: %ld\n",
434 m, sources.array[ m ].start );
439 fprintf( stderr, " offset >= [%d].start: %ld\n",
440 m, sources.array[ m ].start );
446 fprintf( stderr, "found %d\n", lo );
451 static int overlay_merge(char *buf,off_t beg,off_t end) {
453 fprintf( stderr, "merge %ld %ld\n", beg, end );
455 // Find nearest overlay data before or at beg
456 ssize_t p = overlay_prior_fragment( beg );
460 for ( ; p < overlay.count && overlay.table[p].beg < end; p++ ) {
461 if ( overlay.table[p].end < beg ) {
464 if ( overlay.table[p].beg > beg ) {
465 size_t delta = overlay.table[p].beg - beg;
469 size_t size = ( overlay.table[p].end <= end )?
470 ( overlay.table[p].end - beg ) : ( end - beg );
471 lseek( overlay.source.fd, beg, SEEK_SET );
473 size_t n = read( overlay.source.fd, buf, size );
482 // Read <size> bytes from <offset> in file
483 static int fusefile_read(const char *path, char *buf, size_t size,
484 off_t off, struct fuse_file_info *fi)
487 fprintf( stderr, "fusefile_read( %s )\n", path );
489 if( strcmp( path, "/" ) != 0 ) {
493 fprintf( stderr, "read %ld %ld\n", off, size );
495 size_t rr = 0; // total reading
498 fprintf( stderr, " find_source %ld %ld\n", off, size );
500 int i = find_source( off );
502 return ( off == sources.size )? rr : -ENOENT;
504 if ( sources.array[i].fd < 0 ) {
508 print_source( &sources.array[i] );
510 times.atime = time( 0 );
511 size_t b = off - sources.array[i].start + sources.array[i].from;
512 size_t n = sources.array[i].to - b;
516 if ( sources.array[i].dirty ) {
517 fsync( sources.array[i].fd );
518 sources.array[i].dirty = 0;
521 fprintf( stderr, " seek fd=%d to %ld\n", sources.array[i].fd, b );
523 if ( lseek( sources.array[i].fd, b, SEEK_SET ) < 0 ) {
524 perror( sources.array[i].filename );
528 fprintf( stderr, " now read %ld from fd=%d\n",
529 n, sources.array[i].fd );
531 ssize_t r = read( sources.array[i].fd, buf + rr, n );
533 fprintf( stderr, " got %ld bytes\n", r );
536 perror( sources.array[i].filename );
542 if ( overlay.source.filename ) {
543 if ( overlay.source.dirty ) {
544 fsync( overlay.source.fd );
545 overlay.source.dirty = 0;
547 int x = overlay_merge( buf + rr, off + rr, off + rr + r );
557 fprintf( stderr, " total reading %ld bytes\n", rr );
563 * Poll for IO readiness.
565 int fusefile_poll(const char *path, struct fuse_file_info *fi,
566 struct fuse_pollhandle *ph, unsigned *reventsp )
569 fprintf( stderr, "fusefile_poll( %s ) %p %d\n", path, ph, *reventsp );
571 if( strcmp( path, "/" ) != 0 ) {
575 return fuse_notify_poll( ph );
580 static void overlay_load() {
581 lseek( overlay.source.fd, overlay.source.to, SEEK_SET );
583 size_t size = sizeof( overlay.count );
584 if ( read( overlay.source.fd, &x, size ) != size ) {
588 fprintf( stderr, "overlay: %s with %ld regions\n",
589 overlay.source.filename, x );
591 struct Region f = { 0, 0 };
592 size = sizeof( struct Region );
594 if ( read( overlay.source.fd, &f, size ) != size ) {
595 fprintf( stderr, "%s: bad meta data\n", overlay.source.filename );
599 fprintf( stderr, "overlay region: %ld %ld\n", f.beg, f.end );
601 overlay_mark( f.beg, f.end );
606 * Write a full block of data over the sources at the offset
608 static int write_block(off_t off,const char *buf,size_t size) {
610 fprintf( stderr, "write_block( %ld, ?, %ld )\n", off, size );
612 if ( overlay.source.filename ) {
613 overlay_mark( off, off + size ); // Mark region as written
616 int index = find_source( off ); // index of source file
618 return -EIO; // past EOF
620 struct Source *source = overlay.source.filename?
621 &overlay.source : &sources.array[ index ];
622 off_t from = off - source->start + source->from;
623 off_t max = source->to - from;
624 if ( lseek( source->fd, from, SEEK_SET ) < 0 ) {
627 ssize_t todo = ( size < max )? size : max;
629 times.mtime = time( 0 );
630 ssize_t n = write( source->fd, buf, todo );
632 return -EIO; // Something wrong
639 if ( source->dirty++ >= 1000 ) {
647 static int fusefile_write_buf(const char *path, struct fuse_bufvec *buf,
648 off_t off, struct fuse_file_info *fi) {
650 fprintf( stderr, "fusefile_write_buf( %s )\n", path );
652 if ( strcmp( path, "/" ) != 0 ) {
658 for ( i = 0; i < buf->count; i++ ) {
659 struct fuse_buf *p = &buf->buf[i];
660 if ( p->flags & FUSE_BUF_IS_FD ) {
662 fprintf( stderr, "Content held in a file ... HELP!!\n" );
666 if ( write_block( off, (char*) p->mem, p->size ) < 0 ) {
672 fprintf( stderr, "fusefile_write_buf written %ld\n", size );
678 * Write a fragment at <off>. This overwrites files.
680 static int fusefile_write(const char *path, const char *buf, size_t size,
681 off_t off, struct fuse_file_info *fi)
684 fprintf( stderr, "fusefile_write( %s %ld )\n", path, size );
686 if ( strcmp( path, "/" ) != 0 ) {
690 if ( write_block( off, buf, size ) < 0 ) {
696 static void fusefile_destroy(void *data) {
697 char *mnt = (char*) data; // As passed to fuse_main
699 fprintf( stderr, "fusefile_destroy( %s )\n", mnt? mnt : "" );
706 static void fsync_all_dirty() {
708 for ( ; i < sources.count; i++ ) {
709 if ( sources.array[i].dirty ) {
710 fsync( sources.array[i].fd );
711 sources.array[i].dirty = 0;
714 if ( overlay.source.filename && overlay.source.dirty ) {
715 fsync( overlay.source.fd );
716 overlay.source.dirty = 0;
720 static int fusefile_flush(const char *path, struct fuse_file_info *info) {
722 fprintf( stderr, "fusefile_flush( %s )\n", path );
724 if ( strcmp( path, "/" ) != 0 ) {
731 static int fusefile_release(const char *path, struct fuse_file_info *fi) {
733 fprintf( stderr, "fusefile_release( %s, %d )\n", path, fi->flags );
735 if ( strcmp( path, "/" ) != 0 ) {
741 static int fusefile_fsync(const char *path, int x, struct fuse_file_info *fi) {
743 fprintf( stderr, "fusefile_fsync( %s, %d )\n", path, x );
745 if ( strcmp( path, "/" ) != 0 ) {
755 static int fusefile_truncate(const char *path, off_t len) {
757 fprintf( stderr, "fusefile_truncate( %s, %ld )\n", path, len );
759 if ( strcmp( path, "/" ) != 0 ) {
765 void *fusefile_init(struct fuse_conn_info *fci) {
767 fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
769 // Disable asynchronous reading
771 fci->want &= ~FUSE_CAP_ASYNC_READ;
773 fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
778 #define ENDSOURCE( S ) ( S.start + ( S.to - S.from ) )
781 * Dump the current fragmentation to stdout.
783 static int dump_fragments() {
787 while ( src < sources.count ) {
788 size_t x = ( oly < overlay.count )?
789 overlay.table[ oly ].beg : sources.size;
790 for ( ; src < sources.count &&
791 ENDSOURCE( sources.array[ src ] ) <= x; src++ ) {
792 // Dump sources.array[src] in full
793 fprintf( stdout, "%s/%ld:%ld\n",
794 sources.array[ src ].filename,
795 pos - sources.array[ src ].start,
796 sources.array[ src ].to );
797 pos = ENDSOURCE( sources.array[ src ] );
799 if ( sources.array[ src ].start < x ) {
800 // Dump sources.array[src] up to x;
801 fprintf( stdout, "%s/%ld:%ld\n",
802 sources.array[ src ].filename,
803 pos - sources.array[ src ].start,
804 x - sources.array[ src ].start );
805 pos = ENDSOURCE( sources.array[ src ] );
807 if ( oly < overlay.count ) {
808 fprintf( stdout, "%s/%ld:%ld\n",
809 overlay.source.filename,
810 overlay.table[ oly ].beg,
811 overlay.table[ oly ].end );
812 pos = overlay.table[ oly++ ].end;
814 for ( ; src < sources.count &&
815 ENDSOURCE( sources.array[ src ] ) <= pos; src++ ) {
816 // Just skip these fragments.
822 static struct fuse_operations fusefile_oper = {
823 .getattr = fusefile_getattr,
824 // NYI .fgetattr = fusefile_fgetattr,
825 .chmod = fusefile_chmod,
826 .open = fusefile_open,
827 .read = fusefile_read,
828 .poll = fusefile_poll,
829 .write = fusefile_write,
830 .write_buf = fusefile_write_buf,
831 .destroy = fusefile_destroy,
832 // NYI .access = fusefile_access,
833 .flush = fusefile_flush,
834 .release = fusefile_release,
835 .fsync = fusefile_fsync,
836 // NYI .ftruncate = fusefile_ftruncate,
837 .truncate = fusefile_truncate,
838 //.truncate = fusefile_truncate,
839 //.release = fusefile_release,
840 .init = fusefile_init,
843 static void usage() {
845 "Usage: fusefile [ <fuse options> ] <mount> <file/from-to> ... \n"
846 "Mounts a virtual, file that is a concatenation of file fragments\n"
848 fprintf( stderr, "%s", usage );
853 * Set up the arguments for the fuse_main call, adding our own.
854 * argv[argc] is the mount point argument
856 static int setup_argv(int argc,char ***argv) {
857 // note: (*argv)[ argc ] is the mount point argument
859 "-odefault_permissions",
862 #define OURSN ( sizeof( OURS ) / sizeof( char* ) )
863 int N = argc + OURSN;
864 // Allocate new arg array plus terminating null pointer
865 char **out = malloc( ( N + 1 ) * sizeof( char* ) );
867 for ( i = 0; i < argc; i++ ) {
868 out[ i ] = (*argv)[i];
869 //fprintf( stderr, " %s", out[ i ] );
871 for ( i = 0; i < OURSN; i++ ) {
872 out[ argc + i ] = OURS[i];
873 //fprintf( stderr, " %s", out[ i ] );
876 //fprintf( stderr, "\n" );
878 return N; // Don't include the terminating null pointer
882 * Mount a concatenation of files,
883 * [ <fuse options> ] <mount> <file/from-to> ...
885 int main(int argc, char *argv[])
895 for ( i = 1; i < argc; i++ ) {
896 if ( *argv[i] != '-' ) {
900 if ( i > argc - 2 ) { // At least mount point plus one source
904 mnt = argv[ i++ ]; // First non-option argument is the mount pount
905 char *overlaytag = "-overlay:";
906 int overlaytagsize = strlen( overlaytag );
907 if ( strncmp( argv[i], overlaytag, overlaytagsize ) == 0 ) {
908 // consume "-overlay:filename"
909 setup_overlay( argv[i++] + overlaytagsize ); // Need a writable file
914 if ( setup_sources( argv, i, argc-i ) ) {
917 if ( overlay.source.filename ) {
918 overlay.source.to = sources.size; // Register total size.
921 if ( stat( mnt, &stbuf ) == -1 ) {
922 int fd = open( mnt, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR );
927 time_t now = time( 0 );
933 } else if ( ! S_ISREG( stbuf.st_mode ) ) {
934 fprintf( stderr, "mountpoint is not a regular file\n" );
937 times.atime = stbuf.st_atime;
938 times.mtime = stbuf.st_mtime;
939 times.ctime = stbuf.st_ctime;
943 int fd = open( mnt, O_RDWR, S_IRUSR | S_IWUSR );
948 if ( lseek( fd, sources.size, SEEK_SET ) < 0 ) {
952 fuseargc = setup_argv( fuseargc, &argv );
953 if ( strcmp( "-dump", argv[ 1 ] ) == 0 ) {
954 return dump_fragments();
956 struct fuse_args args = FUSE_ARGS_INIT( fuseargc, argv );
957 if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {
960 fuse_opt_free_args( &args );
962 fprintf( stderr, "missing mountpoint parameter\n" );
965 return fuse_main( fuseargc, argv, &fusefile_oper, temporary? mnt : NULL );