Improved C style and added more tests.

This commit is contained in:
Klaas Freitag 2013-03-22 10:23:17 +01:00
parent 79c51540da
commit 5951039894
2 changed files with 45 additions and 30 deletions

View file

@ -30,6 +30,7 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include "c_file.h"
#include "c_string.h"
@ -166,34 +167,38 @@ out:
int c_compare_file( const char *f1, const char *f2 ) {
_TCHAR *wf1, *wf2;
int fd1 = -1, fd2 = -1;
size_t size1, size2, i;
size_t size1, size2;
char buffer1[BUFFER_SIZE];
char buffer2[BUFFER_SIZE];
csync_stat_t stat1;
csync_stat_t stat2;
register const char *s1;
register const char *s2;
int re = -1;
int rc = -1;
if( ! (f1 && f2) ) return -1;
if(f1 == NULL || f2 == NULL) return -1;
wf1 = c_multibyte(f1);
if(wf1 == NULL) {
return -1;
}
wf2 = c_multibyte(f2);
if(wf2 == NULL) {
return -1;
}
/* compare size first. */
if( _tstat(wf1, &stat1) < 0 ) {
re = -1;
rc = _tstat(wf1, &stat1);
if(rc< 0) {
goto out;
}
if( _tstat(wf2, &stat2) < 0 ) {
re = -1;
rc = _tstat(wf2, &stat2);
if(rc < 0) {
goto out;
}
/* if sizes are different, the files can not be equal. */
if( stat1.st_size != stat2.st_size ) {
re = 0;
rc = 0;
goto out;
}
@ -202,10 +207,13 @@ int c_compare_file( const char *f1, const char *f2 ) {
#endif
fd1 = _topen(wf1, O_RDONLY);
if(fd1 < 0) {
rc = -1;
goto out;
}
fd2 = _topen(wf2, O_RDONLY);
if( !(fd1 > -1 && fd2 > -1)) {
re = -1;
if(fd2 < 0) {
rc = -1;
goto out;
}
@ -213,30 +221,26 @@ int c_compare_file( const char *f1, const char *f2 ) {
size2 = read( fd2, buffer2, BUFFER_SIZE );
if( size1 != size2 ) {
re = 0;
rc = 0;
goto out;
}
s1 = buffer1;
s2 = buffer2;
for( i = 0; i < size1; i++ ) {
if( *s1++ != *s2++ ) {
re = 0;
goto out;
}
if(memcmp(buffer1, buffer2, size1) != 0) {
/* buffers are different */
rc = 0;
goto out;
}
}
re = 1;
rc = 1;
out:
if( fd1 > -1) close(fd1);
if( fd2 > -1) close(fd2);
if(fd1 > -1) close(fd1);
if(fd2 > -1) close(fd2);
c_free_multibyte( wf1 );
c_free_multibyte( wf2 );
return re;
c_free_multibyte(wf1);
c_free_multibyte(wf2);
return rc;
}

View file

@ -96,6 +96,19 @@ static void check_c_compare_file(void **state)
rc = c_compare_file( check_src_file, check_dst_file );
assert_int_equal(rc, 1);
/* Check error conditions */
rc = c_compare_file( NULL, check_dst_file );
assert_int_equal(rc, -1);
rc = c_compare_file( check_dst_file, NULL );
assert_int_equal(rc, -1);
rc = c_compare_file( NULL, NULL );
assert_int_equal(rc, -1);
rc = c_compare_file( check_src_file, "/I_do_not_exist_in_the_filesystem.dummy");
assert_int_equal(rc, -1);
rc = c_compare_file( "/I_do_not_exist_in_the_filesystem.dummy", check_dst_file);
assert_int_equal(rc, -1);
rc = system("echo \"hallo42\" > /tmp/check/foo.txt");
assert_int_equal(rc, 0);
rc = system("echo \"hallo52\" > /tmp/check/bar.txt");
@ -123,8 +136,6 @@ static void check_c_compare_file(void **state)
rc = c_copy(check_src_file, check_dst_file, 0644);
assert_int_equal(rc, 0);
rc = c_compare_file( check_src_file, check_dst_file );
assert_int_equal(rc, 1);
}
int torture_run_tests(void)