completed overlay option
[rrq/fusefile.git] / fusefile.c
1 /***
2     fusefile - overlay a file path with a concatenation of parts of
3     other files, read only.
4
5     Copyright (C) 2019  Ralph Ronnquist
6
7     This program is free software: you can redistribute it and/or
8     modify it under the terms of the GNU General Public License as
9     published by the Free Software Foundation, either version 3 of the
10     License, or (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15     General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program. If not, see
19     <http://www.gnu.org/licenses/>.
20
21     This source was inspired by the "null.c" example of the libfuse
22     sources, which is distributed under GPL2, and copyright (C)
23     2001-2007 Miklos Szeredi <miklos@szeredi.hu>.
24 */
25
26 #define FUSE_USE_VERSION 33
27
28 #include <fuse.h>
29 #include <fuse/fuse_lowlevel.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <time.h>
35 #include <errno.h>
36
37 struct Region {
38     off_t pos;
39     size_t size;
40 };
41
42 struct Source {
43     char *filename;
44     ssize_t from;
45     ssize_t to;
46     ssize_t start; // starting position in concatenated file
47     int fd;
48 };
49
50 static struct {
51     struct Source *array;
52     int count;
53     ssize_t size;
54 } sources;
55
56 static struct {
57     time_t atime;
58     time_t mtime;
59     time_t ctime;
60 } times;
61
62 /**
63  * Overlay
64  */
65 static struct {
66     struct Source source;
67     struct Region *table;
68     size_t count;
69     size_t limit;
70 } overlay;
71
72 #define FRAG(m) (overlay.table+m)
73 #define BEG(m) (FRAG(m)->pos)
74 #define END(m) (FRAG(m)->pos + FRAG(m)->size)
75
76 static ssize_t overlay_prior_fragment(off_t pos) {
77     size_t lo = 0, hi = overlay.count;
78     while ( lo < hi ) {
79         size_t m = ( lo + hi ) / 2;
80         if ( m == lo ) {
81             return BEG( m ) < pos? m : -1;
82         }
83         if ( BEG( m ) <= pos ) {
84             lo = m;
85         } else {
86             hi = m;
87         }
88     }
89     return -1;
90 }
91
92 static void overlay_save_count() {
93     lseek( overlay.source.fd, overlay.source.to, SEEK_SET );
94     size_t size = sizeof( overlay.count );
95     char *p = (char *) &overlay.count ;
96     while ( size > 0 ) {
97         size_t n = write( overlay.source.fd, p, size );
98         if ( n < 0 ) {
99             perror( overlay.source.filename );
100             exit( 1 );
101         }
102         size -= n;
103         p += n;
104     }
105 }
106
107 static void overlay_save_table(size_t lo,size_t hi) {
108     char *p = (char *) FRAG(lo);
109     size_t pos =  overlay.source.to + sizeof( overlay.count ) +
110         lo * sizeof( struct Region );
111     size_t size = ( hi - lo ) * sizeof( struct Region );
112     if ( pos != lseek( overlay.source.fd, pos, SEEK_SET ) ) {
113         fprintf( stderr, "%s: seek error\n", overlay.source.filename );
114         exit( 1 );
115     }
116     while ( size > 0 ) {
117         size_t n = write( overlay.source.fd, p, size );
118         if ( n < 0 ) {
119             perror( overlay.source.filename );
120             exit( 1 );
121         }
122         size -= n;
123         p += n;
124     }
125 }
126
127 static void overlay_insert(size_t p,off_t pos,size_t size) {
128     size_t bytes;
129     if ( overlay.count >= overlay.limit ) {
130         overlay.limit = overlay.count + 10;
131         bytes = overlay.limit * sizeof( struct Region );
132         overlay.table = overlay.table?
133             realloc( overlay.table, bytes ) : malloc( bytes );
134     }
135     bytes = ( overlay.count++ - p ) * sizeof( struct Region );
136     if ( bytes ) {
137         memmove( FRAG( p+1 ), FRAG( p ), bytes );
138     }
139     FRAG( p )->pos = pos;
140     FRAG( p )->size = size;
141     overlay_save_count();
142 }
143
144 static void overlay_delete(size_t p) {
145     if ( p < --overlay.count ) {
146         size_t size = ( overlay.count - p ) * sizeof( struct Region );
147         memmove( FRAG(p), FRAG(p+1), size );
148     }
149     overlay_save_count();
150 }
151
152 static void overlay_mark(off_t pos,size_t size) {
153     ssize_t p = overlay_prior_fragment( pos );
154     if ( p >= 0 && pos <= END(p) ) {
155         // Merge new marks with fragment p
156         FRAG(p)->size = pos + size - BEG(p);
157         if ( p+1 < overlay.count && BEG(p+1) <= END(p) ) {
158             FRAG(p)->size = END(p+1) - BEG(p);
159             overlay_delete( p+1 );
160             overlay_save_table( p, overlay.count );
161         } else {
162             overlay_save_table( p, p+1 );
163         }
164         return;
165     }
166     p++; // index of subsequent fragment
167     if ( p < overlay.count && BEG(p) < pos + size ) {
168         // Merge new marks with pragment p+1
169         FRAG(p)->size = END(p) - pos;
170         FRAG(p)->pos = pos;
171         overlay_save_table( p, p+1 );
172     } else {
173         overlay_insert( p, pos, size);
174         overlay_save_table( p, overlay.count );
175     }
176 }
177
178 #if DEBUG
179 static void print_source(struct Source *p) {
180     fprintf( stderr, "%p { %s, %ld, %ld, %ld, %d }\n",
181              p, p->filename, p->from, p->to, p->start, p-> fd );
182 }
183 #endif
184
185 static char *range;
186 static unsigned int c;
187 static int RANGE(int s,int n ) {
188     return ( s == n ) && *(range+c) == 0;
189 }
190
191 static void usage();
192
193 static void setup_overlay(char *filename) {
194     overlay.source.filename = filename;
195     overlay.source.fd = open( filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR );
196     if ( overlay.source.fd < 0 ) {
197         perror( filename );
198         usage();
199     }
200 }
201
202 static int setup_sources(char **argv,int i,int n) {
203     sources.array = calloc( n, sizeof( struct Source ) );
204     if ( sources.array == 0 ) {
205         return 1;
206     }
207     sources.count = n;
208     int j = 0;
209     sources.size = 0;
210     for ( ; j < n; i++, j++ ) {
211         struct stat filestat;
212         struct Source *p = sources.array + j;
213         // Open the fragment file rw if possible, else ro
214         range = strrchr( argv[i], '/' ); // last '/'
215         p->filename = range? strndup( argv[i], range - argv[i] ) : argv[i];
216         p->fd = open( p->filename, O_RDWR );
217         int rdonly = 0;
218         if ( p->fd < 0 ) {
219             rdonly = 1;
220             p->fd = open( p->filename, O_RDONLY );
221         }
222         if ( p->fd < 0 ) {
223             perror( p->filename );
224             return 1; // Error return
225         }
226         if ( stat( p->filename, &filestat ) ) {
227             perror( p->filename );
228             return 1; 
229         }
230         if ( rdonly ) {
231             fprintf( stderr, "** %s opened read-only\n", p->filename );
232         }
233         p->from = 0;
234         p->to = filestat.st_size;
235         // Process any range variation
236         if ( range && *(++range) ) {
237             int a,b;
238             if ( 0 ) {
239             } else if ( RANGE( sscanf( range, "%d:%d%n", &a, &b, &c ), 2 )) {
240                 p->from = ( a < 0 )? ( p->to + a ) : a;
241                 p->to = ( b < 0 )? ( p->to + b ) : b;
242             } else if ( RANGE( sscanf( range, "%d+%d%n", &a, &b, &c ), 2 )) {
243                 p->from = ( a < 0 )? ( p->to + a ) : a;
244                 p->to = ( ( b < 0 )? p->to : p->from ) + b;
245             } else if ( RANGE( sscanf( range, "%d+%n", &a, &c ), 1 )) {
246                 p->from = ( a < 0 )? ( p->to + a ) : a;
247             } else if ( RANGE( sscanf( range, ":%d%n", &b, &c ), 1 )) {
248                 p->to = ( b < 0 )? ( p->to + b ) : b;
249             } else if ( RANGE( sscanf( range, "%d:%n", &a, &c ), 1 )) {
250                 p->from = ( a < 0 )? ( p->to + a ) : a;
251             } else if ( RANGE( sscanf( range, "%d%n", &a, &c ), 1 )) {
252                 if ( a >= 0 ) {
253                     p->from = a;
254                 } else {
255                     p->from = p->to + a;
256                 }
257             } else if ( RANGE( sscanf( range, ":%n", &c), 0 ) ) {
258                 // to end from start
259             } else {
260                 fprintf( stderr, "** BAD RANGE: %s\n", argv[i] );
261                 return 1;
262             }
263         }
264         if ( ( filestat.st_mode &  S_IFMT ) == S_IFCHR ) {
265             filestat.st_size = p->to; // Pretend size of character device
266         }
267         if ( p->from < 0 ) {
268             p->from = 0;
269         }
270         if ( p->to > filestat.st_size ) {
271             p->to = filestat.st_size;
272         }
273         if ( p->from >= p->to || p->from >= filestat.st_size ) {
274             fprintf( stderr, "** BAD RANGE: %s [%ld:%ld]\n",
275                      argv[i], p->from, p->to );
276             return 1;
277         }
278         p->start = sources.size; // the fusefile position of fragment
279         sources.size += p->to - p->from;
280 #if DEBUG
281         print_source( p );
282 #endif
283     }
284     return 0;
285 }
286
287 static int fusefile_getattr(const char *path,struct stat *stbuf) {
288 #if DEBUG
289     fprintf( stderr, "fusefile_getattr( %s )\n", path );
290 #endif
291     if ( strcmp( path, "/" ) != 0 ) {
292         return -ENOENT;
293     }
294 #if DEBUG
295     fprintf( stderr, "getattr %ld\n", sources.size );
296 #endif
297     memset( stbuf, 0, sizeof( struct stat ) );
298     stbuf->st_mode = S_IFREG | 0644; // Hmmm
299     stbuf->st_nlink = 1;
300     stbuf->st_size = sources.size;
301     stbuf->st_atime = times.atime;
302     stbuf->st_mtime = times.mtime;
303     stbuf->st_ctime = times.ctime;
304     stbuf->st_uid = getuid();
305     stbuf->st_gid = getgid();
306     return 0;
307 }
308
309 static int fusefile_chmod(const char *path,mode_t m) {
310 #if DEBUG
311     fprintf( stderr, "fusefile_chmod( %s, %d )\n", path, m );
312 #endif
313     return -1;
314 }
315
316 static int fusefile_open(const char *path,struct fuse_file_info *fi) {
317 #if DEBUG
318     fprintf( stderr, "fusefile_open( %s, %d )\n", path, fi->flags );
319     fprintf( stderr, "fixing( %d )\n", fi->flags | O_CLOEXEC );
320 #endif
321     if ( strcmp( path, "/" ) != 0 ) {
322         return -ENOENT;
323     }
324     // set O-CLOEXEC  for this opening?
325     times.atime = time( 0 );
326     return 0;
327 }
328
329 static int find_source(off_t offset) {
330     int lo = 0;
331     int hi = sources.count;
332     if ( offset >= sources.size ) {
333         return -1;
334     }
335 #if DEBUG
336     fprintf( stderr, "find_source( %ld )\n", offset );
337 #endif
338     while ( lo + 1 < hi ) {
339         int m = ( lo + hi ) / 2;
340         if ( offset < sources.array[ m ].start ) {
341 #if DEBUG
342             fprintf( stderr, "  offset < [%d].start: %ld\n",
343                      m, sources.array[ m ].start );
344 #endif
345             hi = m;
346         } else {
347 #if DEBUG
348             fprintf( stderr, "  offset >= [%d].start: %ld\n",
349                      m, sources.array[ m ].start );
350 #endif
351             lo = m;
352         }
353     }
354 #if DEBUG
355     fprintf( stderr, "found %d\n", lo );
356 #endif
357     return lo;
358 }
359
360 static int overlay_merge(char *buf,off_t off,size_t size) {
361 #if DEBUG
362     fprintf( stderr, "merge %ld %ld\n", off, size );
363 #endif
364     // Find nearest overlay data before or at off
365     ssize_t p = overlay_prior_fragment( off );
366     if ( p < 0 ) {
367         p = 0;
368     }
369     for ( ; p < overlay.count && BEG(p) < off+size; p++ ) {
370         size_t delta = FRAG(p)->size;
371         if ( BEG(p) > off ) {
372             size_t skip = BEG(p) - off;
373             off += skip;
374             size -= skip;
375             buf += skip;
376         } else {
377             delta = off - BEG(p);
378         }
379         if ( delta > size ) {
380             delta = size;
381         }
382         lseek( overlay.source.fd, off, SEEK_SET );
383         while ( delta > 0 ) {
384             size_t n = read( overlay.source.fd, buf, delta );
385             off += n;
386             size -= n;
387             delta -= n;
388             buf += n;
389         }
390     }
391 #if DEBUG
392     fprintf( stderr, "merged\n" );
393 #endif
394     return 0;
395 }
396
397 // Read <size> bytes from <offset> in file
398 static int fusefile_read(const char *path, char *buf, size_t size,
399                          off_t off, struct fuse_file_info *fi)
400 {
401 #if DEBUG
402     fprintf( stderr, "fusefile_read( %s )\n", path );
403 #endif
404     if( strcmp( path, "/" ) != 0 ) {
405         return -ENOENT;
406     }
407 #if DEBUG
408     fprintf( stderr, "read %ld %ld\n", off, size );
409 #endif
410     size_t rr = 0; // total reading
411     while ( size > 0 ) {
412 #if DEBUG
413         fprintf( stderr, "  find_source %ld %ld\n", off, size );
414 #endif
415         int i = find_source( off );
416         if ( i < 0 ) {
417             return ( off == sources.size )? rr : -ENOENT;
418         }
419         if ( sources.array[i].fd < 0 ) {
420             return -ENOENT;
421         }
422 #if DEBUG
423         print_source( &sources.array[i] );
424 #endif
425         times.atime = time( 0 );
426         size_t b = off - sources.array[i].start + sources.array[i].from;
427         size_t n = sources.array[i].to - b;
428         if ( n > size ) {
429             n = size;
430         }
431 #if DEBUG
432         fprintf( stderr, "  seek fd=%d to %ld\n", sources.array[i].fd, b );
433 #endif
434         if ( lseek( sources.array[i].fd, b, SEEK_SET ) < 0 ) {
435             perror( sources.array[i].filename );
436             return -ENOENT;
437         }
438 #if DEBUG
439         fprintf( stderr, "  now read %ld from fd=%d\n",
440                  n, sources.array[i].fd );
441 #endif
442         ssize_t r = read( sources.array[i].fd, buf + rr, n );
443         if ( overlay.source.filename ) {
444             int x = overlay_merge( buf + rr, off + rr, r );
445             if ( x ) {
446                 return x;
447             }
448         }
449 #if DEBUG
450         fprintf( stderr, "  got %ld bytes\n", r );
451 #endif
452         if ( r < 0 ) {
453             perror( sources.array[i].filename );
454             return -ENOENT;
455         }
456         if ( r == 0 ) {
457             break;
458         }
459         rr += r;
460         off += r;
461         size -= r;
462     }
463 #if DEBUG
464     fprintf( stderr, "  total reading %ld bytes\n", rr );
465 #endif
466     return rr;
467 }
468
469 /**
470  * Poll for IO readiness.
471  */
472 int fusefile_poll(const char *path, struct fuse_file_info *fi,
473                    struct fuse_pollhandle *ph, unsigned *reventsp )
474 {
475 #if DEBUG
476     fprintf( stderr, "fusefile_poll( %s ) %p %d\n", path, ph, *reventsp );
477 #endif
478     if( strcmp( path, "/" ) != 0 ) {
479         return -ENOENT;
480     }
481     if ( ph ) {
482         return fuse_notify_poll( ph );
483     }
484     return 0;
485 }
486
487 static void overlay_load() {
488     lseek( overlay.source.fd, overlay.source.to, SEEK_SET );
489     size_t x = 0;
490     size_t size = sizeof( overlay.count );
491     if ( read( overlay.source.fd, &x, size ) != size ) {
492         return;
493     }
494 #if DEBUG
495     fprintf( stderr, "overlay: %s with %ld regions\n",
496              overlay.source.filename, x );
497 #endif
498     struct Region f = { 0, 0 };
499     size = sizeof( struct Region );
500     while ( x-- > 0 ) {
501         if ( read( overlay.source.fd, &f, size ) != size ) {
502             fprintf( stderr, "%s: bad meta data\n", overlay.source.filename );
503             exit( 1 );
504         }
505 #if DEBUG
506         fprintf( stderr, "overlay region: %ld %ld\n", f.pos, f.size );
507 #endif
508         overlay_mark( f.pos, f.size );
509     }
510 }
511
512 /**
513  * Write a full block of data over the sources at the offset
514  */
515 static int write_block(off_t off,const char *buf,size_t size) {
516 #if DEBUG
517     fprintf( stderr, "write_block( %ld, ?, %ld )\n", off, size );
518 #endif
519     while ( size > 0 ) {
520         int index = find_source( off ); // index of source file
521         if ( index < 0 ) {
522             return -EIO; // past EOF
523         }
524         struct Source *source;
525         if ( overlay.source.filename ) {
526             source = &overlay.source;
527             overlay_mark( off, size ); // Mark region as written
528         } else {
529             source = &sources.array[ index ];
530         }
531         off_t from = off - source->start + source->from;
532         off_t max = source->to - from;
533         if ( lseek( source->fd, from, SEEK_SET ) < 0 ) {
534             return -EIO;
535         }
536         ssize_t todo = ( size < max )? size : max;
537         while ( todo > 0 ) {
538             times.mtime = time( 0 );
539             ssize_t n = write( source->fd, buf, todo );
540             if ( n <= 0 ) {
541                 return -EIO; // Something wrong
542             }
543             buf += n;
544             todo -= n;
545             size -= n;
546             off += n;
547         }
548     }
549     return 0;
550 }
551
552 static int fusefile_write_buf(const char *path, struct fuse_bufvec *buf,
553                               off_t off, struct fuse_file_info *fi) {
554 #if DEBUG
555     fprintf( stderr, "fusefile_write_buf( %s )\n", path );
556 #endif
557     if ( strcmp( path, "/" ) != 0 ) {
558         return -ENOENT;
559     }
560
561     size_t size = 0;
562     int i;
563     for ( i = 0; i < buf->count; i++ ) {
564         struct fuse_buf *p = &buf->buf[i];
565         if ( p->flags & FUSE_BUF_IS_FD ) {
566 #if DEBUG
567             fprintf( stderr, "Content held in a file ... HELP!!\n" );
568 #endif
569             return -EIO;
570         }
571         if ( write_block( off, (char*) p->mem, p->size ) < 0 ) {
572             return -EIO;
573         }
574         size += p->size;
575     }
576 #if DEBUG
577     fprintf( stderr, "fusefile_write_buf written %ld\n", size );
578 #endif
579     return size;
580 }
581
582 /**
583  * Write a fragment at <off>. This overwrites files.
584  */
585 static int fusefile_write(const char *path, const char *buf, size_t size,
586                           off_t off, struct fuse_file_info *fi)
587 {
588 #if DEBUG
589     fprintf( stderr, "fusefile_write( %s %ld )\n", path, size );
590 #endif
591     if ( strcmp( path, "/" ) != 0 ) {
592         return -ENOENT;
593     }
594
595     if ( write_block( off, buf, size ) < 0 ) {
596         return -EIO;
597     }
598     return size;
599 }
600
601 static void fusefile_destroy(void *data) {
602     char *mnt = (char*) data; // As passed to fuse_main
603 #if DEBUG
604     fprintf( stderr, "fusefile_destroy( %s )\n", mnt? mnt : "" );
605 #endif
606     if ( mnt ) {
607         unlink( mnt );
608     }
609 }
610
611 static int fusefile_flush(const char *path, struct fuse_file_info *info) {
612 #if DEBUG
613     fprintf( stderr, "fusefile_flush( %s )\n", path );
614 #endif
615     if ( strcmp( path, "/" ) != 0 ) {
616         return -ENOENT;
617     }
618     return 0;
619 }
620
621 static int fusefile_release(const char *path, struct fuse_file_info *fi) {
622 #if DEBUG
623     fprintf( stderr, "fusefile_release( %s, %d )\n", path, fi->flags );
624 #endif
625     if ( strcmp( path, "/" ) != 0 ) {
626         return -ENOENT;
627     }
628     return 0;
629 }
630
631 static int fusefile_fsync(const char *path, int x, struct fuse_file_info *fi) {
632 #if DEBUG
633     fprintf( stderr, "fusefile_fsync( %s, %d )\n", path, x );
634 #endif
635     if ( strcmp( path, "/" ) != 0 ) {
636         return -ENOENT;
637     }
638     return 0;
639 }
640
641 /**
642  * 
643  */
644 static int fusefile_truncate(const char *path, off_t len) {
645 #if DEBUG
646     fprintf( stderr, "fusefile_truncate( %s, %ld )\n", path, len );
647 #endif
648     if ( strcmp( path, "/" ) != 0 ) {
649         return -ENOENT;
650     }
651     return -EIO;
652 }
653
654 void *fusefile_init(struct fuse_conn_info *fci) {
655 #if DEBUG
656     fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
657 #endif
658     // Disable asynchronous reading
659     fci->async_read = 0;
660     fci->want &= ~FUSE_CAP_ASYNC_READ;
661 #if DEBUG
662     fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
663 #endif
664     return 0;
665 }
666
667 static struct fuse_operations fusefile_oper = {
668     .getattr = fusefile_getattr,
669     .chmod = fusefile_chmod,
670     .open = fusefile_open,
671     .read = fusefile_read,
672     .poll = fusefile_poll,
673     .write = fusefile_write,
674     .write_buf = fusefile_write_buf,
675     .destroy = fusefile_destroy,
676     .flush = fusefile_flush,
677     .release = fusefile_release,
678     .fsync = fusefile_fsync,
679     .truncate = fusefile_truncate,
680     //.truncate = fusefile_truncate,
681     //.release = fusefile_release,
682     .init = fusefile_init,
683 };
684
685 static void usage() {
686     char *usage =
687 "Usage: fusefile [ <fuse options> ] <mount> <file/from-to> ... \n"
688 "Mounts a virtual, file that is a concatenation of file fragments\n"
689         ;
690     fprintf( stderr, "%s", usage );
691     exit( 1 );
692 }
693
694 /**
695  * Set up the arguments for the fuse_main call, adding our own.
696  * argv[argc] is the mount point argument
697  */
698 static int setup_argv(int argc,char ***argv) {
699     // note: (*argv)[ argc ] is the mount point argument
700     char *OURS[] = {
701         "-odefault_permissions",
702         (*argv)[ argc ]
703     };
704 #define OURSN ( sizeof( OURS ) / sizeof( char* ) )
705     int N = argc + OURSN;
706     // Allocate new arg array plus terminating null pointer
707     char **out = malloc( ( N + 1 ) * sizeof( char* ) ); 
708     int i;
709     for ( i = 0; i < argc; i++ ) {
710         out[ i ] = (*argv)[i];
711         //fprintf( stderr, " %s", out[ i ] );
712     }
713     for ( i = 0; i < OURSN; i++ ) {
714         out[ argc + i ] = OURS[i];
715         //fprintf( stderr, " %s", out[ i ] );
716     }
717     out[ N ] = 0;
718     //fprintf( stderr, "\n" );
719     (*argv) = out;
720     return N; // Don't include the terminating null pointer
721 }
722
723 /**
724  * Mount a concatenation of files,
725  * [ <fuse options> ] <mount> <file/from-to> ...
726  */
727 int main(int argc, char *argv[])
728 {
729     char *mnt;
730     int mt;
731     int fg;
732     int i;
733     int fuseargc;
734     struct stat stbuf;
735     int temporary = 0;
736     // Scan past options
737     for ( i = 1; i < argc; i++ ) {
738         if ( *argv[i] != '-' ) {
739             break;
740         }
741     }
742     if ( i > argc - 2 ) { // At least mount point plus one source
743         usage();
744     }
745     fuseargc = i;
746     mnt = argv[ i++ ]; // First non-option argument is the mount pount
747     char *overlaytag = "-overlay:";
748     int overlaytagsize = strlen( overlaytag );
749     if ( strncmp( argv[i], overlaytag, overlaytagsize ) == 0 ) {
750         // consume "-overlay:filename"
751         setup_overlay( argv[i++] + overlaytagsize ); // Need a writable file
752         if ( i >= argc ) {
753             usage();
754         }
755     }
756     if ( setup_sources( argv, i, argc-i ) ) {
757         return 1;
758     }
759     if ( overlay.source.filename ) {
760         overlay.source.to = sources.size; // Register total size.
761         overlay_load();
762     }
763     if ( stat( mnt, &stbuf ) == -1 ) {
764         int fd = open( mnt, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR );
765         if ( fd < 0 ) {
766             perror( mnt );
767             return 1;
768         }
769         time_t now = time( 0 );
770         times.atime = now;
771         times.mtime = now;
772         times.ctime = now;
773         temporary = 1;
774         close( fd );
775     } else if ( ! S_ISREG( stbuf.st_mode ) ) {
776         fprintf( stderr, "mountpoint is not a regular file\n" );
777         return 1;
778     } else {
779         times.atime = stbuf.st_atime;
780         times.mtime = stbuf.st_mtime;
781         times.ctime = stbuf.st_ctime;
782     }
783
784     {
785         int fd = open( mnt, O_RDWR, S_IRUSR | S_IWUSR );
786         if ( fd < 0 ) {
787             perror( mnt );
788             return 1;
789         }
790         if ( lseek( fd, sources.size, SEEK_SET ) < 0 ) {
791             return -EIO;
792         }
793     }
794     fuseargc = setup_argv( fuseargc, &argv );
795     struct fuse_args args = FUSE_ARGS_INIT( fuseargc, argv );
796     if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {
797         return 1;
798     }
799     fuse_opt_free_args( &args );
800     if ( ! mnt ) {
801         fprintf( stderr, "missing mountpoint parameter\n" );
802         return 1;
803     }
804     return fuse_main( fuseargc, argv, &fusefile_oper, temporary? mnt : NULL );
805 }