File: affix.cpp

package info (click to toggle)
afflib 3.7.22-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,584 kB
  • sloc: cpp: 20,911; ansic: 15,912; makefile: 520; sh: 436; python: 192
file content (225 lines) | stat: -rw-r--r-- 5,250 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
 * affix.cpp
 *
 * Fix an aff file that is corrupt.
 * Current methodologies:
 *  - If file does not have a GUID, create one.
 * Distributed under the Berkeley 4-part license
 */


#include "affconfig.h"
#include "afflib.h"
#include "afflib_i.h"
#include "vnode_aff.h"			// so we can use magical af_open_with...

#include <ctype.h>

#include <zlib.h>
#include <openssl/md5.h>
#include <openssl/sha.h>
#include <assert.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef WIN32
#include "unix4win32.h"
#include <malloc.h>
#endif

const char *progname = "affix";
int opt_fix = 0;


void usage()
{
    printf("usage: %s [options] file1 [...]\n",progname);
    printf("  -y = Actually modify the files; normally just reports the problems\n");
    printf("  -v = Just print the version number and exit.\n");
    exit(0);
}



/* Returns 0 if this is a valid AFF file, code if it isn't. */
int af_is_valid_afffile(const char *file)
{
    return 0;				// I should write this
}

int fix(const char *infile)
{
    char buf[1024];
    int flags = (opt_fix ? O_RDWR : O_RDONLY) | O_BINARY;
    switch(af_identify_file_type(infile,1)){
    case AF_IDENTIFY_ERR:
	perror(infile);
	return 0;
    default:
	fprintf(stderr,"%s is not an AFF file\n",infile);
	return 0;
    case AF_IDENTIFY_AFF:
	break;
    }

    printf("%s  ",infile);
    int r=0;

    /* First see if the if the file begins with an AFF flag */
    int fd = open(infile,flags,0666);
    if(fd<0) err(1,"fopen(%s)",infile);
    if(read(fd,buf,strlen(AF_HEADER)+1)!=strlen(AF_HEADER)+1)
	err(1,"can't read AFF file header. Stop.");
    if(strcmp(buf,AF_HEADER)!=0)
	err(1,"%s does not begin with an AF_HEADER. Stop.",infile);
    if(read(fd,buf,strlen(AF_SEGHEAD)+1)!=strlen(AF_SEGHEAD)+1)
	err(1,"Can't read AF_SEGHEAD after AF_HEADER. Stop.");
    if(strcmp(buf,AF_SEGHEAD)!=0)
	err(1,"%s does not have an AF_SEGHEAD after AF_SEGEADER. Stop.",infile);

    /* Figure out length */
    off_t len = lseek(fd,0,SEEK_END);
    if(len<0) err(1,"Can't seek to end of %s. Stop.",infile);
    close(fd);
    
    AFFILE *af = af_open_with(infile,AF_HALF_OPEN|flags,0,&vnode_aff);
    printf("Scanning AFF file...\n");
    r = (*af->v->open)(af);
    /* See if we can build a TOC */
    if(r<0){
	printf("AFF file corrupt at %" I64d " out of %" I64d " (%" I64d " bytes from end)\n",
	       ftello(af->aseg),(int64_t)len,len-ftello(af->aseg));
	if(opt_fix){
	    printf("Truncating... %d \n",fileno(af->aseg));
	    if(ftruncate(fileno(af->aseg),ftello(af->aseg))){
		err(1,"ftruncate");
	    }
	}
    }

    /* See if it has a GID or an encrypted GID */
    if(af_get_seg(af,AF_IMAGE_GID,0,0,0)!=0 &&
       af_get_seg(af,AF_IMAGE_GID AF_AES256_SUFFIX,0,0,0)!=0){
	printf("AFF file is missing a GID. ");
	if(opt_fix){
	    printf("Making one...");
	    if(af_make_gid(af)<0) af_err(1,"af_make_gid");
	}
	putchar('\n');
    }

    af_close(af);

    return 0;
}
#if 0
    

    /* See if it ends properly */
    off_t len;
    
    printf("File is %"I64d" bytes long\n",len);

    if(lseek(fd,len-4,SEEK_SET)<0)
	err(1,"Can't backup %d bytes. Stop.",-4);
    r = read(fd,buf,4);
    if(r!=4)
	err(1,"Can't read last %d bytes of file. Read %d. Stop. ", strlen(AF_SEGTAIL)+1,r);
    if(strcmp(buf,AF_SEGTAIL)!=0){
	printf("Does not end with an AF_SEGTAIL. Scanning backwards to find last complete AF_SEGTAIL.\n");
	for(off_t end=len-4;end>4;end--){
	    if(lseek(fd,end,SEEK_SET)<0) err(1,"lseek bad=%"I64d,end);
	    r = read(fd,buf,4);
	    if(r!=4) err(1,"Can't read 4 bytes at %"I64d);
	    if(strcmp(buf,AF_SEGTAIL)==0){
		printf("Valid AF_SEGTAIL found at %"I64d" (%"I64d" bytes in)\n",end,len-end);
		if(!opt_fix) errx(0,"Rerun with -y flag to fix");
		printf("Truncating at %"I64d"\n",end);
		if(ftruncate(fd,end+4)) err(0,"ftruncate");
		break;
	    }
	}
    }
    close(fd);
    exit(0);



    AFFILE *af = af_open(infile,O_RDONLY,0);
    if(!af) af_err(1,infile);
    int  fix = 0;
    bool fix_add_gid = false;

    if(af_get_seg(af,AF_IMAGE_GID,0,0,0)){
	printf("no GID (%s)",AF_IMAGE_GID);
	fix++;
	fix_add_gid = true;
    }
    af_close(af);
    if(opt_fix==0 || fix==0) return 0;

    if(fix){
	af = af_open(infile,O_RDWR,0);
	if(!af){
	    warn(infile);
	    return -1;
	}
	if(fix_add_gid) {
	    printf(" ... adding GID  ",infile);
	    unsigned char bit128[16];
	    RAND_bytes(bit128,sizeof(bit128));
	    if(af_update_seg(af,AF_IMAGE_GID,0,bit128,sizeof(bit128))){
		warn("Cannot write %s: ",AF_IMAGE_GID);
	    }
	}
	if(af_close(af)){
	    warn("Cannot close %s",infile);
	}
    }
    putchar('\n');
    return 0;
}
#endif

int main(int argc,char **argv)
{
    int ch;

    setvbuf(stdout,0,_IONBF,0);		// turn off buffering

    /* Figure out how many cols the screen has... */
    while ((ch = getopt(argc, argv, "yh?v")) != -1) {
	switch (ch) {
	case 'y':
	    opt_fix = 1;
	    break;
	case 'h':
	case '?':
	default:
	    usage();
	    break;
	case 'v':
	    printf("%s version %s\n",progname,PACKAGE_VERSION);
	    exit(0);
	    
	}
    }
    argc -= optind;
    argv += optind;

    if(argc<1){
	usage();
    }


    /* Loop through all of the files */
    while(*argv){
	fix(*argv++);		// get the file
	argc--;				// decrement argument counter
    }
    exit(0);
}