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 );
64 // Scan the source specification, and return the length of the
65 // inclusion. "filename/from,to"
70 static size_t scan_source(char *in,struct Source *p) {
75 // scan for last '/' and last '-'
76 for ( ; i >= 0; i-- ) {
86 fprintf( stderr, "m=%d s=%d\n", m, s );
88 // Copy the filename, and set from and to
89 p->filename = strndup( in, ( s < 0 )? e : s );
91 if ( stat( p->filename, & buf ) ) {
92 perror( p->filename );
95 p->from = ( s < 0 )? 0 : atol( in+s+1 );
100 fprintf( stderr, "p->from=%ld\n", p->from );
102 p->to = ( m < 0 )? buf.st_size : atol( in+m+1 );
103 if ( p->from > p->to || p->to > buf.st_size ) {
109 static int setup_sources(char **argv,int i,int n) {
110 sources.array = calloc( n, sizeof( struct Source ) );
111 if ( sources.array == 0 ) {
117 for ( ; j < n; i++, j++ ) {
118 struct Source *p = sources.array + j;
119 if ( scan_source( argv[i], p ) ) {
120 // should free everything malloc-ed
123 p->start = sources.size;
124 sources.size += p->to - p->from;
125 p->fd = open( p->filename, O_RDWR );
127 fprintf( stderr, "** %s opened read-only\n", p->filename );
128 p->fd = open( p->filename, O_RDONLY );
131 perror( p->filename );
141 static int fusefile_getattr(const char *path,struct stat *stbuf) {
143 fprintf( stderr, "fusefile_getattr( %s )\n", path );
145 if ( strcmp( path, "/" ) != 0 ) {
149 fprintf( stderr, "getattr %ld\n", sources.size );
151 memset( stbuf, 0, sizeof( struct stat ) );
152 stbuf->st_mode = S_IFREG | 0644; // Hmmm
154 stbuf->st_size = sources.size;
155 stbuf->st_atime = times.atime;
156 stbuf->st_mtime = times.mtime;
157 stbuf->st_ctime = times.ctime;
158 stbuf->st_uid = getuid();
159 stbuf->st_gid = getgid();
163 static int fusefile_chmod(const char *path,mode_t m) {
165 fprintf( stderr, "fusefile_chmod( %s, %d )\n", path, m );
170 static int fusefile_open(const char *path,struct fuse_file_info *fi) {
172 fprintf( stderr, "fusefile_open( %s, %d )\n", path, fi->flags );
173 fprintf( stderr, "fixing( %d )\n", fi->flags | O_CLOEXEC );
175 if ( strcmp( path, "/" ) != 0 ) {
178 // set O-CLOEXEC for this opening?
179 times.atime = time( 0 );
183 static int find_source(off_t offset) {
185 int hi = sources.count;
186 if ( offset >= sources.size ) {
189 while ( lo + 1 < hi ) {
190 int m = ( lo + hi ) / 2;
191 if ( offset < sources.array[ m ].start ) {
200 // Read <size> bytes from <offset> in file
201 static int fusefile_read(const char *path, char *buf, size_t size,
202 off_t off, struct fuse_file_info *fi)
205 fprintf( stderr, "fusefile_read( %s )\n", path );
207 if( strcmp( path, "/" ) != 0 ) {
211 fprintf( stderr, "read %ld %ld\n", off, size );
216 fprintf( stderr, "find_source %ld %ld\n", off, size );
218 int i = find_source( off );
220 return ( off == sources.size )? 0 : -ENOENT;
222 if ( sources.array[i].fd < 0 ) {
226 print_source( &sources.array[i] );
228 size_t b = off - sources.array[i].start + sources.array[i].from;
229 size_t n = sources.array[i].to - b;
233 if ( lseek( sources.array[i].fd, b, SEEK_SET ) < 0 ) {
234 perror( sources.array[i].filename );
238 fprintf( stderr, "get %ld bytes at %ld\n", n, rr );
240 ssize_t r = read( sources.array[i].fd, buf + rr, n );
242 fprintf( stderr, "got %ld bytes\n", r );
245 perror( sources.array[i].filename );
255 times.atime = time( 0 );
260 * Write a full block of data over the sources at the offset
262 static int write_block(off_t off,const char *buf,size_t size) {
264 fprintf( stderr, "write_block( %ld, ?, %ld )\n", off, size );
267 int index = find_source( off ); // index of source file
269 return -EIO; // past EOF
271 struct Source *source = &sources.array[ index ];
272 off_t from = off - source->start;
273 off_t max = source->to - source->from - from;
274 if ( lseek( source->fd, from, SEEK_SET ) < 0 ) {
277 ssize_t todo = ( size < max )? size : max;
279 times.mtime = time( 0 );
280 ssize_t n = write( source->fd, buf, todo );
282 return -EIO; // Something wrong
293 static int fusefile_write_buf(const char *path, struct fuse_bufvec *buf,
294 off_t off, struct fuse_file_info *fi) {
296 fprintf( stderr, "fusefile_write_buf( %s )\n", path );
298 if ( strcmp( path, "/" ) != 0 ) {
304 for ( i = 0; i < buf->count; i++ ) {
305 struct fuse_buf *p = &buf->buf[i];
306 if ( p->flags & FUSE_BUF_IS_FD ) {
308 fprintf( stderr, "Content held in a file ... HELP!!\n" );
312 if ( write_block( off, (char*) p->mem, p->size ) < 0 ) {
318 fprintf( stderr, "fusefile_write_buf written %ld\n", size );
324 * Write a fragment at <off>. This overwrites files.
326 static int fusefile_write(const char *path, const char *buf, size_t size,
327 off_t off, struct fuse_file_info *fi)
330 fprintf( stderr, "fusefile_write( %s %ld )\n", path, size );
332 if ( strcmp( path, "/" ) != 0 ) {
336 if ( write_block( off, buf, size ) < 0 ) {
342 static void fusefile_destroy(void *data) {
343 char *mnt = (char*) data; // As passed to fuse_main
345 fprintf( stderr, "fusefile_destroy( %s )\n", mnt? mnt : "" );
352 static int fusefile_flush(const char *path, struct fuse_file_info *info) {
354 fprintf( stderr, "fusefile_flush( %s )\n", path );
356 if ( strcmp( path, "/" ) != 0 ) {
362 static int fusefile_release(const char *path, struct fuse_file_info *fi) {
364 fprintf( stderr, "fusefile_release( %s, %d )\n", path, fi->flags );
366 if ( strcmp( path, "/" ) != 0 ) {
372 static int fusefile_fsync(const char *path, int x, struct fuse_file_info *fi) {
374 fprintf( stderr, "fusefile_fsync( %s, %d )\n", path, x );
376 if ( strcmp( path, "/" ) != 0 ) {
385 static int fusefile_truncate(const char *path, off_t len) {
387 fprintf( stderr, "fusefile_truncate( %s, %ld )\n", path, len );
389 if ( strcmp( path, "/" ) != 0 ) {
395 static struct fuse_operations fusefile_oper = {
396 .getattr = fusefile_getattr,
397 .chmod = fusefile_chmod,
398 .open = fusefile_open,
399 .read = fusefile_read,
400 .write = fusefile_write,
401 .write_buf = fusefile_write_buf,
402 .destroy = fusefile_destroy,
403 .flush = fusefile_flush,
404 .release = fusefile_release,
405 .fsync = fusefile_fsync,
406 .truncate = fusefile_truncate,
407 //.truncate = fusefile_truncate,
408 //.release = fusefile_release,
409 //void *(*init) (struct fuse_conn_info *conn);
412 static void usage() {
414 "Usage: fusefile [ <fuse options> ] <mount> <file/from-to> ... \n"
415 "Mounts a virtual, read-only file that is a concatenation of file fragments\n"
417 fprintf( stderr, "%s", usage );
422 * Set up the arguments for the fuse_main call, adding our own.
424 static int setup_argv(int argc,char ***argv) {
426 "-odefault_permissions",
427 (*argv)[ --argc ] // note: (*argv)[ argc-1 ] = the mount point
429 #define OURSN ( sizeof( OURS ) / sizeof( char* ) )
430 int N = argc + OURSN; // new argv-tobe size, excluding null
431 char **out = malloc( ( N + 1 ) * sizeof( char* ) );
433 for ( i = 0; i < argc; i++ ) {
434 out[ i ] = (*argv)[i];
435 fprintf( stderr, " %s", out[ i ] );
437 for ( i = 0; i < OURSN; i++ ) {
438 out[ argc + i ] = OURS[i];
439 fprintf( stderr, " %s", out[ i ] );
442 fprintf( stderr, "\n" );
448 * Mount a concatenation of files,
449 * [ <fuse options> ] <mount> <file/from-to> ...
451 int main(int argc, char *argv[])
461 for ( i = 1; i < argc; i++ ) {
462 if ( *argv[i] != '-' ) {
466 if ( i > argc - 2 ) { // At least mount point plus one source
469 mnt = argv[ i++ ]; // First non-option argument is the mount pount
471 if ( setup_sources( argv, i, argc-i ) ) {
474 if ( stat( mnt, &stbuf ) == -1 ) {
475 int fd = open( mnt, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR );
480 time_t now = time( 0 );
486 } else if ( ! S_ISREG( stbuf.st_mode ) ) {
487 fprintf( stderr, "mountpoint is not a regular file\n" );
490 times.atime = stbuf.st_atime;
491 times.mtime = stbuf.st_mtime;
492 times.ctime = stbuf.st_ctime;
495 fuseargc = setup_argv( fuseargc, &argv );
496 struct fuse_args args = FUSE_ARGS_INIT( fuseargc, argv );
497 if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {
500 fuse_opt_free_args( &args );
502 fprintf( stderr, "missing mountpoint parameter\n" );
505 return fuse_main( fuseargc, argv, &fusefile_oper, temporary? mnt : NULL );