summaryrefslogtreecommitdiffstats
path: root/usr.sbin/ctm/mkCTM/mkctm.c
blob: 021349ff93db29c2ec903d37695d8d5ca2482514 (plain)
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
/* Still missing:
 *
 * Damage counter
 * Change counter
 * Time stamp
 * prefix
 * cmd-line args
 * %100 deltas
 * delta and Add are different. delta -> Equ.
 *
 * mkctm 
 *	-B regex	Bogus
 *	-I regex	Ignore
 *	-D int		Damage
 *	-v		increase verbosity
 *	-l str		control logging.
 *	name		cvs-cur
 *	prefix		src/secure
 *	dir1		"Soll"
 *	dir2		"Ist"
 *
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <dirent.h>
#include <regex.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <md5.h>
#include <err.h>
#include <signal.h>

#define DEFAULT_IGNORE	"/CVS$|/\\.#"
#define DEFAULT_BOGUS	"\\.core$"
regex_t reg_ignore,  reg_bogus;
int	flag_ignore, flag_bogus;

u_long s1_ignored,	s2_ignored;
u_long s1_bogus,	s2_bogus;
u_long s1_wrong,	s2_wrong;
u_long s_same_dirs,	s_same_files,	s_same_bytes;
u_long 			s_files_chg,	s_bytes_add,	s_bytes_del;
u_long s_new_dirs,	s_new_files,	s_new_bytes;
u_long s_del_dirs,	s_del_files,	s_del_bytes;
u_long            	s_chg_files,	s_chg_bytes;
u_long 			s_edit_files,	s_edit_bytes,	s_edit_saves;
u_long 			s_sub_files,	s_sub_bytes;

void
print_stat(FILE *fd, char *pre)
{
    fprintf(fd,"%sAvoided:\n",pre);
    fprintf(fd,"%s  ignore:  %5lu old   %5lu new\n",
	    pre, s1_ignored, s2_ignored);
    fprintf(fd,"%s  bogus:   %5lu old   %5lu new\n",
	    pre, s1_bogus, s2_bogus);
    fprintf(fd,"%s  wrong:   %5lu old   %5lu new\n", 
	    pre, s1_wrong, s2_wrong);
    fprintf(fd,"%sDelta:\n",pre);
    fprintf(fd,"%s  new:     %5lu dirs  %5lu files  %9lu plus\n", 
	    pre, s_new_dirs, s_new_files, s_new_bytes);
    fprintf(fd,"%s  del:     %5lu dirs  %5lu files                   %9lu minus\n", 
	    pre, s_del_dirs, s_del_files, s_del_bytes);
    fprintf(fd,"%s  chg:                 %5lu files  %9lu plus   %9lu minus\n",
	    pre, s_files_chg, s_bytes_add, s_bytes_del);
    fprintf(fd,"%s  same:    %5lu dirs  %5lu files  %9lu bytes\n", 
	    pre, s_same_dirs, s_same_files, s_same_bytes);
    fprintf(fd,"%sMethod:\n",pre);
    fprintf(fd,"%s  edit:                %5lu files  %9lu bytes  %9lu saved\n", 
	    pre, s_edit_files, s_edit_bytes, s_edit_saves);
    fprintf(fd,"%s  sub:                 %5lu files  %9lu bytes\n", 
	    pre, s_sub_files, s_sub_bytes);
}

void
stat_info(int foo)
{
	signal(SIGINFO,stat_info);
	print_stat(stderr,"INFO: ");
}

void DoDir(const char *dir1, const char *dir2, const char *name);

static struct stat st;
static __inline struct stat *
StatFile(char *name)
{
	if (lstat(name,&st) < 0) 
		err(1,"Couldn't stat %s\n",name);
	return &st;
}

int
dirselect(struct dirent *de)
{
	if (!strcmp(de->d_name,"."))	return 0;
	if (!strcmp(de->d_name,".."))	return 0;
	return 1;
}

void
name_stat(const char *pfx, const char *dir, const char *name, struct dirent *de)
{
	char *buf = alloca(strlen(dir) + strlen(name) + 
		strlen(de->d_name) + 3);
	struct stat *st;

	strcpy(buf,dir); 
		strcat(buf,"/"); strcat(buf,name);
		strcat(buf,"/"); strcat(buf,de->d_name);
	st = StatFile(buf);
	printf("%s %s%s %lu %lu %o", 
	    pfx, name, de->d_name, 
	    st->st_uid, st->st_gid, st->st_mode & ~S_IFMT);
}

void
Equ(const char *dir1, const char *dir2, const char *name, struct dirent *de)
{
	if (de->d_type == DT_DIR) {
		char *p = alloca(strlen(name)+strlen(de->d_name)+2);

		strcpy(p,name);  strcat(p,de->d_name); strcat(p, "/");
		DoDir(dir1,dir2,p);
		s_same_dirs++;
	} else {
		char *buf1 = alloca(strlen(dir1) + strlen(name) + 
			strlen(de->d_name) + 3);
		char *buf2 = alloca(strlen(dir2) + strlen(name) + 
			strlen(de->d_name) + 3);
		char *m1,md5_1[33],*m2, md5_2[33];
		void *p1,*p2;
		int fd1,fd2;
		struct stat s1,s2;

		strcpy(buf1,dir1); 
			strcat(buf1,"/"); strcat(buf1,name);
			strcat(buf1,"/"); strcat(buf1,de->d_name);
		fd1 = open(buf1,O_RDONLY);
		if(fd1 < 0) { perror(buf1); exit(3); }
		fstat(fd1,&s1);
		strcpy(buf2,dir2); 
			strcat(buf2,"/"); strcat(buf2,name);
			strcat(buf2,"/"); strcat(buf2,de->d_name);
		fd2 = open(buf2,O_RDONLY);
		if(fd2 < 0) { perror(buf2); exit(3); }
		fstat(fd2,&s2);
#if 1
		if (s1.st_size == s2.st_size) {
			s_same_files++;
			s_same_bytes += s1.st_size;
			close(fd1);
			close(fd2);
			goto finish;
		}
#endif
		p1=mmap(0,s1.st_size,PROT_READ,MAP_PRIVATE,fd1,0);
		if ((int)p1 == -1) { perror(buf1); exit(3); }
		close(fd1);

		p2=mmap(0,s2.st_size,PROT_READ,MAP_PRIVATE,fd2,0);
		if ((int)p2 == -1) { perror(buf2); exit(3); }
		close(fd2);

		/* If identical, we're done. */
		if((s1.st_size == s2.st_size) && !memcmp(p1,p2,s1.st_size)) {
			s_same_files++;
			s_same_bytes += s1.st_size;
			goto finish;
		}

		s_files_chg++;
		if (s1.st_size > s2.st_size)
			s_bytes_del += (s1.st_size - s2.st_size);
		else
			s_bytes_add += (s2.st_size - s1.st_size);

		m1 = MD5Data(p1, s1.st_size, md5_1);
		m2 = MD5Data(p2, s2.st_size, md5_2);
		
		/* Just a curiosity... */
		if(!strcmp(m1,m2)) {
			if (s1.st_size != s2.st_size) 
				fprintf(stderr,
		"Notice: MD5 same for files of diffent size:\n\t%s\n\t%s\n",
					buf1,buf2);
			goto finish;
		}

		{
			u_long l = s1.st_size + s2.st_size;
			u_char *ob = alloca(l);
			Dissect(p1,p1+s1.st_size,p2,p2+s2.st_size,ob,&l);
			if (l && l < s2.st_size) {
				name_stat("CTMFB",dir2,name,de);
				printf(" %s %s %d\n",m1,m2,(unsigned)s1.st_size);
				s_edit_files++;
				s_edit_bytes += l;
				s_edit_saves += (s2.st_size - l);
				fwrite(ob,1,l,stdout);
				putchar('\n');
			} else {
				name_stat("CTMFS",dir2,name,de);
				printf(" %s %s %u\n",m1,m2,(unsigned)s2.st_size);
				fwrite(p2,1,s2.st_size,stdout);
				s_sub_files++;
				s_sub_bytes += s2.st_size;
			}
		}
	    finish:
		munmap(p1,s1.st_size);
		munmap(p2,s2.st_size);
	}
}

