static struct {
struct Source *array;
int count;
- int limit;
ssize_t size;
} sources;
time_t ctime;
} times;
-#define SOURCEARRAYP(i) ((void*)&sources.array[ i ])
-
-/**
- * Holds info about scratch pad file for 'write' events.
- */
-static struct {
- char *filename;
- int fd;
-} pad;
-
#if DEBUG
static void print_source(struct Source *p) {
fprintf( stderr, "%p { %s, %ld, %ld, %ld, %d }\n",
- p, p->filename, p->from, p->to, p->start, p->fd );
+ p, p->filename, p->from, p->to, p->start, p-> fd );
}
#endif
return 1;
}
sources.count = n;
- sources.limit = n;
int j = 0;
sources.size = 0;
for ( ; j < n; i++, j++ ) {
}
p->start = sources.size;
sources.size += p->to - p->from;
- p->fd = open( p->filename, O_RDONLY );
+ p->fd = open( p->filename, O_RDWR );
+ if ( p->fd < 0 ) {
+ fprintf( stderr, "** %s opened read-only\n", p->filename );
+ p->fd = open( p->filename, O_RDONLY );
+ }
if ( p->fd < 0 ) {
perror( p->filename );
return 1;
fprintf( stderr, "getattr %ld\n", sources.size );
#endif
memset( stbuf, 0, sizeof( struct stat ) );
- stbuf->st_mode = S_IFREG | 0444; // Hmmm
- if ( pad.filename ) {
- stbuf->st_mode |= 0200;
- }
+ stbuf->st_mode = S_IFREG | 0644; // Hmmm
stbuf->st_nlink = 1;
stbuf->st_size = sources.size;
stbuf->st_atime = times.atime;
return 0;
}
+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 );
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 + 1 < hi ) {
return lo;
}
-/**
- * Insert a source fragment description into the table at <off>.
- */
-static int insert_source(struct Source *source,size_t off) {
- int index = find_source( off );
- int i;
- // Ensure at least 5 "free" Sources in <source.array>
- // and allocate space for 20 new otherwise.
- if ( sources.count + 5 > sources.limit ) {
- size_t size = sources.limit + 20;
- struct Source *new = realloc(
- sources.array, size * sizeof( struct Source ) );
- if ( new == 0 ) {
- return -1;
- }
- sources.array = new;
- sources.limit = size;
- }
-#if DEBUG
- fprintf( stderr, "index=%d\n", index );
-#endif
- if ( index < sources.count ) {
- ssize_t b = ( sources.count - index ) * sizeof(struct Source);
-#if DEBUG
- fprintf( stderr, "b=%ld\n", b );
-#endif
- if ( sources.array[ index ].start < off ) {
- // Split the <index> record at <off>
- // and adjust index
- memcpy( SOURCEARRAYP( index+2 ), SOURCEARRAYP( index ), b );
- sources.count += 2;
- b = off - sources.array[ index ].start;
- sources.array[ index + 2 ].from += b; // adjust tail fragment
- sources.array[ index++ ].to = b; // adjust head fragment
-#if DEBUG
- print_source( &sources.array[ index-1 ] );
- print_source( &sources.array[ index ] );
- print_source( &sources.array[ index+1 ] );
- fprintf( stderr, "---\n");
-#endif
- } else {
- // Insert the new source at <index>
- memcpy( SOURCEARRAYP( index+1 ), SOURCEARRAYP( index ), b );
- sources.count += 1;
- }
- } else {
- // Append the new source to <sources> (at <index>)
- sources.count += 1;
- }
- sources.array[ index ].filename = source->filename;
- sources.array[ index ].fd = source->fd;
- sources.array[ index ].from = source->from;
- sources.array[ index ].to = source->to;
- for ( i = index; i < sources.count; i++ ) {
- sources.array[ i ].start = off;
- off += sources.array[ i ].to - sources.array[ i ].from;
-#if DEBUG
- print_source( &sources.array[ i ] );
-#endif
- }
- sources.size = off;
-#if DEBUG
- fprintf( stderr, "count=%d size=%ld\n", sources.count, sources.size );
-#endif
- return index;
-}
-
-// Read <size> bytes from <off> in file
+// 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 DEBUG
fprintf( stderr, "fusefile_read( %s )\n", path );
#endif
- if ( strcmp( path, "/" ) != 0 ) {
+ if( strcmp( path, "/" ) != 0 ) {
return -ENOENT;
}
#if DEBUG
#endif
int i = find_source( off );
if ( i < 0 ) {
- return -ENOENT;
+ return ( off == sources.size )? 0 : -ENOENT;
}
if ( sources.array[i].fd < 0 ) {
return -ENOENT;
}
/**
- * Write a full block of data.
+ * Write a full block of data over the sources at the offset
*/
-static int write_block(int fd,const char *buf,size_t size) {
- size_t orig = size;
+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 ) {
- ssize_t n = write( fd, buf, size );
- if ( n <= 0 ) {
- return n;
+ 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;
}
- buf += n;
- size -= n;
}
- return orig;
+ return 0;
}
static int fusefile_write_buf(const char *path, struct fuse_bufvec *buf,
if ( strcmp( path, "/" ) != 0 ) {
return -ENOENT;
}
-
- // Ensure a pad was nominated
- if ( pad.filename == 0 ) {
- return 1;
- }
- // Determine total size
size_t size = 0;
int i;
-#if DEBUG
- fprintf( stderr, "count = %ld\n", buf->count );
-#endif
- for ( i = 0; i < buf->count; i++ ) {
- struct fuse_buf *p = &buf->buf[ i ];
- size += p->size;
- }
- static char meta[ 100 ];
- sprintf( meta, "%ld\n%ld\n", off, size );
-#if DEBUG
- fprintf( stderr, "meta( %ld %ld )\n", off, size );
-#endif
- if ( write_block( pad.fd, meta, strlen( meta ) ) <= 0 ) {
- perror( pad.filename );
- return -EIO;
- }
- struct Source source = {
- .filename = pad.filename,
- .fd = pad.fd,
- .from = lseek( pad.fd, 0, SEEK_END ),
- .to = 0,
- .start = 0
- };
for ( i = 0; i < buf->count; i++ ) {
struct fuse_buf *p = &buf->buf[i];
if ( p->flags & FUSE_BUF_IS_FD ) {
fprintf( stderr, "Content held in a file ... HELP!!\n" );
#endif
return -EIO;
- } else {
- ssize_t n = write_block( pad.fd, (char*) p->mem, p->size );
- if ( n != p->size ) {
- return -EIO;
- }
}
+ if ( write_block( off, (char*) p->mem, p->size ) < 0 ) {
+ return -EIO;
+ }
+ size += p->size;
}
- source.to = source.from + size;
- insert_source( &source, off );
- times.mtime = time( 0 );
+#if DEBUG
+ fprintf( stderr, "fusefile_write_buf written %ld\n", size );
+#endif
return size;
}
/**
- * Insert a fragment at <off>. The data is appended to the pad file,
- * and a descriptor is inserted; the fragment containing <off> is
- * first split, unless <off> is at its start, and then new fragment
- * descriptor is inserted.
+ * 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)
return -ENOENT;
}
- // Ensure a pad was nominated
- if ( pad.filename == 0 ) {
- return 1;
- }
- static char meta[ 100 ];
- sprintf( meta, "%ld\n%ld\n", off, size );
- if ( write_block( pad.fd, meta, strlen( meta ) ) <= 0 ) {
- perror( pad.filename );
+ if ( write_block( off, buf, size ) < 0 ) {
return -EIO;
}
- struct Source source = {
- .filename = pad.filename,
- .fd = pad.fd,
- .from = lseek( pad.fd, 0, SEEK_END ),
- .to = 0,
- .start = 0
- };
- ssize_t n = write_block( pad.fd, buf, size );
- if ( n != size ) {
- return n;
- }
- source.to = source.from + size;
- insert_source( &source, off );
- times.mtime = time( 0 );
return size;
}
static struct fuse_operations fusefile_oper = {
.getattr = fusefile_getattr,
+ .chmod = fusefile_chmod,
.open = fusefile_open,
.read = fusefile_read,
.write = fusefile_write,
/**
* 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) {
- int n = argc + 1;
- char **out = calloc( n--, sizeof( char* ) );
- memcpy( (void*) out, (void*) (*argv), argc-- * sizeof( char* ) );
- out[ n++ ] = out[ argc ]; // mount point
- out[ argc++ ] = "-odefault_permissions";
+ // 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;
+ return N; // Don't include the terminating null pointer
}
/**
if ( i > argc - 2 ) { // At least mount point plus one source
usage();
}
- mnt = argv[ i++ ]; // First non-option argument is the mount pount
fuseargc = i;
- if ( strncmp( argv[ i ], "pad=", 4 ) == 0 ) {
- // First argument is the pad, if any, signaled with "pad=" prefix
- pad.filename = argv[ i++ ] + 4; // (also move arg index)
-#if DEBUG
- fprintf( stderr, "scratch pad=%s\n", pad.filename );
-#endif
- pad.fd = open( pad.filename, O_RDWR | O_CREAT, 0600 );
- if ( pad.fd < 0 ) {
- perror( pad.filename );
- exit( errno );
- }
- lseek( pad.fd, 0, SEEK_END ); // initial seek
- }
+ mnt = argv[ i++ ]; // First non-option argument is the mount pount
if ( setup_sources( argv, i, argc-i ) ) {
return 1;
}