mirror of
https://github.com/xfarrow/dircomp.git
synced 2025-06-05 21:49:19 +02:00
2
build.sh
2
build.sh
@@ -1,2 +1,2 @@
|
|||||||
#! /bin/bash
|
#! /bin/bash
|
||||||
gcc -o3 dircomp.c -lssl -lcrypto -o dircomp
|
gcc -o3 dircomp.c -o dircomp
|
||||||
|
89
dircomp.c
89
dircomp.c
@@ -81,8 +81,9 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
|
|||||||
printf("\nAnalyzing directories %s %s\n", directory_to_analyze_1, directory_to_analyze_2);
|
printf("\nAnalyzing directories %s %s\n", directory_to_analyze_1, directory_to_analyze_2);
|
||||||
|
|
||||||
bool is_directory_equal = true;
|
bool is_directory_equal = true;
|
||||||
int stat_result;
|
int stat_result, file_equality_result;
|
||||||
char fullpath_file_helper[512];
|
char fullpath_file_helper[512];
|
||||||
|
char fullpath_file_helper2[512];
|
||||||
char* subdirectory1;
|
char* subdirectory1;
|
||||||
char* subdirectory2;
|
char* subdirectory2;
|
||||||
struct dirent *element;
|
struct dirent *element;
|
||||||
@@ -109,27 +110,32 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
|
|||||||
, directory_to_analyze_2);
|
, directory_to_analyze_2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if files have the same signature
|
// Check if the files are the same
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
strcpy(fullpath_file_helper, directory_to_analyze_1);
|
strcpy(fullpath_file_helper, directory_to_analyze_1);
|
||||||
strcat(fullpath_file_helper, "/");
|
strcat(fullpath_file_helper, "/");
|
||||||
strcat(fullpath_file_helper, element->d_name);
|
strcat(fullpath_file_helper, element->d_name);
|
||||||
unsigned char *hash_file_1 = get_sha1_file(fullpath_file_helper);
|
strcpy(fullpath_file_helper2, directory_to_analyze_2);
|
||||||
strcpy(fullpath_file_helper, directory_to_analyze_2);
|
strcat(fullpath_file_helper2, "/");
|
||||||
strcat(fullpath_file_helper, "/");
|
strcat(fullpath_file_helper2, element->d_name);
|
||||||
strcat(fullpath_file_helper, element->d_name);
|
file_equality_result = are_files_equal(fullpath_file_helper, fullpath_file_helper2);
|
||||||
unsigned char *hash_file_2 = get_sha1_file(fullpath_file_helper);
|
if (file_equality_result != 1)
|
||||||
if (strcmp(hash_file_1, hash_file_2) != 0)
|
|
||||||
{
|
{
|
||||||
is_directory_equal = false;
|
is_directory_equal = false;
|
||||||
if (arguments->v == true)
|
if(file_equality_result == 0 && arguments->v == true)
|
||||||
printf("File %s in %s has a different signature in %s\n", element->d_name
|
{
|
||||||
, directory_to_analyze_1
|
printf("File %s in %s is different in %s\n" , element->d_name
|
||||||
, directory_to_analyze_2);
|
, directory_to_analyze_1
|
||||||
|
, directory_to_analyze_2);
|
||||||
|
}
|
||||||
|
else if(file_equality_result == -1)
|
||||||
|
{
|
||||||
|
printf("Error while comparing file %s in the directories %s, %s", element->d_name
|
||||||
|
, directory_to_analyze_1
|
||||||
|
, directory_to_analyze_2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
free(hash_file_1);
|
|
||||||
free(hash_file_2);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,7 +154,7 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
|
|||||||
struct stat *dummy_structure = malloc(sizeof(struct stat));
|
struct stat *dummy_structure = malloc(sizeof(struct stat));
|
||||||
stat_result = stat(fullpath_file_helper, dummy_structure);
|
stat_result = stat(fullpath_file_helper, dummy_structure);
|
||||||
free(dummy_structure);
|
free(dummy_structure);
|
||||||
if (stat_result == -1)
|
if (stat_result == -1) // directory does not exist
|
||||||
{
|
{
|
||||||
is_directory_equal = false;
|
is_directory_equal = false;
|
||||||
if (arguments->v == true)
|
if (arguments->v == true)
|
||||||
@@ -225,34 +231,37 @@ bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyz
|
|||||||
return is_directory_equal;
|
return is_directory_equal;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char *get_sha1_file(char *filename)
|
int are_files_equal(char* filename1, char* filename2){
|
||||||
{
|
struct stat stat1, stat2;
|
||||||
FILE *f = fopen(filename, "rb");
|
|
||||||
if (f == NULL)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Couldn't open %s\n", filename);
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// For a matter of efficiency, we do not read
|
if ( stat(filename1, &stat1) != 0 || stat(filename2, &stat2) != 0)
|
||||||
// the whole file at once. It'd be heavy on RAM.
|
return -1; // error opening files
|
||||||
// Instead, we read BYTES_TO_READ_AT_ONCE at time
|
|
||||||
#define BYTES_TO_READ_AT_ONCE 512000 // 500KiB
|
if(stat1.st_size != stat2.st_size)
|
||||||
unsigned int bytes; // how many bytes we have actually read from fread
|
return 0; // files are not the same as they have a different dimension
|
||||||
#if BYTES_TO_READ_AT_ONCE > UINT_MAX
|
|
||||||
#error Trying to read more bytes than what is possible to handle. Recompile using unsigned long or reduce BYTES_TO_READ_AT_ONCE
|
FILE *file1 = fopen(filename1, "rb");
|
||||||
#endif
|
FILE *file2 = fopen(filename2, "rb");
|
||||||
SHA_CTX context;
|
if (file1 == NULL || file2 == NULL)
|
||||||
unsigned char *hash = malloc(SHA_DIGEST_LENGTH * sizeof(unsigned char)); // result will be here
|
|
||||||
unsigned char databuffer[BYTES_TO_READ_AT_ONCE];
|
|
||||||
SHA1_Init(&context);
|
|
||||||
while ((bytes = fread(databuffer, 1, BYTES_TO_READ_AT_ONCE, f)) != 0)
|
|
||||||
{
|
{
|
||||||
SHA1_Update(&context, databuffer, bytes);
|
return -1; // error opening files
|
||||||
}
|
}
|
||||||
SHA1_Final(hash, &context);
|
#define BYTES_TO_READ_AT_ONCE 512000
|
||||||
fclose(f);
|
unsigned int bytes;
|
||||||
return hash;
|
#if BYTES_TO_READ_AT_ONCE > UINT_MAX
|
||||||
|
#error Trying to read more bytes than what is possible to handle. Recompile using unsigned long or reduce BYTES_TO_READ_AT_ONCE
|
||||||
|
#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)
|
||||||
|
{
|
||||||
|
fread(databuffer2, 1, BYTES_TO_READ_AT_ONCE, file2);
|
||||||
|
if(strcmp(databuffer1, databuffer2) != 0)
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
fclose(file1);
|
||||||
|
fclose(file2);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_help(void)
|
void print_help(void)
|
||||||
|
@@ -14,7 +14,6 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <openssl/sha.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@@ -33,8 +32,7 @@ struct arguments get_arguments(int, char**);
|
|||||||
// Reference: https://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html
|
// Reference: https://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html
|
||||||
bool analyze_directories(char*, char*, struct arguments*);
|
bool analyze_directories(char*, char*, struct arguments*);
|
||||||
|
|
||||||
// Reference: https://www.openssl.org/docs/man1.1.1/man3/SHA512_Init.html
|
int are_files_equal(char*, char*);
|
||||||
unsigned char* get_sha1_file(char *);
|
|
||||||
|
|
||||||
void print_help(void);
|
void print_help(void);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user