void
Add(const char *dir1, const char *dir2, const char *name, struct dirent *de)
{
	if (de->d_type == DT_DIR) {
		char *p = alloca(strlen(name)+strlen(de->d_name)+2);
		strcpy(p,name);  strcat(p,de->d_name); strcat(p, "/");
		name_stat("CTMDM",dir2,name,de);
		putchar('\n');
		s_new_dirs++;
		DoDir(dir1,dir2,p);
	} else if (de->d_type == DT_REG) {
		char *buf2 = alloca(strlen(dir2) + strlen(name) + 
			strlen(de->d_name) + 3);
		char *m2, md5_2[33];
		u_char *p1;
		struct stat st;
		int fd1;

		strcpy(buf2,dir2); 
			strcat(buf2,"/"); strcat(buf2,name);
			strcat(buf2,"/"); strcat(buf2,de->d_name);
		fd1 = open(buf2,O_RDONLY);
		if (fd1 < 0) {perror(buf2); exit (3); }
		fstat(fd1,&st);
		p1=mmap(0,st.st_size,PROT_READ,MAP_PRIVATE,fd1,0);
		if ((int)p1 == -1) { perror(buf2); exit(3); }
		close(fd1);
		m2 = MD5Data(p1, st.st_size, md5_2);
		name_stat("CTMFM",dir2,name,de);
		printf(" %s %u\n",m2,(unsigned)st.st_size);
		fwrite(p1,1,st.st_size,stdout);
		putchar('\n');
		munmap(p1,st.st_size);
		s_new_files++;
		s_new_bytes += st.st_size;
	}
}

