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 ssize_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 fprintf( stderr, "m=%d s=%d\n", m, s );
80 // Copy the filename, and set from and to
81 p->filename = strndup( in, ( s < 0 )? e : s );
83 if ( stat( p->filename, & buf ) ) {
84 perror( p->filename );
87 p->from = ( s < 0 )? 0 : atol( in+s+1 );
91 fprintf( stderr, "p->from=%ld\n", p->from );
92 p->to = ( m < 0 )? buf.st_size : atol( in+m+1 );
93 if ( p->from > p->to || p->to > buf.st_size ) {
99 static int setup_sources(char **argv,int i,int n) {
100 sources.array = calloc( n, sizeof( struct Source ) );
101 if ( sources.array == 0 ) {
107 for ( ; j < n; i++, j++ ) {
108 struct Source *p = sources.array + j;
109 if ( scan_source( argv[i], p ) ) {
110 // should free everything malloc-ed
113 p->start = sources.size;
114 sources.size += p->to - p->from;
115 p->fd = open( p->filename, O_RDONLY );
117 perror( p->filename );
127 static int fusefile_getattr(const char *path, struct stat *stbuf )
129 if ( strcmp( path, "/" ) != 0 ) {
133 fprintf( stderr, "getattr %ld\n", sources.size );
135 memset( stbuf, 0, sizeof( struct stat ) );
136 stbuf->st_mode = S_IFREG | 0444; // Hmmm
138 stbuf->st_size = sources.size;
139 time_t now = time( 0 );
140 stbuf->st_atime = now;
141 stbuf->st_mtime = now;
142 stbuf->st_ctime = now;
143 stbuf->st_uid = getuid();
144 stbuf->st_gid = getgid();
148 static int fusefile_open(const char *path, struct fuse_file_info *fi)
150 if ( strcmp( path, "/" ) != 0 ) {
156 static int find_source(off_t offset) {
158 int hi = sources.count;
159 if ( offset > sources.size ) {
163 int m = ( lo + hi ) / 2;
164 if ( sources.array[m].start > offset ) {
166 } else if ( m+1 < hi && sources.array[m+1].start < offset ) {
175 // Read <size> bytes from <offset> in file
176 static int fusefile_read(const char *path, char *buf, size_t size,
177 off_t offset, struct fuse_file_info *fi)
179 if( strcmp( path, "/" ) != 0 ) {
183 fprintf( stderr, "read %ld %ld\n", offset, size );
188 fprintf( stderr, "find_source %ld %ld\n", offset, size );
190 int i = find_source( offset );
194 if ( sources.array[i].fd < 0 ) {
198 print_source( &sources.array[i] );
200 size_t b = offset - sources.array[i].start + sources.array[i].from;
201 size_t n = sources.array[i].to - b;
205 if ( lseek( sources.array[i].fd, b, SEEK_SET ) < 0 ) {
206 perror( sources.array[i].filename );
210 fprintf( stderr, "get %ld bytes at %ld\n", n, rr );
212 ssize_t r = read( sources.array[i].fd, buf + rr, n );
214 fprintf( stderr, "got %ld bytes\n", r );
217 perror( sources.array[i].filename );
230 static void fusefile_destroy(void *data) {
231 char *mnt = (char*) data;
237 static struct fuse_operations fusefile_oper = {
238 .getattr = fusefile_getattr,
239 .open = fusefile_open,
240 .read = fusefile_read,
241 .destroy = fusefile_destroy,
244 static void usage() {
246 "Usage: fusefile [ <fuse options> ] <mount> <file/from-to> ... \n"
247 "Mounts a virtual, read-only file that is a concatenation of file fragments\n"
249 fprintf( stderr, "%s", usage );
254 * Mount a concatenation of files,
255 * [ <fuse options> ] <mount> <file/from-to> ...
257 int main(int argc, char *argv[])
266 for ( i = 1; i < argc; i++ ) {
267 if ( *argv[i] != '-' ) {
271 if ( i > argc - 2 ) { // At least one source
275 if ( setup_sources( argv, i, argc-i ) ) {
279 if ( stat( mnt, &stbuf ) == -1 ) {
280 int fd = open( mnt, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR );
287 } else if ( ! S_ISREG( stbuf.st_mode ) ) {
288 fprintf( stderr, "mountpoint is not a regular file\n" );
291 struct fuse_args args = FUSE_ARGS_INIT( i, argv );
292 if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {
295 fuse_opt_free_args( &args );
297 fprintf( stderr, "missing mountpoint parameter\n" );
300 return fuse_main( i, argv, &fusefile_oper, temporary? mnt : NULL );