2 fusefile - overlay a file path with a concatenation of parts of
3 other files, read only.
5 Copyright (C) 2019 Ralph Ronnquist
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.
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.
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/>.
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>.
26 #define FUSE_USE_VERSION 31
28 #include <fuse/fuse.h>
29 #include <fuse/fuse_lowlevel.h>
41 size_t start; // starting position in concatenated file
52 static void print_source(struct Source *p) {
53 fprintf( stderr, "%p { %s, %ld, %ld, %ld, %d }\n",
54 p, p->filename, p->from, p->to, p->start, p-> fd );
58 // Scan the source specification, and return the length of the
59 // inclusion. "filename/from,to"
64 static size_t scan_source(char *in,struct Source *p) {
69 // scan for last '/' and last '-'
70 for ( ; i >= 0; i-- ) {
79 // Copy the filename, and set from and to
80 p->filename = strndup( in, ( s < 0 )? e : s );
82 if ( stat( p->filename, & buf ) ) {
83 perror( p->filename );
86 p->from = ( s < 0 )? 0 : atol( in+s+1 );
90 p->to = ( m < 0 )? buf.st_size : atol( in+m+1 );
91 if ( p->from > p->to || p->to > buf.st_size ) {
97 static int setup_sources(char **argv,int i,int n) {
98 sources.array = calloc( n, sizeof( struct Source ) );
99 if ( sources.array == 0 ) {
105 for ( ; j < n; i++, j++ ) {
106 struct Source *p = sources.array + j;
107 if ( scan_source( argv[i], p ) ) {
108 // should free everything malloc-ed
111 p->start = sources.size;
112 sources.size += p->to - p->from;
113 p->fd = open( p->filename, O_RDONLY );
115 perror( p->filename );
125 static int fusefile_getattr(const char *path, struct stat *stbuf )
127 if ( strcmp( path, "/" ) != 0 ) {
131 fprintf( stderr, "getattr %ld\n", sources.size );
133 memset( stbuf, 0, sizeof( struct stat ) );
134 stbuf->st_mode = S_IFREG | 0444; // Hmmm
136 stbuf->st_size = sources.size;
137 time_t now = time( 0 );
138 stbuf->st_atime = now;
139 stbuf->st_mtime = now;
140 stbuf->st_ctime = now;
141 stbuf->st_uid = getuid();
142 stbuf->st_gid = getgid();
146 static int fusefile_open(const char *path, struct fuse_file_info *fi)
148 if ( strcmp( path, "/" ) != 0 ) {
154 static int find_source(off_t offset) {
156 int hi = sources.count;
157 if ( offset > sources.size ) {
161 int m = ( lo + hi ) / 2;
162 if ( sources.array[m].start > offset ) {
164 } else if ( m+1 < hi && sources.array[m+1].start < offset ) {
173 // Read <size> bytes from <offset> in file
174 static int fusefile_read(const char *path, char *buf, size_t size,
175 off_t offset, struct fuse_file_info *fi)
177 if( strcmp( path, "/" ) != 0 ) {
181 fprintf( stderr, "read %ld %ld\n", offset, size );
186 fprintf( stderr, "find_source %ld %ld\n", offset, size );
188 int i = find_source( offset );
192 if ( sources.array[i].fd < 0 ) {
196 print_source( &sources.array[i] );
198 size_t b = offset - sources.array[i].start + sources.array[i].from;
199 size_t n = sources.array[i].to - b;
203 if ( lseek( sources.array[i].fd, b, SEEK_SET ) < 0 ) {
204 perror( sources.array[i].filename );
208 fprintf( stderr, "get %ld bytes at %ld\n", n, rr );
210 ssize_t r = read( sources.array[i].fd, buf + rr, n );
212 fprintf( stderr, "got %ld bytes\n", r );
215 perror( sources.array[i].filename );
228 static void fusefile_destroy(void *data) {
229 char *mnt = (char*) data;
235 static struct fuse_operations fusefile_oper = {
236 .getattr = fusefile_getattr,
237 .open = fusefile_open,
238 .read = fusefile_read,
239 .destroy = fusefile_destroy,
242 static void usage() {
244 "Usage: fusefile [ <fuse options> ] <mount> <file/from-to> ... \n"
245 "Mounts a virtual, read-only file that is a concatenation of file fragments\n"
247 fprintf( stderr, "%s", usage );
252 * Mount a concatenation of files,
253 * [ <fuse options> ] <mount> <file/from-to> ...
255 int main(int argc, char *argv[])
264 for ( i = 1; i < argc; i++ ) {
265 if ( *argv[i] != '-' ) {
269 if ( i > argc - 2 ) { // At least one source
273 if ( setup_sources( argv, i, argc-i ) ) {
277 if ( stat( mnt, &stbuf ) == -1 ) {
278 int fd = open( mnt, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR );
285 } else if ( ! S_ISREG( stbuf.st_mode ) ) {
286 fprintf( stderr, "mountpoint is not a regular file\n" );
289 struct fuse_args args = FUSE_ARGS_INIT( i, argv );
290 if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {
293 fuse_opt_free_args( &args );
295 fprintf( stderr, "missing mountpoint parameter\n" );
298 return fuse_main( i, argv, &fusefile_oper, temporary? mnt : NULL );