2 fusefile-merge - merge a fusefile overlay into its underling
4 Copyright (C) 2023- Ralph Ronnquist
6 This program is free software: you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation, either version 3 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see
18 <http://www.gnu.org/licenses/>.
27 #include <sys/types.h>
33 * An overlay file consists of a data block followed by a count of
34 * struct Region elements plus that many elements.
46 struct Region regions[];
54 struct FileInfo basefile, olyfile;
59 static struct Table *loadtable() {
60 if ( basefile.size >= olyfile.size ) {
62 fprintf( stderr, "basefile.size >= olyfile.size\n" );
66 size_t tbl = ( olyfile.size - basefile.size );
67 if ( ( tbl % sizeof( off_t ) ) != 0 ) {
69 fprintf( stderr, "( tbl %% sizeof( off_t ) ) != 0\n" );
73 size_t count = tbl / sizeof( off_t );
74 if ( ( count & 1 ) == 0 ) { // Needs an odd number of off_t values
76 fprintf( stderr, "( count & 1 ) == 0\n" );
80 count = ( count - 1 ) / 2;
81 int fd = open( olyfile.name, O_RDONLY );
83 perror( olyfile.name );
87 mmap( 0, tbl, PROT_READ, MAP_PRIVATE, fd, basefile.size );
88 return ( ptr->count != count )? 0 : ptr;
92 fprintf( stderr, "Usage: basefile overlayfile\n" );
97 * Application entry. This program dumps the overlay table as a
98 * fusefile fragments sequence, with newline?
100 int main(int argc,char **argv) {
101 static struct stat info;
106 basefile.name = argv[1];
107 if ( stat( basefile.name, &info ) ) {
108 perror( basefile.name );
111 basefile.size = info.st_size;
113 olyfile.name = argv[2];
114 if ( stat( olyfile.name, &info ) ) {
115 perror( olyfile.name );
118 olyfile.size = info.st_size;
120 struct Table *table = loadtable();
122 fprintf( stderr, "%s is not onverlay for %s\n",
123 olyfile.name, basefile.name );
124 fprintf( stderr, " %s: %ld\n", basefile.name, basefile.size );
125 fprintf( stderr, " %s: %ld\n", olyfile.name, olyfile.size );
130 for ( i = 0; i < table->count; i++ ) {
131 struct Region *r = &(table->regions[i]);
132 if ( pos < r->beg ) {
133 fprintf( stdout, "%s/%ld:%ld\n", basefile.name, pos, r->beg );
135 fprintf( stdout, "%s/%ld:%ld\n", olyfile.name, r->beg, r->end );
138 if ( pos < basefile.size ) {
139 fprintf( stdout, "%s/%ld:%ld\n", basefile.name, pos, basefile.size );