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 size_t delta = FRAG(p)->size;
420 if ( BEG(p) > off ) {
421 size_t skip = BEG(p) - off;
426 delta = off - BEG(p);
428 if ( delta > size ) {
431 lseek( overlay.source.fd, off, SEEK_SET );
432 while ( delta > 0 ) {
433 size_t n = read( overlay.source.fd, buf, delta );
441 fprintf( stderr, "merged\n" );
446 // Read <size> bytes from <offset> in file
447 static int fusefile_read(const char *path, char *buf, size_t size,
448 off_t off, struct fuse_file_info *fi)
451 fprintf( stderr, "fusefile_read( %s )\n", path );
453 if( strcmp( path, "/" ) != 0 ) {
457 fprintf( stderr, "read %ld %ld\n", off, size );
459 size_t rr = 0; // total reading
462 fprintf( stderr, " find_source %ld %ld\n", off, size );
464 int i = find_source( off );
466 return ( off == sources.size )? rr : -ENOENT;
468 if ( sources.array[i].fd < 0 ) {
472 print_source( &sources.array[i] );
474 times.atime = time( 0 );
475 size_t b = off - sources.array[i].start + sources.array[i].from;
476 size_t n = sources.array[i].to - b;
480 if ( sources.array[i].dirty ) {
481 fsync( sources.array[i].fd );
482 sources.array[i].dirty = 0;
485 fprintf( stderr, " seek fd=%d to %ld\n", sources.array[i].fd, b );
487 if ( lseek( sources.array[i].fd, b, SEEK_SET ) < 0 ) {
488 perror( sources.array[i].filename );
492 fprintf( stderr, " now read %ld from fd=%d\n",
493 n, sources.array[i].fd );
495 ssize_t r = read( sources.array[i].fd, buf + rr, n );
497 fprintf( stderr, " got %ld bytes\n", r );
500 perror( sources.array[i].filename );
506 if ( overlay.source.filename ) {
507 if ( overlay.source.dirty ) {
508 fsync( overlay.source.fd );
509 overlay.source.dirty = 0;
511 int x = overlay_merge( buf + rr, off + rr, r );
521 fprintf( stderr, " total reading %ld bytes\n", rr );
527 * Poll for IO readiness.
529 int fusefile_poll(const char *path, struct fuse_file_info *fi,
530 struct fuse_pollhandle *ph, unsigned *reventsp )
533 fprintf( stderr, "fusefile_poll( %s ) %p %d\n", path, ph, *reventsp );
535 if( strcmp( path, "/" ) != 0 ) {
539 return fuse_notify_poll( ph );
544 static void overlay_load() {
545 lseek( overlay.source.fd, overlay.source.to, SEEK_SET );
547 size_t size = sizeof( overlay.count );
548 if ( read( overlay.source.fd, &x, size ) != size ) {
552 fprintf( stderr, "overlay: %s with %ld regions\n",
553 overlay.source.filename, x );
555 struct Region f = { 0, 0 };
556 size = sizeof( struct Region );
558 if ( read( overlay.source.fd, &f, size ) != size ) {
559 fprintf( stderr, "%s: bad meta data\n", overlay.source.filename );
563 fprintf( stderr, "overlay region: %ld %ld\n", f.pos, f.size );
565 overlay_mark( f.pos, f.size );
570 * Write a full block of data over the sources at the offset
572 static int write_block(off_t off,const char *buf,size_t size) {
574 fprintf( stderr, "write_block( %ld, ?, %ld )\n", off, size );
576 if ( overlay.source.filename ) {
577 overlay_mark( off, size ); // Mark region as written
580 int index = find_source( off ); // index of source file
582 return -EIO; // past EOF
584 struct Source *source = overlay.source.filename?
585 &overlay.source : &sources.array[ index ];
586 off_t from = off - source->start + source->from;
587 off_t max = source->to - from;
588 if ( lseek( source->fd, from, SEEK_SET ) < 0 ) {
591 ssize_t todo = ( size < max )? size : max;
593 times.mtime = time( 0 );
594 ssize_t n = write( source->fd, buf, todo );
596 return -EIO; // Something wrong
603 if ( source->dirty++ >= 1000 ) {
611 static int fusefile_write_buf(const char *path, struct fuse_bufvec *buf,
612 off_t off, struct fuse_file_info *fi) {
614 fprintf( stderr, "fusefile_write_buf( %s )\n", path );
616 if ( strcmp( path, "/" ) != 0 ) {
622 for ( i = 0; i < buf->count; i++ ) {
623 struct fuse_buf *p = &buf->buf[i];
624 if ( p->flags & FUSE_BUF_IS_FD ) {
626 fprintf( stderr, "Content held in a file ... HELP!!\n" );
630 if ( write_block( off, (char*) p->mem, p->size ) < 0 ) {
636 fprintf( stderr, "fusefile_write_buf written %ld\n", size );
642 * Write a fragment at <off>. This overwrites files.
644 static int fusefile_write(const char *path, const char *buf, size_t size,
645 off_t off, struct fuse_file_info *fi)
648 fprintf( stderr, "fusefile_write( %s %ld )\n", path, size );
650 if ( strcmp( path, "/" ) != 0 ) {
654 if ( write_block( off, buf, size ) < 0 ) {
660 static void fusefile_destroy(void *data) {
661 char *mnt = (char*) data; // As passed to fuse_main
663 fprintf( stderr, "fusefile_destroy( %s )\n", mnt? mnt : "" );
670 static void fsync_all_dirty() {
672 for ( ; i < sources.count; i++ ) {
673 if ( sources.array[i].dirty ) {
674 fsync( sources.array[i].fd );
675 sources.array[i].dirty = 0;
678 if ( overlay.source.filename && overlay.source.dirty ) {
679 fsync( overlay.source.fd );
680 overlay.source.dirty = 0;
684 static int fusefile_flush(const char *path, struct fuse_file_info *info) {
686 fprintf( stderr, "fusefile_flush( %s )\n", path );
688 if ( strcmp( path, "/" ) != 0 ) {
695 static int fusefile_release(const char *path, struct fuse_file_info *fi) {
697 fprintf( stderr, "fusefile_release( %s, %d )\n", path, fi->flags );
699 if ( strcmp( path, "/" ) != 0 ) {
705 static int fusefile_fsync(const char *path, int x, struct fuse_file_info *fi) {
707 fprintf( stderr, "fusefile_fsync( %s, %d )\n", path, x );
709 if ( strcmp( path, "/" ) != 0 ) {
719 static int fusefile_truncate(const char *path, off_t len) {
721 fprintf( stderr, "fusefile_truncate( %s, %ld )\n", path, len );
723 if ( strcmp( path, "/" ) != 0 ) {
729 void *fusefile_init(struct fuse_conn_info *fci) {
731 fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
733 // Disable asynchronous reading
735 fci->want &= ~FUSE_CAP_ASYNC_READ;
737 fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
742 static struct fuse_operations fusefile_oper = {
743 .getattr = fusefile_getattr,
744 .chmod = fusefile_chmod,
745 .open = fusefile_open,
746 .read = fusefile_read,
747 .poll = fusefile_poll,
748 .write = fusefile_write,
749 .write_buf = fusefile_write_buf,
750 .destroy = fusefile_destroy,
751 .flush = fusefile_flush,
752 .release = fusefile_release,
753 .fsync = fusefile_fsync,
754 .truncate = fusefile_truncate,
755 //.truncate = fusefile_truncate,
756 //.release = fusefile_release,
757 .init = fusefile_init,
760 static void usage() {
762 "Usage: fusefile [ <fuse options> ] <mount> <file/from-to> ... \n"
763 "Mounts a virtual, file that is a concatenation of file fragments\n"
765 fprintf( stderr, "%s", usage );
770 * Set up the arguments for the fuse_main call, adding our own.
771 * argv[argc] is the mount point argument
773 static int setup_argv(int argc,char ***argv) {
774 // note: (*argv)[ argc ] is the mount point argument
776 "-odefault_permissions",
779 #define OURSN ( sizeof( OURS ) / sizeof( char* ) )
780 int N = argc + OURSN;
781 // Allocate new arg array plus terminating null pointer
782 char **out = malloc( ( N + 1 ) * sizeof( char* ) );
784 for ( i = 0; i < argc; i++ ) {
785 out[ i ] = (*argv)[i];
786 //fprintf( stderr, " %s", out[ i ] );
788 for ( i = 0; i < OURSN; i++ ) {
789 out[ argc + i ] = OURS[i];
790 //fprintf( stderr, " %s", out[ i ] );
793 //fprintf( stderr, "\n" );
795 return N; // Don't include the terminating null pointer
799 * Mount a concatenation of files,
800 * [ <fuse options> ] <mount> <file/from-to> ...
802 int main(int argc, char *argv[])
812 for ( i = 1; i < argc; i++ ) {
813 if ( *argv[i] != '-' ) {
817 if ( i > argc - 2 ) { // At least mount point plus one source
821 mnt = argv[ i++ ]; // First non-option argument is the mount pount
822 char *overlaytag = "-overlay:";
823 int overlaytagsize = strlen( overlaytag );
824 if ( strncmp( argv[i], overlaytag, overlaytagsize ) == 0 ) {
825 // consume "-overlay:filename"
826 setup_overlay( argv[i++] + overlaytagsize ); // Need a writable file
831 if ( setup_sources( argv, i, argc-i ) ) {
834 if ( overlay.source.filename ) {
835 overlay.source.to = sources.size; // Register total size.
838 if ( stat( mnt, &stbuf ) == -1 ) {
839 int fd = open( mnt, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR );
844 time_t now = time( 0 );
850 } else if ( ! S_ISREG( stbuf.st_mode ) ) {
851 fprintf( stderr, "mountpoint is not a regular file\n" );
854 times.atime = stbuf.st_atime;
855 times.mtime = stbuf.st_mtime;
856 times.ctime = stbuf.st_ctime;
860 int fd = open( mnt, O_RDWR, S_IRUSR | S_IWUSR );
865 if ( lseek( fd, sources.size, SEEK_SET ) < 0 ) {
869 fuseargc = setup_argv( fuseargc, &argv );
870 struct fuse_args args = FUSE_ARGS_INIT( fuseargc, argv );
871 if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {
874 fuse_opt_free_args( &args );
876 fprintf( stderr, "missing mountpoint parameter\n" );
879 return fuse_main( fuseargc, argv, &fusefile_oper, temporary? mnt : NULL );