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 #define SOURCEARRAYP(i) ((void*)&sources.array[ i ])
61 * Holds info about scratch pad file for 'write' events.
69 static void print_source(struct Source *p) {
70 fprintf( stderr, "%p { %s, %ld, %ld, %ld, %d }\n",
71 p, p->filename, p->from, p->to, p->start, p->fd );
75 // Scan the source specification, and return the length of the
76 // inclusion. "filename/from,to"
81 static size_t scan_source(char *in,struct Source *p) {
86 // scan for last '/' and last '-'
87 for ( ; i >= 0; i-- ) {
97 fprintf( stderr, "m=%d s=%d\n", m, s );
99 // Copy the filename, and set from and to
100 p->filename = strndup( in, ( s < 0 )? e : s );
102 if ( stat( p->filename, & buf ) ) {
103 perror( p->filename );
106 p->from = ( s < 0 )? 0 : atol( in+s+1 );
111 fprintf( stderr, "p->from=%ld\n", p->from );
113 p->to = ( m < 0 )? buf.st_size : atol( in+m+1 );
114 if ( p->from > p->to || p->to > buf.st_size ) {
120 static int setup_sources(char **argv,int i,int n) {
121 sources.array = calloc( n, sizeof( struct Source ) );
122 if ( sources.array == 0 ) {
129 for ( ; j < n; i++, j++ ) {
130 struct Source *p = sources.array + j;
131 if ( scan_source( argv[i], p ) ) {
132 // should free everything malloc-ed
135 p->start = sources.size;
136 sources.size += p->to - p->from;
137 p->fd = open( p->filename, O_RDONLY );
139 perror( p->filename );
149 static int fusefile_getattr(const char *path,struct stat *stbuf) {
151 fprintf( stderr, "fusefile_getattr( %s )\n", path );
153 if ( strcmp( path, "/" ) != 0 ) {
157 fprintf( stderr, "getattr %ld\n", sources.size );
159 memset( stbuf, 0, sizeof( struct stat ) );
160 stbuf->st_mode = S_IFREG | 0444; // Hmmm
161 if ( pad.filename ) {
162 stbuf->st_mode |= 0200;
165 stbuf->st_size = sources.size;
166 stbuf->st_atime = times.atime;
167 stbuf->st_mtime = times.mtime;
168 stbuf->st_ctime = times.ctime;
169 stbuf->st_uid = getuid();
170 stbuf->st_gid = getgid();
174 static int fusefile_open(const char *path,struct fuse_file_info *fi) {
176 fprintf( stderr, "fusefile_open( %s, %d )\n", path, fi->flags );
177 fprintf( stderr, "fixing( %d )\n", fi->flags | O_CLOEXEC );
179 if ( strcmp( path, "/" ) != 0 ) {
182 // set O-CLOEXEC for this opening?
183 times.atime = time( 0 );
187 static int find_source(off_t offset) {
189 int hi = sources.count;
190 if ( offset > sources.size ) {
193 while ( lo + 1 < hi ) {
194 int m = ( lo + hi ) / 2;
195 if ( offset < sources.array[ m ].start ) {
205 * Insert a source fragment description into the table at <off>.
207 static int insert_source(struct Source *source,size_t off) {
208 int index = find_source( off );
210 // Ensure at least 5 "free" Sources in <source.array>
211 // and allocate space for 20 new otherwise.
212 if ( sources.count + 5 > sources.limit ) {
213 size_t size = sources.limit + 20;
214 struct Source *new = realloc(
215 sources.array, size * sizeof( struct Source ) );
220 sources.limit = size;
223 fprintf( stderr, "index=%d\n", index );
225 if ( index < sources.count ) {
226 ssize_t b = ( sources.count - index ) * sizeof(struct Source);
228 fprintf( stderr, "b=%ld\n", b );
230 if ( sources.array[ index ].start < off ) {
231 // Split the <index> record at <off>
233 memcpy( SOURCEARRAYP( index+2 ), SOURCEARRAYP( index ), b );
235 b = off - sources.array[ index ].start;
236 sources.array[ index + 2 ].from += b; // adjust tail fragment
237 sources.array[ index++ ].to = b; // adjust head fragment
239 print_source( &sources.array[ index-1 ] );
240 print_source( &sources.array[ index ] );
241 print_source( &sources.array[ index+1 ] );
242 fprintf( stderr, "---\n");
245 // Insert the new source at <index>
246 memcpy( SOURCEARRAYP( index+1 ), SOURCEARRAYP( index ), b );
250 // Append the new source to <sources> (at <index>)
253 sources.array[ index ].filename = source->filename;
254 sources.array[ index ].fd = source->fd;
255 sources.array[ index ].from = source->from;
256 sources.array[ index ].to = source->to;
257 for ( i = index; i < sources.count; i++ ) {
258 sources.array[ i ].start = off;
259 off += sources.array[ i ].to - sources.array[ i ].from;
261 print_source( &sources.array[ i ] );
266 fprintf( stderr, "count=%d size=%ld\n", sources.count, sources.size );
271 // Read <size> bytes from <off> in file
272 static int fusefile_read(const char *path, char *buf, size_t size,
273 off_t off, struct fuse_file_info *fi)
276 fprintf( stderr, "fusefile_read( %s )\n", path );
278 if ( strcmp( path, "/" ) != 0 ) {
282 fprintf( stderr, "read %ld %ld\n", off, size );
287 fprintf( stderr, "find_source %ld %ld\n", off, size );
289 int i = find_source( off );
293 if ( sources.array[i].fd < 0 ) {
297 print_source( &sources.array[i] );
299 size_t b = off - sources.array[i].start + sources.array[i].from;
300 size_t n = sources.array[i].to - b;
304 if ( lseek( sources.array[i].fd, b, SEEK_SET ) < 0 ) {
305 perror( sources.array[i].filename );
309 fprintf( stderr, "get %ld bytes at %ld\n", n, rr );
311 ssize_t r = read( sources.array[i].fd, buf + rr, n );
313 fprintf( stderr, "got %ld bytes\n", r );
316 perror( sources.array[i].filename );
326 times.atime = time( 0 );
331 * Write a full block of data.
333 static int write_block(int fd,const char *buf,size_t size) {
336 ssize_t n = write( fd, buf, size );
346 static int fusefile_write_buf(const char *path, struct fuse_bufvec *buf,
347 off_t off, struct fuse_file_info *fi) {
349 fprintf( stderr, "fusefile_write_buf( %s )\n", path );
351 if ( strcmp( path, "/" ) != 0 ) {
355 // Ensure a pad was nominated
356 if ( pad.filename == 0 ) {
360 // Determine total size
364 fprintf( stderr, "count = %ld\n", buf->count );
366 for ( i = 0; i < buf->count; i++ ) {
367 struct fuse_buf *p = &buf->buf[ i ];
370 static char meta[ 100 ];
371 sprintf( meta, "%ld\n%ld\n", off, size );
373 fprintf( stderr, "meta( %ld %ld )\n", off, size );
375 if ( write_block( pad.fd, meta, strlen( meta ) ) <= 0 ) {
376 perror( pad.filename );
379 struct Source source = {
380 .filename = pad.filename,
382 .from = lseek( pad.fd, 0, SEEK_END ),
386 for ( i = 0; i < buf->count; i++ ) {
387 struct fuse_buf *p = &buf->buf[i];
388 if ( p->flags & FUSE_BUF_IS_FD ) {
390 fprintf( stderr, "Content held in a file ... HELP!!\n" );
394 ssize_t n = write_block( pad.fd, (char*) p->mem, p->size );
395 if ( n != p->size ) {
400 source.to = source.from + size;
401 insert_source( &source, off );
402 times.mtime = time( 0 );
407 * Insert a fragment at <off>. The data is appended to the pad file,
408 * and a descriptor is inserted; the fragment containing <off> is
409 * first split, unless <off> is at its start, and then new fragment
410 * descriptor is inserted.
412 static int fusefile_write(const char *path, const char *buf, size_t size,
413 off_t off, struct fuse_file_info *fi)
416 fprintf( stderr, "fusefile_write( %s %ld )\n", path, size );
418 if ( strcmp( path, "/" ) != 0 ) {
422 // Ensure a pad was nominated
423 if ( pad.filename == 0 ) {
426 static char meta[ 100 ];
427 sprintf( meta, "%ld\n%ld\n", off, size );
428 if ( write_block( pad.fd, meta, strlen( meta ) ) <= 0 ) {
429 perror( pad.filename );
432 struct Source source = {
433 .filename = pad.filename,
435 .from = lseek( pad.fd, 0, SEEK_END ),
439 ssize_t n = write_block( pad.fd, buf, size );
443 source.to = source.from + size;
444 insert_source( &source, off );
445 times.mtime = time( 0 );
449 static void fusefile_destroy(void *data) {
450 char *mnt = (char*) data; // As passed to fuse_main
452 fprintf( stderr, "fusefile_destroy( %s )\n", mnt? mnt : "" );
459 static int fusefile_flush(const char *path, struct fuse_file_info *info) {
461 fprintf( stderr, "fusefile_flush( %s )\n", path );
463 if ( strcmp( path, "/" ) != 0 ) {
469 static int fusefile_release(const char *path, struct fuse_file_info *fi) {
471 fprintf( stderr, "fusefile_release( %s, %d )\n", path, fi->flags );
473 if ( strcmp( path, "/" ) != 0 ) {
479 static int fusefile_fsync(const char *path, int x, struct fuse_file_info *fi) {
481 fprintf( stderr, "fusefile_fsync( %s, %d )\n", path, x );
483 if ( strcmp( path, "/" ) != 0 ) {
492 static int fusefile_truncate(const char *path, off_t len) {
494 fprintf( stderr, "fusefile_truncate( %s, %ld )\n", path, len );
496 if ( strcmp( path, "/" ) != 0 ) {
502 static struct fuse_operations fusefile_oper = {
503 .getattr = fusefile_getattr,
504 .open = fusefile_open,
505 .read = fusefile_read,
506 .write = fusefile_write,
507 .write_buf = fusefile_write_buf,
508 .destroy = fusefile_destroy,
509 .flush = fusefile_flush,
510 .release = fusefile_release,
511 .fsync = fusefile_fsync,
512 .truncate = fusefile_truncate,
513 //.truncate = fusefile_truncate,
514 //.release = fusefile_release,
515 //void *(*init) (struct fuse_conn_info *conn);
518 static void usage() {
520 "Usage: fusefile [ <fuse options> ] <mount> <file/from-to> ... \n"
521 "Mounts a virtual, read-only file that is a concatenation of file fragments\n"
523 fprintf( stderr, "%s", usage );
528 * Set up the arguments for the fuse_main call, adding our own.
530 static int setup_argv(int argc,char ***argv) {
532 char **out = calloc( n--, sizeof( char* ) );
533 memcpy( (void*) out, (void*) (*argv), argc-- * sizeof( char* ) );
534 out[ n++ ] = out[ argc ]; // mount point
535 out[ argc++ ] = "-odefault_permissions";
541 * Mount a concatenation of files,
542 * [ <fuse options> ] <mount> <file/from-to> ...
544 int main(int argc, char *argv[])
554 for ( i = 1; i < argc; i++ ) {
555 if ( *argv[i] != '-' ) {
559 if ( i > argc - 2 ) { // At least mount point plus one source
562 mnt = argv[ i++ ]; // First non-option argument is the mount pount
564 if ( strncmp( argv[ i ], "pad=", 4 ) == 0 ) {
565 // First argument is the pad, if any, signaled with "pad=" prefix
566 pad.filename = argv[ i++ ] + 4; // (also move arg index)
568 fprintf( stderr, "scratch pad=%s\n", pad.filename );
570 pad.fd = open( pad.filename, O_RDWR | O_CREAT, 0600 );
572 perror( pad.filename );
575 lseek( pad.fd, 0, SEEK_END ); // initial seek
577 if ( setup_sources( argv, i, argc-i ) ) {
580 if ( stat( mnt, &stbuf ) == -1 ) {
581 int fd = open( mnt, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR );
586 time_t now = time( 0 );
592 } else if ( ! S_ISREG( stbuf.st_mode ) ) {
593 fprintf( stderr, "mountpoint is not a regular file\n" );
596 times.atime = stbuf.st_atime;
597 times.mtime = stbuf.st_mtime;
598 times.ctime = stbuf.st_ctime;
601 fuseargc = setup_argv( fuseargc, &argv );
602 struct fuse_args args = FUSE_ARGS_INIT( fuseargc, argv );
603 if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {
606 fuse_opt_free_args( &args );
608 fprintf( stderr, "missing mountpoint parameter\n" );
611 return fuse_main( fuseargc, argv, &fusefile_oper, temporary? mnt : NULL );