#include <unistd.h>
#include <time.h>
#include <errno.h>
+#include <sys/ioctl.h>
+#include <linux/fs.h>
struct Region {
off_t beg;
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
+ // First try the fragment in full, thereafter with range appendix
+ if ( stat( frag, &filestat ) == 0 ) {
+ p->filename = strdup( frag );
+ range = 0;
+ } else {
+ 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 ( ( range == 0 ) && 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 size;
}
+#define PUSHBUF 104857600
+/**
+ * Write data from overlay to source.
+ */
+static void push_oly(off_t beg, off_t end) {
+ static char * buffer = 0;
+ // Pretend that there isn't an overlay
+ char *filename = overlay.source.filename;
+ if ( buffer == 0 ) {
+ buffer = malloc( PUSHBUF );
+ if ( buffer == 0 ) {
+ fprintf( stderr, "** OOM!!\n" );
+ exit( 1 );
+ }
+ }
+ overlay.source.filename = 0;
+ while ( beg < end ) {
+ off_t size = end - beg;
+ if ( size > PUSHBUF ) {
+ size = PUSHBUF;
+ }
+ if ( lseek( overlay.source.fd, beg, SEEK_SET ) < 0 ) {
+ fprintf( stderr, "** Cannot seek overlay at %ld\n", beg );
+ break;
+ }
+ size = read( overlay.source.fd, buffer, size );
+ if ( write_block( beg, buffer, size ) < 0 ) {
+ fprintf( stderr, "** Cannot push %ld bytes at %ld\n", size, beg );
+ }
+ beg += size;
+ }
+ overlay.source.filename = filename;
+}
+
static void fusefile_destroy(void *data) {
char *mnt = (char*) data; // As passed to fuse_main
#if DEBUG
/**
* Dump the current fragmentation to stdout.
*/
-static int dump_fragments() {
+static int dump_fragments(int push) {
int oly = 0;
int src = 0;
size_t pos = 0;
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 );
+ if ( !push ) {
+ 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 ) {
+ if ( ( src < sources.count ) && ( 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 );
+ if ( !push ) {
+ 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 );
+ if ( !push ) {
+ fprintf( stdout, "%s/%ld:%ld\n",
+ overlay.source.filename,
+ overlay.table[ oly ].beg,
+ overlay.table[ oly ].end );
+ } else {
+ push_oly( overlay.table[ oly ].beg, overlay.table[ oly ].end );
+ }
pos = overlay.table[ oly++ ].end;
}
for ( ; src < sources.count &&
}
fuseargc = setup_argv( fuseargc, &argv );
if ( strcmp( "-dump", argv[ 1 ] ) == 0 ) {
- return dump_fragments();
+ return dump_fragments( 0 );
+ }
+ if ( strcmp( "-push", argv[ 1 ] ) == 0 ) {
+ return dump_fragments( 1 );
}
struct fuse_args args = FUSE_ARGS_INIT( fuseargc, argv );
if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {