Allow for fragment pathnames including "/" without range appendix.
[rrq/fusefile.git] / fusefile.c
index f886287b3f806bd70da4cc7fa9830a55b5fe0f21..37f8a2c0448de17eb35174090fdae01393cdbcec 100644 (file)
@@ -33,6 +33,8 @@
 #include <unistd.h>
 #include <time.h>
 #include <errno.h>
+#include <sys/ioctl.h>
+#include <linux/fs.h>
 
 struct Region {
     off_t beg;
@@ -293,8 +295,14 @@ static int RANGE(int s,int n ) {
 static int setup_source(struct Source *p,char *frag) {
     struct stat filestat;
     // Open the fragment file rw if possible, else ro
-    range = strrchr( frag, '/' ); // last '/'
-    p->filename = range? strndup( frag, range - frag ) : frag;
+    // 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 ) {
@@ -305,7 +313,7 @@ static int setup_source(struct Source *p,char *frag) {
        perror( p->filename );
        return 1; // Error return
     }
-    if ( stat( p->filename, &filestat ) ) {
+    if ( ( range == 0 ) && stat( p->filename, &filestat ) ) {
        perror( p->filename );
        return 1; 
     }
@@ -313,24 +321,33 @@ static int setup_source(struct Source *p,char *frag) {
        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) ) {
-       int a,b;
+       long int a,b;
        if ( 0 ) {
-       } else if ( RANGE( sscanf( range, "%d:%d%n", &a, &b, &c ), 2 )) {
+       } 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, "%d+%d%n", &a, &b, &c ), 2 )) {
+       } 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, "%d+%n", &a, &c ), 1 )) {
+       } else if ( RANGE( sscanf( range, "%ld+%n", &a, &c ), 1 )) {
            p->from = ( a < 0 )? ( p->to + a ) : a;
-       } else if ( RANGE( sscanf( range, ":%d%n", &b, &c ), 1 )) {
+       } else if ( RANGE( sscanf( range, ":%ld%n", &b, &c ), 1 )) {
            p->to = ( b < 0 )? ( p->to + b ) : b;
-       } else if ( RANGE( sscanf( range, "%d:%n", &a, &c ), 1 )) {
+       } else if ( RANGE( sscanf( range, "%ld:%n", &a, &c ), 1 )) {
            p->from = ( a < 0 )? ( p->to + a ) : a;
-       } else if ( RANGE( sscanf( range, "%d%n", &a, &c ), 1 )) {
+       } else if ( RANGE( sscanf( range, "%ld%n", &a, &c ), 1 )) {
            if ( a >= 0 ) {
                p->from = a;
            } else {
@@ -375,8 +392,6 @@ static int setup_sources(char **argv,int i,int n) {
        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