-
Notifications
You must be signed in to change notification settings - Fork 15
/
fsdiff.c
502 lines (421 loc) · 10.9 KB
/
fsdiff.c
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
* Copyright (c) 2003 Regents of The University of Michigan.
* All Rights Reserved. See COPYRIGHT.
*/
#include "config.h"
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <openssl/evp.h>
#include "applefile.h"
#include "transcript.h"
#include "pathcmp.h"
#include "radstat.h"
void (*logger)( char * ) = NULL;
extern char *version, *checksumlist;
void fs_walk( char *, struct stat *, char *, struct applefileinfo *,
int, int, int );
int dodots = 0;
int dotfd;
int lastpercent = -1;
int case_sensitive = 1;
int tran_format = -1;
int ignore_timestamps = 0;
extern int exclude_warnings;
const EVP_MD *md;
static struct fs_list *fs_insert( struct fs_list **, struct fs_list *,
char *, int (*)( const char *, const char * ));
struct fs_list {
struct fs_list *fl_next;
char *fl_name;
struct stat fl_stat;
char fl_type;
struct applefileinfo fl_afinfo;
};
static struct fs_list *
fs_insert( struct fs_list **head, struct fs_list *last,
char *name, int (*cmp)( const char *, const char * ))
{
struct fs_list **current, *new;
if (( last != NULL ) && ( (*cmp)( name, last->fl_name ) > 0 )) {
current = &last->fl_next;
} else {
current = head;
}
/* find where in the list to put the new entry */
for ( ; *current != NULL; current = &(*current)->fl_next) {
if ( (*cmp)( name, (*current)->fl_name ) <= 0 ) {
break;
}
}
if (( new = malloc( sizeof( struct fs_list ))) == NULL ) {
return( NULL );
}
if (( new->fl_name = strdup( name )) == NULL ) {
free( new );
return( NULL );
}
new->fl_next = *current;
*current = new;
return( new );
}
void
fs_walk( char *path, struct stat *st, char *type, struct applefileinfo *afinfo,
int start, int finish, int pdel )
{
DIR *dir;
struct dirent *de;
struct fs_list *head = NULL, *cur, *new = NULL, *next;
int len;
int count = 0;
int del_parent;
float chunk, f = start;
char temp[ MAXPATHLEN ];
struct transcript *tran;
int (*cmp)( const char *, const char * );
if (( finish > 0 ) && ( start != lastpercent )) {
lastpercent = start;
printf( "%%%.2d %s\n", start, path );
fflush( stdout );
}
/* call the transcript code */
switch ( transcript( path, st, type, afinfo, pdel )) {
case 2 : /* negative directory */
for (;;) {
tran = transcript_select();
if ( tran->t_eof ) {
return;
}
if ( ischildcase( tran->t_pinfo.pi_name, path, case_sensitive )) {
struct stat st0;
char type0;
struct applefileinfo afinfo0;
strcpy( temp, tran->t_pinfo.pi_name );
switch ( radstat( temp, &st0, &type0, &afinfo0 )) {
case 0:
break;
case 1:
fprintf( stderr, "%s is of an unknown type\n", temp );
exit( 2 );
default:
if (( errno != ENOTDIR ) && ( errno != ENOENT )) {
perror( path );
exit( 2 );
}
}
fs_walk( temp, &st0, &type0, &afinfo0, start, finish, pdel );
} else {
return;
}
}
case 0 : /* not a directory */
return;
case 1 : /* directory */
if ( skip ) {
return;
}
break;
default :
fprintf( stderr, "transcript returned an unexpected value!\n" );
exit( 2 );
}
/*
* store whether object is to be deleted. if we get here, object
* is a directory, which should mean that if fs_minus == 1 all
* child objects should be removed as well. tracking this allows
* us to zap excluded objects whose parent dir will be deleted.
*
* del_parent is passed into subsequent fs_walk and transcript
* calls, where * it's checked when considering whether to
* exclude an object.
*/
del_parent = fs_minus;
if ( case_sensitive ) {
cmp = strcmp;
} else {
cmp = strcasecmp;
}
if ( chdir( path ) < 0 ) {
perror( path );
exit( 2 );
}
/* open directory */
if (( dir = opendir( "." )) == NULL ) {
perror( path );
exit( 2 );
}
/* read contents of directory */
while (( de = readdir( dir )) != NULL ) {
/* don't include . and .. */
if (( strcmp( de->d_name, "." ) == 0 ) ||
( strcmp( de->d_name, ".." ) == 0 )) {
continue;
}
count++;
if (( new = fs_insert( &head, new, de->d_name, cmp )) == NULL ) {
perror( "malloc" );
exit( 1 );
}
switch ( radstat( new->fl_name, &new->fl_stat, &new->fl_type,
&new->fl_afinfo )) {
case 0:
break;
case 1:
fprintf( stderr, "%s is of an unknown type\n", path );
exit( 2 );
default:
if (( errno != ENOTDIR ) && ( errno != ENOENT )) {
perror( path );
exit( 2 );
}
}
}
if ( closedir( dir ) != 0 ) {
perror( "closedir" );
exit( 2 );
}
if ( fchdir( dotfd ) < 0 ) {
perror( "OOPS!" );
exit( 2 );
}
chunk = (( finish - start ) / ( float )count );
len = strlen( path );
/* call fswalk on each element in the sorted list */
for ( cur = head; cur != NULL; cur = next ) {
if ( path[ len - 1 ] == '/' ) {
if ( snprintf( temp, MAXPATHLEN, "%s%s", path, cur->fl_name )
>= MAXPATHLEN ) {
fprintf( stderr, "%s%s: path too long\n", path, cur->fl_name );
exit( 2 );
}
} else {
if ( snprintf( temp, MAXPATHLEN, "%s/%s", path, cur->fl_name )
>= MAXPATHLEN ) {
fprintf( stderr, "%s/%s: path too long\n", path, cur->fl_name );
exit( 2 );
}
}
fs_walk( temp, &cur->fl_stat, &cur->fl_type, &cur->fl_afinfo,
(int)f, (int)( f + chunk ), del_parent );
f += chunk;
next = cur->fl_next;
free( cur->fl_name );
free( cur );
}
return;
}
static char *
canonicalized_path( char *path )
{
int len;
static char cpath[ MAXPATHLEN ];
if ( path == NULL ) {
return( NULL );
}
len = strlen( path );
if ( len >= sizeof( cpath )) {
fprintf( stderr, "fsdiff: path too long: %s\n", path );
exit( 2 );
}
strcpy( cpath, path );
/* Clip trailing '/' */
if (( len > 1 ) && ( cpath[ len - 1 ] == '/' )) {
cpath[ len - 1 ] = '\0';
len--;
}
/*
* If prefix doesn't contain a directory, canonicalize it by prepending a
* "./". This allow paths to be dynamically converted between relative and
* absolute paths without breaking sort order.
*/
switch( cpath[ 0 ] ) {
case '/':
break;
case '.':
/* Don't rewrite '.' or paths starting with './' */
if ( len == 1 || cpath[ 1 ] == '/' ) {
break;
}
default:
if ( len + 2 >= sizeof( cpath )) {
fprintf( stderr, "fsdiff: path too long: ./%s\n", cpath );
exit( 2 );
}
memmove( cpath + 2, cpath, len );
cpath[ 0 ] = '.'; cpath[ 1 ] = '/';
break;
}
/*
* Determine if called with relative or absolute pathing. Path is relative
* if it's just '.' or starts with './'. File names that start with a '.'
* are absolute.
*/
if ( cpath[ 0 ] == '.' ) {
if ( len == 1 ) {
tran_format = T_RELATIVE;
} else if ( cpath[ 1 ] == '/' ) {
tran_format = T_RELATIVE;
} else {
tran_format = T_ABSOLUTE;
}
} else {
tran_format = T_ABSOLUTE;
}
return( cpath );
}
static void
fsdiff( char *path, char *kfile, int start, int finish, int pdel )
{
struct applefileinfo afinfo;
struct stat st;
char type;
char lpath[ MAXPATHLEN ];
int len;
if (( dotfd = open( ".", O_RDONLY, 0 )) < 0 ) {
perror( "OOPS!" );
exit( 2 );
}
if ( skip && strcmp( path, "-" ) == 0 ) {
/* leave excludes in place */
skip = skip & ~T_SKIP_EXCLUDES;
path_prefix = "/";
transcript_init( kfile, K_CLIENT );
/* run -1 against every line we get from stdin */
while ( fgets( lpath, sizeof( lpath ), stdin ) != NULL ) {
len = strlen( lpath );
if ( lpath[ len - 1 ] != '\n' ) {
fprintf( stderr, "fsdiff: fgets: line too long\n" );
exit( 2 );
}
lpath[ len - 1 ] = '\0';
path = canonicalized_path( lpath );
if ( radstat( path, &st, &type, &afinfo ) != 0 ) {
if ( errno != ENOENT ) {
perror( lpath );
exit( 2 );
}
fprintf( stderr, "Warning: %s: %s\n", path, strerror( errno ));
continue;
}
(void)transcript( path, &st, &type, &afinfo, pdel );
}
if ( ferror( stdin )) {
perror( "fgets" );
exit( 2 );
}
} else {
path_prefix = canonicalized_path( path );
/* initialize the transcripts */
transcript_init( kfile, K_CLIENT );
if ( radstat( path_prefix, &st, &type, &afinfo ) != 0 ) {
perror( path_prefix );
exit( 2 );
}
fs_walk( path_prefix, &st, &type, &afinfo, start, finish, pdel );
}
if ( finish > 0 ) {
printf( "%%%d\n", ( int )finish );
}
/* free the transcripts */
transcript_free( );
hardlink_free( );
}
int
main( int argc, char **argv )
{
extern char *optarg;
extern int optind;
char *kfile = _RADMIND_COMMANDFILE;
int c, len, edit_path_change = 0;
int errflag = 0, use_outfile = 0;
int finish = 0;
edit_path = CREATABLE;
cksum = 0;
outtran = stdout;
while (( c = getopt( argc, argv, "%1ACc:IK:o:tVvW" )) != EOF ) {
switch( c ) {
case '%':
case 'v':
finish = 100;
break;
case 'c':
OpenSSL_add_all_digests();
md = EVP_get_digestbyname( optarg );
if ( !md ) {
fprintf( stderr, "%s: unsupported checksum\n", optarg );
exit( 2 );
}
cksum = 1;
break;
case 'I':
case_sensitive = 0;
break;
case 'o':
if (( outtran = fopen( optarg, "w" )) == NULL ) {
perror( optarg );
exit( 2 );
}
use_outfile = 1;
break;
case 'K':
kfile = optarg;
break;
case '1':
skip = T_SKIP_ALL;
case 'C':
edit_path_change++;
edit_path = CREATABLE;
break;
case 'A':
edit_path_change++;
edit_path = APPLICABLE;
break;
case 'V':
printf( "%s\n", version );
printf( "%s\n", checksumlist );
exit( 0 );
case 'W': /* print a warning when excluding an object */
exclude_warnings = 1;
break;
case 't': /* ignore files for which only the time has changed */
ignore_timestamps = 1;
break;
case '?':
printf( "bad %c\n", c );
errflag++;
break;
default:
break;
}
}
if (( finish != 0 ) && ( !use_outfile )) {
errflag++;
}
if (( edit_path == APPLICABLE ) && ( skip )) {
errflag++;
}
if ( edit_path_change > 1 ) {
errflag++;
}
/* Check that kfile isn't an abvious directory */
len = strlen( kfile );
if ( kfile[ len - 1 ] == '/' ) {
errflag++;
}
if ( errflag || ( argc - optind != 1 )) {
fprintf( stderr, "usage: %s { -C | -A | -1 } " "[ -IVW ] ", argv[ 0 ] );
fprintf( stderr, "[ -K command ] " );
fprintf( stderr, "[ -c checksum ] [ -o file [ -%% ] ] path\n" );
exit ( 2 );
}
fsdiff( argv[ optind ], kfile, 0, finish, 0 );
/* close the output file */
fclose( outtran );
exit( 0 );
}