change version
[rrq/fusefile.git] / fusefile.c
index 632221f917b33c12e51892d6956c8893e3138a14..55ba86aae686986003fefb3b6e083abfb44d4b75 100644 (file)
@@ -1,17 +1,31 @@
-/*
-  FUSE: Filesystem in Userspace
-  Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
+/***
+    fusefile - overlay a file path with a concatenation of parts of
+    other files, read only.
 
-  This program can be distributed under the terms of the GNU GPL.
-  See the file COPYING.
+    Copyright (C) 2019  Ralph Ronnquist
 
-  Overlay a file path with a concatenation of parts of other files.
-  read only
+    This program is free software: you can redistribute it and/or
+    modify it under the terms of the GNU General Public License as
+    published by the Free Software Foundation, either version 3 of the
+    License, or (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+    General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program. If not, see
+    <http://www.gnu.org/licenses/>.
+
+    This source was inspired by the "null.c" example of the libfuse
+    sources, which is distributed under GPL2, and copyright (C)
+    2001-2007 Miklos Szeredi <miklos@szeredi.hu>.
 */
 
-#define FUSE_USE_VERSION 31
+#define FUSE_USE_VERSION 33
 
-#include <fuse/fuse.h>
+#include <fuse.h>
 #include <fuse/fuse_lowlevel.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 struct Source {
     char *filename;
-    size_t from;
-    size_t to;
-    size_t start; // starting position in concatenated file
+    ssize_t from;
+    ssize_t to;
+    ssize_t start; // starting position in concatenated file
     int fd;
 };
 
 static struct {
     struct Source *array;
     int count;
-    size_t size;
+    int limit;
+    ssize_t size;
 } sources;
 
+static struct {
+    time_t atime;
+    time_t mtime;
+    time_t ctime;
+} times;
+    
+#define SOURCEARRAYP(i) ((void*)&sources.array[ i ])
+
+/**
+ * Holds info about scratch pad file for 'write' events.
+ */
+static struct {
+    char *filename;
+    int fd;
+} pad;
+
 #if DEBUG
 static void print_source(struct Source *p) {
     fprintf( stderr, "%p { %s, %ld, %ld, %ld, %d }\n",
-            p, p->filename, p->from, p->to, p->start, p-> fd );
+            p, p->filename, p->from, p->to, p->start, p->fd );
 }
 #endif
 
@@ -62,6 +93,9 @@ static size_t scan_source(char *in,struct Source *p) {
            m = i;
        }
     }
+#if DEBUG
+    fprintf( stderr, "m=%d s=%d\n", m, s );
+#endif
     // Copy the filename, and set from and to
     p->filename = strndup( in, ( s < 0 )? e : s );
     struct stat buf;
