Merge branch 'suites/experimental'
authorRalph Ronnquist <ralph.ronnquist@gmail.com>
Sun, 31 Jul 2022 23:52:19 +0000 (09:52 +1000)
committerRalph Ronnquist <ralph.ronnquist@gmail.com>
Sun, 31 Jul 2022 23:52:19 +0000 (09:52 +1000)
Makefile
README.adoc [new file with mode: 0644]
debian/changelog [new file with mode: 0644]
debian/control [new file with mode: 0644]
debian/copyright [new file with mode: 0644]
debian/gbp.conf [new file with mode: 0644]
debian/rules [new file with mode: 0755]
debian/source/format [new file with mode: 0644]
fusefile.8
fusefile.c

index d3c33efe9a9e5071b2ccf8d56da099a284211222..c2a0ba66fdb62d94ab77c8c75ca851008ac50d71 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,9 +2,40 @@ BINS = fusefile
 
 default: $(BINS)
 
-fusefile: CFLAGS = -Wall -D_FILE_OFFSET_BITS=64
-fusefile: LDFLAGS = -lfuse
-fusefile: fusefile.c
+ifneq (${DEBUG},)
+fusefile: CFLAGS += -DDEBUG=1 -g
+endif
+
+fusefile: CFLAGS += -Wall -D_FILE_OFFSET_BITS=64
+fusefile: LDFLAGS = -lfuse -pthread
+
+.INTERMEDIATE: fusefile.o
+fusefile.o: fusefile.c
+
+fusefile: fusefile.o
+       $(CC) $(CFLAGS) $(CPPFLAGS) $? $(LDFLAGS) $(TARGET_ARCH) -o $@
 
 clean:
-       rm $(BINS)
+       rm -f $(BINS)
+
+README.html: README.adoc
+       asciidoctor $< > $@
+
+# Building a debian package
+
+SBINDIR = $(DESTDIR)/usr/sbin
+MAN8DIR = $(DESTDIR)/usr/share/man/man8
+
+SBINFILES = fusefile
+MAN8FILES = fusefile.8
+
+INSTALLTARGETS = $(addprefix $(SBINDIR)/,$(SBINFILES))
+INSTALLTARGETS += $(addprefix $(MAN8DIR)/,$(MAN8FILES))
+
+$(SBINDIR)/% $(MAN8DIR)/%: %
+       install -D -T $< $@
+
+install: $(INSTALLTARGETS)
+
+deb:
+       PREFIX= INCLUDE_PREFIX=/usr dpkg-buildpackage --build=full -uc -us
diff --git a/README.adoc b/README.adoc
new file mode 100644 (file)
index 0000000..500e81b
--- /dev/null
@@ -0,0 +1,82 @@
+fusefile
+========
+
+This project implements a "fuse" device to mount as a single file that
+is a concatenation of fragments of one or more files. The fused file
+allows overwriting the parts files, but not changing their sizes, and
+only for parts files that are writable upon first access.
+
+FUSE file mount for combining file fragments.
+== SYNOPSIS
+
+====
+*fusefile* [ _fuse options_ ] *mountpoint*  +_filename_/from:to+ ...
+====
+
+## DESCRIPTION
+
+*fusefile* is FUSE file mount that presents a series of fragments of
+other files as a contiguous concatenation. It bind mounts a driver on
+top of the file mountpoint to present the nominated file fragments as
+a single, contiguous file.
+
+The fragment arguments include the filename of a source file, and
+optionally start and end byte positions. All in all there five
+variations:
+
+ * __filename__ include all of the file.
+
+ * __filename/__ include all of the file named with "/" in the pathname. This case requires a final "/", since the last "/" separates the filename from the position details.
+
+ * __filename/from__ include the file from the given start position, to end.
+
+ * __filename/-to__ include the file from beginning to the given end position (not included).
+
+ * __filename/from:to__ include the file from the given start position, up to the given end position (not included). 
+
+## EXAMPLES
+
+.Insert file "y" into file "x" at position 1200:
+  
+    $ fusefile -ononempty x x/:1200 y x/1200:
+
+That mount will shadow the original file "x", and presents the
+fused file instead.
+
+.Make file y be a swap of the beginning and end of file "x", at position 2442:
+
+    $ fusefile y x/2442: x/:2442
+
+.Replace a partition in an image file with a different file
+
+    $ partx -oNR,START,SECTORS disk.raw
+    NR   START  SECTORS
+     1    2048  2097152
+     2 2099200   409600
+     3 2508800 14268383
+    # Replace partition 2 of 409600 sectors from 2099200 with
+    # the file "insert.fat" clipped to 409600 sectors.
+    $ fusefile -ononempty disk.raw \
+         disk.raw/0:$(( 2099200*512 )) \
+         insert.fat/0:$(( 409600*512 )) \
+         disk.raw/$(( (2099200+409600)*512 )):
+
+
+## NOTES
+
+Note that **fusefile** opens the nominated source file(s) before bind
+mounting. With the fuse option __-ononempty__ it will bind over an
+non-empty file, which may be useful. The source file descriptors
+remain open, but the source fragments are not recomputed. If a source
+file changes or reduces in size "behind" the fused file, then anything
+may happen.
+
+If the mountpoint file doesn't exist, then **fusefile** creates it and
+removes it when unmounted.
+
+Unmounting is done with "fusermount -u __fused file__" as usual.
+
+## AUTHOR
+
+Ralph Rönnquist <ralph.ronnquist@gmail.com> 
diff --git a/debian/changelog b/debian/changelog
new file mode 100644 (file)
index 0000000..b31afc7
--- /dev/null
@@ -0,0 +1,20 @@
+fusefile (0.4) unstable; urgency=medium
+
+  * Revised into overwriting in sources without size change.
+
+ -- Ralph Ronnquist <ralph.ronnquist@gmail.com>  Fri, 15 Jul 2022 12:17:30 +1000
+fusefile (0.3) unstable; urgency=medium
+
+  * Added writeability variation
+
+ -- Ralph Ronnquist <ralph.ronnquist@gmail.com>  Thu, 26 May 2022 19:53:05 +1000
+fusefile (0.2) unstable; urgency=medium
+
+  * Code refresh for chimaera (bullseye).
+
+ -- Ralph Ronnquist <ralph.ronnquist@gmail.com>  Tue, 05 Apr 2022 19:43:49 +1000
+fusefile (0.1) unstable; urgency=medium
+
+  * Initial Release.
+
+ -- Ralph Ronnquist <ralph.ronnquist@gmail.com>  Mon, 11 Mar 2018 10:00:00 +1100
diff --git a/debian/control b/debian/control
new file mode 100644 (file)
index 0000000..9ad6d54
--- /dev/null
@@ -0,0 +1,16 @@
+Source: fusefile
+Section: admin
+Priority: optional
+Maintainer: Ralph Ronnquist <ralph.ronnquist@gmail.com>
+Build-Depends: debhelper-compat (= 13), libfuse-dev
+Standards-Version: 4.6.0
+Homepage: https://git.devuan.org/rrq/fusefile.git
+Vcs-Git: https://git.devuan.org/rrq/fusefile.git
+
+Package: fusefile
+Architecture: any
+Depends: ${shlibs:Depends}, ${misc:Depends}, fuse
+Description: FUSE file mount to present a series of fragments of files
+ as if a real, read-only file. It simply bind mounts a driver on top
+ of the file mountpoint to present the nominated file fragments as a
+ single, contiguous file.
diff --git a/debian/copyright b/debian/copyright
new file mode 100644 (file)
index 0000000..802bc86
--- /dev/null
@@ -0,0 +1,24 @@
+Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
+Upstream-Name: fusefile
+Source: https://git.devuan.org/rrq/fusefile.git
+
+Files: *
+Copyright: 2018 Ralph Ronnquist <ralph.ronnquist@gmail.com>
+License: GPL-3.0+
+
+License: GPL-3.0+
+ 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 package 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 <https://www.gnu.org/licenses/>.
+ .
+ On Debian systems, the complete text of the GNU General
+ Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".
diff --git a/debian/gbp.conf b/debian/gbp.conf
new file mode 100644 (file)
index 0000000..df491d8
--- /dev/null
@@ -0,0 +1,4 @@
+[DEFAULT]
+debian-branch=suites/experimental
+ignore-branch=True
+compression-level=9
diff --git a/debian/rules b/debian/rules
new file mode 100755 (executable)
index 0000000..9ba3ef6
--- /dev/null
@@ -0,0 +1,27 @@
+#!/usr/bin/make -f
+# See debhelper(7) (uncomment to enable)
+# output every command that modifies files on the build system.
+#export DH_VERBOSE = 1
+
+
+# see FEATURE AREAS in dpkg-buildflags(1)
+#export DEB_BUILD_MAINT_OPTIONS = hardening=+all
+
+# see ENVIRONMENT in dpkg-buildflags(1)
+# package maintainers to append CFLAGS
+#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
+# package maintainers to append LDFLAGS
+#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed
+
+
+%:
+       dh $@
+
+
+# dh_make generated override targets
+# This is example for Cmake (See https://bugs.debian.org/641051 )
+#override_dh_auto_configure:
+#      dh_auto_configure -- #  -DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH)
+
+override_dh_usrlocal:
+       true
diff --git a/debian/source/format b/debian/source/format
new file mode 100644 (file)
index 0000000..89ae9db
--- /dev/null
@@ -0,0 +1 @@
+3.0 (native)
index 5dba7e47ea60b6476cff4d7f10761f0d3fbb130e..b4bd50d314f7b3504677285e7660e1fbc17da885 100644 (file)
@@ -1,66 +1,84 @@
 .mso www.tmac
 .TH fusefile 8
 .SH NAME