void
Del (const char *dir1, const char *dir2, const char *name, struct dirent *de)
{
	if (de->d_type == DT_DIR) {
		char *p = alloca(strlen(name)+strlen(de->d_name)+2);
		strcpy(p,name);  strcat(p,de->d_name); strcat(p, "/");
		DoDir(dir1,dir2,p);
		printf("CTMDR %s%s\n",name,de->d_name);
		s_del_dirs++;
	} else if (de->d_type == DT_REG) {
		char *buf1 = alloca(strlen(dir1) + strlen(name) + 
			strlen(de->d_name) + 3);
		char *m1, md5_1[33];
		strcpy(buf1,dir1); 
			strcat(buf1,"/"); strcat(buf1,name);
			strcat(buf1,"/"); strcat(buf1,de->d_name);
		m1 = MD5File(buf1, md5_1);
		printf("CTMFR %s%s %s\n",name,de->d_name,m1);
		s_del_files++;
		s_del_bytes += StatFile(buf1)->st_size;
	}
}

void
GetNext(int *i, int *n, struct dirent **nl, const char *dir, const char *name, u_long *ignored, u_long *bogus, u_long *wrong)
{
	char buf[BUFSIZ];

	for (;;) {
		for (;;) {
			(*i)++;
			if (*i >= *n)
				return;
			*buf = 0;
			if (*dir != '/')
				strcat(buf,"/");
			strcat(buf,dir); 
			if (buf[strlen(buf)-1] != '/')
				strcat(buf,"/"); 
			strcat(buf,name);
			if (buf[strlen(buf)-1] != '/')
				strcat(buf,"/"); 
			fprintf(stderr,">%d<%s>\n",strlen(nl[*i]->d_name),nl[*i]->d_name);
			fflush(stderr);
			strcat(buf,nl[*i]->d_name);
			if (flag_ignore && !regexec(&reg_ignore,buf,0,0,0))
				(*ignored)++;
			else if (flag_bogus && !regexec(&reg_bogus,buf,0,0,0))
				(*bogus)++;
			else
				break;
			free(nl[*i]); nl[*i] = 0;
		}
		/* If the filesystem didn't tell us, find type */
		if (nl[*i]->d_type == DT_UNKNOWN) 
			nl[*i]->d_type = IFTODT(StatFile(buf)->st_mode);
		if (nl[*i]->d_type == DT_REG || nl[*i]->d_type == DT_DIR)
			break;
		(*wrong)++;
		free(nl[*i]); nl[*i] = 0;
	}
}

