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_source(struct Source *p,char *frag) {
294 struct stat filestat;
295 // Open the fragment file rw if possible, else ro
296 range = strrchr( frag, '/' ); // last '/'
297 p->filename = range? strndup( frag, range - frag ) : frag;
298 p->fd = open( p->filename, O_RDWR );
302 p->fd = open( p->filename, O_RDONLY );
305 perror( p->filename );
306 return 1; // Error return
308 if ( stat( p->filename, &filestat ) ) {
309 perror( p->filename );
313 fprintf( stderr, "** %s opened read-only\n", p->filename );
316 p->to = filestat.st_size;
317 // Process any range variation
318 if ( range && *(++range) ) {
321 } else if ( RANGE( sscanf( range, "%d:%d%n", &a, &b, &c ), 2 )) {
322 p->from = ( a < 0 )? ( p->to + a ) : a;
323 p->to = ( b < 0 )? ( p->to + b ) : b;
324 } else if ( RANGE( sscanf( range, "%d+%d%n", &a, &b, &c ), 2 )) {
325 p->from = ( a < 0 )? ( p->to + a ) : a;
326 p->to = ( ( b < 0 )? p->to : p->from ) + b;
327 } else if ( RANGE( sscanf( range, "%d+%n", &a, &c ), 1 )) {
328 p->from = ( a < 0 )? ( p->to + a ) : a;
329 } else if ( RANGE( sscanf( range, ":%d%n", &b, &c ), 1 )) {
330 p->to = ( b < 0 )? ( p->to + b ) : b;
331 } else if ( RANGE( sscanf( range, "%d:%n", &a, &c ), 1 )) {
332 p->from = ( a < 0 )? ( p->to + a ) : a;
333 } else if ( RANGE( sscanf( range, "%d%n", &a, &c ), 1 )) {
339 } else if ( RANGE( sscanf( range, ":%n", &c), 0 ) ) {
342 fprintf( stderr, "** BAD RANGE: %s\n", frag );
346 if ( ( filestat.st_mode & S_IFMT ) == S_IFCHR ) {
347 filestat.st_size = p->to; // Pretend size of character device
352 if ( p->to > filestat.st_size ) {
353 p->to = filestat.st_size;
355 if ( p->from >= p->to || p->from >= filestat.st_size ) {
356 fprintf( stderr, "** BAD RANGE: %s [%ld:%ld]\n",
357 frag, p->from, p->to );
360 p->start = sources.size; // the fusefile position of fragment
361 sources.size += p->to - p->from;
365 static int setup_sources(char **argv,int i,int n) {
366 sources.array = calloc( n, sizeof( struct Source ) );
367 if ( sources.array == 0 ) {
373 for ( ; j < n; i++, j++ ) {
374 struct Source *p = sources.array + j;
375 if ( setup_source( p, argv[i] ) ) {
378 p->start = sources.size; // the fusefile position of fragment
379 sources.size += p->to - p->from;
387 static int fusefile_getattr(const char *path,struct stat *stbuf) {
389 fprintf( stderr, "fusefile_getattr( %s )\n", path );
391 if ( strcmp( path, "/" ) != 0 ) {
395 fprintf( stderr, "getattr %ld\n", sources.size );
397 memset( stbuf, 0, sizeof( struct stat ) );
398 stbuf->st_mode = S_IFREG | 0644; // Hmmm
400 stbuf->st_size = sources.size;
401 stbuf->st_atime = times.atime;
402 stbuf->st_mtime = times.mtime;
403 stbuf->st_ctime = times.ctime;
404 stbuf->st_uid = getuid();
405 stbuf->st_gid = getgid();
409 static int fusefile_chmod(const char *path,mode_t m) {
411 fprintf( stderr, "fusefile_chmod( %s, %d )\n", path, m );
416 static int fusefile_open(const char *path,struct fuse_file_info *fi) {
418 fprintf( stderr, "fusefile_open( %s, %d )\n", path, fi->flags );
419 fprintf( stderr, "fixing( %d )\n", fi->flags | O_CLOEXEC );
421 if ( strcmp( path, "/" ) != 0 ) {
424 // set O-CLOEXEC for this opening?
425 times.atime = time( 0 );
429 static int find_source(off_t offset) {
431 int hi = sources.count;
432 if ( offset >= sources.size ) {
436 fprintf( stderr, "find_source( %ld )\n", offset );
438 while ( lo + 1 < hi ) {
439 int m = ( lo + hi ) / 2;
440 if ( offset < sources.array[ m ].start ) {
442 fprintf( stderr, " offset < [%d].start: %ld\n",
443 m, sources.array[ m ].start );
448 fprintf( stderr, " offset >= [%d].start: %ld\n",
449 m, sources.array[ m ].start );
455 fprintf( stderr, "found %d\n", lo );
460 static int overlay_merge(char *buf,off_t beg,off_t end) {
462 fprintf( stderr, "merge %ld %ld\n", beg, end );
464 // Find nearest overlay data before or at beg
465 ssize_t p = overlay_prior_fragment( beg );
469 for ( ; p < overlay.count && overlay.table[p].beg < end; p++ ) {
470 if ( overlay.table[p].end < beg ) {
473 if ( overlay.table[p].beg > beg ) {
474 size_t delta = overlay.table[p].beg - beg;
478 size_t size = ( overlay.table[p].end <= end )?
479 ( overlay.table[p].end - beg ) : ( end - beg );
480 lseek( overlay.source.fd, beg, SEEK_SET );
482 size_t n = read( overlay.source.fd, buf, size );
491 // Read <size> bytes from <offset> in file
492 static int fusefile_read(const char *path, char *buf, size_t size,
493 off_t off, struct fuse_file_info *fi)
496 fprintf( stderr, "fusefile_read( %s )\n", path );
498 if( strcmp( path, "/" ) != 0 ) {
502 fprintf( stderr, "read %ld %ld\n", off, size );
504 size_t rr = 0; // total reading
507 fprintf( stderr, " find_source %ld %ld\n", off, size );
509 int i = find_source( off );
511 return ( off == sources.size )? rr : -ENOENT;
513 if ( sources.array[i].fd < 0 ) {
517 print_source( &sources.array[i] );
519 times.atime = time( 0 );
520 size_t b = off - sources.array[i].start + sources.array[i].from;
521 size_t n = sources.array[i].to - b;
525 if ( sources.array[i].dirty ) {
526 fsync( sources.array[i].fd );
527 sources.array[i].dirty = 0;
530 fprintf( stderr, " seek fd=%d to %ld\n", sources.array[i].fd, b );
532 if ( lseek( sources.array[i].fd, b, SEEK_SET ) < 0 ) {
533 perror( sources.array[i].filename );
537 fprintf( stderr, " now read %ld from fd=%d\n",
538 n, sources.array[i].fd );
540 ssize_t r = read( sources.array[i].fd, buf + rr, n );
542 fprintf( stderr, " got %ld bytes\n", r );
545 perror( sources.array[i].filename );
551 if ( overlay.source.filename ) {
552 if ( overlay.source.dirty ) {
553 fsync( overlay.source.fd );
554 overlay.source.dirty = 0;
556 int x = overlay_merge( buf + rr, off + rr, off + rr + r );
566 fprintf( stderr, " total reading %ld bytes\n", rr );
572 * Poll for IO readiness.
574 int fusefile_poll(const char *path, struct fuse_file_info *fi,
575 struct fuse_pollhandle *ph, unsigned *reventsp )
578 fprintf( stderr, "fusefile_poll( %s ) %p %d\n", path, ph, *reventsp );
580 if( strcmp( path, "/" ) != 0 ) {
584 return fuse_notify_poll( ph );
589 static void overlay_load() {
590 lseek( overlay.source.fd, overlay.source.to, SEEK_SET );
592 size_t size = sizeof( overlay.count );
593 if ( read( overlay.source.fd, &x, size ) != size ) {
597 fprintf( stderr, "overlay: %s with %ld regions\n",
598 overlay.source.filename, x );
600 struct Region f = { 0, 0 };
601 size = sizeof( struct Region );
603 if ( read( overlay.source.fd, &f, size ) != size ) {
604 fprintf( stderr, "%s: bad meta data\n", overlay.source.filename );
608 fprintf( stderr, "overlay region: %ld %ld\n", f.beg, f.end );
610 overlay_mark( f.beg, f.end );
615 * Write a full block of data over the sources at the offset
617 static int write_block(off_t off,const char *buf,size_t size) {
619 fprintf( stderr, "write_block( %ld, ?, %ld )\n", off, size );
621 if ( overlay.source.filename ) {
622 overlay_mark( off, off + size ); // Mark region as written
625 int index = find_source( off ); // index of source file
627 return -EIO; // past EOF
629 struct Source *source = overlay.source.filename?
630 &overlay.source : &sources.array[ index ];
631 off_t from = off - source->start + source->from;
632 off_t max = source->to - from;
633 if ( lseek( source->fd, from, SEEK_SET ) < 0 ) {
636 ssize_t todo = ( size < max )? size : max;
638 times.mtime = time( 0 );
639 ssize_t n = write( source->fd, buf, todo );
641 return -EIO; // Something wrong
648 if ( source->dirty++ >= 1000 ) {
656 static int fusefile_write_buf(const char *path, struct fuse_bufvec *buf,
657 off_t off, struct fuse_file_info *fi) {
659 fprintf( stderr, "fusefile_write_buf( %s )\n", path );
661 if ( strcmp( path, "/" ) != 0 ) {
667 for ( i = 0; i < buf->count; i++ ) {
668 struct fuse_buf *p = &buf->buf[i];
669 if ( p->flags & FUSE_BUF_IS_FD ) {
671 fprintf( stderr, "Content held in a file ... HELP!!\n" );
675 if ( write_block( off, (char*) p->mem, p->size ) < 0 ) {
681 fprintf( stderr, "fusefile_write_buf written %ld\n", size );
687 * Write a fragment at <off>. This overwrites files.
689 static int fusefile_write(const char *path, const char *buf, size_t size,
690 off_t off, struct fuse_file_info *fi)
693 fprintf( stderr, "fusefile_write( %s %ld )\n", path, size );
695 if ( strcmp( path, "/" ) != 0 ) {
699 if ( write_block( off, buf, size ) < 0 ) {
705 static void fusefile_destroy(void *data) {
706 char *mnt = (char*) data; // As passed to fuse_main
708 fprintf( stderr, "fusefile_destroy( %s )\n", mnt? mnt : "" );
715 static void fsync_all_dirty() {
717 for ( ; i < sources.count; i++ ) {
718 if ( sources.array[i].dirty ) {
719 fsync( sources.array[i].fd );
720 sources.array[i].dirty = 0;
723 if ( overlay.source.filename && overlay.source.dirty ) {
724 fsync( overlay.source.fd );
725 overlay.source.dirty = 0;
729 static int fusefile_flush(const char *path, struct fuse_file_info *info) {
731 fprintf( stderr, "fusefile_flush( %s )\n", path );
733 if ( strcmp( path, "/" ) != 0 ) {
740 static int fusefile_release(const char *path, struct fuse_file_info *fi) {
742 fprintf( stderr, "fusefile_release( %s, %d )\n", path, fi->flags );
744 if ( strcmp( path, "/" ) != 0 ) {
750 static int fusefile_fsync(const char *path, int x, struct fuse_file_info *fi) {
752 fprintf( stderr, "fusefile_fsync( %s, %d )\n", path, x );
754 if ( strcmp( path, "/" ) != 0 ) {
764 static int fusefile_truncate(const char *path, off_t len) {
766 fprintf( stderr, "fusefile_truncate( %s, %ld )\n", path, len );
768 if ( strcmp( path, "/" ) != 0 ) {
774 void *fusefile_init(struct fuse_conn_info *fci) {
776 fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
778 // Disable asynchronous reading
780 fci->want &= ~FUSE_CAP_ASYNC_READ;
782 fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
787 #define ENDSOURCE( S ) ( S.start + ( S.to - S.from ) )
790 * Dump the current fragmentation to stdout.
792 static int dump_fragments() {
796 while ( src < sources.count ) {
797 size_t x = ( oly < overlay.count )?
798 overlay.table[ oly ].beg : sources.size;
799 for ( ; src < sources.count &&
800 ENDSOURCE( sources.array[ src ] ) <= x; src++ ) {
801 // Dump sources.array[src] in full
802 fprintf( stdout, "%s/%ld:%ld\n",
803 sources.array[ src ].filename,
804 pos - sources.array[ src ].start,
805 sources.array[ src ].to );
806 pos = ENDSOURCE( sources.array[ src ] );
808 if ( sources.array[ src ].start < x ) {
809 // Dump sources.array[src] up to x;
810 fprintf( stdout, "%s/%ld:%ld\n",
811 sources.array[ src ].filename,
812 pos - sources.array[ src ].start,
813 x - sources.array[ src ].start );
814 pos = ENDSOURCE( sources.array[ src ] );
816 if ( oly < overlay.count ) {
817 fprintf( stdout, "%s/%ld:%ld\n",
818 overlay.source.filename,
819 overlay.table[ oly ].beg,
820 overlay.table[ oly ].end );
821 pos = overlay.table[ oly++ ].end;
823 for ( ; src < sources.count &&
824 ENDSOURCE( sources.array[ src ] ) <= pos; src++ ) {
825 // Just skip these fragments.
831 static struct fuse_operations fusefile_oper = {
832 .getattr = fusefile_getattr,
833 // NYI .fgetattr = fusefile_fgetattr,
834 .chmod = fusefile_chmod,
835 .open = fusefile_open,
836 .read = fusefile_read,
837 .poll = fusefile_poll,
838 .write = fusefile_write,
839 .write_buf = fusefile_write_buf,
840 .destroy = fusefile_destroy,
841 // NYI .access = fusefile_access,
842 .flush = fusefile_flush,
843 .release = fusefile_release,
844 .fsync = fusefile_fsync,
845 // NYI .ftruncate = fusefile_ftruncate,
846 .truncate = fusefile_truncate,
847 //.truncate = fusefile_truncate,
848 //.release = fusefile_release,
849 .init = fusefile_init,
852 static void usage() {
854 "Usage: fusefile [ <fuse options> ] <mount> <file/from-to> ... \n"
855 "Mounts a virtual, file that is a concatenation of file fragments\n"
857 fprintf( stderr, "%s", usage );
862 * Set up the arguments for the fuse_main call, adding our own.
863 * argv[argc] is the mount point argument
865 static int setup_argv(int argc,char ***argv) {
866 // note: (*argv)[ argc ] is the mount point argument
868 "-odefault_permissions",
871 #define OURSN ( sizeof( OURS ) / sizeof( char* ) )
872 int N = argc + OURSN;
873 // Allocate new arg array plus terminating null pointer
874 char **out = malloc( ( N + 1 ) * sizeof( char* ) );
876 for ( i = 0; i < argc; i++ ) {
877 out[ i ] = (*argv)[i];
878 //fprintf( stderr, " %s", out[ i ] );
880 for ( i = 0; i < OURSN; i++ ) {
881 out[ argc + i ] = OURS[i];
882 //fprintf( stderr, " %s", out[ i ] );
885 //fprintf( stderr, "\n" );
887 return N; // Don't include the terminating null pointer
891 * Mount a concatenation of files,
892 * [ <fuse options> ] <mount> <file/from-to> ...
894 int main(int argc, char *argv[])
904 for ( i = 1; i < argc; i++ ) {
905 if ( *argv[i] != '-' ) {
909 if ( i > argc - 2 ) { // At least mount point plus one source
913 mnt = argv[ i++ ]; // First non-option argument is the mount pount
914 char *overlaytag = "-overlay:";
915 int overlaytagsize = strlen( overlaytag );
916 if ( strncmp( argv[i], overlaytag, overlaytagsize ) == 0 ) {
917 // consume "-overlay:filename"
918 setup_overlay( argv[i++] + overlaytagsize ); // Need a writable file
923 if ( setup_sources( argv, i, argc-i ) ) {
926 if ( overlay.source.filename ) {
927 overlay.source.to = sources.size; // Register total size.
930 if ( stat( mnt, &stbuf ) == -1 ) {
931 int fd = open( mnt, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR );
936 time_t now = time( 0 );
942 } else if ( ! S_ISREG( stbuf.st_mode ) ) {
943 fprintf( stderr, "mountpoint is not a regular file\n" );
946 times.atime = stbuf.st_atime;
947 times.mtime = stbuf.st_mtime;
948 times.ctime = stbuf.st_ctime;
952 int fd = open( mnt, O_RDWR, S_IRUSR | S_IWUSR );
957 if ( lseek( fd, sources.size, SEEK_SET ) < 0 ) {
961 fuseargc = setup_argv( fuseargc, &argv );
962 if ( strcmp( "-dump", argv[ 1 ] ) == 0 ) {
963 return dump_fragments();
965 struct fuse_args args = FUSE_ARGS_INIT( fuseargc, argv );
966 if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {
969 fuse_opt_free_args( &args );
971 fprintf( stderr, "missing mountpoint parameter\n" );
974 return fuse_main( fuseargc, argv, &fusefile_oper, temporary? mnt : NULL );