-fusefile \- FUSE file mount for combining file fragments read-only
+fusefile \- FUSE file mount for combining file fragments
 
 .SH SYNOPSIS
 .B fusefile \fR[fuse options\fR] \fBmountpoint\fR \fIfilename/from-to\fR ...
 
 .SH DESCRIPTION
 
-\fBfusefile\fR is FUSE file mount that presents a series of fragments of
-other files as a contiguous concatenation. It bind mounts a driver on
-top of the file mountpoint to present the nominated file fragments as
-a single, contiguous file.
+\fBfusefile\fR is FUSE file mount that presents a series of fragments
+of other files as a contiguous concatenation. It bind mounts a driver
+on top of the file mountpoint to present the nominated file fragments
+as a single, contiguous file. It accepts over-writing on the fused
+file which gets distributed accordingly to the fragments, but cannot
+change size.
 
 The fragment arguments include the filename of a source file, and
 optionally start and end byte positions. All in all there five
 variations:
+
 .TP
-\fIfilename\fR
-include all of the file.
-.TP
-\fIfilename/\fR
-include all of the file named with "/" in the pathname. This case
-requires a final "/", since the last "/" separates the filename from
-the position details.
+\fIfilename\fR or \fIfilename/\fR
+include all of the file. A pathname that includes "/" must be ended
+with an extra "/" since that last "/" separates the filename from the
+range detail.
+
 .TP
-\fIfilename/from\fR
-include the file from the given start position, to end.
+\fIfilename/start:end\fR
+include the range from the given start to end. Either "start" or "end"
+or both may be omitted, to mean the beginning and end of the file
+respectively. If "start" or "end" are less than 0 then it means
+relative to the end of the file.
+
 .TP
-\fIfilename/-to\fR
-include the file from beginning to the given end position (not
-included).
+\fIfilename/start+length\fR
+include "length" bytes from the given start. A negative "start" means
+relative to the end of the file. If "length" is negative or omitted it
+means that position relative to the end.
+
 .TP
