fprintf( stderr, "** OOM when expanding frament table\n" );
usage();
}
- memcpy( &sources.array[ index+1 ], &sources.array[ index ],
- sizeof( struct Source ) );
+ // copy elements from [index] to [count-1] one element down
+ size_t size = ( sources.count - index - 1 ) * sizeof( struct Source );
+ memmove( &sources.array[ index+1 ], &sources.array[ index ], size );
}
#if DEBUG
if ( offset >= sources.size ) {
return -1;
}
-#if DEBUG
- fprintf( stderr, "find_source( %ld )\n", offset );
-#endif
while ( lo + 1 < hi ) {
int m = ( lo + hi ) / 2;
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 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;
}
static int fusefile_read(const char *path, char *buf, size_t size,
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", off, size );
-#endif
size_t rr = 0; // total reading
- while ( size > 0 ) {
#if DEBUG
- fprintf( stderr, " find_source %ld %ld\n", off, size );
+ fprintf( stderr, "fusefile_read %ld + %ld\n", off, size );
#endif
+ while ( size > 0 ) {
int i = find_source( off );
if ( i < 0 ) {
return ( off == sources.size )? rr : -ENOENT;
}
+#if DEBUG
+ fprintf( stderr, " item: %d ", i );
+ print_source(& sources.array[i] );
+#endif
if ( sources.array[i].fd < 0 ) {
return -ENOENT;
}
-#if DEBUG
- print_source( &sources.array[i] );
-#endif
times.atime = time( 0 );
size_t b = off - sources.array[i].start + sources.array[i].from;
size_t n = sources.array[i].to - b;
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 ( lseek( sources.array[i].fd, b, SEEK_SET ) < 0 ) {
perror( sources.array[i].filename );
return -ENOENT;
}
-#if DEBUG
- 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 of %ld at %ld\n", r, n, rr );
#endif
if ( r < 0 ) {
perror( sources.array[i].filename );
}
struct Region frags[3] = {
{ sources.array[ index ].start, beg },
- { beg, ENDSOURCE( sources.array[ index ] ) },
- { ENDSOURCE( sources.array[ index ] ), end } };
+ { beg, end },
+ { end, ENDSOURCE( sources.array[ index ] ) } };
+#if DEBUG
+ int i;
+ for ( i = 0; i < 3; i++ ) {
+ fprintf( stderr, "frags[%d] = (%ld, %ld)\n",
+ i, frags[i].beg, frags[i].end );
+ }
+#endif
ssize_t size = frags[0].end - frags[0].beg;
if ( size ) {
- // "Duplicate" the indexed source data, copying the filename
+ // Handle any portion before injection point.
dup_source_item( index );
- sources.array[ index ].to = sources.array[ index ].from + size;
+ off_t point = sources.array[ index ].from + size;
+ sources.array[ index ].to = point;
+#if DEBUG
+ fprintf( stderr, "item %d ", index );
+ print_source( &sources.array[ index ] );
+#endif
+ // Adjust item after injection point
index++;
sources.array[ index ].start = beg;
- sources.array[ index ].from = sources.array[ index-1 ].to;
+ sources.array[ index ].from = point;
+#if DEBUG
+ fprintf( stderr, "item %d adjust ", index );
+ print_source( &sources.array[ index ] );
+#endif
}
size = frags[2].end - frags[2].beg;
if ( size ) {
+ // Handle any remaining portion following injection fragment
dup_source_item( index );
sources.array[ index+1 ].start = frags[2].beg;
- sources.array[ index+1 ].from = sources.array[ index+1 ].to -size;
+ sources.array[ index+1 ].from += frags[1].end - frags[1].beg;
+#if DEBUG
+ fprintf( stderr, "item %d ", index+1 );
+ print_source( &sources.array[ index+1 ] );
+#endif
}
- // Replace the [index] fragment
+ // Set up the injection fragment
sources.array[ index ].filename = overlay.source.filename;
- sources.array[ index ].start = beg;
sources.array[ index ].from = beg;
sources.array[ index ].to = end;
- sources.array[ index ].fd = overlay.source.fd; //?
+ sources.array[ index ].fd = overlay.source.fd;
+#if DEBUG
+ fprintf( stderr, "item %d ", index );
+ print_source( &sources.array[ index ] );
+#endif
return end;
}
}
size_t count = 0;
size_t size = sizeof( overlay.count );
- if ( read( overlay.source.fd, &count, size ) != size ) {
- fprintf( stderr, "** error injecting %s\n", overlay.source.filename );
- usage();
+ size_t n;
+ if ( ( n = read( overlay.source.fd, &count, size ) ) != size ) {
+ if ( n != 0 ) {
+ fprintf( stderr, "** error injecting %s\n",
+ overlay.source.filename );
+ usage();
+ }
+ fprintf( stderr, "** ignoring empty %s\n", overlay.source.filename );
}
if ( count == 0 ) {
close( overlay.source.fd );
for ( i = 0; i < count; i++ ) {
off_t beg = overlay.table[i].beg;
while ( beg < overlay.table[i].end ) {
+#if DEBUG
+ fprintf( stderr, "inject [%ld,%ld] ", beg, overlay.table[i].end );
+ print_source( &overlay.source );
+#endif
+
beg = overlay_inject_from_region( beg, overlay.table[i].end );
}
}
free( overlay.table );
overlay.table = 0;
- close( overlay.source.fd );
}
/**
static void overlay_post_setup() {
char *end;
while ( ( end = strchr( overlay.source.filename, ':' ) ) ) {
- *end = 0; //
+ *end = 0; //
overlay_inject();
overlay.source.filename = end + 1;
}
if ( strcmp( path, "/" ) != 0 ) {
return -ENOENT;
}
-
size_t size = 0;
int i;
for ( i = 0; i < buf->count; i++ ) {
ENDSOURCE( sources.array[ src ] ) <= x; src++ ) {
// Dump sources.array[src] in full
if ( !push ) {
- fprintf( stdout, "%s/%ld:%ld\n",
+ fprintf( stdout, "A %s/%ld:%ld\n",
sources.array[ src ].filename,
- pos - sources.array[ src ].start,
+ pos - sources.array[ src ].start +
+ sources.array[ src ].from,
sources.array[ src ].to );
}
pos = ENDSOURCE( sources.array[ src ] );
if ( ( src < sources.count ) && ( sources.array[ src ].start < x ) ) {
// Dump sources.array[src] up to x;
if ( !push ) {
- fprintf( stdout, "%s/%ld:%ld\n",
+ fprintf( stdout, "B %s/%ld:%ld\n",
sources.array[ src ].filename,
- pos - sources.array[ src ].start,
- x - sources.array[ src ].start );
+ sources.array[ src ].from,
+ //pos - sources.array[ src ].start,
+ x - sources.array[ src ].start +
+ sources.array[ src ].from
+ );
}
pos = ENDSOURCE( sources.array[ src ] );
}
// note: (*argv)[ argc ] is the mount point argument
char *OURS[] = {
"-odefault_permissions",
+ //"-s", // Forced single-threading
(*argv)[ argc ]
};
#define OURSN ( sizeof( OURS ) / sizeof( char* ) )