Fixed an endless cycle with early EOF

This commit is contained in:
Thomas Schmitt 2008-02-08 09:49:44 +00:00
parent 8815ffe6e0
commit 3322d5552d

View File

@ -1,5 +1,6 @@
/* /*
Compare two copies of a file object in as many aspects as i can imagine. Compare two copies of a file object in as many aspects as i can imagine
to make sense. (E.g.: comparing atime makes no sense.)
To compare tree /media/dvd and /original/dir : To compare tree /media/dvd and /original/dir :
find /media/dvd -exec compare_file '{}' /media/dvd /original/dir ';' find /media/dvd -exec compare_file '{}' /media/dvd /original/dir ';'
@ -29,7 +30,7 @@
int Compare_2_files(char *adr1, char *adr2, char *adrc, int flag) int Compare_2_files(char *adr1, char *adr2, char *adrc, int flag)
{ {
struct stat s1, s2; struct stat s1, s2;
int ret, differs= 0, r1, r2, fd1, fd2, i; int ret, differs= 0, r1, r2, fd1= -1, fd2= -1, i, done;
char buf1[4096], buf2[4096], a[4096]; char buf1[4096], buf2[4096], a[4096];
off_t r1count= 0, r2count= 0, diffcount= 0, first_diff= -1; off_t r1count= 0, r2count= 0, diffcount= 0, first_diff= -1;
@ -126,7 +127,8 @@ int Compare_2_files(char *adr1, char *adr2, char *adrc, int flag)
} }
/* Content */ /* Content */
while(1) { done= 0;
while(!done) {
r1= read(fd1, buf1, sizeof(buf1)); r1= read(fd1, buf1, sizeof(buf1));
r2= read(fd2, buf2, sizeof(buf2)); r2= read(fd2, buf2, sizeof(buf2));
if((r1==EOF && r2==EOF) || (r1==0 && r2==0)) if((r1==EOF && r2==EOF) || (r1==0 && r2==0))
@ -135,7 +137,7 @@ int Compare_2_files(char *adr1, char *adr2, char *adrc, int flag)
if(r1==EOF) if(r1==EOF)
r1= 0; r1= 0;
if(s1.st_size > r1count + r1) if(s1.st_size > r1count + r1)
printf("- %s : early EOF after %d bytes\n", adr1, r1count); printf("- %s : early EOF after %.f bytes\n", adr1, (double) r1count);
differs= 1; differs= 1;
} }
r1count+= r1; r1count+= r1;
@ -143,13 +145,15 @@ int Compare_2_files(char *adr1, char *adr2, char *adrc, int flag)
if(r2==EOF) if(r2==EOF)
r2= 0; r2= 0;
if(s2.st_size > r2count + r2) if(s2.st_size > r2count + r2)
printf("- %s : early EOF after %d bytes\n", adr2, r2count); printf("- %s : early EOF after %.f bytes\n", adr2, (double) r2count);
differs= 1; differs= 1;
done= 1;
} }
if(r2>r1) { if(r2>r1) {
if(s1.st_size > r1count + r1) if(s1.st_size > r1count + r1)
printf("- %s : early EOF after %d bytes\n", adr1, r1count); printf("- %s : early EOF after %.f bytes\n", adr1, (double) r1count);
differs= 1; differs= 1;
done= 1;
} }
r2count+= r2; r2count+= r2;
if(r1>r2) if(r1>r2)
@ -166,14 +170,15 @@ int Compare_2_files(char *adr1, char *adr2, char *adrc, int flag)
if(first_diff<0) if(first_diff<0)
first_diff= (r1count>r2count ? r2count : r1count); first_diff= (r1count>r2count ? r2count : r1count);
printf("%s : %s : differs by at least %.f bytes. First at %.f\n", a, printf("%s : %s : differs by at least %.f bytes. First at %.f\n", a,
(s1.st_size==s2.st_size || s1.st_mtime==s2.st_mtime ? (s1.st_mtime==s2.st_mtime ? "CONTENT":"content"),
"CONTENT":"content"),
(double) (diffcount + abs(r1count-r2count)), (double) first_diff); (double) (diffcount + abs(r1count-r2count)), (double) first_diff);
differs= 1; differs= 1;
} }
} }
close(fd1); if(fd1!=-1)
close(fd2); close(fd1);
if(fd2!=-1)
close(fd2);
return(!differs); return(!differs);
} }