-\fIfilename/from-to\fR
-include the file from the given start position, up to the given end
-position (not included).
+\fIfilename/start\fR
+include bytes from the given start. This is the same as "/start+"
+
+.P
+Note that a negative start position is clipped to 0 and a too large
+end position  is clipped to the end of the file.
+
+.P
+Charater devices are treated as being of any given finite size, but
+have size 0 by default. For example, "/dev/zero/:100" means a fragment
+of 100 NUL bytes.
 
 .SH EXAMPLES
 
 Insert file "y" into file "x" at position 1200:
 .RS
-\fB$ fusefile -ononempty x x/-1200 y x/1200\fR
+\fB$ fusefile -ononempty x x/:1200 y x/1200:\fR
 .RE
 The bind mount shadows the original file "x", and presents the
-composite instead.
+fused file instead.
 
-Make file y be a swap of the beginning and end of file "x", at position 2442:
+Make fused file y be a swap of the beginning and end of file "x", at
+position 2442:
 .RS
-\fB$ fusefile y x/2442 x/-2442\fR
+\fB$ fusefile y x/2442: x/:2442\fR
 .RE
 
 .SH NOTES
 
-Note that \fBfusefile\fR opens the nominated source file(s) before bind
-mounting. With the fuse option \fI-ononempty\fR it will bind over an
-non-empty file, which may be useful. The source file descriptors
-remain open, but the source fragments are not recomputed. If a source
-file changes or reduces in size, anything may happen.
+Note that \fBfusefile\fR opens the nominated source file or files
+before bind mounting. With the fuse option \fI-ononempty\fR it will
+bind over an non-empty file, which may be useful. The source file
+descriptors remain open, but the source fragments are not recomputed.
+If a source file changes the fused file will present the new content.
+If a source is reduced in size, access will be inconsistent.
 
 If the mountpoint file doesn't exist, then \fBfusefile\fR creates it,
 and removes it when unmounted.
 
+
 .SH AUTHOR
 
 Ralph Rönnquist <ralph.ronnquist@gmail.com>
index 5aa29170a23e3ebc3f7cf195d486092f4fa3ab87..59dcaa5fb746b13f90098c69c7e278b21b73490e 100644 (file)
@@ -23,9 +23,9 @@
     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;
+    ssize_t size;
 } sources;
 
