/***
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 pos;
- size_t size;
+ off_t beg;
+ off_t end;
};
struct Source {
static void usage();
-#define FRAG(m) (overlay.table+m)
-#define BEG(m) (FRAG(m)->pos)
-#define END(m) (FRAG(m)->pos + FRAG(m)->size)
-
+/**
+ * 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 BEG( m ) < pos? m : -1;
+ return overlay.table[m].beg <= pos? m : -1;
}
- if ( BEG( m ) <= pos ) {
+ 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 );
}
}
+/**
+ * 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 *) FRAG(lo);
+ 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 );
}
}
-static void overlay_insert(size_t p,off_t pos,size_t size) {
+/**
+ * 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 );
}
bytes = ( overlay.count++ - p ) * sizeof( struct Region );
if ( bytes ) {
- memmove( FRAG( p+1 ), FRAG( p ), bytes );
+ memmove( (char*) &overlay.table[ p+1 ],
+ (char*) &overlay.table[ p ],
+ bytes );
}
- FRAG( p )->pos = pos;
- FRAG( p )->size = size;
+ 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) {
- if ( p < --overlay.count ) {
- size_t size = ( overlay.count - p ) * sizeof( struct Region );
- memmove( FRAG(p), FRAG(p+1), size );
+ size_t bytes = ( --overlay.count - p ) * sizeof( struct Region );
+ if ( bytes ) {
+ memmove( (char*) &overlay.table[ p ],
+ (char*) &overlay.table[ p+1 ],
+ bytes );
}
- overlay_save_count();
}
-static void overlay_mark(off_t pos,size_t size) {
+/**
+ * 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", pos, size );
+ fprintf( stderr, "overlay_mark( %ld, %ld )\n", beg, end );
#endif
int deleted = 0;
ssize_t q;
- ssize_t p = overlay_prior_fragment( pos );
- // p is the nearest region below pos (or -1)
- if ( p >= 0 && pos <= END(p) ) {
+ 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(p) >= pos + size ) {
+ if ( end <= overlay.table[p].end ) {
+ // region p covers mark region already
#if DEBUG
- fprintf( stderr, "overlay size 1( %ld )\n", FRAG(p)->size );
+ fprintf( stderr, "overlay covering ( %ld %ld )\n",
+ overlay.table[p].beg, overlay.table[p].end );
#endif
- return; // new mark within existing.
+ return;
}
- // new mark region extends existing
- FRAG(p)->size = pos + size - BEG(p);
+ // the new mark region extends region p
+ overlay.table[p].end = end;
q = p+1;
- while ( q < overlay.count && BEG(q) <= END(p) ) {
- if ( END(q) > END(p) ) {
- FRAG(p)->size = END(q) - BEG(p);
+ 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++;
}
- overlay_save_table( p, deleted? overlay.count : q );
+ if ( deleted ) {
+ overlay_save_count();
+ q = overlay.count;
+ }
+ overlay_save_table( p, q );
#if DEBUG
- fprintf( stderr, "overlay size 2( %ld ) deleted %d\n",
- FRAG(p)->size, deleted );
+ fprintf( stderr, "overlay expand ( %ld %ld ) deleted %d\n",
+ overlay.table[p].beg, overlay.table[p].end, deleted );
#endif
return;
}
- // The region p does not expand into new mark region
- p++; // subsequent region
- if ( p >= overlay.count || BEG(p) > pos + size ) {
- // New mark is separate region at p
- overlay_insert( p, pos, size);
+ // 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 size 4( %ld )\n", FRAG(p)->size );
+ 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 the region
- if ( BEG(p) + FRAG(p)->size < pos + size ) {
- FRAG(p)->size = size; // new mark covers old region
- } else {
- FRAG(p)->size += BEG(p) - pos;
- }
- BEG(p) = pos;
+ // 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;
- while ( q < overlay.count && BEG(q) <= END(p) ) {
- if ( END(q) > END(p) ) {
- FRAG(p)->size = END(q) - BEG(p);
+ 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_delete( q );
- deleted++;
}
- overlay_save_table( p, deleted? overlay.count : q );
+ overlay_save_table( p, q );
#if DEBUG
- fprintf( stderr, "overlay size 4( %ld ) deleted %d\n",
- FRAG(p)->size, deleted );
+ fprintf( stderr, "overlay before ( %ld %ld ) deleted %d\n",
+ overlay.table[p].beg, overlay.table[p].end, deleted );
#endif
}
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) ) {
+ long int a,b;
+ if ( 0 ) {
+ } else if ( RANGE( sscanf( range, "%ld:%ld%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, "%ld+%ld%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, "%ld+%n", &a, &c ), 1 )) {
+ p->from = ( a < 0 )? ( p->to + a ) : a;
+ } else if ( RANGE( sscanf( range, ":%ld%n", &b, &c ), 1 )) {
+ p->to = ( b < 0 )? ( p->to + b ) : b;
+ } else if ( RANGE( sscanf( range, "%ld:%n", &a, &c ), 1 )) {
+ p->from = ( a < 0 )? ( p->to + a ) : a;
+ } else if ( RANGE( sscanf( range, "%ld%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 off,size_t size) {
+static int overlay_merge(char *buf,off_t beg,off_t end) {
#if DEBUG
- fprintf( stderr, "merge %ld %ld\n", off, size );
+ fprintf( stderr, "merge %ld %ld\n", beg, end );
#endif
- // Find nearest overlay data before or at off
- ssize_t p = overlay_prior_fragment( off );
+ // Find nearest overlay data before or at beg
+ ssize_t p = overlay_prior_fragment( beg );
if ( p < 0 ) {
p = 0;
}
- for ( ; p < overlay.count && BEG(p) < off+size; p++ ) {
- size_t delta = FRAG(p)->size;
- if ( BEG(p) > off ) {
- size_t skip = BEG(p) - off;
- off += skip;
- size -= skip;
- buf += skip;
- } else {
- delta = off - BEG(p);
+ for ( ; p < overlay.count && overlay.table[p].beg < end; p++ ) {
+ if ( overlay.table[p].end < beg ) {
+ continue;
}
- if ( delta > size ) {
- delta = size;
+ if ( overlay.table[p].beg > beg ) {
+ size_t delta = overlay.table[p].beg - beg;
+ buf += delta;
+ beg += delta;
}
- lseek( overlay.source.fd, off, SEEK_SET );
- while ( delta > 0 ) {
- size_t n = read( overlay.source.fd, buf, delta );
- off += n;
+ 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;
- delta -= n;
buf += n;
+ beg += n; //
}
}
-#if DEBUG
- fprintf( stderr, "merged\n" );
-#endif
return 0;
}
fsync( overlay.source.fd );
overlay.source.dirty = 0;
}
- int x = overlay_merge( buf + rr, off + rr, r );
+ int x = overlay_merge( buf + rr, off + rr, off + rr + r );
if ( x ) {
return x;
}
exit( 1 );
}
#if DEBUG
- fprintf( stderr, "overlay region: %ld %ld\n", f.pos, f.size );
+ fprintf( stderr, "overlay region: %ld %ld\n", f.beg, f.end );
#endif
- overlay_mark( f.pos, f.size );
+ overlay_mark( f.beg, f.end );
}
}
fprintf( stderr, "write_block( %ld, ?, %ld )\n", off, size );
#endif
if ( overlay.source.filename ) {
- overlay_mark( off, size ); // Mark region as written
+ overlay_mark( off, off + size ); // Mark region as written
}
while ( size > 0 ) {
int index = find_source( off ); // index of source file
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 = 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;