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
52 #define SOURCEARRAYP(i) ((void*)&sources.array[ i ])
55 * Holds info about scratch pad file for 'write' events.
63 static void print_source(struct Source *p) {
64 fprintf( stderr, "%p { %s, %ld, %ld, %ld, %d }\n",
65 p, p->filename, p->from, p->to, p->start, p->fd );
69 // Scan the source specification, and return the length of the
70 // inclusion. "filename/from,to"
75 static size_t scan_source(char *in,struct Source *p) {
80 // scan for last '/' and last '-'
81 for ( ; i >= 0; i-- ) {
91 fprintf( stderr, "m=%d s=%d\n", m, s );
93 // Copy the filename, and set from and to
94 p->filename = strndup( in, ( s < 0 )? e : s );
96 if ( stat( p->filename, & buf ) ) {
97 perror( p->filename );
100 p->from = ( s < 0 )? 0 : atol( in+s+1 );
105 fprintf( stderr, "p->from=%ld\n", p->from );
107 p->to = ( m < 0 )? buf.st_size : atol( in+m+1 );
108 if ( p->from > p->to || p->to > buf.st_size ) {
114 static int setup_sources(char **argv,int i,int n) {
115 sources.array = calloc( n, sizeof( struct Source ) );
116 if ( sources.array == 0 ) {
123 for ( ; j < n; i++, j++ ) {
124 struct Source *p = sources.array + j;
125 if ( scan_source( argv[i], p ) ) {
126 // should free everything malloc-ed
129 p->start = sources.size;
130 sources.size += p->to - p->from;
131 p->fd = open( p->filename, O_RDONLY );
133 perror( p->filename );
143 static int fusefile_getattr(const char *path,struct stat *stbuf) {
145 fprintf( stderr, "fusefile_getattr( %s )\n", path );
147 if ( strcmp( path, "/" ) != 0 ) {
151 fprintf( stderr, "getattr %ld\n", sources.size );
153 memset( stbuf, 0, sizeof( struct stat ) );
154 stbuf->st_mode = S_IFREG | 0444; // Hmmm
155 if ( pad.filename ) {
156 stbuf->st_mode |= 0200;
159 stbuf->st_size = sources.size;
160 time_t now = time( 0 );
161 stbuf->st_atime = now;
162 stbuf->st_mtime = now;
163 stbuf->st_ctime = now;
164 stbuf->st_uid = getuid();
165 stbuf->st_gid = getgid();
169 static int fusefile_open(const char *path,struct fuse_file_info *fi) {
171 fprintf( stderr, "fusefile_open( %s, %d )\n", path, fi->flags );
172 fprintf( stderr, "fixing( %d )\n", fi->flags | O_CLOEXEC );
174 if ( strcmp( path, "/" ) != 0 ) {
177 // set O-CLOEXEC for this opening?
181 static int find_source(off_t offset) {
183 int hi = sources.count;
184 if ( offset > sources.size ) {
188 int m = ( lo + hi ) / 2;
189 if ( sources.array[m].start > offset ) {
191 } else if ( m+1 < hi && sources.array[m+1].start < offset ) {
201 * Insert a source fragment description into the table at <off>.
203 static int insert_source(struct Source *source,size_t off) {
204 int index = find_source( off );
205 // Ensure at least 5 "free" Sources in <source.array>
206 // and allocate space for 20 new otherwise.
207 if ( sources.count + 5 > sources.limit ) {
208 size_t size = sources.limit + 20;
209 struct Source *new = realloc(
210 sources.array, size * sizeof( struct Source ) );
215 sources.limit = size;
217 if ( index < sources.count ) {
218 ssize_t b = ( sources.count - index ) * sizeof(struct Source);
219 if ( sources.array[ index ].start < off ) {
220 // Split the <index> record at <off>
223 memcpy( SOURCEARRAYP( index+2 ), SOURCEARRAYP( index ), b );
225 b = off - sources.array[ index ].start;
226 sources.array[ index + 2 ].from += b; // adjust tail fragment
227 sources.array[ index++ ].to = b; // adjust head fragment
229 // Insert the new source at <index>
230 memcpy( SOURCEARRAYP( index+1 ), SOURCEARRAYP( index ), b );
234 // Append the new source to <sources> (at <index>)
237 sources.array[ index ].filename = source->filename;
238 sources.array[ index ].fd = source->fd;
239 sources.array[ index ].from = source->from;
240 sources.array[ index ].to = source->to;
241 for ( ; index < sources.count; index++ ) {
242 sources.array[ index ].start = off;
243 off += sources.array[ index ].to - sources.array[ index ].from;
249 // Read <size> bytes from <off> in file
250 static int fusefile_read(const char *path, char *buf, size_t size,
251 off_t off, struct fuse_file_info *fi)
254 fprintf( stderr, "fusefile_read( %s )\n", path );
256 if ( strcmp( path, "/" ) != 0 ) {
260 fprintf( stderr, "read %ld %ld\n", off, size );
265 fprintf( stderr, "find_source %ld %ld\n", off, size );
267 int i = find_source( off );
271 if ( sources.array[i].fd < 0 ) {
275 print_source( &sources.array[i] );
277 size_t b = off - sources.array[i].start + sources.array[i].from;
278 size_t n = sources.array[i].to - b;
282 if ( lseek( sources.array[i].fd, b, SEEK_SET ) < 0 ) {
283 perror( sources.array[i].filename );
287 fprintf( stderr, "get %ld bytes at %ld\n", n, rr );
289 ssize_t r = read( sources.array[i].fd, buf + rr, n );
291 fprintf( stderr, "got %ld bytes\n", r );
294 perror( sources.array[i].filename );
308 * Write a full block of data.
310 static int write_block(int fd,const char *buf,size_t size) {
313 ssize_t n = write( fd, buf, size );
322 * Insert a fragment at <off>. The data is appended to the pad file,
323 * and a descriptor is inserted; the fragment containing <off> is
324 * first split, unless <off> is at its start, and then new fragment
325 * descriptor is inserted.
327 static int fusefile_write(const char *path, const char *buf, size_t size,
328 off_t off, struct fuse_file_info *fi)
331 fprintf( stderr, "fusefile_write( %s )\n", path );
333 if ( strcmp( path, "/" ) != 0 ) {
337 fprintf( stderr, "write %ld %ld\n", off, size );
339 // Ensure a pad was nominated
340 if ( pad.filename == 0 ) {
343 static char meta[ 100 ];
344 sprintf( meta, "%ld\n%ld\n", off, size );
345 if ( write_block( pad.fd, meta, strlen( meta ) ) <= 0 ) {
346 perror( pad.filename );
349 struct Source source = {
350 .filename = pad.filename,
352 .from = lseek( pad.fd, 0, SEEK_END ),
356 ssize_t n = write_block( pad.fd, buf, size );
360 source.to = source.from + size;
361 insert_source( &source, off );
365 static void fusefile_destroy(void *data) {
366 char *mnt = (char*) data; // As passed to fuse_main
368 fprintf( stderr, "fusefile_destroy( %s )\n", mnt? mnt : "" );
375 static int fusefile_flush(const char *path, struct fuse_file_info *info) {
377 fprintf( stderr, "fusefile_flush( %s )\n", path );
379 if ( strcmp( path, "/" ) != 0 ) {
385 static int fusefile_release(const char *path, struct fuse_file_info *fi) {
387 fprintf( stderr, "fusefile_release( %s, %d )\n", path, fi->flags );
389 if ( strcmp( path, "/" ) != 0 ) {
395 static int fusefile_fsync(const char *path, int x, struct fuse_file_info *fi) {
397 fprintf( stderr, "fusefile_fsync( %s, %d )\n", path, x );
399 if ( strcmp( path, "/" ) != 0 ) {
405 static struct fuse_operations fusefile_oper = {
406 .getattr = fusefile_getattr,
407 .open = fusefile_open,
408 .read = fusefile_read,
409 .write = fusefile_write,
410 .destroy = fusefile_destroy,
411 .flush = fusefile_flush,
412 .release = fusefile_release,
413 .fsync = fusefile_fsync,
414 //void *(*init) (struct fuse_conn_info *conn);
415 //int (*write_buf) (const char *, struct fuse_bufvec *buf, off_t off,
416 // struct fuse_file_info *);
417 //.truncate = fusefile_truncate,
418 //.flush = fusefile_flush,
419 //.release = fusefile_release,
422 static void usage() {
424 "Usage: fusefile [ <fuse options> ] <mount> <file/from-to> ... \n"
425 "Mounts a virtual, read-only file that is a concatenation of file fragments\n"
427 fprintf( stderr, "%s", usage );
432 * Set up the arguments for the fuse_main call, adding our own.
434 static int setup_argv(int argc,char ***argv) {
436 char **out = calloc( n--, sizeof( char* ) );
437 memcpy( (void*) out, (void*) (*argv), argc-- * sizeof( char* ) );
438 out[ n++ ] = out[ argc ]; // mount point
439 out[ argc++ ] = "-odefault_permissions";
445 * Mount a concatenation of files,
446 * [ <fuse options> ] <mount> <file/from-to> ...
448 int main(int argc, char *argv[])
458 for ( i = 1; i < argc; i++ ) {
459 if ( *argv[i] != '-' ) {
463 if ( i > argc - 2 ) { // At least mount point plus one source
466 mnt = argv[ i++ ]; // First non-option argument is the mount pount
468 if ( strncmp( argv[ i ], "pad=", 4 ) == 0 ) {
469 // First argument is the pad, if any, signaled with "pad=" prefix
470 pad.filename = argv[ i++ ] + 4; // (also move arg index)
472 fprintf( stderr, "scratch pad=%s\n", pad.filename );
474 pad.fd = open( pad.filename, O_RDWR | O_CREAT, 0600 );
476 perror( pad.filename );
479 lseek( pad.fd, 0, SEEK_END ); // initial seek
481 if ( setup_sources( argv, i, argc-i ) ) {
484 if ( stat( mnt, &stbuf ) == -1 ) {
485 int fd = open( mnt, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR );
492 } else if ( ! S_ISREG( stbuf.st_mode ) ) {
493 fprintf( stderr, "mountpoint is not a regular file\n" );
496 fuseargc = setup_argv( fuseargc, &argv );
497 struct fuse_args args = FUSE_ARGS_INIT( fuseargc, argv );
498 if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {
501 fuse_opt_free_args( &args );
503 fprintf( stderr, "missing mountpoint parameter\n" );
506 return fuse_main( fuseargc, argv, &fusefile_oper, temporary? mnt : NULL );