+static struct {
+    time_t atime;
+    time_t mtime;
+    time_t ctime;
+} times;
+    
 #if DEBUG
 static void print_source(struct Source *p) {
     fprintf( stderr, "%p { %s, %ld, %ld, %ld, %d }\n",
@@ -55,43 +61,10 @@ static void print_source(struct Source *p) {
 }
 #endif
 
-// Scan the source specification, and return the length of the
-// inclusion. "filename/from,to"
-// filename
-// filename/from
-// filename/-to
-// filename/from-to
-static size_t scan_source(char *in,struct Source *p) {
-    int e = strlen( in );
-    int i = e-1;
-    int s = -1;
-    int m = -1;
-    // scan for last '/' and last '-'
-    for ( ; i >= 0; i-- ) {
-       if ( in[i] == '/' ) {
-           s = i;
-           break;
-       }
-       if ( in[i] == '-' ) {
-           m = i;
-       }
-    }
-    // Copy the filename, and set from and to
-    p->filename = strndup( in, ( s < 0 )? e : s );
-    struct stat buf;
-    if ( stat( p->filename, & buf ) ) {
-       perror( p->filename );
-       return 1; 
-    }
-    p->from = ( s < 0 )? 0 : atol( in+s+1 );
-    if ( p->from < 0 ) {
-       p->from = 0;
-    }
-    p->to = ( m < 0 )? buf.st_size : atol( in+m+1 );
-    if ( p->from > p->to || p->to > buf.st_size ) {
-       return 1;
-    }
-    return 0;
+static char *range;
+static unsigned int c;
+static int RANGE(int s,int n ) {
+    return ( s == n ) && *(range+c) == 0;
 }
 
 static int setup_sources(char **argv,int i,int n) {
@@ -103,18 +76,75 @@ static int setup_sources(char **argv,int i,int n) {
     int j = 0;
     sources.size = 0;
     for ( ; j < n; i++, j++ ) {
+       struct stat filestat;
        struct Source *p = sources.array + j;
-       if ( scan_source( argv[i], p ) ) {
-           // should free everything malloc-ed
-           return 1;
+       // Open the fragment file rw if possible, else ro
+       range = strrchr( argv[i], '/' ); // last '/'
+       p->filename = range? strndup( argv[i], range - argv[i] ) : argv[i];
+       p->fd = open( p->filename, O_RDWR );
+       int rdonly = 0;
+       if ( p->fd < 0 ) {
+           rdonly = 1;
+           p->fd = open( p->filename, O_RDONLY );
        }
-       p->start = sources.size;
-       sources.size += p->to - p->from;
-       p->fd = open( p->filename, O_RDONLY );
        if ( p->fd < 0 ) {
            perror( p->filename );
+           return 1; // Error return
+       }
+       if ( stat( p->filename, &filestat ) ) {
+           perror( p->filename );
+           return 1; 
+       }
+       if ( rdonly ) {
+           fprintf( stderr, "** %s opened read-only\n", p->filename );
+       }
+       p->from = 0;
+       p->to = filestat.st_size;
+       // Process any range variation
+       if ( range && *(++range) ) {
+           int a,b;
+           if ( 0 ) {
+           } else if ( RANGE( sscanf( range, "%d:%d%n", &a, &b, &c ), 2 )) {
+               p->from = ( a < 0 )? ( p->to + a ) : a;
+               p->to = ( b < 0 )? ( p->to + b ) : b;
+           } else if ( RANGE( sscanf( range, "%d+%d%n", &a, &b, &c ), 2 )) {
+               p->from = ( a < 0 )? ( p->to + a ) : a;
+               p->to = ( ( b < 0 )? p->to : p->from ) + b;
+           } else if ( RANGE( sscanf( range, "%d+%n", &a, &c ), 1 )) {
+               p->from = ( a < 0 )? ( p->to + a ) : a;
+           } else if ( RANGE( sscanf( range, ":%d%n", &b, &c ), 1 )) {
+               p->to = ( b < 0 )? ( p->to + b ) : b;
+           } else if ( RANGE( sscanf( range, "%d:%n", &a, &c ), 1 )) {
+               p->from = ( a < 0 )? ( p->to + a ) : a;
+           } else if ( RANGE( sscanf( range, "%d%n", &a, &c ), 1 )) {
+               if ( a >= 0 ) {
+                   p->from = a;
+               } else {
+                   p->from = p->to + a;
+               }
+           } else if ( RANGE( sscanf( range, ":%n", &c), 0 ) ) {
+               // to end from start
+           } else {
+               fprintf( stderr, "** BAD RANGE: %s\n", argv[i] );
+               return 1;
+           }
+       }
+       if ( ( filestat.st_mode &  S_IFMT ) == S_IFCHR ) {
+           filestat.st_size = p->to; // Pretend size of character device
+       }
+       if ( p->from < 0 ) {
+           p->from = 0;
+       }
+       if ( p->to > filestat.st_size ) {
+           p->to = filestat.st_size;
+       }
+       if ( p->from >= p->to || p->from >= filestat.st_size ) {
+           fprintf( stderr, "** BAD RANGE: %s [%ld:%ld]\n",
+                    argv[i], p->from, p->to );
            return 1;
        }
+       p->start = sources.size; // the fusefile position of fragment
+       sources.size += p->to - p->from;
 #if DEBUG
        print_source( p );
 #endif
@@ -122,8 +152,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;
     }
@@ -131,63 +163,89 @@ static int fusefile_getattr(const char *path, struct stat *stbuf )
     fprintf( stderr, "getattr %ld\n", sources.size );
 #endif
     memset( stbuf, 0, sizeof( struct stat ) );
-    stbuf->st_mode = S_IFREG | 0444; // Hmmm
+    stbuf->st_mode = S_IFREG | 0644; // Hmmm
     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_chmod(const char *path,mode_t m) {
+#if DEBUG
+    fprintf( stderr, "fusefile_chmod( %s, %d )\n", path, m );
+#endif
+    return -1;
+}
+
+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;
 }
 
 static int find_source(off_t offset) {
     int lo = 0;
     int hi = sources.count;
-    if ( offset > sources.size ) {
+    if ( offset >= sources.size ) {
        return -1;
     }
-    while ( lo < hi ) {
+#if DEBUG
+    fprintf( stderr, "find_source( %ld )\n", offset );
+#endif
+    while ( lo + 1 < hi ) {
        int m = ( lo + hi ) / 2;
-       if ( sources.array[m].start > offset ) {
+       if ( offset < sources.array[ m ].start ) {
+#if DEBUG
+           fprintf( stderr, "  offset < [%d].start: %ld\n",
+                    m, sources.array[ m ].start );
+#endif
            hi = m;
-       } else if ( m+1 < hi && sources.array[m+1].start < offset ) {
-           lo = m+1;
        } else {
-           return m;
+#if DEBUG
+           fprintf( stderr, "  offset >= [%d].start: %ld\n",
+                    m, sources.array[ m ].start );
+#endif
+           lo = m;
        }
     }
+#if DEBUG
+    fprintf( stderr, "found %d\n", lo );
+#endif
     return lo;
 }
 
 // Read <size> bytes from <offset> 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 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;
+    size_t rr = 0; // total reading
     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;
+           return ( off == sources.size )? rr : -ENOENT;
        }
        if ( sources.array[i].fd < 0 ) {
            return -ENOENT;
@@ -195,21 +253,26 @@ 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;
+       times.atime = time( 0 );
+       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;
        }
+#if DEBUG
+       fprintf( stderr, "  seek fd=%d to %ld\n", sources.array[i].fd, b );
+#endif
        if ( lseek( sources.array[i].fd, b, SEEK_SET ) < 0 ) {
            perror( sources.array[i].filename );
            return -ENOENT;
        }
 #if DEBUG
-       fprintf( stderr, "get %ld bytes at %ld\n", n, rr );
+       fprintf( stderr, "  now read %ld from fd=%d\n",
+                n, sources.array[i].fd );
 #endif
        ssize_t r = read( sources.array[i].fd, buf + rr, n );
 #if DEBUG
-       fprintf( stderr, "got %ld bytes\n", r );
+       fprintf( stderr, "  got %ld bytes\n", r );
 #endif
        if ( r < 0 ) {
            perror( sources.array[i].filename );
@@ -219,35 +282,239 @@ static int fusefile_read(const char *path, char *buf, size_t size,
            break;
        }
        rr += r;
-       offset += r;
+       off += r;
        size -= r;
     }
+#if DEBUG
+    fprintf( stderr, "  total reading %ld bytes\n", rr );
+#endif
     return rr;
 }
 
+/**
+ * Poll for IO readiness.
+ */
+int fusefile_poll(const char *path, struct fuse_file_info *fi,
+                  struct fuse_pollhandle *ph, unsigned *reventsp )
+{
+#if DEBUG
+    fprintf( stderr, "fusefile_poll( %s ) %p %d\n", path, ph, *reventsp );
+#endif
+    if( strcmp( path, "/" ) != 0 ) {
+       return -ENOENT;
+    }
+    if ( ph ) {
+       return fuse_notify_poll( ph );
+    }
+    return 0;
+}
+
+
+/**
+ * Write a full block of data over the sources at the offset
+ */
+static int write_block(off_t off,const char *buf,size_t size) {
+#if DEBUG
+    fprintf( stderr, "write_block( %ld, ?, %ld )\n", off, size );
+#endif
+    while ( size > 0 ) {
+       int index = find_source( off ); // index of source file
+       if ( index < 0 ) {
+           return -EIO; // past EOF
+       }
+       struct Source *source = &sources.array[ index ];
+       off_t from = off - source->start + source->from;
+       off_t max = source->to - from;
+       if ( lseek( source->fd, from, SEEK_SET ) < 0 ) {
+           return -EIO;
+       }
+       ssize_t todo = ( size < max )? size : max;
+       while ( todo > 0 ) {
+           times.mtime = time( 0 );
+           ssize_t n = write( source->fd, buf, todo );
+           if ( n <= 0 ) {
+               return -EIO; // Something wrong
+           }
+           buf += n;
+           todo -= n;
+           size -= n;
+           off += n;
+       }
+    }
+    return 0;
+}
+
+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;
+    }
+
+    size_t size = 0;
+    int i;
+    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;
+       }
+       if ( write_block( off, (char*) p->mem, p->size ) < 0 ) {
+           return -EIO;
+       }
+       size += p->size;
+    }
+#if DEBUG
+    fprintf( stderr, "fusefile_write_buf written %ld\n", size );
+#endif
+    return size;
+}
+
+/**
+ * Write a fragment at <off>. This overwrites files.
+ */
+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;
+    }
+
+    if ( write_block( off, buf, size ) < 0 ) {
+       return -EIO;
+    }
+    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;
+}
+
+void *fusefile_init(struct fuse_conn_info *fci) {
+#if DEBUG
+    fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
+#endif
+    // Disable asynchronous reading
+    fci->async_read = 0;
+    fci->want &= ~FUSE_CAP_ASYNC_READ;
+#if DEBUG
+    fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
+#endif
+    return 0;
+}
+
 static struct fuse_operations fusefile_oper = {
     .getattr = fusefile_getattr,
+    .chmod = fusefile_chmod,
     .open = fusefile_open,
     .read = fusefile_read,
+    .poll = fusefile_poll,
+    .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,
+    .init = fusefile_init,
 };
 
 static void usage() {
     char *usage =
 "Usage: fusefile [ <fuse options> ] <mount> <file/from-to> ... \n"
-"Mounts a virtual, read-only file that is a concatenation of file fragments\n"
+"Mounts a virtual, 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.
+ * argv[argc] is the mount point argument
+ */
+static int setup_argv(int argc,char ***argv) {
+    // note: (*argv)[ argc ] is the mount point argument
+    char *OURS[] = {
+       "-odefault_permissions",
+       (*argv)[ argc ]
+    };
+#define OURSN ( sizeof( OURS ) / sizeof( char* ) )
+    int N = argc + OURSN;
+    // Allocate new arg array plus terminating null pointer
+    char **out = malloc( ( N + 1 ) * sizeof( char* ) ); 
+    int i;
+    for ( i = 0; i < argc; i++ ) {
+       out[ i ] = (*argv)[i];
+       //fprintf( stderr, " %s", out[ i ] );
+    }
+    for ( i = 0; i < OURSN; i++ ) {
+       out[ argc + i ] = OURS[i];
+       //fprintf( stderr, " %s", out[ i ] );
+    }
+    out[ N ] = 0;
+    //fprintf( stderr, "\n" );
+    (*argv) = out;
+    return N; // Don't include the terminating null pointer
+}
+
 /**
  * Mount a concatenation of files,
  * [ <fuse options> ] <mount> <file/from-to> ...
@@ -258,6 +525,7 @@ int main(int argc, char *argv[])
     int mt;
     int fg;
     int i;
+    int fuseargc;
     struct stat stbuf;
     int temporary = 0;
     // Scan past options
@@ -266,27 +534,47 @@ 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++;
+    fuseargc = i;
+    mnt = argv[ i++ ]; // First non-option argument is the mount pount
     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;
+    }
+
+    {
+       int fd = open( mnt, O_RDWR, S_IRUSR | S_IWUSR );
+       if ( fd < 0 ) {
+           perror( mnt );
+           return 1;
+       }
+       if ( lseek( fd, sources.size, SEEK_SET ) < 0 ) {
+           return -EIO;
+       }
     }
-    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;
     }
@@ -295,5 +583,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 );
 }