2023-04-13 09:46:28 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
|
|
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2023-04-14 22:36:09 +02:00
|
|
|
#include "dircomp.h"
|
2023-04-13 09:46:28 +02:00
|
|
|
|
2023-04-14 22:36:09 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2023-04-13 10:14:32 +02:00
|
|
|
struct arguments arguments = get_arguments(argc, argv);
|
2023-04-14 22:36:09 +02:00
|
|
|
if (arguments.h == true)
|
|
|
|
{
|
2023-04-13 17:58:24 +02:00
|
|
|
print_help();
|
2023-04-13 10:14:32 +02:00
|
|
|
return 0;
|
2023-04-13 17:58:24 +02:00
|
|
|
}
|
2023-04-14 22:36:09 +02:00
|
|
|
|
|
|
|
char* directory_to_analyze1 = malloc(strlen(arguments.directory1) * sizeof(char));
|
|
|
|
char* directory_to_analyze2 = malloc(strlen(arguments.directory2) * sizeof(char));
|
|
|
|
strcpy(directory_to_analyze1, arguments.directory1);
|
|
|
|
strcpy(directory_to_analyze2, arguments.directory2);
|
|
|
|
|
|
|
|
if (analyze_directories(directory_to_analyze1, directory_to_analyze2, &arguments))
|
|
|
|
{
|
2023-04-14 18:20:44 +02:00
|
|
|
printf("Directories are equal\n");
|
|
|
|
}
|
2023-04-14 22:36:09 +02:00
|
|
|
else
|
|
|
|
{
|
2023-04-14 18:20:44 +02:00
|
|
|
printf("Directories are not equal\n");
|
|
|
|
}
|
2023-04-13 10:14:32 +02:00
|
|
|
return 0;
|
2023-04-13 09:46:28 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 22:36:09 +02:00
|
|
|
struct arguments get_arguments(int argc, char **argv)
|
|
|
|
{
|
2023-04-14 18:20:44 +02:00
|
|
|
struct arguments provided_arguments = {"", "", false, false, false};
|
2023-04-14 22:51:43 +02:00
|
|
|
if(argc == 1){
|
|
|
|
provided_arguments.h = true;
|
|
|
|
return provided_arguments;
|
|
|
|
}
|
2023-04-13 09:46:28 +02:00
|
|
|
char option;
|
2023-04-14 22:36:09 +02:00
|
|
|
while ((option = getopt(argc, argv, "rnsvh")) != -1)
|
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'r':
|
|
|
|
provided_arguments.r = true;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
provided_arguments.v = true;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
provided_arguments.h = true;
|
|
|
|
break;
|
2023-04-13 10:14:32 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-13 09:46:28 +02:00
|
|
|
|
2023-04-13 23:48:16 +02:00
|
|
|
// Get directories
|
2023-04-14 22:36:09 +02:00
|
|
|
if ((argc - optind) < 2)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Not enough directories.\n");
|
|
|
|
exit(-1);
|
2023-04-13 23:48:16 +02:00
|
|
|
}
|
2023-04-14 22:36:09 +02:00
|
|
|
else if ((argc - optind) > 2)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Too many directories.\n");
|
|
|
|
exit(-1);
|
2023-04-13 23:48:16 +02:00
|
|
|
}
|
2023-04-14 22:36:09 +02:00
|
|
|
provided_arguments.directory1 = malloc((strlen(argv[optind]) * sizeof(char)) + 1);
|
2023-04-13 23:48:16 +02:00
|
|
|
strcpy(provided_arguments.directory1, argv[optind]);
|
2023-04-14 22:36:09 +02:00
|
|
|
provided_arguments.directory2 = malloc((strlen(argv[optind + 1]) * sizeof(char)) + 1);
|
2023-04-13 23:48:16 +02:00
|
|
|
strcpy(provided_arguments.directory2, argv[optind + 1]);
|
|
|
|
return provided_arguments;
|
2023-04-13 09:46:28 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 22:36:09 +02:00
|
|
|
bool analyze_directories(char* directory_to_analyze_1, char* directory_to_analyze_2, struct arguments *arguments)
|
|
|
|
{
|
|
|
|
if (arguments->v == true)
|
|
|
|
printf("\nAnalyzing directories %s %s\n", directory_to_analyze_1, directory_to_analyze_2);
|
2023-04-13 09:49:03 +02:00
|
|
|
|
2023-04-14 18:20:44 +02:00
|
|
|
bool is_directory_equal = true;
|
2023-04-15 22:19:04 +02:00
|
|
|
int stat_result, file_equality_result;
|
2023-04-14 22:36:09 +02:00
|
|
|
char fullpath_file_helper[512];
|
2023-04-15 22:19:04 +02:00
|
|
|
char fullpath_file_helper2[512];
|
2023-04-14 22:36:09 +02:00
|
|
|
char* subdirectory1;
|
|
|
|
char* subdirectory2;
|
|
|
|
struct dirent *element;
|
2023-04-14 18:20:44 +02:00
|
|
|
DIR *directory;
|
|
|
|
|
2023-04-14 22:36:09 +02:00
|
|
|
directory = opendir(directory_to_analyze_1);
|
|
|
|
if (directory)
|
|
|
|
{
|
|
|
|
while ((element = readdir(directory)) != NULL)
|
|
|
|
{
|
2023-04-14 18:20:44 +02:00
|
|
|
// Is file
|
2023-04-14 22:36:09 +02:00
|
|
|
if (element->d_type == DT_REG)
|
2023-04-13 10:14:32 +02:00
|
|
|
{
|
2023-04-14 18:20:44 +02:00
|
|
|
// Check wether it exists in directory2
|
2023-04-14 22:36:09 +02:00
|
|
|
strcpy(fullpath_file_helper, directory_to_analyze_2);
|
|
|
|
strcat(fullpath_file_helper, "/");
|
|
|
|
strcat(fullpath_file_helper, element->d_name);
|
|
|
|
if (access(fullpath_file_helper, F_OK) == -1)
|
2023-04-14 18:20:44 +02:00
|
|
|
{
|
|
|
|
is_directory_equal = false;
|
2023-04-14 22:36:09 +02:00
|
|
|
if (arguments->v == true)
|
|
|
|
printf("File %s exists in %s but does not in %s\n" , element->d_name
|
|
|
|
, directory_to_analyze_1
|
|
|
|
, directory_to_analyze_2);
|
2023-04-14 18:20:44 +02:00
|
|
|
}
|
2023-04-14 22:36:09 +02:00
|
|
|
|
2023-04-15 22:19:04 +02:00
|
|
|
// Check if the files are the same
|
2023-04-14 22:36:09 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
strcpy(fullpath_file_helper, directory_to_analyze_1);
|
|
|
|
strcat(fullpath_file_helper, "/");
|
|
|
|
strcat(fullpath_file_helper, element->d_name);
|
2023-04-15 22:19:04 +02:00
|
|
|
strcpy(fullpath_file_helper2, directory_to_analyze_2);
|
|
|
|
strcat(fullpath_file_helper2, "/");
|
|
|
|
strcat(fullpath_file_helper2, element->d_name);
|
|
|
|
file_equality_result = are_files_equal(fullpath_file_helper, fullpath_file_helper2);
|
|
|
|
if (file_equality_result != 1)
|
2023-04-14 22:36:09 +02:00
|
|
|
{
|
2023-04-14 18:20:44 +02:00
|
|
|
is_directory_equal = false;
|
2023-04-15 22:19:04 +02:00
|
|
|
if(file_equality_result == 0 && arguments->v == true)
|
|
|
|
{
|
|
|
|
printf("File %s in %s is different in %s\n" , element->d_name
|
|
|
|
, 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);
|
|
|
|
}
|
2023-04-14 18:20:44 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-13 10:14:32 +02:00
|
|
|
}
|
2023-04-13 09:49:03 +02:00
|
|
|
|
2023-04-14 18:20:44 +02:00
|
|
|
// Is directory
|
2023-04-14 22:36:09 +02:00
|
|
|
else if (element->d_type == DT_DIR)
|
2023-04-13 10:14:32 +02:00
|
|
|
{
|
2023-04-14 22:36:09 +02:00
|
|
|
if (strcmp(element->d_name, ".") == 0 || strcmp(element->d_name, "..") == 0)
|
|
|
|
{
|
|
|
|
continue;
|
2023-04-14 18:20:44 +02:00
|
|
|
}
|
2023-04-14 22:36:09 +02:00
|
|
|
// Check wether a folder with the same name exists in directory2
|
|
|
|
strcpy(fullpath_file_helper, directory_to_analyze_2);
|
|
|
|
strcat(fullpath_file_helper, "/");
|
|
|
|
strcat(fullpath_file_helper, element->d_name);
|
|
|
|
// Allocate heap memory in order to be able to free it before a potential recursive call starts
|
|
|
|
struct stat *dummy_structure = malloc(sizeof(struct stat));
|
|
|
|
stat_result = stat(fullpath_file_helper, dummy_structure);
|
2023-04-14 18:20:44 +02:00
|
|
|
free(dummy_structure);
|
2023-04-15 22:19:04 +02:00
|
|
|
if (stat_result == -1) // directory does not exist
|
2023-04-14 22:36:09 +02:00
|
|
|
{
|
|
|
|
is_directory_equal = false;
|
|
|
|
if (arguments->v == true)
|
|
|
|
printf("Sub-directory %s exists in %s but does not in %s\n" , element->d_name
|
|
|
|
, directory_to_analyze_1
|
|
|
|
, directory_to_analyze_2);
|
|
|
|
}
|
2023-04-14 18:20:44 +02:00
|
|
|
// Analyze recursively
|
2023-04-14 22:36:09 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (arguments->r == true)
|
|
|
|
{
|
|
|
|
|
|
|
|
subdirectory1 = malloc(sizeof(char) * (strlen(directory_to_analyze_1) + strlen(element->d_name) + 2));
|
|
|
|
strcpy(subdirectory1, directory_to_analyze_1);
|
|
|
|
strcat(subdirectory1, "/");
|
|
|
|
strcat(subdirectory1, element->d_name);
|
|
|
|
subdirectory2 = malloc(sizeof(char) * (strlen(directory_to_analyze_2) + strlen(element->d_name) + 2));
|
|
|
|
strcpy(subdirectory2, directory_to_analyze_2);
|
|
|
|
strcat(subdirectory2, "/");
|
|
|
|
strcat(subdirectory2, element->d_name);
|
|
|
|
is_directory_equal = analyze_directories(subdirectory1, subdirectory2, arguments) && is_directory_equal;
|
2023-04-14 18:20:44 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-13 10:14:32 +02:00
|
|
|
}
|
2023-04-14 18:20:44 +02:00
|
|
|
}
|
|
|
|
closedir(directory);
|
|
|
|
}
|
2023-04-13 09:49:03 +02:00
|
|
|
|
2023-04-14 22:36:09 +02:00
|
|
|
directory = opendir(directory_to_analyze_2);
|
|
|
|
if (directory)
|
2023-04-14 18:20:44 +02:00
|
|
|
{
|
2023-04-14 22:36:09 +02:00
|
|
|
while ((element = readdir(directory)) != NULL)
|
2023-04-14 18:20:44 +02:00
|
|
|
{
|
|
|
|
// Is file
|
2023-04-14 22:36:09 +02:00
|
|
|
if (element->d_type == DT_REG)
|
2023-04-14 18:20:44 +02:00
|
|
|
{
|
|
|
|
// Check wether it exists in directory1
|
2023-04-14 22:36:09 +02:00
|
|
|
strcpy(fullpath_file_helper, directory_to_analyze_1);
|
|
|
|
strcat(fullpath_file_helper, "/");
|
|
|
|
strcat(fullpath_file_helper, element->d_name);
|
|
|
|
if (access(fullpath_file_helper, F_OK) == -1)
|
2023-04-14 18:20:44 +02:00
|
|
|
{
|
|
|
|
is_directory_equal = false;
|
2023-04-14 22:36:09 +02:00
|
|
|
if (arguments->v == true)
|
|
|
|
printf("File %s exists in %s but does not in %s\n" , element->d_name
|
|
|
|
, directory_to_analyze_2
|
|
|
|
, directory_to_analyze_1);
|
2023-04-14 18:20:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is directory
|
2023-04-14 22:36:09 +02:00
|
|
|
else if (element->d_type == DT_DIR)
|
2023-04-14 18:20:44 +02:00
|
|
|
{
|
2023-04-14 22:36:09 +02:00
|
|
|
// Check wether a folder with the same name exists in directory1
|
|
|
|
strcpy(fullpath_file_helper, directory_to_analyze_1);
|
|
|
|
strcat(fullpath_file_helper, "/");
|
|
|
|
strcat(fullpath_file_helper, element->d_name);
|
2023-04-14 18:20:44 +02:00
|
|
|
struct stat dummy_structure; // no need to be malloc-ed, as it'll be automatically free-d as the call ends
|
2023-04-14 22:36:09 +02:00
|
|
|
if (stat(fullpath_file_helper, &dummy_structure) == -1)
|
|
|
|
{
|
2023-04-14 18:20:44 +02:00
|
|
|
is_directory_equal = false;
|
2023-04-14 22:36:09 +02:00
|
|
|
if (arguments->v == true)
|
|
|
|
printf("Sub-directory %s exists in %s but does not in %s\n" , element->d_name
|
|
|
|
, directory_to_analyze_2
|
|
|
|
, directory_to_analyze_1);
|
2023-04-14 18:20:44 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-13 10:14:32 +02:00
|
|
|
}
|
2023-04-14 18:20:44 +02:00
|
|
|
closedir(directory);
|
2023-04-13 10:14:32 +02:00
|
|
|
}
|
2023-04-14 22:36:09 +02:00
|
|
|
free(directory_to_analyze_1);
|
|
|
|
free(directory_to_analyze_2);
|
2023-04-14 18:20:44 +02:00
|
|
|
return is_directory_equal;
|
2023-04-13 09:46:28 +02:00
|
|
|
}
|
2023-04-13 23:48:16 +02:00
|
|
|
|
2023-04-15 22:19:04 +02:00
|
|
|
int are_files_equal(char* filename1, char* filename2){
|
|
|
|
struct stat stat1, stat2;
|
|
|
|
|
|
|
|
if ( stat(filename1, &stat1) != 0 || stat(filename2, &stat2) != 0)
|
|
|
|
return -1; // error opening files
|
|
|
|
|
|
|
|
if(stat1.st_size != stat2.st_size)
|
|
|
|
return 0; // files are not the same as they have a different dimension
|
|
|
|
|
|
|
|
FILE *file1 = fopen(filename1, "rb");
|
|
|
|
FILE *file2 = fopen(filename2, "rb");
|
|
|
|
if (file1 == NULL || file2 == NULL)
|
2023-04-14 22:36:09 +02:00
|
|
|
{
|
2023-04-15 22:19:04 +02:00
|
|
|
return -1; // error opening files
|
2023-04-14 09:53:56 +02:00
|
|
|
}
|
2023-04-15 22:19:04 +02:00
|
|
|
#define BYTES_TO_READ_AT_ONCE 512000
|
|
|
|
unsigned int bytes;
|
|
|
|
#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)
|
2023-04-14 22:36:09 +02:00
|
|
|
{
|
2023-04-15 22:19:04 +02:00
|
|
|
fread(databuffer2, 1, BYTES_TO_READ_AT_ONCE, file2);
|
|
|
|
if(strcmp(databuffer1, databuffer2) != 0)
|
|
|
|
return 0;
|
2023-04-14 09:53:56 +02:00
|
|
|
}
|
2023-04-15 22:19:04 +02:00
|
|
|
fclose(file1);
|
|
|
|
fclose(file2);
|
|
|
|
return 1;
|
2023-04-13 23:48:16 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 22:36:09 +02:00
|
|
|
void print_help(void)
|
|
|
|
{
|
2023-04-14 22:51:43 +02:00
|
|
|
printf("usage: dircomp directory1 directory2 [-r] [-v] [-h]\n");
|
2023-04-14 22:36:09 +02:00
|
|
|
printf(" -r \t\t Recursive\n");
|
2023-04-13 23:48:16 +02:00
|
|
|
printf(" -v \t\t Verbose\n");
|
2023-04-14 22:51:43 +02:00
|
|
|
printf(" -h \t\t Print this help and quit\n");
|
2023-04-14 10:04:56 +02:00
|
|
|
}
|