.mso www.tmac
.TH fusefile 8
.SH NAME
-fusefile \- FUSE file mount for combining file fragments
+fusefile, fusedisk \- FUSE file mount for combining file fragments
.SH SYNOPSIS
-.B fusefile \fR[fuse options\fR] \fBmountpoint\fR \fIfilename/from-to\fR ...
+.B fusefile \fR[\fIfuse-opts\fR] \fBmountpoint\fR \fR[\fIoverlay\fR] \fIfilename/from-to\fR ...
+.br
+.B fusefile \fB-dump\fR \fR[\fIfuse-opts\fR] \fBmountpoint\fR \fR[\fIoverlay\fR] \fIfilename/from-to\fR ...
+.br
+.B fusedisk \fR[\fIfuse-opts\fR] \fBmountpoint\fR \fR[\fIoverlay\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. It accepts over-writing on the fused
-file which gets distributed accordingly to the fragments, but cannot
-change size.
+\fBfusefile\fR is a FUSE \fIfile mount\fR that presents a series of
+fragments of other files as a contiguous concatenation. Technically it
+bind mounts a driver on top of the filename mountpoint to provide
+access to the given file fragments as if in a single, contiguous file.
+
+\fBfusefile\fR accepts over-writing on the fused file (i.e. the
+mountpoint) which gets distributed accordingly to the fragments. But
+neither the fused file nor the fragments can change size; any writing
+thus merely over-writes content without truncating fragments. All
+fragment files are held open while \fBfusefile\fR is active.
+
+By using the optional \fB-overlay:\fIfilename\fR argument between the
+mount point and the fragments, an overlay file may be set up. The
+overlay file will then be used by \fBfusefile\fR for capturing writes
+to the fused file (i.e. the mountpoint). The overlay file will contain
+any new written fused file regions followed by meta data to
+distinguish between new, written content and old content that comes
+from the fragments.
+
+The option \fB-dump\fR as first argument together with a fusefile
+setup will print the setup to standard output rather than establishing
+a fusefile mount. This is of most use with a prior overlay setup,
+where then the printout includes the portions of updates that have
+been captured in the overlay. The printout is the series of fusefile
+fragment argments to give in order to intersperse the captured overlay
+portions according to the overlay table.
+
+\fBfusedisk\fR is a helper script to set up a \fBfusefile\fR as a
+block device (via \fIfuseblk\fR) by using the device mapper
+(\fBdmsetup\fR) to manage an empty block device mapping where content
+is handled at the mountpoint via \fBfusefile\fR. (Note that the same
+thing may be done with the device manager directly, but then all
+fragments need to be in sectors of N*512 bytes whereas with
+\fBfusedisk\fR, only the fused file as a whole is "clipped" at nearest
+N*512 bytes below actual size)
+
+.SH FRAGMENT ARGUMENTS
The fragment arguments include the filename of a source file, and
optionally start and end byte positions. All in all there five
means that position relative to the end.
.TP
-\fIfilename/start\fR
-include bytes from the given start. This is the same as "/start+"
+\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.
+end position is clipped to the end of the file.
.P
-Charater devices are treated as being of any given finite size, but
+Character 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 OPTIONS
+
+This section enumerates the most interesting options to use with
+\fBfuesfile\fR. See "man fuse" and "man mount" for more options.
+
+.TP
+\fB-dump\fR
+
+The \fB-dump\fR "option" tells \fBfusefile\fR to print out the
+applicable fragment sequence for the current setup, including the
+overlay table, if any. The printout is done instead of setting up a
+mount point.
+
+.TP
+\fB-o\fIallow_other\fB
+
+The fuse option \fI-oallow_other\fR is needed for sharing the fused
+file with other users who otherwise will not have access to it
+(including "root"). Note however that this must first be enabled in
+\fI/etc/fuse.conf\fR.
+
+.TP
+\fB-o\fInonempty\fR
+
+The fuse option \fI-ononempty\fR may need to be used when reusing an
+existing file as mountpoint.
+
+.TP
+\fB-o\fIuid=...\fR and \fB-o\fIgid=...\fR,
+
+These mount options, where \fI...\fR is a user or group id
+respectively, are useful for root when using \fBfusedisk\fR and
+thereby give user or group ownership for the mount to the nominated
+user or group.
+
.SH EXAMPLES
+This section illustrates uses of \fBfusefile\fR.
-Insert file "y" into file "x" at position 1200:
+.SS Exanple 1.
+Insert a file "y" into a file "x" at position 1200.
.RS
\fB$ fusefile -ononempty x x/:1200 y x/1200:\fR
.RE
-The bind mount shadows the original file "x", and presents the
-fused file instead.
+This also shadows the original file "x" and presents the fused file
+instead.
+.SS Example 2.
Make fused file y be a swap of the beginning and end of file "x", at
-position 2442:
+position 2442.
.RS
\fB$ fusefile y x/2442: x/:2442\fR
.RE
+.SS Example 3.
+Replace partition 2 of an image file, \fIA\fR, with a different
+file, \fIX\fR. For this example the partition table looks as follows.
+.RS
+\fB$ partx -oNR,START,SECTORS \fIA\fR
+ NR START SECTORS
+ 1 2048 2097152
+ 2 2099200 409600
+ 3 2508800 14268383
+.RE
+.br
+As each sector is 512 bytes the clipping points around partition 2 are
+1074790400 and 1284505600 and the insertion size is 209715200 bytes.
+The \fBfusefile\fR comman will therefore be as follows.
+.RS
+\fB$ fusefile -ononempty \fIA\fB \fIA\fB/:1074790400 \fIX\fB/:209715200 \fIA\fB/1284505600\fR
+.RE
+Note that the fused file shadows the original file \fIA\fR.
+
+.SS Example 4.
+Protect raw disk image file with an overlay:
+.RS
+\fB$ fusefile -ononempty disk.raw -overlay:today disk.raw\fR
+.RE
+By that set up, the overlay file, "today", will protect the disk image
+file, "disk.raw" from changes, and also override the pathname
+"disk.raw" to be the fused file.
+
+.SS Example 5.
+A fusefile mount with an \fIoverlay file\fR is writable regardless of
+the fused fragments, but all updates are written to the overlay file
+instead of to the fragments.
+
+.RS
+\fB$ fusefile -ononempty \fIA\fR \fB-overlay:\fIB\fB \fIA\fR
+.RE
+
+The overlay file, \fIB\fR in the example, contains all changes to the
+shadowed original file, \fIA\fR. The overlay file contains only the
+newly written regions and content is otherwise obtained from the
+original file.
+
+To that end, the overlay file also contains a "marker table" at the
+end as if appended to the fused file. That part of the file is outside
+of the fused file; and it's simply an element count followed by pairs
+of byte addresses that tell which regions of the fused file have been
+captured into the overlay file. (The marker table is of course
+maintained so that adjoining regions are collapsed)
+
+Thus, an overlay file may be reused to later re-establish the same
+fused file with overlay as previously, to continue capturing more
+changes.
+
+.SS Example 6.
+As final example, we set up a fused block device \fIy\fR as a swap of
+the beginning and end of file "x", at position 2442:
+.RS
+\fB$ sudo fusedisk -ouid=1000 y x/2442: x/:2442\fR
+.RE
+Note the use of \fBsudo\fR for becoming \fIroot\fR, which is required
+for block device handling, and also the \fB-ouid=1000\fR option so as
+to make the block device \fIy\fR be owned by the user with id 1000.
+
.SH NOTES
-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.
+\fBfusefile\fR opens the nominated source files before any bind
+mounting. With the fuse option \fI-ononempty\fR it will bind over an
+non-empty file, which may be useful. The source files 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.
-If the mountpoint file doesn't exist, then \fBfusefile\fR creates it,
-and removes it when unmounted.
+Unmounting is done with "\fBfusermount -u\fR \fImountpoint\fR" as
+usual. A \fBfusedisk\fR mount is unmounted by \fIroot\fR using
+\fBumount\fR.
+.SH SEE ALSO
+\fBfuse, fusermount, mount, dmsetup\fR
.SH AUTHOR
-Ralph Rönnquist <ralph.ronnquist@gmail.com>
+Ralph Rönnquist <ralph.ronnquist@gmail.com>.
+
/***
fusefile - overlay a file path with a concatenation of parts of
- other files, read only.
+ other files.
- Copyright (C) 2019 Ralph Ronnquist
+ Copyright (C) 2019- Ralph Ronnquist
This program is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
#include <unistd.h>
#include <time.h>
#include <errno.h>
+#include <sys/ioctl.h>
+#include <linux/fs.h>
+
+struct Region {
+ off_t beg;
+ off_t end;
+};
struct Source {
char *filename;
ssize_t to;
ssize_t start; // starting position in concatenated file
int fd;
+ int dirty;
};
static struct {
time_t mtime;
time_t ctime;
} times;
-
+
+/**
+ * Overlay
+ */
+static struct {
+ struct Source source;
+ struct Region *table;
+ size_t count;
+ size_t limit;
+} overlay;
+
+static void usage();
+
+/**
+ * Find the nearest overlay.table region below pos. Returns the index,
+ * or -1 if there is none, i.e. pos < overlay.table[0].
+ */
+static ssize_t overlay_prior_fragment(off_t pos) {
+ size_t lo = 0, hi = overlay.count;
+ while ( lo < hi ) {
+ size_t m = ( lo + hi ) / 2;
+ if ( m == lo ) {
+ return overlay.table[m].beg <= pos? m : -1;
+ }
+ if ( overlay.table[m].beg <= pos ) {
+ lo = m;
+ } else {
+ hi = m;
+ }
+ }
+ return -1;
+}
+
+/**
+ * Save the entry count for overlay.table as 64-bit integer
+ * immediately following the overlay content at the index
+ * corresponding to the fused file size.
+ */
+static void overlay_save_count() {
+ lseek( overlay.source.fd, overlay.source.to, SEEK_SET );
+ size_t size = sizeof( overlay.count );
+ char *p = (char *) &overlay.count ;
+ while ( size > 0 ) {
+ size_t n = write( overlay.source.fd, p, size );
+ if ( n < 0 ) {
+ perror( overlay.source.filename );
+ exit( 1 );
+ }
+ size -= n;
+ p += n;
+ }
+ if ( overlay.source.dirty++ > 1000 ) {
+ fsync( overlay.source.fd );
+ overlay.source.dirty = 0;
+ }
+}
+
+/**
+ * Update the on-disk cache of overlay.table between the given
+ * indexes. The table is laid out immediately following the table
+ * count with each region saved as two 64-bit unsigned integers.
+ */
+static void overlay_save_table(size_t lo,size_t hi) {
+ char *p = (char *) &overlay.table[ lo ];
+ size_t pos = overlay.source.to + sizeof( overlay.count ) +
+ lo * sizeof( struct Region );
+ size_t size = ( hi - lo ) * sizeof( struct Region );
+ if ( pos != lseek( overlay.source.fd, pos, SEEK_SET ) ) {
+ fprintf( stderr, "%s: seek error\n", overlay.source.filename );
+ exit( 1 );
+ }
+ while ( size > 0 ) {
+ size_t n = write( overlay.source.fd, p, size );
+ if ( n < 0 ) {
+ perror( overlay.source.filename );
+ exit( 1 );
+ }
+ size -= n;
+ p += n;
+ }
+ if ( overlay.source.dirty++ > 1000 ) {
+ fsync( overlay.source.fd );
+ overlay.source.dirty = 0;
+ }
+}
+
+/**
+ * Insert a new region at index p, with previous portion [p,count]
+ * moved up to make space.
+ */
+static void overlay_insert(size_t p,off_t beg,off_t end) {
+ size_t bytes;
+ // Grow the table if needed
+ if ( overlay.count >= overlay.limit ) {
+ overlay.limit = overlay.count + 10;
+ bytes = overlay.limit * sizeof( struct Region );
+ overlay.table = overlay.table?
+ realloc( overlay.table, bytes ) : malloc( bytes );
+ }
+ bytes = ( overlay.count++ - p ) * sizeof( struct Region );
+ if ( bytes ) {
+ memmove( (char*) &overlay.table[ p+1 ],
+ (char*) &overlay.table[ p ],
+ bytes );
+ }
+ overlay.table[ p ].beg = beg;
+ overlay.table[ p ].end = end;
+ overlay_save_count();
+}
+
+/**
+ * Delete the region entry at p by moving the portion [p+1,count]
+ * down.
+ */
+static void overlay_delete(size_t p) {
+ size_t bytes = ( --overlay.count - p ) * sizeof( struct Region );
+ if ( bytes ) {
+ memmove( (char*) &overlay.table[ p ],
+ (char*) &overlay.table[ p+1 ],
+ bytes );
+ }
+}
+
+/**
+ * Mark the given region as updated, i.e. written to the overlay. The
+ * mark region may attach to prior marked regions or be a new,
+ * separate region. If attaching, it causes the prior regions to
+ * expand and the table adjusted by deleting any regions that become
+ * fully contained in other regions.
+ */
+static void overlay_mark(off_t beg,off_t end) {
+#if DEBUG
+ fprintf( stderr, "overlay_mark( %ld, %ld )\n", beg, end );
+#endif
+ int deleted = 0;
+ ssize_t q;
+ ssize_t p = overlay_prior_fragment( beg );
+ // p is the nearest region below or at beg (or -1)
+ if ( p >= 0 && beg <= overlay.table[p].end ) {
+ // p overlaps mark region
+ if ( end <= overlay.table[p].end ) {
+ // region p covers mark region already
+#if DEBUG
+ fprintf( stderr, "overlay covering ( %ld %ld )\n",
+ overlay.table[p].beg, overlay.table[p].end );
+#endif
+ return;
+ }
+ // the new mark region extends region p
+ overlay.table[p].end = end;
+ q = p+1;
+ while ( q < overlay.count &&
+ overlay.table[q].beg <= overlay.table[p].end ) {
+ // Extended region merges with subsequent region
+ if ( overlay.table[p].end < overlay.table[q].end ) {
+ overlay.table[p].end = overlay.table[q].end;
+ }
+ overlay_delete( q );
+ deleted++;
+ }
+ if ( deleted ) {
+ overlay_save_count();
+ q = overlay.count;
+ }
+ overlay_save_table( p, q );
+#if DEBUG
+ fprintf( stderr, "overlay expand ( %ld %ld ) deleted %d\n",
+ overlay.table[p].beg, overlay.table[p].end, deleted );
+#endif
+ return;
+ }
+ // The prior region p does not expand into new mark region
+ p++; // subsequent region
+ if ( p >= overlay.count || end < overlay.table[p].beg ) {
+ // New mark region is a separate region at p
+ overlay_insert( p, beg, end );
+#if DEBUG
+ fprintf( stderr, "overlay new ( %ld %ld )\n",
+ overlay.table[p].beg, overlay.table[p].end );
+#endif
+ overlay_save_table( p, overlay.count );
+ return;
+ }
+ // New marks start before and overlap with region p => change p
+ // and handle any subsequent regions being covered
+ overlay.table[p].beg = beg;
+ q = p+1;
+ if ( overlay.table[p].end < end ) {
+ overlay.table[p].end = end;
+ while ( q < overlay.count &&
+ overlay.table[q].beg <= overlay.table[p].end ) {
+ if ( overlay.table[p].end < overlay.table[q].end ) {
+ overlay.table[p].end = overlay.table[q].end;
+ }
+ overlay_delete( q );
+ deleted++;
+ }
+ if ( deleted ) {
+ overlay_save_count();
+ q = overlay.count;
+ }
+ }
+ overlay_save_table( p, q );
+#if DEBUG
+ fprintf( stderr, "overlay before ( %ld %ld ) deleted %d\n",
+ overlay.table[p].beg, overlay.table[p].end, deleted );
+#endif
+}
+
+static void setup_overlay(char *filename) {
+ overlay.source.filename = filename;
+ overlay.source.fd = open( filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR );
+ if ( overlay.source.fd < 0 ) {
+ perror( filename );
+ usage();
+ }
+}
+
#if DEBUG
static void print_source(struct Source *p) {
fprintf( stderr, "%p { %s, %ld, %ld, %ld, %d }\n",
return ( s == n ) && *(range+c) == 0;
}
+static int setup_source(struct Source *p,char *frag) {
+ struct stat filestat;
+ // Open the fragment file rw if possible, else ro
+ range = strrchr( frag, '/' ); // last '/'
+ p->filename = range? strndup( frag, range - frag ) : frag;
+ p->fd = open( p->filename, O_RDWR );
+ int rdonly = 0;
+ if ( p->fd < 0 ) {
+ rdonly = 1;
+ 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;
+ if ( S_ISBLK( filestat.st_mode ) ) {
+ // Block devices report size differently:
+ if ( ioctl( p->fd, BLKGETSIZE64, &filestat.st_size ) < 0 ) {
+ perror( p->filename );
+ }
+#if DEBUG
+ fprintf( stderr, "block device size = %ld\n", filestat.st_size );
+#endif
+ }
+ 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", frag );
+ 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",
+ frag, p->from, p->to );
+ return 1;
+ }
+ p->start = sources.size; // the fusefile position of fragment
+ sources.size += p->to - p->from;
+ return 0;
+}
+
static int setup_sources(char **argv,int i,int n) {
sources.array = calloc( n, sizeof( struct Source ) );
if ( sources.array == 0 ) {
int j = 0;
sources.size = 0;
for ( ; j < n; i++, j++ ) {
- struct stat filestat;
struct Source *p = sources.array + j;
- // 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 );
- }
- 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 );
+ if ( setup_source( p, argv[i] ) ) {
return 1;
}
- p->start = sources.size; // the fusefile position of fragment
- sources.size += p->to - p->from;
#if DEBUG
print_source( p );
#endif
return lo;
}
+static int overlay_merge(char *buf,off_t beg,off_t end) {
+#if DEBUG
+ fprintf( stderr, "merge %ld %ld\n", beg, end );
+#endif
+ // Find nearest overlay data before or at beg
+ ssize_t p = overlay_prior_fragment( beg );
+ if ( p < 0 ) {
+ p = 0;
+ }
+ for ( ; p < overlay.count && overlay.table[p].beg < end; p++ ) {
+ if ( overlay.table[p].end < beg ) {
+ continue;
+ }
+ if ( overlay.table[p].beg > beg ) {
+ size_t delta = overlay.table[p].beg - beg;
+ buf += delta;
+ beg += delta;
+ }
+ size_t size = ( overlay.table[p].end <= end )?
+ ( overlay.table[p].end - beg ) : ( end - beg );
+ lseek( overlay.source.fd, beg, SEEK_SET );
+ while ( size > 0 ) {
+ size_t n = read( overlay.source.fd, buf, size );
+ size -= n;
+ buf += n;
+ beg += n; //
+ }
+ }
+ return 0;
+}
+
// Read <size> bytes from <offset> in file
static int fusefile_read(const char *path, char *buf, size_t size,
off_t off, struct fuse_file_info *fi)
if ( n > size ) {
n = size;
}
+ if ( sources.array[i].dirty ) {
+ fsync( sources.array[i].fd );
+ sources.array[i].dirty = 0;
+ }
#if DEBUG
fprintf( stderr, " seek fd=%d to %ld\n", sources.array[i].fd, b );
#endif
if ( r == 0 ) {
break;
}
+ if ( overlay.source.filename ) {
+ if ( overlay.source.dirty ) {
+ fsync( overlay.source.fd );
+ overlay.source.dirty = 0;
+ }
+ int x = overlay_merge( buf + rr, off + rr, off + rr + r );
+ if ( x ) {
+ return x;
+ }
+ }
rr += r;
off += r;
size -= r;
return 0;
}
+static void overlay_load() {
+ lseek( overlay.source.fd, overlay.source.to, SEEK_SET );
+ size_t x = 0;
+ size_t size = sizeof( overlay.count );
+ if ( read( overlay.source.fd, &x, size ) != size ) {
+ return;
+ }
+#if DEBUG
+ fprintf( stderr, "overlay: %s with %ld regions\n",
+ overlay.source.filename, x );
+#endif
+ struct Region f = { 0, 0 };
+ size = sizeof( struct Region );
+ while ( x-- > 0 ) {
+ if ( read( overlay.source.fd, &f, size ) != size ) {
+ fprintf( stderr, "%s: bad meta data\n", overlay.source.filename );
+ exit( 1 );
+ }
+#if DEBUG
+ fprintf( stderr, "overlay region: %ld %ld\n", f.beg, f.end );
+#endif
+ overlay_mark( f.beg, f.end );
+ }
+}
/**
* Write a full block of data over the sources at the offset
#if DEBUG
fprintf( stderr, "write_block( %ld, ?, %ld )\n", off, size );
#endif
+ if ( overlay.source.filename ) {
+ overlay_mark( off, off + size ); // Mark region as written
+ }
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 ];
+ struct Source *source = overlay.source.filename?
+ &overlay.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 ) {
size -= n;
off += n;
}
+ if ( source->dirty++ >= 1000 ) {
+ fsync( source->fd );
+ source->dirty = 0;
+ }
}
return 0;
}
}
}
+static void fsync_all_dirty() {
+ int i = 0;
+ for ( ; i < sources.count; i++ ) {
+ if ( sources.array[i].dirty ) {
+ fsync( sources.array[i].fd );
+ sources.array[i].dirty = 0;
+ }
+ }
+ if ( overlay.source.filename && overlay.source.dirty ) {
+ fsync( overlay.source.fd );
+ overlay.source.dirty = 0;
+ }
+}
+
static int fusefile_flush(const char *path, struct fuse_file_info *info) {
#if DEBUG
fprintf( stderr, "fusefile_flush( %s )\n", path );
if ( strcmp( path, "/" ) != 0 ) {
return -ENOENT;
}
+ fsync_all_dirty();
return 0;
}
if ( strcmp( path, "/" ) != 0 ) {
return -ENOENT;
}
+ fsync_all_dirty();
return 0;
}
return 0;
}
+#define ENDSOURCE( S ) ( S.start + ( S.to - S.from ) )
+
+/**
+ * Dump the current fragmentation to stdout.
+ */
+static int dump_fragments() {
+ int oly = 0;
+ int src = 0;
+ size_t pos = 0;
+ while ( src < sources.count ) {
+ size_t x = ( oly < overlay.count )?
+ overlay.table[ oly ].beg : sources.size;
+ for ( ; src < sources.count &&
+ ENDSOURCE( sources.array[ src ] ) <= x; src++ ) {
+ // Dump sources.array[src] in full
+ fprintf( stdout, "%s/%ld:%ld\n",
+ sources.array[ src ].filename,
+ pos - sources.array[ src ].start,
+ sources.array[ src ].to );
+ pos = ENDSOURCE( sources.array[ src ] );
+ }
+ if ( sources.array[ src ].start < x ) {
+ // Dump sources.array[src] up to x;
+ fprintf( stdout, "%s/%ld:%ld\n",
+ sources.array[ src ].filename,
+ pos - sources.array[ src ].start,
+ x - sources.array[ src ].start );
+ pos = ENDSOURCE( sources.array[ src ] );
+ }
+ if ( oly < overlay.count ) {
+ fprintf( stdout, "%s/%ld:%ld\n",
+ overlay.source.filename,
+ overlay.table[ oly ].beg,
+ overlay.table[ oly ].end );
+ pos = overlay.table[ oly++ ].end;
+ }
+ for ( ; src < sources.count &&
+ ENDSOURCE( sources.array[ src ] ) <= pos; src++ ) {
+ // Just skip these fragments.
+ }
+ }
+ return( 0 );
+}
+
static struct fuse_operations fusefile_oper = {
.getattr = fusefile_getattr,
+ // NYI .fgetattr = fusefile_fgetattr,
.chmod = fusefile_chmod,
.open = fusefile_open,
.read = fusefile_read,
.write = fusefile_write,
.write_buf = fusefile_write_buf,
.destroy = fusefile_destroy,
+ // NYI .access = fusefile_access,
.flush = fusefile_flush,
.release = fusefile_release,
.fsync = fusefile_fsync,
+ // NYI .ftruncate = fusefile_ftruncate,
.truncate = fusefile_truncate,
//.truncate = fusefile_truncate,
//.release = fusefile_release,
}
fuseargc = i;
mnt = argv[ i++ ]; // First non-option argument is the mount pount
+ char *overlaytag = "-overlay:";
+ int overlaytagsize = strlen( overlaytag );
+ if ( strncmp( argv[i], overlaytag, overlaytagsize ) == 0 ) {
+ // consume "-overlay:filename"
+ setup_overlay( argv[i++] + overlaytagsize ); // Need a writable file
+ if ( i >= argc ) {
+ usage();
+ }
+ }
if ( setup_sources( argv, i, argc-i ) ) {
return 1;
}
+ if ( overlay.source.filename ) {
+ overlay.source.to = sources.size; // Register total size.
+ overlay_load();
+ }
if ( stat( mnt, &stbuf ) == -1 ) {
int fd = open( mnt, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR );
if ( fd < 0 ) {
}
}
fuseargc = setup_argv( fuseargc, &argv );
+ if ( strcmp( "-dump", argv[ 1 ] ) == 0 ) {
+ return dump_fragments();
+ }
struct fuse_args args = FUSE_ARGS_INIT( fuseargc, argv );
if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {
return 1;