@@ -73,6 +107,9 @@ static size_t scan_source(char *in,struct Source *p) {
     if ( p->from < 0 ) {
        p->from = 0;
     }
+#if DEBUG
+    fprintf( stderr, "p->from=%ld\n", p->from );
+#endif
     p->to = ( m < 0 )? buf.st_size : atol( in+m+1 );
     if ( p->from > p->to || p->to > buf.st_size ) {
        return 1;
@@ -86,6 +123,7 @@ static int setup_sources(char **argv,int i,int n) {
        return 1;
     }
     sources.count = n;
+    sources.limit = n;
     int j = 0;
     sources.size = 0;
     for ( ; j < n; i++, j++ ) {
@@ -108,8 +146,10 @@ static int setup_sources(char **argv,int i,int n) {
     return 0;
 }
 
-static int fusefile_getattr(const char *path, struct stat *stbuf )
-{
+static int fusefile_getattr(const char *path,struct stat *stbuf) {
+#if DEBUG
+    fprintf( stderr, "fusefile_getattr( %s )\n", path );
+#endif
     if ( strcmp( path, "/" ) != 0 ) {
        return -ENOENT;
     }
@@ -118,22 +158,29 @@ static int fusefile_getattr(const char *path, struct stat *stbuf )
 #endif
     memset( stbuf, 0, sizeof( struct stat ) );
     stbuf->st_mode = S_IFREG | 0444; // Hmmm
+    if ( pad.filename ) {
+       stbuf->st_mode |= 0200;
+    }
     stbuf->st_nlink = 1;
     stbuf->st_size = sources.size;
-    time_t now = time( 0 );
-    stbuf->st_atime = now;
-    stbuf->st_mtime = now;
-    stbuf->st_ctime = now;
+    stbuf->st_atime = times.atime;
+    stbuf->st_mtime = times.mtime;
+    stbuf->st_ctime = times.ctime;
     stbuf->st_uid = getuid();
     stbuf->st_gid = getgid();
     return 0;
 }
 
-static int fusefile_open(const char *path, struct fuse_file_info *fi)
-{
+static int fusefile_open(const char *path,struct fuse_file_info *fi) {
+#if DEBUG
+    fprintf( stderr, "fusefile_open( %s, %d )\n", path, fi->flags );
+    fprintf( stderr, "fixing( %d )\n", fi->flags | O_CLOEXEC );
+#endif
     if ( strcmp( path, "/" ) != 0 ) {
        return -ENOENT;
     }
+    // set O-CLOEXEC  for this opening?
+    times.atime = time( 0 );
     return 0;
 }
 
@@ -143,35 +190,103 @@ static int find_source(off_t offset) {
     if ( offset > sources.size ) {
        return -1;
     }
-    while ( lo < hi ) {
+    while ( lo + 1 < hi ) {
        int m = ( lo + hi ) / 2;
-       if ( sources.array[m].start > offset ) {
+       if ( offset < sources.array[ m ].start ) {
            hi = m;
-       } else if ( m+1 < hi && sources.array[m+1].start < offset ) {
-           lo = m+1;
        } else {
-           return m;
+           lo = m;
        }
     }
     return lo;
 }
 
-// Read <size> bytes from <offset> in file
+/**
+ * Insert a source fragment description into the table at <off>.
+ */
+static int insert_source(struct Source *source,size_t off) {
+    int index = find_source( off );
+    int i;
+    // Ensure at least 5 "free" Sources in <source.array>
+    // and allocate space for 20 new otherwise.
+    if ( sources.count + 5 > sources.limit ) {
+       size_t size = sources.limit + 20;
+       struct Source *new = realloc(
+           sources.array, size * sizeof( struct Source ) );
+       if ( new == 0 ) {
+           return -1;
+       }
+       sources.array = new;
+       sources.limit = size;
+    }
+#if DEBUG
+    fprintf( stderr, "index=%d\n", index );
+#endif
+    if ( index < sources.count ) {
+       ssize_t b = ( sources.count - index ) * sizeof(struct Source);
+#if DEBUG
+    fprintf( stderr, "b=%ld\n", b );
+#endif
+       if ( sources.array[ index ].start < off ) {
+           // Split the <index> record at <off>
+           // and adjust index
+           memcpy( SOURCEARRAYP( index+2 ), SOURCEARRAYP( index ), b );
+           sources.count += 2;
+           b = off - sources.array[ index ].start;
+           sources.array[ index + 2 ].from += b; // adjust tail fragment
+           sources.array[ index++ ].to = b; // adjust head fragment
+#if DEBUG
+           print_source( &sources.array[ index-1 ] );
+           print_source( &sources.array[ index ] );
+           print_source( &sources.array[ index+1 ] );
+           fprintf( stderr, "---\n");
+#endif
+       } else {
+           // Insert the new source at <index>
+           memcpy( SOURCEARRAYP( index+1 ), SOURCEARRAYP( index ), b );
+           sources.count += 1;
+       }
+    } else {
+       // Append the new source to <sources> (at <index>)
+       sources.count += 1;
+    }
+    sources.array[ index ].filename = source->filename;
+    sources.array[ index ].fd = source->fd;
+    sources.array[ index ].from = source->from;
+    sources.array[ index ].to = source->to;
+    for ( i = index; i < sources.count; i++ ) {
+       sources.array[ i ].start = off;
+       off += sources.array[ i ].to - sources.array[ i ].from;
+#if DEBUG
+       print_source( &sources.array[ i ] );
+#endif
+    }
+    sources.size = off;
+#if DEBUG
+    fprintf( stderr, "count=%d size=%ld\n", sources.count, sources.size );
+#endif
+    return index;
+}
+
+// Read <size> bytes from <off> in file
 static int fusefile_read(const char *path, char *buf, size_t size,
-                     off_t offset, struct fuse_file_info *fi)
+                        off_t off, struct fuse_file_info *fi)
 {
-    if( strcmp( path, "/" ) != 0 ) {
+#if DEBUG
+    fprintf( stderr, "fusefile_read( %s )\n", path );
+#endif
+    if ( strcmp( path, "/" ) != 0 ) {
        return -ENOENT;
     }
 #if DEBUG
-    fprintf( stderr, "read %ld %ld\n", offset, size );
+    fprintf( stderr, "read %ld %ld\n", off, size );
 #endif
     size_t rr = 0;
     while ( size > 0 ) {
 #if DEBUG
-       fprintf( stderr, "find_source %ld %ld\n", offset, size );
+       fprintf( stderr, "find_source %ld %ld\n", off, size );
 #endif
-       int i = find_source( offset );
+       int i = find_source( off );
        if ( i < 0 ) {
            return -ENOENT;
        }
@@ -181,7 +296,7 @@ static int fusefile_read(const char *path, char *buf, size_t size,
 #if DEBUG
        print_source( &sources.array[i] );
 #endif
-       size_t b = offset - sources.array[i].start + sources.array[i].from;
+       size_t b = off - sources.array[i].start + sources.array[i].from;
        size_t n = sources.array[i].to - b;
        if ( n > size ) {
            n = size;
@@ -205,38 +320,226 @@ static int fusefile_read(const char *path, char *buf, size_t size,
            break;
        }
        rr += r;
-       offset += r;
+       off += r;
        size -= r;
     }
+    times.atime = time( 0 );
     return rr;
 }
 
+/**
+ * Write a full block of data.
+ */
+static int write_block(int fd,const char *buf,size_t size) {
+    size_t orig = size;
+    while ( size > 0 ) {
+       ssize_t n = write( fd, buf, size );
+       if ( n <= 0 ) {
+           return n;
+       }
+       buf += n;
+       size -= n;
+    }
+    return orig;
+}
+
+static int fusefile_write_buf(const char *path, struct fuse_bufvec *buf,
+                             off_t off, struct fuse_file_info *fi) {
+#if DEBUG
+    fprintf( stderr, "fusefile_write_buf( %s )\n", path );
+#endif
+    if ( strcmp( path, "/" ) != 0 ) {
+       return -ENOENT;
+    }
+    
+    // Ensure a pad was nominated
+    if ( pad.filename == 0 ) {
+       return 1;
+    }
+
+    // Determine total size
+    size_t size = 0;
+    int i;
+#if DEBUG
+    fprintf( stderr, "count = %ld\n", buf->count );
+#endif
+    for ( i = 0; i < buf->count; i++ ) {
+       struct fuse_buf *p = &buf->buf[ i ];
+       size += p->size;
+    }
+    static char meta[ 100 ];
+    sprintf( meta, "%ld\n%ld\n", off, size );
+#if DEBUG
+    fprintf( stderr, "meta( %ld %ld )\n", off, size );
+#endif
+    if ( write_block( pad.fd, meta, strlen( meta ) ) <= 0 ) {
+       perror( pad.filename );
+       return -EIO;
+    }
+    struct Source source = {
+       .filename = pad.filename,
+       .fd = pad.fd,
+       .from = lseek( pad.fd, 0, SEEK_END ),
+       .to = 0,
+       .start = 0
+    };
+    for ( i = 0; i < buf->count; i++ ) {
+       struct fuse_buf *p = &buf->buf[i];
+       if ( p->flags & FUSE_BUF_IS_FD ) {
+#if DEBUG
+           fprintf( stderr, "Content held in a file ... HELP!!\n" );
+#endif
+           return -EIO;
+       } else {
+           ssize_t n = write_block( pad.fd, (char*) p->mem, p->size );
+           if ( n != p->size ) {
+               return -EIO;
+           }
+       }
+    }
+    source.to = source.from + size;
+    insert_source( &source, off );
+    times.mtime = time( 0 );
+    return size;
+}
+
+/**
+ * Insert a fragment at <off>. The data is appended to the pad file,
+ * and a descriptor is inserted; the fragment containing <off> is
+ * first split, unless <off> is at its start, and then new fragment
+ * descriptor is inserted.
+ */
+static int fusefile_write(const char *path, const char *buf, size_t size,
+                         off_t off, struct fuse_file_info *fi)
+{
+#if DEBUG
+    fprintf( stderr, "fusefile_write( %s %ld )\n", path, size );
+#endif
+    if ( strcmp( path, "/" ) != 0 ) {
+       return -ENOENT;
+    }
+
+    // Ensure a pad was nominated
+    if ( pad.filename == 0 ) {
+       return 1;
+    }
+    static char meta[ 100 ];
+    sprintf( meta, "%ld\n%ld\n", off, size );
+    if ( write_block( pad.fd, meta, strlen( meta ) ) <= 0 ) {
+       perror( pad.filename );
+       return -EIO;
+    }
+    struct Source source = {
+       .filename = pad.filename,
+       .fd = pad.fd,
+       .from = lseek( pad.fd, 0, SEEK_END ),
+       .to = 0,
+       .start = 0
+    };
+    ssize_t n = write_block( pad.fd, buf, size );
+    if ( n != size ) {
+       return n;
+    }
+    source.to = source.from + size;
+    insert_source( &source, off );
+    times.mtime = time( 0 );
+    return size;
+}
+
 static void fusefile_destroy(void *data) {
-    char *mnt = (char*) data;
+    char *mnt = (char*) data; // As passed to fuse_main
+#if DEBUG
+    fprintf( stderr, "fusefile_destroy( %s )\n", mnt? mnt : "" );
+#endif
     if ( mnt ) {
        unlink( mnt );
     }
 }
 
+static int fusefile_flush(const char *path, struct fuse_file_info *info) {
+#if DEBUG
+    fprintf( stderr, "fusefile_flush( %s )\n", path );
+#endif
+    if ( strcmp( path, "/" ) != 0 ) {
+       return -ENOENT;
+    }
+    return 0;
+}
+
+static int fusefile_release(const char *path, struct fuse_file_info *fi) {
+#if DEBUG
+    fprintf( stderr, "fusefile_release( %s, %d )\n", path, fi->flags );
+#endif
+    if ( strcmp( path, "/" ) != 0 ) {
+       return -ENOENT;
+    }
+    return 0;
+}
+
+static int fusefile_fsync(const char *path, int x, struct fuse_file_info *fi) {
+#if DEBUG
+    fprintf( stderr, "fusefile_fsync( %s, %d )\n", path, x );
+#endif
+    if ( strcmp( path, "/" ) != 0 ) {
+       return -ENOENT;
+    }
+    return 0;
+}
+
+/**
+ * 
+ */
+static int fusefile_truncate(const char *path, off_t len) {
+#if DEBUG
+    fprintf( stderr, "fusefile_truncate( %s, %ld )\n", path, len );
+#endif
+    if ( strcmp( path, "/" ) != 0 ) {
+       return -ENOENT;
+    }
+    return -EIO;
+}
+
 static struct fuse_operations fusefile_oper = {
-    .getattr        = fusefile_getattr,
-    .open           = fusefile_open,
-    .read           = fusefile_read,
+    .getattr = fusefile_getattr,
+    .open = fusefile_open,
+    .read = fusefile_read,
+    .write = fusefile_write,
+    .write_buf = fusefile_write_buf,
     .destroy = fusefile_destroy,
+    .flush = fusefile_flush,
+    .release = fusefile_release,
+    .fsync = fusefile_fsync,
+    .truncate = fusefile_truncate,
+    //.truncate = fusefile_truncate,
+    //.release = fusefile_release,
+    //void *(*init) (struct fuse_conn_info *conn);
 };
 
 static void usage() {
     char *usage =
-"Usage: catfs [ <fuse options> ] <mount> <file/from-to> ... \n"
-"Mount a concatenation of files\n"
+"Usage: fusefile [ <fuse options> ] <mount> <file/from-to> ... \n"
+"Mounts a virtual, read-only file that is a concatenation of file fragments\n"
        ;
     fprintf( stderr, "%s", usage );
     exit( 1 );
 }
 
+/**
+ * Set up the arguments for the fuse_main call, adding our own.
+ */
+static int setup_argv(int argc,char ***argv) {
+    int n = argc + 1;
+    char **out = calloc( n--, sizeof( char* ) );
+    memcpy( (void*) out, (void*) (*argv), argc-- * sizeof( char* ) ); 
+    out[ n++ ] = out[ argc ]; // mount point
+    out[ argc++ ] = "-odefault_permissions";
+    (*argv) = out;
+    return n;
+}
+
 /**
  * Mount a concatenation of files,
- * [ <fuse options> ] <mount> <file:from,to> ...
+ * [ <fuse options> ] <mount> <file/from-to> ...
  */
 int main(int argc, char *argv[])
 {
@@ -244,6 +547,7 @@ int main(int argc, char *argv[])
     int mt;
     int fg;
     int i;
+    int fuseargc;
     struct stat stbuf;
     int temporary = 0;
     // Scan past options
@@ -252,27 +556,50 @@ int main(int argc, char *argv[])
            break;
        }
     }
-    if ( i > argc - 2 ) { // At least one source
+    if ( i > argc - 2 ) { // At least mount point plus one source
        usage();
     }
-    i++;
+    mnt = argv[ i++ ]; // First non-option argument is the mount pount
+    fuseargc = i;
+    if ( strncmp( argv[ i ], "pad=", 4 ) == 0 ) {
+       // First argument is the pad, if any, signaled with "pad=" prefix
+       pad.filename = argv[ i++ ] + 4; // (also move arg index)
+#if DEBUG
+       fprintf( stderr, "scratch pad=%s\n", pad.filename );
+#endif
+       pad.fd = open( pad.filename, O_RDWR | O_CREAT, 0600 );
+       if ( pad.fd < 0 ) {
+           perror( pad.filename );
+           exit( errno );
+       }
+       lseek( pad.fd, 0, SEEK_END ); // initial seek
+    }
     if ( setup_sources( argv, i, argc-i ) ) {
        return 1;
     }
-    mnt = argv[i-1];
     if ( stat( mnt, &stbuf ) == -1 ) {
        int fd = open( mnt, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR );
        if ( fd < 0 ) {
            perror( mnt );
            return 1;
        }
+       time_t now = time( 0 );
+       times.atime = now;
+       times.mtime = now;
+       times.ctime = now;
        temporary = 1;
        close( fd );
     } else if ( ! S_ISREG( stbuf.st_mode ) ) {
        fprintf( stderr, "mountpoint is not a regular file\n" );
        return 1;
+    } else {
+       times.atime = stbuf.st_atime;
+       times.mtime = stbuf.st_mtime;
+       times.ctime = stbuf.st_ctime;
     }
-    struct fuse_args args = FUSE_ARGS_INIT( i, argv );
+
+    fuseargc = setup_argv( fuseargc, &argv );
+    struct fuse_args args = FUSE_ARGS_INIT( fuseargc, argv );
     if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {
        return 1;
     }
@@ -281,5 +608,5 @@ int main(int argc, char *argv[])
        fprintf( stderr, "missing mountpoint parameter\n" );
        return 1;
     }
-    return fuse_main( i, argv, &fusefile_oper, temporary? mnt : NULL );
+    return fuse_main( fuseargc, argv, &fusefile_oper, temporary? mnt : NULL );
 }