2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
8 Overlay a file path with a concatenation of parts of other files.
12 #define FUSE_USE_VERSION 31
14 #include <fuse/fuse.h>
15 #include <fuse/fuse_lowlevel.h>
27 size_t start; // starting position in concatenated file
38 static void print_source(struct Source *p) {
39 fprintf( stderr, "%p { %s, %ld, %ld, %ld, %d }\n",
40 p, p->filename, p->from, p->to, p->start, p-> fd );
44 // Scan the source specification, and return the length of the
45 // inclusion. "filename/from,to"
50 static size_t scan_source(char *in,struct Source *p) {
55 // scan for last '/' and last '-'
56 for ( ; i >= 0; i-- ) {
65 // Copy the filename, and set from and to
66 p->filename = strndup( in, ( s < 0 )? e : s );
68 if ( stat( p->filename, & buf ) ) {
69 perror( p->filename );
72 p->from = ( s < 0 )? 0 : atol( in+s+1 );
76 p->to = ( m < 0 )? buf.st_size : atol( in+m+1 );
77 if ( p->from > p->to || p->to > buf.st_size ) {
83 static int setup_sources(char **argv,int i,int n) {
84 sources.array = calloc( n, sizeof( struct Source ) );
85 if ( sources.array == 0 ) {
91 for ( ; j < n; i++, j++ ) {
92 struct Source *p = sources.array + j;
93 if ( scan_source( argv[i], p ) ) {
94 // should free everything malloc-ed
97 p->start = sources.size;
98 sources.size += p->to - p->from;
99 p->fd = open( p->filename, O_RDONLY );
101 perror( p->filename );
111 static int fusefile_getattr(const char *path, struct stat *stbuf )
113 if ( strcmp( path, "/" ) != 0 ) {
117 fprintf( stderr, "getattr %ld\n", sources.size );
119 memset( stbuf, 0, sizeof( struct stat ) );
120 stbuf->st_mode = S_IFREG | 0444; // Hmmm
122 stbuf->st_size = sources.size;
123 time_t now = time( 0 );
124 stbuf->st_atime = now;
125 stbuf->st_mtime = now;
126 stbuf->st_ctime = now;
127 stbuf->st_uid = getuid();
128 stbuf->st_gid = getgid();
132 static int fusefile_open(const char *path, struct fuse_file_info *fi)
134 if ( strcmp( path, "/" ) != 0 ) {
140 static int find_source(off_t offset) {
142 int hi = sources.count;
143 if ( offset > sources.size ) {
147 int m = ( lo + hi ) / 2;
148 if ( sources.array[m].start > offset ) {
150 } else if ( m+1 < hi && sources.array[m+1].start < offset ) {
159 // Read <size> bytes from <offset> in file
160 static int fusefile_read(const char *path, char *buf, size_t size,
161 off_t offset, struct fuse_file_info *fi)
163 if( strcmp( path, "/" ) != 0 ) {
167 fprintf( stderr, "read %ld %ld\n", offset, size );
172 fprintf( stderr, "find_source %ld %ld\n", offset, size );
174 int i = find_source( offset );
178 if ( sources.array[i].fd < 0 ) {
182 print_source( &sources.array[i] );
184 size_t b = offset - sources.array[i].start + sources.array[i].from;
185 size_t n = sources.array[i].to - b;
189 if ( lseek( sources.array[i].fd, b, SEEK_SET ) < 0 ) {
190 perror( sources.array[i].filename );
194 fprintf( stderr, "get %ld bytes at %ld\n", n, rr );
196 ssize_t r = read( sources.array[i].fd, buf + rr, n );
198 fprintf( stderr, "got %ld bytes\n", r );
201 perror( sources.array[i].filename );
214 static void fusefile_destroy(void *data) {
215 char *mnt = (char*) data;
221 static struct fuse_operations fusefile_oper = {
222 .getattr = fusefile_getattr,
223 .open = fusefile_open,
224 .read = fusefile_read,
225 .destroy = fusefile_destroy,
228 static void usage() {
230 "Usage: catfs [ <fuse options> ] <mount> <file/from-to> ... \n"
231 "Mount a concatenation of files\n"
233 fprintf( stderr, "%s", usage );
238 * Mount a concatenation of files,
239 * [ <fuse options> ] <mount> <file:from,to> ...
241 int main(int argc, char *argv[])
250 for ( i = 1; i < argc; i++ ) {
251 if ( *argv[i] != '-' ) {
255 if ( i > argc - 2 ) { // At least one source
259 if ( setup_sources( argv, i, argc-i ) ) {
263 if ( stat( mnt, &stbuf ) == -1 ) {
264 int fd = open( mnt, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR );
271 } else if ( ! S_ISREG( stbuf.st_mode ) ) {
272 fprintf( stderr, "mountpoint is not a regular file\n" );
275 struct fuse_args args = FUSE_ARGS_INIT( i, argv );
276 if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {
279 fuse_opt_free_args( &args );
281 fprintf( stderr, "missing mountpoint parameter\n" );
284 return fuse_main( i, argv, &fusefile_oper, temporary? mnt : NULL );