void
DoDir(const char *dir1, const char *dir2, const char *name)
{
	int i1,i2,n1,n2,i;
	struct dirent **nl1,**nl2;
	char *buf1 = alloca(strlen(dir1) + strlen(name) + 4);
	char *buf2 = alloca(strlen(dir2) + strlen(name) + 4);

	strcpy(buf1,dir1); strcat(buf1,"/"); strcat(buf1,name);
	strcpy(buf2,dir2); strcat(buf2,"/"); strcat(buf2,name);
	n1 = scandir(buf1, &nl1, dirselect, alphasort);
	n2 = scandir(buf2, &nl2, dirselect, alphasort);
	i1 = i2 = -1;
	GetNext(&i1, &n1, nl1, dir1, name, &s1_ignored, &s1_bogus, &s1_wrong);
	GetNext(&i2, &n2, nl2, dir2, name, &s2_ignored, &s2_bogus, &s2_wrong);
	for (;i1 < n1 || i2 < n2;) {

		/* Get next item from list 1 */
		if (i1 < n1 && !nl1[i1]) 
			GetNext(&i1, &n1, nl1, dir1, name, 
				&s1_ignored, &s1_bogus, &s1_wrong);

		/* Get next item from list 2 */
		if (i2 < n2 && !nl2[i2]) 
			GetNext(&i2, &n2, nl2, dir2, name, 
				&s2_ignored, &s2_bogus, &s2_wrong);

		if (i1 >= n1 && i2 >= n2) {
			/* Done */
			break;
		} else if (i1 >= n1 && i2 < n2) {
			/* end of list 1, add anything left on list 2 */
			Add(dir1,dir2,name,nl2[i2]);
			free(nl2[i2]); nl2[i2] = 0;
		} else if (i1 < n1 && i2 >= n2) {
			/* end of list 2, delete anything left on list 1 */
			Del(dir1,dir2,name,nl1[i1]);
			free(nl1[i1]); nl1[i1] = 0;
		} else if (!(i = strcmp(nl1[i1]->d_name, nl2[i2]->d_name))) {
			/* Identical names */
			if (nl1[i1]->d_type == nl2[i2]->d_type) {
				/* same type */
				Equ(dir1,dir2,name,nl1[i1]);
			} else {
				/* different types */
				Del(dir1,dir2,name,nl1[i1]);
				Add(dir1,dir2,name,nl2[i2]);
			}
			free(nl1[i1]); nl1[i1] = 0;
			free(nl2[i2]); nl2[i2] = 0;
		} else if (i < 0) {
			/* Something extra in list 1, delete it */
			Del(dir1,dir2,name,nl1[i1]);
			free(nl1[i1]); nl1[i1] = 0;
		} else {
			/* Something extra in list 2, add it */
			Add(dir1,dir2,name,nl2[i2]);
			free(nl2[i2]); nl2[i2] = 0;
		}
	}
	if (n1 >= 0)
		free(nl1);
	if (n2 >= 0)
		free(nl2);
}

int
main(int argc, char **argv)
{
	int i;
	extern char *optarg;
	extern int optind;

	if (regcomp(&reg_bogus,DEFAULT_BOGUS, REG_EXTENDED | REG_NEWLINE))
		/* XXX use regerror to explain it */
		err(1,"Default regular expression argument to -B is botched");
	flag_bogus = 1;

	if (regcomp(&reg_ignore,DEFAULT_IGNORE, REG_EXTENDED | REG_NEWLINE))
		/* XXX use regerror to explain it */
		err(1,"Default regular expression argument to -I is botched");
	flag_ignore = 1;

	while ((i = getopt(argc,argv,"I:B:")) != EOF)
		switch (i) {
		case 'I':
			if (flag_ignore)
				regfree(&reg_ignore);
			flag_ignore = 0;
			if (!*optarg)
				break;
			if (regcomp(&reg_ignore,optarg,
			    REG_EXTENDED | REG_NEWLINE))
				/* XXX use regerror to explain it */
				err(1,"Regular expression argument to -I is botched");
			flag_ignore = 1;
			break;
		case 'B':
			if (flag_bogus)
				regfree(&reg_bogus);
			flag_bogus = 0;
			if (!*optarg)
				break;
			if (regcomp(&reg_bogus,optarg,
			    REG_EXTENDED | REG_NEWLINE))
				/* XXX use regerror to explain it */
				err(1,"Regular expression argument to -B is botched");
			flag_bogus = 1;
			break;
		case '?':
		default:
			fprintf(stderr,"Usage:\n\t%s: %s\n", argv[0],
"[-I ignore_re] [-B bogus_re]");
			return (1);
		}
	argc -= optind;
	argv += optind;

	setbuf(stdout,0);

	signal(SIGINFO,stat_info);
	printf("CTM_BEGIN 2.0 tst 0 950326022230Z .\n");
	DoDir(argv[0],argv[1],"");
	printf("CTM_END ");
	print_stat(stderr,"");
	exit(0);
}
OpenPOWER on IntegriCloud