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>
46 ssize_t start; // starting position in concatenated file
75 #define FRAG(m) (overlay.table+m)
76 #define BEG(m) (FRAG(m)->pos)
77 #define END(m) (FRAG(m)->pos + FRAG(m)->size)
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 BEG( m ) < pos? m : -1;
86 if ( BEG( m ) <= pos ) {
95 static void overlay_save_count() {
96 lseek( overlay.source.fd, overlay.source.to, SEEK_SET );
97 size_t size = sizeof( overlay.count );
98 char *p = (char *) &overlay.count ;
100 size_t n = write( overlay.source.fd, p, size );
102 perror( overlay.source.filename );
108 if ( overlay.source.dirty++ > 1000 ) {
109 fsync( overlay.source.fd );
110 overlay.source.dirty = 0;
114 static void overlay_save_table(size_t lo,size_t hi) {
115 char *p = (char *) FRAG(lo);
116 size_t pos = overlay.source.to + sizeof( overlay.count ) +
117 lo * sizeof( struct Region );
118 size_t size = ( hi - lo ) * sizeof( struct Region );
119 if ( pos != lseek( overlay.source.fd, pos, SEEK_SET ) ) {
120 fprintf( stderr, "%s: seek error\n", overlay.source.filename );
124 size_t n = write( overlay.source.fd, p, size );
126 perror( overlay.source.filename );
132 if ( overlay.source.dirty++ > 1000 ) {
133 fsync( overlay.source.fd );
134 overlay.source.dirty = 0;
138 static void overlay_insert(size_t p,off_t pos,size_t size) {
140 if ( overlay.count >= overlay.limit ) {
141 overlay.limit = overlay.count + 10;
142 bytes = overlay.limit * sizeof( struct Region );
143 overlay.table = overlay.table?
144 realloc( overlay.table, bytes ) : malloc( bytes );
146 bytes = ( overlay.count++ - p ) * sizeof( struct Region );
148 memmove( FRAG( p+1 ), FRAG( p ), bytes );
150 FRAG( p )->pos = pos;
151 FRAG( p )->size = size;
152 overlay_save_count();
155 static void overlay_delete(size_t p) {
156 if ( p < --overlay.count ) {
157 size_t size = ( overlay.count - p ) * sizeof( struct Region );
158 memmove( FRAG(p), FRAG(p+1), size );
160 overlay_save_count();
163 static void overlay_mark(off_t pos,size_t size) {
165 fprintf( stderr, "overlay_mark( %ld, %ld )\n", pos, size );
169 ssize_t p = overlay_prior_fragment( pos );
170 // p is the nearest region below pos (or -1)
171 if ( p >= 0 && pos <= END(p) ) {
172 // p overlaps mark region
173 if ( END(p) >= pos + size ) {
175 fprintf( stderr, "overlay size 1( %ld )\n", FRAG(p)->size );
177 return; // new mark within existing.
179 // new mark region extends existing
180 FRAG(p)->size = pos + size - BEG(p);
182 while ( q < overlay.count && BEG(q) <= END(p) ) {
183 if ( END(q) > END(p) ) {
184 FRAG(p)->size = END(q) - BEG(p);
189 overlay_save_table( p, deleted? overlay.count : q );
191 fprintf( stderr, "overlay size 2( %ld ) deleted %d\n",
192 FRAG(p)->size, deleted );
196 // The region p does not expand into new mark region
197 p++; // subsequent region
198 if ( p >= overlay.count || BEG(p) > pos + size ) {
199 // New mark is separate region at p
200 overlay_insert( p, pos, size);
202 fprintf( stderr, "overlay size 4( %ld )\n", FRAG(p)->size );
204 overlay_save_table( p, overlay.count );
207 // New marks start before and overlap with the region
208 if ( BEG(p) + FRAG(p)->size < pos + size ) {
209 FRAG(p)->size = size; // new mark covers old region
211 FRAG(p)->size += BEG(p) - pos;
215 while ( q < overlay.count && BEG(q) <= END(p) ) {
216 if ( END(q) > END(p) ) {
217 FRAG(p)->size = END(q) - BEG(p);
222 overlay_save_table( p, deleted? overlay.count : q );
224 fprintf( stderr, "overlay size 4( %ld ) deleted %d\n",
225 FRAG(p)->size, deleted );
229 static void setup_overlay(char *filename) {
230 overlay.source.filename = filename;
231 overlay.source.fd = open( filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR );
232 if ( overlay.source.fd < 0 ) {
239 static void print_source(struct Source *p) {
240 fprintf( stderr, "%p { %s, %ld, %ld, %ld, %d }\n",
241 p, p->filename, p->from, p->to, p->start, p-> fd );
246 static unsigned int c;
247 static int RANGE(int s,int n ) {
248 return ( s == n ) && *(range+c) == 0;
251 static int setup_sources(char **argv,int i,int n) {
252 sources.array = calloc( n, sizeof( struct Source ) );
253 if ( sources.array == 0 ) {
259 for ( ; j < n; i++, j++ ) {
260 struct stat filestat;
261 struct Source *p = sources.array + j;
262 // Open the fragment file rw if possible, else ro
263 range = strrchr( argv[i], '/' ); // last '/'
264 p->filename = range? strndup( argv[i], range - argv[i] ) : argv[i];
265 p->fd = open( p->filename, O_RDWR );
269 p->fd = open( p->filename, O_RDONLY );
272 perror( p->filename );
273 return 1; // Error return
275 if ( stat( p->filename, &filestat ) ) {
276 perror( p->filename );
280 fprintf( stderr, "** %s opened read-only\n", p->filename );
283 p->to = filestat.st_size;
284 // Process any range variation
285 if ( range && *(++range) ) {
288 } else if ( RANGE( sscanf( range, "%d:%d%n", &a, &b, &c ), 2 )) {
289 p->from = ( a < 0 )? ( p->to + a ) : a;
290 p->to = ( b < 0 )? ( p->to + b ) : b;
291 } else if ( RANGE( sscanf( range, "%d+%d%n", &a, &b, &c ), 2 )) {
292 p->from = ( a < 0 )? ( p->to + a ) : a;
293 p->to = ( ( b < 0 )? p->to : p->from ) + b;
294 } else if ( RANGE( sscanf( range, "%d+%n", &a, &c ), 1 )) {
295 p->from = ( a < 0 )? ( p->to + a ) : a;
296 } else if ( RANGE( sscanf( range, ":%d%n", &b, &c ), 1 )) {
297 p->to = ( b < 0 )? ( p->to + b ) : b;
298 } else if ( RANGE( sscanf( range, "%d:%n", &a, &c ), 1 )) {
299 p->from = ( a < 0 )? ( p->to + a ) : a;
300 } else if ( RANGE( sscanf( range, "%d%n", &a, &c ), 1 )) {
306 } else if ( RANGE( sscanf( range, ":%n", &c), 0 ) ) {
309 fprintf( stderr, "** BAD RANGE: %s\n", argv[i] );
313 if ( ( filestat.st_mode & S_IFMT ) == S_IFCHR ) {
314 filestat.st_size = p->to; // Pretend size of character device
319 if ( p->to > filestat.st_size ) {
320 p->to = filestat.st_size;
322 if ( p->from >= p->to || p->from >= filestat.st_size ) {
323 fprintf( stderr, "** BAD RANGE: %s [%ld:%ld]\n",
324 argv[i], p->from, p->to );
327 p->start = sources.size; // the fusefile position of fragment
328 sources.size += p->to - p->from;
336 static int fusefile_getattr(const char *path,struct stat *stbuf) {
338 fprintf( stderr, "fusefile_getattr( %s )\n", path );
340 if ( strcmp( path, "/" ) != 0 ) {
344 fprintf( stderr, "getattr %ld\n", sources.size );
346 memset( stbuf, 0, sizeof( struct stat ) );
347 stbuf->st_mode = S_IFREG | 0644; // Hmmm
349 stbuf->st_size = sources.size;
350 stbuf->st_atime = times.atime;
351 stbuf->st_mtime = times.mtime;
352 stbuf->st_ctime = times.ctime;
353 stbuf->st_uid = getuid();
354 stbuf->st_gid = getgid();
358 static int fusefile_chmod(const char *path,mode_t m) {
360 fprintf( stderr, "fusefile_chmod( %s, %d )\n", path, m );
365 static int fusefile_open(const char *path,struct fuse_file_info *fi) {
367 fprintf( stderr, "fusefile_open( %s, %d )\n", path, fi->flags );
368 fprintf( stderr, "fixing( %d )\n", fi->flags | O_CLOEXEC );
370 if ( strcmp( path, "/" ) != 0 ) {
373 // set O-CLOEXEC for this opening?
374 times.atime = time( 0 );
378 static int find_source(off_t offset) {
380 int hi = sources.count;
381 if ( offset >= sources.size ) {
385 fprintf( stderr, "find_source( %ld )\n", offset );
387 while ( lo + 1 < hi ) {
388 int m = ( lo + hi ) / 2;
389 if ( offset < sources.array[ m ].start ) {
391 fprintf( stderr, " offset < [%d].start: %ld\n",
392 m, sources.array[ m ].start );
397 fprintf( stderr, " offset >= [%d].start: %ld\n",
398 m, sources.array[ m ].start );
404 fprintf( stderr, "found %d\n", lo );
409 static int overlay_merge(char *buf,off_t off,size_t size) {
411 fprintf( stderr, "merge %ld %ld\n", off, size );
413 // Find nearest overlay data before or at off
414 ssize_t p = overlay_prior_fragment( off );
418 for ( ; p < overlay.count && BEG(p) < off+size; p++ ) {
419 if ( END(p) < off ) {
422 size_t delta = FRAG(p)->size;
423 if ( BEG(p) < off ) {
424 delta -= off - BEG(p);
426 size_t skip = BEG(p) - off;
431 if ( delta > size ) {
434 lseek( overlay.source.fd, off, SEEK_SET );
435 while ( delta > 0 ) {
436 size_t n = read( overlay.source.fd, buf, delta );
444 fprintf( stderr, "merged\n" );
449 // Read <size> bytes from <offset> in file
450 static int fusefile_read(const char *path, char *buf, size_t size,
451 off_t off, struct fuse_file_info *fi)
454 fprintf( stderr, "fusefile_read( %s )\n", path );
456 if( strcmp( path, "/" ) != 0 ) {
460 fprintf( stderr, "read %ld %ld\n", off, size );
462 size_t rr = 0; // total reading
465 fprintf( stderr, " find_source %ld %ld\n", off, size );
467 int i = find_source( off );
469 return ( off == sources.size )? rr : -ENOENT;
471 if ( sources.array[i].fd < 0 ) {
475 print_source( &sources.array[i] );
477 times.atime = time( 0 );
478 size_t b = off - sources.array[i].start + sources.array[i].from;
479 size_t n = sources.array[i].to - b;
483 if ( sources.array[i].dirty ) {
484 fsync( sources.array[i].fd );
485 sources.array[i].dirty = 0;
488 fprintf( stderr, " seek fd=%d to %ld\n", sources.array[i].fd, b );
490 if ( lseek( sources.array[i].fd, b, SEEK_SET ) < 0 ) {
491 perror( sources.array[i].filename );
495 fprintf( stderr, " now read %ld from fd=%d\n",
496 n, sources.array[i].fd );
498 ssize_t r = read( sources.array[i].fd, buf + rr, n );
500 fprintf( stderr, " got %ld bytes\n", r );
503 perror( sources.array[i].filename );
509 if ( overlay.source.filename ) {
510 if ( overlay.source.dirty ) {
511 fsync( overlay.source.fd );
512 overlay.source.dirty = 0;
514 int x = overlay_merge( buf + rr, off + rr, r );
524 fprintf( stderr, " total reading %ld bytes\n", rr );
530 * Poll for IO readiness.
532 int fusefile_poll(const char *path, struct fuse_file_info *fi,
533 struct fuse_pollhandle *ph, unsigned *reventsp )
536 fprintf( stderr, "fusefile_poll( %s ) %p %d\n", path, ph, *reventsp );
538 if( strcmp( path, "/" ) != 0 ) {
542 return fuse_notify_poll( ph );
547 static void overlay_load() {
548 lseek( overlay.source.fd, overlay.source.to, SEEK_SET );
550 size_t size = sizeof( overlay.count );
551 if ( read( overlay.source.fd, &x, size ) != size ) {
555 fprintf( stderr, "overlay: %s with %ld regions\n",
556 overlay.source.filename, x );
558 struct Region f = { 0, 0 };
559 size = sizeof( struct Region );
561 if ( read( overlay.source.fd, &f, size ) != size ) {
562 fprintf( stderr, "%s: bad meta data\n", overlay.source.filename );
566 fprintf( stderr, "overlay region: %ld %ld\n", f.pos, f.size );
568 overlay_mark( f.pos, f.size );
573 * Write a full block of data over the sources at the offset
575 static int write_block(off_t off,const char *buf,size_t size) {
577 fprintf( stderr, "write_block( %ld, ?, %ld )\n", off, size );
579 if ( overlay.source.filename ) {
580 overlay_mark( off, size ); // Mark region as written
583 int index = find_source( off ); // index of source file
585 return -EIO; // past EOF
587 struct Source *source = overlay.source.filename?
588 &overlay.source : &sources.array[ index ];
589 off_t from = off - source->start + source->from;
590 off_t max = source->to - from;
591 if ( lseek( source->fd, from, SEEK_SET ) < 0 ) {
594 ssize_t todo = ( size < max )? size : max;
596 times.mtime = time( 0 );
597 ssize_t n = write( source->fd, buf, todo );
599 return -EIO; // Something wrong
606 if ( source->dirty++ >= 1000 ) {
614 static int fusefile_write_buf(const char *path, struct fuse_bufvec *buf,
615 off_t off, struct fuse_file_info *fi) {
617 fprintf( stderr, "fusefile_write_buf( %s )\n", path );
619 if ( strcmp( path, "/" ) != 0 ) {
625 for ( i = 0; i < buf->count; i++ ) {
626 struct fuse_buf *p = &buf->buf[i];
627 if ( p->flags & FUSE_BUF_IS_FD ) {
629 fprintf( stderr, "Content held in a file ... HELP!!\n" );
633 if ( write_block( off, (char*) p->mem, p->size ) < 0 ) {
639 fprintf( stderr, "fusefile_write_buf written %ld\n", size );
645 * Write a fragment at <off>. This overwrites files.
647 static int fusefile_write(const char *path, const char *buf, size_t size,
648 off_t off, struct fuse_file_info *fi)
651 fprintf( stderr, "fusefile_write( %s %ld )\n", path, size );
653 if ( strcmp( path, "/" ) != 0 ) {
657 if ( write_block( off, buf, size ) < 0 ) {
663 static void fusefile_destroy(void *data) {
664 char *mnt = (char*) data; // As passed to fuse_main
666 fprintf( stderr, "fusefile_destroy( %s )\n", mnt? mnt : "" );
673 static void fsync_all_dirty() {
675 for ( ; i < sources.count; i++ ) {
676 if ( sources.array[i].dirty ) {
677 fsync( sources.array[i].fd );
678 sources.array[i].dirty = 0;
681 if ( overlay.source.filename && overlay.source.dirty ) {
682 fsync( overlay.source.fd );
683 overlay.source.dirty = 0;
687 static int fusefile_flush(const char *path, struct fuse_file_info *info) {
689 fprintf( stderr, "fusefile_flush( %s )\n", path );
691 if ( strcmp( path, "/" ) != 0 ) {
698 static int fusefile_release(const char *path, struct fuse_file_info *fi) {
700 fprintf( stderr, "fusefile_release( %s, %d )\n", path, fi->flags );
702 if ( strcmp( path, "/" ) != 0 ) {
708 static int fusefile_fsync(const char *path, int x, struct fuse_file_info *fi) {
710 fprintf( stderr, "fusefile_fsync( %s, %d )\n", path, x );
712 if ( strcmp( path, "/" ) != 0 ) {
722 static int fusefile_truncate(const char *path, off_t len) {
724 fprintf( stderr, "fusefile_truncate( %s, %ld )\n", path, len );
726 if ( strcmp( path, "/" ) != 0 ) {
732 void *fusefile_init(struct fuse_conn_info *fci) {
734 fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
736 // Disable asynchronous reading
738 fci->want &= ~FUSE_CAP_ASYNC_READ;
740 fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
745 static struct fuse_operations fusefile_oper = {
746 .getattr = fusefile_getattr,
747 .chmod = fusefile_chmod,
748 .open = fusefile_open,
749 .read = fusefile_read,
750 .poll = fusefile_poll,
751 .write = fusefile_write,
752 .write_buf = fusefile_write_buf,
753 .destroy = fusefile_destroy,
754 .flush = fusefile_flush,
755 .release = fusefile_release,
756 .fsync = fusefile_fsync,
757 .truncate = fusefile_truncate,
758 //.truncate = fusefile_truncate,
759 //.release = fusefile_release,
760 .init = fusefile_init,
763 static void usage() {
765 "Usage: fusefile [ <fuse options> ] <mount> <file/from-to> ... \n"
766 "Mounts a virtual, file that is a concatenation of file fragments\n"
768 fprintf( stderr, "%s", usage );
773 * Set up the arguments for the fuse_main call, adding our own.
774 * argv[argc] is the mount point argument
776 static int setup_argv(int argc,char ***argv) {
777 // note: (*argv)[ argc ] is the mount point argument
779 "-odefault_permissions",
782 #define OURSN ( sizeof( OURS ) / sizeof( char* ) )
783 int N = argc + OURSN;
784 // Allocate new arg array plus terminating null pointer
785 char **out = malloc( ( N + 1 ) * sizeof( char* ) );
787 for ( i = 0; i < argc; i++ ) {
788 out[ i ] = (*argv)[i];
789 //fprintf( stderr, " %s", out[ i ] );
791 for ( i = 0; i < OURSN; i++ ) {
792 out[ argc + i ] = OURS[i];
793 //fprintf( stderr, " %s", out[ i ] );
796 //fprintf( stderr, "\n" );
798 return N; // Don't include the terminating null pointer
802 * Mount a concatenation of files,
803 * [ <fuse options> ] <mount> <file/from-to> ...
805 int main(int argc, char *argv[])
815 for ( i = 1; i < argc; i++ ) {
816 if ( *argv[i] != '-' ) {
820 if ( i > argc - 2 ) { // At least mount point plus one source
824 mnt = argv[ i++ ]; // First non-option argument is the mount pount
825 char *overlaytag = "-overlay:";
826 int overlaytagsize = strlen( overlaytag );
827 if ( strncmp( argv[i], overlaytag, overlaytagsize ) == 0 ) {
828 // consume "-overlay:filename"
829 setup_overlay( argv[i++] + overlaytagsize ); // Need a writable file
834 if ( setup_sources( argv, i, argc-i ) ) {
837 if ( overlay.source.filename ) {
838 overlay.source.to = sources.size; // Register total size.
841 if ( stat( mnt, &stbuf ) == -1 ) {
842 int fd = open( mnt, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR );
847 time_t now = time( 0 );
853 } else if ( ! S_ISREG( stbuf.st_mode ) ) {
854 fprintf( stderr, "mountpoint is not a regular file\n" );
857 times.atime = stbuf.st_atime;
858 times.mtime = stbuf.st_mtime;
859 times.ctime = stbuf.st_ctime;
863 int fd = open( mnt, O_RDWR, S_IRUSR | S_IWUSR );
868 if ( lseek( fd, sources.size, SEEK_SET ) < 0 ) {
872 fuseargc = setup_argv( fuseargc, &argv );
873 struct fuse_args args = FUSE_ARGS_INIT( fuseargc, argv );
874 if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {
877 fuse_opt_free_args( &args );
879 fprintf( stderr, "missing mountpoint parameter\n" );
882 return fuse_main( fuseargc, argv, &fusefile_oper, temporary? mnt : NULL );