This commit is contained in:
Alessandro Ferro 2023-04-16 13:03:41 +02:00
parent 2670996a2f
commit 0f7d9b3e86
1 changed files with 18 additions and 10 deletions

View File

@ -133,7 +133,7 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
} }
else if(file_equality_result == -1) else if(file_equality_result == -1)
{ {
printf("Error while comparing file %s in the directories %s, %s", element->d_name printf("Error while comparing file %s in the directories %s, %s\n", element->d_name
, directory_to_analyze_1 , directory_to_analyze_1
, directory_to_analyze_2); , directory_to_analyze_2);
} }
@ -234,6 +234,10 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
} }
int are_files_equal(char* filename1, char* filename2){ int are_files_equal(char* filename1, char* filename2){
if(strcmp(filename1, filename2) == 0)
return 1; // it's the same path, so it's the same file
struct stat stat1, stat2; struct stat stat1, stat2;
if ( stat(filename1, &stat1) != 0 || stat(filename2, &stat2) != 0) if ( stat(filename1, &stat1) != 0 || stat(filename2, &stat2) != 0)
@ -249,17 +253,21 @@ int are_files_equal(char* filename1, char* filename2){
return -1; // error opening files return -1; // error opening files
} }
#define BYTES_TO_READ_AT_ONCE 512000 #define BYTES_TO_READ_AT_ONCE 512000
unsigned int bytes; unsigned char databuffer1[BYTES_TO_READ_AT_ONCE] = "";
#if BYTES_TO_READ_AT_ONCE > UINT_MAX unsigned char databuffer2[BYTES_TO_READ_AT_ONCE] = "";
#error Trying to read more bytes than what is possible to handle. Recompile using unsigned long or reduce BYTES_TO_READ_AT_ONCE size_t bytes;
#endif
unsigned char databuffer1[BYTES_TO_READ_AT_ONCE];
unsigned char databuffer2[BYTES_TO_READ_AT_ONCE];
while ((bytes = fread(databuffer1, 1, BYTES_TO_READ_AT_ONCE, file1)) != 0) while ((bytes = fread(databuffer1, 1, BYTES_TO_READ_AT_ONCE, file1)) != 0)
{ {
fread(databuffer2, 1, BYTES_TO_READ_AT_ONCE, file2); if(fread(databuffer2, 1, bytes, file2) != bytes){
if(strcmp(databuffer1, databuffer2) != 0) fclose(file1);
return 0; fclose(file2);
return -1; // error while reading the file(s)
}
if(memcmp(databuffer1, databuffer2, bytes) != 0){
fclose(file1);
fclose(file2);
return 0; // files are not the same
}
} }
fclose(file1); fclose(file1);
fclose(file2); fclose(file2);