Added c_compare_file function and test - WIP.

This commit is contained in:
Klaas Freitag 2013-03-22 10:23:17 +01:00
parent 8b229f6413
commit 3ce614039a
3 changed files with 101 additions and 0 deletions

View file

@ -163,3 +163,80 @@ out:
#endif
}
int c_compare_file( const char *f1, const char *f2 ) {
_TCHAR *wf1, *wf2;
int fd1 = -1, fd2 = -1;
size_t size1, size2, i;
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;
if( ! (f1 && f2) ) return -1;
wf1 = c_multibyte(f1);
wf2 = c_multibyte(f2);
/* compare size first. */
if( _tstat(wf1, &stat1) < 0 ) {
re = -1;
goto out;
}
if( _tstat(wf2, &stat2) < 0 ) {
re = -1;
goto out;
}
/* if sizes are different, the files can not be equal. */
if( stat1.st_size != stat2.st_size ) {
re = 0;
goto out;
}
#ifdef _WIN32
_fmode = _O_BINARY;
#endif
fd1 = _topen(wf1, O_RDONLY);
fd2 = _topen(wf2, O_RDONLY);
if( !(fd1 > -1 && fd2 > -1)) {
re = -1;
goto out;
}
while( (size1 = read(fd1, buffer1, BUFFER_SIZE)) > 0 ) {
size2 = read( fd2, buffer2, BUFFER_SIZE );
if( size1 != size2 ) {
re = 0;
goto out;
}
s1 = buffer1;
s2 = buffer2;
for( i = 0; i < size1; i++ ) {
if( *s1++ != *s2++ ) {
re = 0;
goto out;
}
}
}
re = 1;
out:
if( fd1 > -1) close(fd1);
if( fd2 > -1) close(fd2);
c_free_multibyte( wf1 );
c_free_multibyte( wf2 );
return re;
}

View file

@ -63,6 +63,8 @@ int c_isfile(const char *path);
*/
int c_copy(const char *src, const char *dst, mode_t mode);
int c_compare_file( const char *f1, const char *f2 );
/**
* }@
*/

View file

@ -85,12 +85,34 @@ static void check_c_copy_isdir(void **state)
assert_int_equal(errno, EISDIR);
}
static void check_c_compare_file(void **state)
{
int rc;
(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);
rc = system("echo \"hallo42\" > /tmp/check/foo.txt");
assert_int_equal(rc, 0);
rc = system("echo \"hallo52\" > /tmp/check/bar.txt");
assert_int_equal(rc, 0);
rc = c_compare_file( check_src_file, check_dst_file );
assert_int_equal(rc, 0);
}
int torture_run_tests(void)
{
const UnitTest tests[] = {
unit_test_setup_teardown(check_c_copy, setup, teardown),
unit_test(check_c_copy_same_file),
unit_test_setup_teardown(check_c_copy_isdir, setup, teardown),
unit_test_setup_teardown(check_c_compare_file, setup, teardown),
};
return run_tests(tests);