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>
41 ssize_t start; // starting position in concatenated file
58 static void print_source(struct Source *p) {
59 fprintf( stderr, "%p { %s, %ld, %ld, %ld, %d }\n",
60 p, p->filename, p->from, p->to, p->start, p-> fd );
65 static unsigned int c;
66 static int RANGE(int s,int n ) {
67 return ( s == n ) && *(range+c) == 0;
70 static int setup_sources(char **argv,int i,int n) {
71 sources.array = calloc( n, sizeof( struct Source ) );
72 if ( sources.array == 0 ) {
78 for ( ; j < n; i++, j++ ) {
80 struct Source *p = sources.array + j;
81 // Open the fragment file rw if possible, else ro
82 range = strrchr( argv[i], '/' ); // last '/'
83 p->filename = range? strndup( argv[i], range - argv[i] ) : argv[i];
84 p->fd = open( p->filename, O_RDWR );
88 p->fd = open( p->filename, O_RDONLY );
91 perror( p->filename );
92 return 1; // Error return
94 if ( stat( p->filename, &filestat ) ) {
95 perror( p->filename );
99 fprintf( stderr, "** %s opened read-only\n", p->filename );
102 p->to = filestat.st_size;
103 // Process any range variation
104 if ( range && *(++range) ) {
107 } else if ( RANGE( sscanf( range, "%d:%d%n", &a, &b, &c ), 2 )) {
108 p->from = ( a < 0 )? ( p->to + a ) : a;
109 p->to = ( b < 0 )? ( p->to + b ) : b;
110 } else if ( RANGE( sscanf( range, "%d+%d%n", &a, &b, &c ), 2 )) {
111 p->from = ( a < 0 )? ( p->to + a ) : a;
112 p->to = ( ( b < 0 )? p->to : p->from ) + b;
113 } else if ( RANGE( sscanf( range, "%d+%n", &a, &c ), 1 )) {
114 p->from = ( a < 0 )? ( p->to + a ) : a;
115 } else if ( RANGE( sscanf( range, ":%d%n", &b, &c ), 1 )) {
116 p->to = ( b < 0 )? ( p->to + b ) : b;
117 } else if ( RANGE( sscanf( range, "%d:%n", &a, &c ), 1 )) {
118 p->from = ( a < 0 )? ( p->to + a ) : a;
119 } else if ( RANGE( sscanf( range, "%d%n", &a, &c ), 1 )) {
125 } else if ( RANGE( sscanf( range, ":%n", &c), 0 ) ) {
128 fprintf( stderr, "** BAD RANGE: %s\n", argv[i] );
132 if ( ( filestat.st_mode & S_IFMT ) == S_IFCHR ) {
133 filestat.st_size = p->to; // Pretend size of character device
138 if ( p->to > filestat.st_size ) {
139 p->to = filestat.st_size;
141 if ( p->from >= p->to || p->from >= filestat.st_size ) {
142 fprintf( stderr, "** BAD RANGE: %s [%ld:%ld]\n",
143 argv[i], p->from, p->to );
146 p->start = sources.size; // the fusefile position of fragment
147 sources.size += p->to - p->from;
155 static int fusefile_getattr(const char *path,struct stat *stbuf) {
157 fprintf( stderr, "fusefile_getattr( %s )\n", path );
159 if ( strcmp( path, "/" ) != 0 ) {
163 fprintf( stderr, "getattr %ld\n", sources.size );
165 memset( stbuf, 0, sizeof( struct stat ) );
166 stbuf->st_mode = S_IFREG | 0644; // Hmmm
168 stbuf->st_size = sources.size;
169 stbuf->st_atime = times.atime;
170 stbuf->st_mtime = times.mtime;
171 stbuf->st_ctime = times.ctime;
172 stbuf->st_uid = getuid();
173 stbuf->st_gid = getgid();
177 static int fusefile_chmod(const char *path,mode_t m) {
179 fprintf( stderr, "fusefile_chmod( %s, %d )\n", path, m );
184 static int fusefile_open(const char *path,struct fuse_file_info *fi) {
186 fprintf( stderr, "fusefile_open( %s, %d )\n", path, fi->flags );
187 fprintf( stderr, "fixing( %d )\n", fi->flags | O_CLOEXEC );
189 if ( strcmp( path, "/" ) != 0 ) {
192 // set O-CLOEXEC for this opening?
193 times.atime = time( 0 );
197 static int find_source(off_t offset) {
199 int hi = sources.count;
200 if ( offset >= sources.size ) {
203 while ( lo + 1 < hi ) {
204 int m = ( lo + hi ) / 2;
205 if ( offset < sources.array[ m ].start ) {
214 // Read <size> bytes from <offset> in file
215 static int fusefile_read(const char *path, char *buf, size_t size,
216 off_t off, struct fuse_file_info *fi)
219 fprintf( stderr, "fusefile_read( %s )\n", path );
221 if( strcmp( path, "/" ) != 0 ) {
225 fprintf( stderr, "read %ld %ld\n", off, size );
230 fprintf( stderr, "find_source %ld %ld\n", off, size );
232 int i = find_source( off );
234 return ( off == sources.size )? rr : -ENOENT;
236 if ( sources.array[i].fd < 0 ) {
240 print_source( &sources.array[i] );
242 times.atime = time( 0 );
243 size_t b = off - sources.array[i].start + sources.array[i].from;
244 size_t n = sources.array[i].to - b;
248 if ( lseek( sources.array[i].fd, b, SEEK_SET ) < 0 ) {
249 perror( sources.array[i].filename );
253 fprintf( stderr, "get %ld bytes at %ld\n", n, rr );
255 ssize_t r = read( sources.array[i].fd, buf + rr, n );
257 fprintf( stderr, "got %ld bytes\n", r );
260 perror( sources.array[i].filename );
274 * Poll for IO readiness.
276 int fusefile_poll(const char *path, struct fuse_file_info *fi,
277 struct fuse_pollhandle *ph, unsigned *reventsp )
280 fprintf( stderr, "fusefile_poll( %s ) %p %d\n", path, ph, *reventsp );
282 if( strcmp( path, "/" ) != 0 ) {
286 return fuse_notify_poll( ph );
293 * Write a full block of data over the sources at the offset
295 static int write_block(off_t off,const char *buf,size_t size) {
297 fprintf( stderr, "write_block( %ld, ?, %ld )\n", off, size );
300 int index = find_source( off ); // index of source file
302 return -EIO; // past EOF
304 struct Source *source = &sources.array[ index ];
305 off_t from = off - source->start + source->from;
306 off_t max = source->to - from;
307 if ( lseek( source->fd, from, SEEK_SET ) < 0 ) {
310 ssize_t todo = ( size < max )? size : max;
312 times.mtime = time( 0 );
313 ssize_t n = write( source->fd, buf, todo );
315 return -EIO; // Something wrong
326 static int fusefile_write_buf(const char *path, struct fuse_bufvec *buf,
327 off_t off, struct fuse_file_info *fi) {
329 fprintf( stderr, "fusefile_write_buf( %s )\n", path );
331 if ( strcmp( path, "/" ) != 0 ) {
337 for ( i = 0; i < buf->count; i++ ) {
338 struct fuse_buf *p = &buf->buf[i];
339 if ( p->flags & FUSE_BUF_IS_FD ) {
341 fprintf( stderr, "Content held in a file ... HELP!!\n" );
345 if ( write_block( off, (char*) p->mem, p->size ) < 0 ) {
351 fprintf( stderr, "fusefile_write_buf written %ld\n", size );
357 * Write a fragment at <off>. This overwrites files.
359 static int fusefile_write(const char *path, const char *buf, size_t size,
360 off_t off, struct fuse_file_info *fi)
363 fprintf( stderr, "fusefile_write( %s %ld )\n", path, size );
365 if ( strcmp( path, "/" ) != 0 ) {
369 if ( write_block( off, buf, size ) < 0 ) {
375 static void fusefile_destroy(void *data) {
376 char *mnt = (char*) data; // As passed to fuse_main
378 fprintf( stderr, "fusefile_destroy( %s )\n", mnt? mnt : "" );
385 static int fusefile_flush(const char *path, struct fuse_file_info *info) {
387 fprintf( stderr, "fusefile_flush( %s )\n", path );
389 if ( strcmp( path, "/" ) != 0 ) {
395 static int fusefile_release(const char *path, struct fuse_file_info *fi) {
397 fprintf( stderr, "fusefile_release( %s, %d )\n", path, fi->flags );
399 if ( strcmp( path, "/" ) != 0 ) {
405 static int fusefile_fsync(const char *path, int x, struct fuse_file_info *fi) {
407 fprintf( stderr, "fusefile_fsync( %s, %d )\n", path, x );
409 if ( strcmp( path, "/" ) != 0 ) {
418 static int fusefile_truncate(const char *path, off_t len) {
420 fprintf( stderr, "fusefile_truncate( %s, %ld )\n", path, len );
422 if ( strcmp( path, "/" ) != 0 ) {
428 void *fusefile_init(struct fuse_conn_info *fci) {
430 fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
432 // Disable asynchronous reading
434 fci->want &= ~FUSE_CAP_ASYNC_READ;
436 fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
441 static struct fuse_operations fusefile_oper = {
442 .getattr = fusefile_getattr,
443 .chmod = fusefile_chmod,
444 .open = fusefile_open,
445 .read = fusefile_read,
446 .poll = fusefile_poll,
447 .write = fusefile_write,
448 .write_buf = fusefile_write_buf,
449 .destroy = fusefile_destroy,
450 .flush = fusefile_flush,
451 .release = fusefile_release,
452 .fsync = fusefile_fsync,
453 .truncate = fusefile_truncate,
454 //.truncate = fusefile_truncate,
455 //.release = fusefile_release,
456 .init = fusefile_init,
459 static void usage() {
461 "Usage: fusefile [ <fuse options> ] <mount> <file/from-to> ... \n"
462 "Mounts a virtual, read-only file that is a concatenation of file fragments\n"
464 fprintf( stderr, "%s", usage );
469 * Set up the arguments for the fuse_main call, adding our own.
470 * argv[argc] is the mount point argument
472 static int setup_argv(int argc,char ***argv) {
473 // note: (*argv)[ argc ] is the mount point argument
475 "-odefault_permissions",
478 #define OURSN ( sizeof( OURS ) / sizeof( char* ) )
479 int N = argc + OURSN;
480 // Allocate new arg array plus terminating null pointer
481 char **out = malloc( ( N + 1 ) * sizeof( char* ) );
483 for ( i = 0; i < argc; i++ ) {
484 out[ i ] = (*argv)[i];
485 //fprintf( stderr, " %s", out[ i ] );
487 for ( i = 0; i < OURSN; i++ ) {
488 out[ argc + i ] = OURS[i];
489 //fprintf( stderr, " %s", out[ i ] );
492 //fprintf( stderr, "\n" );
494 return N; // Don't include the terminating null pointer
498 * Mount a concatenation of files,
499 * [ <fuse options> ] <mount> <file/from-to> ...
501 int main(int argc, char *argv[])
511 for ( i = 1; i < argc; i++ ) {
512 if ( *argv[i] != '-' ) {
516 if ( i > argc - 2 ) { // At least mount point plus one source
520 mnt = argv[ i++ ]; // First non-option argument is the mount pount
521 if ( setup_sources( argv, i, argc-i ) ) {
524 if ( stat( mnt, &stbuf ) == -1 ) {
525 int fd = open( mnt, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR );
530 time_t now = time( 0 );
536 } else if ( ! S_ISREG( stbuf.st_mode ) ) {
537 fprintf( stderr, "mountpoint is not a regular file\n" );
540 times.atime = stbuf.st_atime;
541 times.mtime = stbuf.st_mtime;
542 times.ctime = stbuf.st_ctime;
545 fuseargc = setup_argv( fuseargc, &argv );
546 struct fuse_args args = FUSE_ARGS_INIT( fuseargc, argv );
547 if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {
550 fuse_opt_free_args( &args );
552 fprintf( stderr, "missing mountpoint parameter\n" );
555 return fuse_main( fuseargc, argv, &fusefile_oper, temporary? mnt : NULL );