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
72 #define FRAG(m) (overlay.table+m)
73 #define BEG(m) (FRAG(m)->pos)
74 #define END(m) (FRAG(m)->pos + FRAG(m)->size)
76 static ssize_t overlay_prior_fragment(off_t pos) {
77 size_t lo = 0, hi = overlay.count;
79 size_t m = ( lo + hi ) / 2;
81 return BEG( m ) < pos? m : -1;
83 if ( BEG( m ) <= pos ) {
92 static void overlay_save_count() {
93 lseek( overlay.source.fd, overlay.source.to, SEEK_SET );
94 size_t size = sizeof( overlay.count );
95 char *p = (char *) &overlay.count ;
97 size_t n = write( overlay.source.fd, p, size );
99 perror( overlay.source.filename );
107 static void overlay_save_table(size_t lo,size_t hi) {
108 char *p = (char *) FRAG(lo);
109 size_t pos = overlay.source.to + sizeof( overlay.count ) +
110 lo * sizeof( struct Region );
111 size_t size = ( hi - lo ) * sizeof( struct Region );
112 if ( pos != lseek( overlay.source.fd, pos, SEEK_SET ) ) {
113 fprintf( stderr, "%s: seek error\n", overlay.source.filename );
117 size_t n = write( overlay.source.fd, p, size );
119 perror( overlay.source.filename );
127 static void overlay_insert(size_t p,off_t pos,size_t size) {
129 if ( overlay.count >= overlay.limit ) {
130 overlay.limit = overlay.count + 10;
131 bytes = overlay.limit * sizeof( struct Region );
132 overlay.table = overlay.table?
133 realloc( overlay.table, bytes ) : malloc( bytes );
135 bytes = ( overlay.count++ - p ) * sizeof( struct Region );
137 memmove( FRAG( p+1 ), FRAG( p ), bytes );
139 FRAG( p )->pos = pos;
140 FRAG( p )->size = size;
141 overlay_save_count();
144 static void overlay_delete(size_t p) {
145 if ( p < --overlay.count ) {
146 size_t size = ( overlay.count - p ) * sizeof( struct Region );
147 memmove( FRAG(p), FRAG(p+1), size );
149 overlay_save_count();
152 static void overlay_mark(off_t pos,size_t size) {
153 ssize_t p = overlay_prior_fragment( pos );
154 if ( p >= 0 && pos <= END(p) ) {
155 // Merge new marks with fragment p
156 FRAG(p)->size = pos + size - BEG(p);
157 if ( p+1 < overlay.count && BEG(p+1) <= END(p) ) {
158 FRAG(p)->size = END(p+1) - BEG(p);
159 overlay_delete( p+1 );
160 overlay_save_table( p, overlay.count );
162 overlay_save_table( p, p+1 );
166 p++; // index of subsequent fragment
167 if ( p < overlay.count && BEG(p) < pos + size ) {
168 // Merge new marks with pragment p+1
169 FRAG(p)->size = END(p) - pos;
171 overlay_save_table( p, p+1 );
173 overlay_insert( p, pos, size);
174 overlay_save_table( p, overlay.count );
179 static void print_source(struct Source *p) {
180 fprintf( stderr, "%p { %s, %ld, %ld, %ld, %d }\n",
181 p, p->filename, p->from, p->to, p->start, p-> fd );
186 static unsigned int c;
187 static int RANGE(int s,int n ) {
188 return ( s == n ) && *(range+c) == 0;
193 static void setup_overlay(char *filename) {
194 overlay.source.filename = filename;
195 overlay.source.fd = open( filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR );
196 if ( overlay.source.fd < 0 ) {
202 static int setup_sources(char **argv,int i,int n) {
203 sources.array = calloc( n, sizeof( struct Source ) );
204 if ( sources.array == 0 ) {
210 for ( ; j < n; i++, j++ ) {
211 struct stat filestat;
212 struct Source *p = sources.array + j;
213 // Open the fragment file rw if possible, else ro
214 range = strrchr( argv[i], '/' ); // last '/'
215 p->filename = range? strndup( argv[i], range - argv[i] ) : argv[i];
216 p->fd = open( p->filename, O_RDWR );
220 p->fd = open( p->filename, O_RDONLY );
223 perror( p->filename );
224 return 1; // Error return
226 if ( stat( p->filename, &filestat ) ) {
227 perror( p->filename );
231 fprintf( stderr, "** %s opened read-only\n", p->filename );
234 p->to = filestat.st_size;
235 // Process any range variation
236 if ( range && *(++range) ) {
239 } else if ( RANGE( sscanf( range, "%d:%d%n", &a, &b, &c ), 2 )) {
240 p->from = ( a < 0 )? ( p->to + a ) : a;
241 p->to = ( b < 0 )? ( p->to + b ) : b;
242 } else if ( RANGE( sscanf( range, "%d+%d%n", &a, &b, &c ), 2 )) {
243 p->from = ( a < 0 )? ( p->to + a ) : a;
244 p->to = ( ( b < 0 )? p->to : p->from ) + b;
245 } else if ( RANGE( sscanf( range, "%d+%n", &a, &c ), 1 )) {
246 p->from = ( a < 0 )? ( p->to + a ) : a;
247 } else if ( RANGE( sscanf( range, ":%d%n", &b, &c ), 1 )) {
248 p->to = ( b < 0 )? ( p->to + b ) : b;
249 } else if ( RANGE( sscanf( range, "%d:%n", &a, &c ), 1 )) {
250 p->from = ( a < 0 )? ( p->to + a ) : a;
251 } else if ( RANGE( sscanf( range, "%d%n", &a, &c ), 1 )) {
257 } else if ( RANGE( sscanf( range, ":%n", &c), 0 ) ) {
260 fprintf( stderr, "** BAD RANGE: %s\n", argv[i] );
264 if ( ( filestat.st_mode & S_IFMT ) == S_IFCHR ) {
265 filestat.st_size = p->to; // Pretend size of character device
270 if ( p->to > filestat.st_size ) {
271 p->to = filestat.st_size;
273 if ( p->from >= p->to || p->from >= filestat.st_size ) {
274 fprintf( stderr, "** BAD RANGE: %s [%ld:%ld]\n",
275 argv[i], p->from, p->to );
278 p->start = sources.size; // the fusefile position of fragment
279 sources.size += p->to - p->from;
287 static int fusefile_getattr(const char *path,struct stat *stbuf) {
289 fprintf( stderr, "fusefile_getattr( %s )\n", path );
291 if ( strcmp( path, "/" ) != 0 ) {
295 fprintf( stderr, "getattr %ld\n", sources.size );
297 memset( stbuf, 0, sizeof( struct stat ) );
298 stbuf->st_mode = S_IFREG | 0644; // Hmmm
300 stbuf->st_size = sources.size;
301 stbuf->st_atime = times.atime;
302 stbuf->st_mtime = times.mtime;
303 stbuf->st_ctime = times.ctime;
304 stbuf->st_uid = getuid();
305 stbuf->st_gid = getgid();
309 static int fusefile_chmod(const char *path,mode_t m) {
311 fprintf( stderr, "fusefile_chmod( %s, %d )\n", path, m );
316 static int fusefile_open(const char *path,struct fuse_file_info *fi) {
318 fprintf( stderr, "fusefile_open( %s, %d )\n", path, fi->flags );
319 fprintf( stderr, "fixing( %d )\n", fi->flags | O_CLOEXEC );
321 if ( strcmp( path, "/" ) != 0 ) {
324 // set O-CLOEXEC for this opening?
325 times.atime = time( 0 );
329 static int find_source(off_t offset) {
331 int hi = sources.count;
332 if ( offset >= sources.size ) {
336 fprintf( stderr, "find_source( %ld )\n", offset );
338 while ( lo + 1 < hi ) {
339 int m = ( lo + hi ) / 2;
340 if ( offset < sources.array[ m ].start ) {
342 fprintf( stderr, " offset < [%d].start: %ld\n",
343 m, sources.array[ m ].start );
348 fprintf( stderr, " offset >= [%d].start: %ld\n",
349 m, sources.array[ m ].start );
355 fprintf( stderr, "found %d\n", lo );
360 static int overlay_merge(char *buf,off_t off,size_t size) {
362 fprintf( stderr, "merge %ld %ld\n", off, size );
364 // Find nearest overlay data before or at off
365 ssize_t p = overlay_prior_fragment( off );
369 for ( ; p < overlay.count && BEG(p) < off+size; p++ ) {
370 size_t delta = FRAG(p)->size;
371 if ( BEG(p) > off ) {
372 size_t skip = BEG(p) - off;
377 delta = off - BEG(p);
379 if ( delta > size ) {
382 lseek( overlay.source.fd, off, SEEK_SET );
383 while ( delta > 0 ) {
384 size_t n = read( overlay.source.fd, buf, delta );
392 fprintf( stderr, "merged\n" );
397 // Read <size> bytes from <offset> in file
398 static int fusefile_read(const char *path, char *buf, size_t size,
399 off_t off, struct fuse_file_info *fi)
402 fprintf( stderr, "fusefile_read( %s )\n", path );
404 if( strcmp( path, "/" ) != 0 ) {
408 fprintf( stderr, "read %ld %ld\n", off, size );
410 size_t rr = 0; // total reading
413 fprintf( stderr, " find_source %ld %ld\n", off, size );
415 int i = find_source( off );
417 return ( off == sources.size )? rr : -ENOENT;
419 if ( sources.array[i].fd < 0 ) {
423 print_source( &sources.array[i] );
425 times.atime = time( 0 );
426 size_t b = off - sources.array[i].start + sources.array[i].from;
427 size_t n = sources.array[i].to - b;
432 fprintf( stderr, " seek fd=%d to %ld\n", sources.array[i].fd, b );
434 if ( lseek( sources.array[i].fd, b, SEEK_SET ) < 0 ) {
435 perror( sources.array[i].filename );
439 fprintf( stderr, " now read %ld from fd=%d\n",
440 n, sources.array[i].fd );
442 ssize_t r = read( sources.array[i].fd, buf + rr, n );
443 if ( overlay.source.filename ) {
444 int x = overlay_merge( buf + rr, off + rr, r );
450 fprintf( stderr, " got %ld bytes\n", r );
453 perror( sources.array[i].filename );
464 fprintf( stderr, " total reading %ld bytes\n", rr );
470 * Poll for IO readiness.
472 int fusefile_poll(const char *path, struct fuse_file_info *fi,
473 struct fuse_pollhandle *ph, unsigned *reventsp )
476 fprintf( stderr, "fusefile_poll( %s ) %p %d\n", path, ph, *reventsp );
478 if( strcmp( path, "/" ) != 0 ) {
482 return fuse_notify_poll( ph );
487 static void overlay_load() {
488 lseek( overlay.source.fd, overlay.source.to, SEEK_SET );
490 size_t size = sizeof( overlay.count );
491 if ( read( overlay.source.fd, &x, size ) != size ) {
495 fprintf( stderr, "overlay: %s with %ld regions\n",
496 overlay.source.filename, x );
498 struct Region f = { 0, 0 };
499 size = sizeof( struct Region );
501 if ( read( overlay.source.fd, &f, size ) != size ) {
502 fprintf( stderr, "%s: bad meta data\n", overlay.source.filename );
506 fprintf( stderr, "overlay region: %ld %ld\n", f.pos, f.size );
508 overlay_mark( f.pos, f.size );
513 * Write a full block of data over the sources at the offset
515 static int write_block(off_t off,const char *buf,size_t size) {
517 fprintf( stderr, "write_block( %ld, ?, %ld )\n", off, size );
520 int index = find_source( off ); // index of source file
522 return -EIO; // past EOF
524 struct Source *source;
525 if ( overlay.source.filename ) {
526 source = &overlay.source;
527 overlay_mark( off, size ); // Mark region as written
529 source = &sources.array[ index ];
531 off_t from = off - source->start + source->from;
532 off_t max = source->to - from;
533 if ( lseek( source->fd, from, SEEK_SET ) < 0 ) {
536 ssize_t todo = ( size < max )? size : max;
538 times.mtime = time( 0 );
539 ssize_t n = write( source->fd, buf, todo );
541 return -EIO; // Something wrong
552 static int fusefile_write_buf(const char *path, struct fuse_bufvec *buf,
553 off_t off, struct fuse_file_info *fi) {
555 fprintf( stderr, "fusefile_write_buf( %s )\n", path );
557 if ( strcmp( path, "/" ) != 0 ) {
563 for ( i = 0; i < buf->count; i++ ) {
564 struct fuse_buf *p = &buf->buf[i];
565 if ( p->flags & FUSE_BUF_IS_FD ) {
567 fprintf( stderr, "Content held in a file ... HELP!!\n" );
571 if ( write_block( off, (char*) p->mem, p->size ) < 0 ) {
577 fprintf( stderr, "fusefile_write_buf written %ld\n", size );
583 * Write a fragment at <off>. This overwrites files.
585 static int fusefile_write(const char *path, const char *buf, size_t size,
586 off_t off, struct fuse_file_info *fi)
589 fprintf( stderr, "fusefile_write( %s %ld )\n", path, size );
591 if ( strcmp( path, "/" ) != 0 ) {
595 if ( write_block( off, buf, size ) < 0 ) {
601 static void fusefile_destroy(void *data) {
602 char *mnt = (char*) data; // As passed to fuse_main
604 fprintf( stderr, "fusefile_destroy( %s )\n", mnt? mnt : "" );
611 static int fusefile_flush(const char *path, struct fuse_file_info *info) {
613 fprintf( stderr, "fusefile_flush( %s )\n", path );
615 if ( strcmp( path, "/" ) != 0 ) {
621 static int fusefile_release(const char *path, struct fuse_file_info *fi) {
623 fprintf( stderr, "fusefile_release( %s, %d )\n", path, fi->flags );
625 if ( strcmp( path, "/" ) != 0 ) {
631 static int fusefile_fsync(const char *path, int x, struct fuse_file_info *fi) {
633 fprintf( stderr, "fusefile_fsync( %s, %d )\n", path, x );
635 if ( strcmp( path, "/" ) != 0 ) {
644 static int fusefile_truncate(const char *path, off_t len) {
646 fprintf( stderr, "fusefile_truncate( %s, %ld )\n", path, len );
648 if ( strcmp( path, "/" ) != 0 ) {
654 void *fusefile_init(struct fuse_conn_info *fci) {
656 fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
658 // Disable asynchronous reading
660 fci->want &= ~FUSE_CAP_ASYNC_READ;
662 fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
667 static struct fuse_operations fusefile_oper = {
668 .getattr = fusefile_getattr,
669 .chmod = fusefile_chmod,
670 .open = fusefile_open,
671 .read = fusefile_read,
672 .poll = fusefile_poll,
673 .write = fusefile_write,
674 .write_buf = fusefile_write_buf,
675 .destroy = fusefile_destroy,
676 .flush = fusefile_flush,
677 .release = fusefile_release,
678 .fsync = fusefile_fsync,
679 .truncate = fusefile_truncate,
680 //.truncate = fusefile_truncate,
681 //.release = fusefile_release,
682 .init = fusefile_init,
685 static void usage() {
687 "Usage: fusefile [ <fuse options> ] <mount> <file/from-to> ... \n"
688 "Mounts a virtual, file that is a concatenation of file fragments\n"
690 fprintf( stderr, "%s", usage );
695 * Set up the arguments for the fuse_main call, adding our own.
696 * argv[argc] is the mount point argument
698 static int setup_argv(int argc,char ***argv) {
699 // note: (*argv)[ argc ] is the mount point argument
701 "-odefault_permissions",
704 #define OURSN ( sizeof( OURS ) / sizeof( char* ) )
705 int N = argc + OURSN;
706 // Allocate new arg array plus terminating null pointer
707 char **out = malloc( ( N + 1 ) * sizeof( char* ) );
709 for ( i = 0; i < argc; i++ ) {
710 out[ i ] = (*argv)[i];
711 //fprintf( stderr, " %s", out[ i ] );
713 for ( i = 0; i < OURSN; i++ ) {
714 out[ argc + i ] = OURS[i];
715 //fprintf( stderr, " %s", out[ i ] );
718 //fprintf( stderr, "\n" );
720 return N; // Don't include the terminating null pointer
724 * Mount a concatenation of files,
725 * [ <fuse options> ] <mount> <file/from-to> ...
727 int main(int argc, char *argv[])
737 for ( i = 1; i < argc; i++ ) {
738 if ( *argv[i] != '-' ) {
742 if ( i > argc - 2 ) { // At least mount point plus one source
746 mnt = argv[ i++ ]; // First non-option argument is the mount pount
747 char *overlaytag = "-overlay:";
748 int overlaytagsize = strlen( overlaytag );
749 if ( strncmp( argv[i], overlaytag, overlaytagsize ) == 0 ) {
750 // consume "-overlay:filename"
751 setup_overlay( argv[i++] + overlaytagsize ); // Need a writable file
756 if ( setup_sources( argv, i, argc-i ) ) {
759 if ( overlay.source.filename ) {
760 overlay.source.to = sources.size; // Register total size.
763 if ( stat( mnt, &stbuf ) == -1 ) {
764 int fd = open( mnt, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR );
769 time_t now = time( 0 );
775 } else if ( ! S_ISREG( stbuf.st_mode ) ) {
776 fprintf( stderr, "mountpoint is not a regular file\n" );
779 times.atime = stbuf.st_atime;
780 times.mtime = stbuf.st_mtime;
781 times.ctime = stbuf.st_ctime;
785 int fd = open( mnt, O_RDWR, S_IRUSR | S_IWUSR );
790 if ( lseek( fd, sources.size, SEEK_SET ) < 0 ) {
794 fuseargc = setup_argv( fuseargc, &argv );
795 struct fuse_args args = FUSE_ARGS_INIT( fuseargc, argv );
796 if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {
799 fuse_opt_free_args( &args );
801 fprintf( stderr, "missing mountpoint parameter\n" );
804 return fuse_main( fuseargc, argv, &fusefile_oper, temporary? mnt : NULL );