1/* Still missing: 2 * 3 * Damage counter 4 * Change counter 5 * Time stamp 6 * prefix 7 * cmd-line args 8 * %100 deltas 9 * delta and Add are different. delta -> Equ. 10 * 11 * mkctm 12 * -B regex Bogus 13 * -I regex Ignore |
14 * -D int Damage 15 * -q decrease verbosity |
16 * -v increase verbosity |
17 * (-l str control logging.) |
18 * name cvs-cur 19 * prefix src/secure 20 * dir1 "Soll" 21 * dir2 "Ist" 22 * 23 */ 24 25#include <sys/types.h> --- 6 unchanged lines hidden (view full) --- 32#include <fcntl.h> 33#include <string.h> 34#include <stdlib.h> 35#include <unistd.h> 36#include <md5.h> 37#include <err.h> 38#include <signal.h> 39 |
40#define DEFAULT_IGNORE "/CVS$|/\\.#|00_TRANS\\.TBL$" 41#define DEFAULT_BOGUS "\\.core$|\\.orig$|\\.rej$" |
42regex_t reg_ignore, reg_bogus; 43int flag_ignore, flag_bogus; 44 |
45int verbose; 46int damage, damage_limit; 47int change; 48 |
49u_long s1_ignored, s2_ignored; 50u_long s1_bogus, s2_bogus; 51u_long s1_wrong, s2_wrong; |
52u_long s_new_dirs, s_new_files, s_new_bytes; |
53u_long s_del_dirs, s_del_files, s_del_bytes; 54u_long s_files_chg, s_bytes_add, s_bytes_del; 55u_long s_same_dirs, s_same_files, s_same_bytes; |
56u_long s_edit_files, s_edit_bytes, s_edit_saves; 57u_long s_sub_files, s_sub_bytes; 58 59void |
60Usage(void) 61{ 62 fprintf(stderr, "Usage:\n"); 63 fprintf(stderr, "\tmkctm [-options] name number timestamp prefix"); 64 fprintf(stderr, " dir1 dir2"); 65 fprintf(stderr, "Options:\n"); 66 fprintf(stderr, "\t\t-B bogus_regexp\n"); 67 fprintf(stderr, "\t\t-D damage_limit\n"); 68 fprintf(stderr, "\t\t-I ignore_regexp\n"); 69 fprintf(stderr, "\t\t-q\n"); 70 fprintf(stderr, "\t\t-v\n"); 71} 72 73void |
74print_stat(FILE *fd, char *pre) 75{ 76 fprintf(fd,"%sAvoided:\n",pre); 77 fprintf(fd,"%s ignore: %5lu old %5lu new\n", 78 pre, s1_ignored, s2_ignored); 79 fprintf(fd,"%s bogus: %5lu old %5lu new\n", 80 pre, s1_bogus, s2_bogus); 81 fprintf(fd,"%s wrong: %5lu old %5lu new\n", --- 49 unchanged lines hidden (view full) --- 131 132 strcpy(buf,dir); 133 strcat(buf,"/"); strcat(buf,name); 134 strcat(buf,"/"); strcat(buf,de->d_name); 135 st = StatFile(buf); 136 printf("%s %s%s %lu %lu %o", 137 pfx, name, de->d_name, 138 st->st_uid, st->st_gid, st->st_mode & ~S_IFMT); |
139 if (verbose > 1) { 140 fprintf(stderr,"%s %s%s\n", pfx, name, de->d_name); 141 } |
142} 143 144void 145Equ(const char *dir1, const char *dir2, const char *name, struct dirent *de) 146{ 147 if (de->d_type == DT_DIR) { 148 char *p = alloca(strlen(name)+strlen(de->d_name)+2); 149 150 strcpy(p,name); strcat(p,de->d_name); strcat(p, "/"); 151 DoDir(dir1,dir2,p); 152 s_same_dirs++; 153 } else { 154 char *buf1 = alloca(strlen(dir1) + strlen(name) + 155 strlen(de->d_name) + 3); 156 char *buf2 = alloca(strlen(dir2) + strlen(name) + 157 strlen(de->d_name) + 3); 158 char *m1,md5_1[33],*m2, md5_2[33]; |
159 u_char *p1,*p2; |
160 int fd1,fd2; 161 struct stat s1,s2; 162 163 strcpy(buf1,dir1); 164 strcat(buf1,"/"); strcat(buf1,name); 165 strcat(buf1,"/"); strcat(buf1,de->d_name); 166 fd1 = open(buf1,O_RDONLY); 167 if(fd1 < 0) { perror(buf1); exit(3); } 168 fstat(fd1,&s1); 169 strcpy(buf2,dir2); 170 strcat(buf2,"/"); strcat(buf2,name); 171 strcat(buf2,"/"); strcat(buf2,de->d_name); 172 fd2 = open(buf2,O_RDONLY); 173 if(fd2 < 0) { perror(buf2); exit(3); } 174 fstat(fd2,&s2); |
175#if 0 176 /* XXX if we could just trust the size to change... */ |
177 if (s1.st_size == s2.st_size) { 178 s_same_files++; 179 s_same_bytes += s1.st_size; 180 close(fd1); 181 close(fd2); 182 goto finish; 183 } 184#endif --- 8 unchanged lines hidden (view full) --- 193 /* If identical, we're done. */ 194 if((s1.st_size == s2.st_size) && !memcmp(p1,p2,s1.st_size)) { 195 s_same_files++; 196 s_same_bytes += s1.st_size; 197 goto finish; 198 } 199 200 s_files_chg++; |
201 damage++; 202 change++; |
203 if (s1.st_size > s2.st_size) 204 s_bytes_del += (s1.st_size - s2.st_size); 205 else 206 s_bytes_add += (s2.st_size - s1.st_size); 207 208 m1 = MD5Data(p1, s1.st_size, md5_1); 209 m2 = MD5Data(p2, s2.st_size, md5_2); 210 211 /* Just a curiosity... */ 212 if(!strcmp(m1,m2)) { 213 if (s1.st_size != s2.st_size) 214 fprintf(stderr, 215 "Notice: MD5 same for files of diffent size:\n\t%s\n\t%s\n", 216 buf1,buf2); 217 goto finish; 218 } 219 220 { |
221 u_long l = s2.st_size + 2; 222 u_char *cmd = alloca(strlen(buf1)+strlen(buf2)+100); 223 u_char *ob = alloca(l), *p; 224 int j; 225 FILE *F; 226 227 if (p1[s1.st_size-1] != '\n') { 228 if (verbose > 0) 229 fprintf(stderr, 230 "last char != \\n in %s\n", 231 buf1); 232 goto subst; 233 } 234 235 if (p2[s2.st_size-1] != '\n') { 236 if (verbose > 0) 237 fprintf(stderr, 238 "last char != \\n in %s\n", 239 buf2); 240 goto subst; 241 } 242 243 for (p=p1; p<p1+s1.st_size; p++) 244 if (!*p) { 245 if (verbose > 0) 246 fprintf(stderr, 247 "NULL char in %s\n", 248 buf1); 249 goto subst; 250 } 251 252 for (p=p2; p<p2+s2.st_size; p++) 253 if (!*p) { 254 if (verbose > 0) 255 fprintf(stderr, 256 "NULL char in %s\n", 257 buf2); 258 goto subst; 259 } 260 261 strcpy(cmd, "diff -n "); 262 strcat(cmd, buf1); 263 strcat(cmd, " "); 264 strcat(cmd, buf2); 265 F = popen(cmd,"r"); 266 for (j = 1, l = 0; l < s2.st_size; ) { 267 j = fread(ob+l, 1, s2.st_size - l, F); 268 if (j < 1) 269 break; 270 l += j; 271 continue; 272 } 273 if (j) { 274 l = 0; 275 while (EOF != fgetc(F)) 276 continue; 277 } 278 pclose(F); 279 |
280 if (l && l < s2.st_size) { |
281 name_stat("CTMFN",dir2,name,de); 282 printf(" %s %s %d\n",m1,m2,(unsigned)l); 283 fwrite(ob,1,l,stdout); 284 putchar('\n'); |
285 s_edit_files++; 286 s_edit_bytes += l; 287 s_edit_saves += (s2.st_size - l); |
288 } else { |
289 subst: |
290 name_stat("CTMFS",dir2,name,de); 291 printf(" %s %s %u\n",m1,m2,(unsigned)s2.st_size); 292 fwrite(p2,1,s2.st_size,stdout); |
293 putchar('\n'); |
294 s_sub_files++; 295 s_sub_bytes += s2.st_size; 296 } 297 } 298 finish: 299 munmap(p1,s1.st_size); 300 munmap(p2,s2.st_size); 301 } 302} 303 304void 305Add(const char *dir1, const char *dir2, const char *name, struct dirent *de) 306{ |
307 change++; |
308 if (de->d_type == DT_DIR) { 309 char *p = alloca(strlen(name)+strlen(de->d_name)+2); 310 strcpy(p,name); strcat(p,de->d_name); strcat(p, "/"); 311 name_stat("CTMDM",dir2,name,de); 312 putchar('\n'); 313 s_new_dirs++; 314 DoDir(dir1,dir2,p); 315 } else if (de->d_type == DT_REG) { --- 22 unchanged lines hidden (view full) --- 338 s_new_files++; 339 s_new_bytes += st.st_size; 340 } 341} 342 343void 344Del (const char *dir1, const char *dir2, const char *name, struct dirent *de) 345{ |
346 damage++; 347 change++; |
348 if (de->d_type == DT_DIR) { 349 char *p = alloca(strlen(name)+strlen(de->d_name)+2); 350 strcpy(p,name); strcat(p,de->d_name); strcat(p, "/"); 351 DoDir(dir1,dir2,p); 352 printf("CTMDR %s%s\n",name,de->d_name); 353 s_del_dirs++; 354 } else if (de->d_type == DT_REG) { 355 char *buf1 = alloca(strlen(dir1) + strlen(name) + --- 23 unchanged lines hidden (view full) --- 379 if (*dir != '/') 380 strcat(buf,"/"); 381 strcat(buf,dir); 382 if (buf[strlen(buf)-1] != '/') 383 strcat(buf,"/"); 384 strcat(buf,name); 385 if (buf[strlen(buf)-1] != '/') 386 strcat(buf,"/"); |
387 strcat(buf,nl[*i]->d_name); |
388 if (flag_ignore && 389 !regexec(®_ignore,buf,0,0,0)) { |
390 (*ignored)++; |
391 if (verbose > 2) { 392 fprintf(stderr,"Ignore %s\n",buf); 393 } 394 } else if (flag_bogus && 395 !regexec(®_bogus,buf,0,0,0)) { |
396 (*bogus)++; |
397 if (verbose > 0) { 398 fprintf(stderr,"Bogus %s\n",buf); 399 } 400 } else { |
401 break; |
402 } |
403 free(nl[*i]); nl[*i] = 0; 404 } 405 /* If the filesystem didn't tell us, find type */ 406 if (nl[*i]->d_type == DT_UNKNOWN) 407 nl[*i]->d_type = IFTODT(StatFile(buf)->st_mode); 408 if (nl[*i]->d_type == DT_REG || nl[*i]->d_type == DT_DIR) 409 break; 410 (*wrong)++; |
411 if (verbose > 0) 412 fprintf(stderr,"Wrong %s\n",buf); |
413 free(nl[*i]); nl[*i] = 0; 414 } 415} 416 417void 418DoDir(const char *dir1, const char *dir2, const char *name) 419{ 420 int i1,i2,n1,n2,i; --- 5 unchanged lines hidden (view full) --- 426 strcpy(buf2,dir2); strcat(buf2,"/"); strcat(buf2,name); 427 n1 = scandir(buf1, &nl1, dirselect, alphasort); 428 n2 = scandir(buf2, &nl2, dirselect, alphasort); 429 i1 = i2 = -1; 430 GetNext(&i1, &n1, nl1, dir1, name, &s1_ignored, &s1_bogus, &s1_wrong); 431 GetNext(&i2, &n2, nl2, dir2, name, &s2_ignored, &s2_bogus, &s2_wrong); 432 for (;i1 < n1 || i2 < n2;) { 433 |
434 if (damage_limit && damage > damage_limit) 435 break; 436 |
437 /* Get next item from list 1 */ 438 if (i1 < n1 && !nl1[i1]) 439 GetNext(&i1, &n1, nl1, dir1, name, 440 &s1_ignored, &s1_bogus, &s1_wrong); 441 442 /* Get next item from list 2 */ 443 if (i2 < n2 && !nl2[i2]) 444 GetNext(&i2, &n2, nl2, dir2, name, --- 40 unchanged lines hidden (view full) --- 485 486int 487main(int argc, char **argv) 488{ 489 int i; 490 extern char *optarg; 491 extern int optind; 492 |
493 setbuf(stderr, NULL); 494 |
495 if (regcomp(®_bogus,DEFAULT_BOGUS, REG_EXTENDED | REG_NEWLINE)) 496 /* XXX use regerror to explain it */ 497 err(1,"Default regular expression argument to -B is botched"); 498 flag_bogus = 1; 499 500 if (regcomp(®_ignore,DEFAULT_IGNORE, REG_EXTENDED | REG_NEWLINE)) 501 /* XXX use regerror to explain it */ 502 err(1,"Default regular expression argument to -I is botched"); 503 flag_ignore = 1; 504 |
505 while ((i = getopt(argc,argv,"D:I:B:qv")) != EOF) |
506 switch (i) { |
507 case 'D': 508 damage_limit = strtol(optarg,0,0); 509 if (damage_limit < 0) 510 err(1,"Damage limit must be positive"); 511 break; |
512 case 'I': 513 if (flag_ignore) 514 regfree(®_ignore); 515 flag_ignore = 0; 516 if (!*optarg) 517 break; 518 if (regcomp(®_ignore,optarg, 519 REG_EXTENDED | REG_NEWLINE)) --- 8 unchanged lines hidden (view full) --- 528 if (!*optarg) 529 break; 530 if (regcomp(®_bogus,optarg, 531 REG_EXTENDED | REG_NEWLINE)) 532 /* XXX use regerror to explain it */ 533 err(1,"Regular expression argument to -B is botched"); 534 flag_bogus = 1; 535 break; |
536 case 'q': 537 verbose--; 538 break; 539 case 'v': 540 verbose++; 541 break; |
542 case '?': 543 default: |
544 Usage(); |
545 return (1); 546 } 547 argc -= optind; 548 argv += optind; 549 550 setbuf(stdout,0); 551 |
552 if (argc != 6) { 553 Usage(); 554 return (1); 555 } 556 |
557 signal(SIGINFO,stat_info); |
558 559 printf("CTM_BEGIN 2.0 %s %s %s %s\n", 560 argv[0], argv[1], argv[2], argv[3]); 561 DoDir(argv[4],argv[5],""); 562 if (damage_limit && damage > damage_limit) { 563 print_stat(stderr,""); 564 err(1,"Damage would exceede %d files", damage_limit); 565 } else if (!change) { 566 err(1,"No changes"); 567 } else { 568 printf("CTM_END "); 569 print_stat(stderr,""); 570 } |
571 exit(0); 572} |