From 1239d44f29cbaaf3b56983fee8c0f0fa6fc386e2 Mon Sep 17 00:00:00 2001 From: Zachareee Date: Mon, 8 Jan 2024 10:14:04 +0800 Subject: [PATCH 1/4] Starting work on #19 --- dir.c | 5 +++-- files.c | 14 ++++++++++++++ headers/files.h | 1 + main.c | 3 ++- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/dir.c b/dir.c index b8f4e34..2105844 100644 --- a/dir.c +++ b/dir.c @@ -74,10 +74,11 @@ void free_dir(dir_t *dir) { } // write the whole dir struct to checksum file -void write_dir_to_file(dir_t *dir, int level, FILE *f) { +void write_dir_to_file(dir_t *dir, int level, FILE *f, int *file_count) { for (int i = 0; i < level; i++) write_to_file(f, "%s", "|--"); if (level) write_to_file(f, "%s\n", dir->name); for (int i = 0; i < dir->num; i++) { - write_dir_to_file(dir->folder[i], level + 1, f); + write_dir_to_file(dir->folder[i], level + 1, f, file_count); } + (*file_count)++; } diff --git a/files.c b/files.c index 1fc66bf..5ae8362 100644 --- a/files.c +++ b/files.c @@ -49,3 +49,17 @@ void write_to_file(FILE *f, char *format, char *line) { } fflush(f); } + +void write_dir_and_filecount(FILE *f, dir_t *dir, char *string) { + if (!dir) { + write_to_file(string); + return; + } + + int count = 0; + long pos = ftell(f); + write_dir_to_file(dir, 0, f, &count); + fseek(f, pos, SEEK_SET); + write_to_file(f, string, count); + fseek(f, 0, SEEK_END); +} \ No newline at end of file diff --git a/headers/files.h b/headers/files.h index afe7afe..b5851f7 100644 --- a/headers/files.h +++ b/headers/files.h @@ -6,3 +6,4 @@ char *get_line(FILE *f); int check_exists(char *path, int file); void write_to_file(FILE *f, char *format, char *line); void free_files(); +void write_dir_and_filecount(FILE *f, dir_t *dir, char *string); \ No newline at end of file diff --git a/main.c b/main.c index 5c1d82a..aaf5d19 100644 --- a/main.c +++ b/main.c @@ -111,7 +111,8 @@ int main(int argc, char **argv) { write_to_file(checkfile, "\n%s files OK\n", length); snprintf(length, 10, "%d", arr[3]); - write_to_file(checkfile, "\n%s files were not found in the hashfile:\n", length); + write_dir_and_filecount(checkfile, extra, "\n%s files were not found in the hashfile:\n"); + write_to_file(checkfile,, length); write_dir_to_file(extra, 0, checkfile); snprintf(length, 10, "%d", arr[2]); From 39b632e07132b6d2bd66495a2f8448890bcc3ba1 Mon Sep 17 00:00:00 2001 From: Zachareee Date: Mon, 8 Jan 2024 13:02:13 +0800 Subject: [PATCH 2/4] Fixed compile errors/warnings, testing phase to come later --- files.c | 14 ++++++-------- getdent.c | 4 ++-- headers/dir.h | 2 +- headers/files.h | 4 ++-- main.c | 26 +++++++++----------------- 5 files changed, 20 insertions(+), 30 deletions(-) diff --git a/files.c b/files.c index 5ae8362..3bfbc33 100644 --- a/files.c +++ b/files.c @@ -50,16 +50,14 @@ void write_to_file(FILE *f, char *format, char *line) { fflush(f); } -void write_dir_and_filecount(FILE *f, dir_t *dir, char *string) { - if (!dir) { - write_to_file(string); - return; - } - - int count = 0; +unsigned long write_dir_and_filecount(FILE *f, dir_t *dir, char *string) { + unsigned long count = 0; long pos = ftell(f); write_dir_to_file(dir, 0, f, &count); fseek(f, pos, SEEK_SET); - write_to_file(f, string, count); + char length[11] = {0}; + snprintf(length, 10, "%lu", count); + write_to_file(f, string, length); fseek(f, 0, SEEK_END); + return count; } \ No newline at end of file diff --git a/getdent.c b/getdent.c index fb58d77..d4f3e5f 100644 --- a/getdent.c +++ b/getdent.c @@ -109,13 +109,13 @@ void getdent(char *name) { dir_t *pass, *fail, *extras; char *dir, *src, *dst; -unsigned int *count; +unsigned long *count; define_func // iterate through directories, path is absolute path void file_iterator(char *dirl, char *srcl, char *dstl, - dir_t *passl, dir_t *faill, dir_t *extral, unsigned int *countl) { + dir_t *passl, dir_t *faill, dir_t *extral, unsigned long *countl) { pass = passl; fail = faill; extras = extral; diff --git a/headers/dir.h b/headers/dir.h index d96d5c3..ec53842 100644 --- a/headers/dir.h +++ b/headers/dir.h @@ -2,5 +2,5 @@ void add_path_to_dir(char *path, dir_t *dir); void free_dir(dir_t *dir); -void write_dir_to_file(dir_t *dir, int level, FILE *f); +void write_dir_to_file(dir_t *dir, int level, FILE *f, unsigned long *count); int path_exists(dir_t *dir, const char *path); diff --git a/headers/files.h b/headers/files.h index b5851f7..abdacc4 100644 --- a/headers/files.h +++ b/headers/files.h @@ -1,9 +1,9 @@ #include "datatypes.h" void file_iterator(char *dir, char *src, char *dst, - dir_t *pass, dir_t *fail, dir_t *extra, unsigned int *count); + dir_t *pass, dir_t *fail, dir_t *extra, unsigned long *count); char *get_line(FILE *f); int check_exists(char *path, int file); void write_to_file(FILE *f, char *format, char *line); void free_files(); -void write_dir_and_filecount(FILE *f, dir_t *dir, char *string); \ No newline at end of file +unsigned long write_dir_and_filecount(FILE *f, dir_t *dir, char *string); \ No newline at end of file diff --git a/main.c b/main.c index aaf5d19..a688545 100644 --- a/main.c +++ b/main.c @@ -64,7 +64,7 @@ int main(int argc, char **argv) { char ptr[PATH_MAX]; char *state; // 0: OK, 1: MISSING, 2: FAILED, 3: EXTRA - unsigned int arr[4] = {0}; + unsigned long count = 0, extra_files = 0; passed = calloc(1, sizeof(dir_t)); missing = calloc(1, sizeof(dir_t)); @@ -84,17 +84,15 @@ int main(int argc, char **argv) { case 0: add_path_to_dir(ptr, passed); state = "OK"; - arr[0]++; + count++; break; case -2: add_path_to_dir(ptr, missing); state = "MISSING"; - arr[1]++; break; default: add_path_to_dir(ptr, failed); state = "FAILED"; - arr[2]++; } write_to_file(checkfile, "%s\n", state); @@ -102,28 +100,22 @@ int main(int argc, char **argv) { fclose(hashfile); - file_iterator(dir, src, dst, passed, failed, extra, arr + 3); + file_iterator(dir, src, dst, passed, failed, extra, &extra_files); // creates a char array which can hold the number of files as text char length[11] = {0}; - - snprintf(length, 10, "%d", arr[0]); + snprintf(length, 10, "%lu", count); write_to_file(checkfile, "\n%s files OK\n", length); - snprintf(length, 10, "%d", arr[3]); - write_dir_and_filecount(checkfile, extra, "\n%s files were not found in the hashfile:\n"); - write_to_file(checkfile,, length); - write_dir_to_file(extra, 0, checkfile); + unsigned long not_found = write_dir_and_filecount(checkfile, extra, "\n%s files were not found in the hashfile:\n"); - snprintf(length, 10, "%d", arr[2]); - write_to_file(checkfile, "\n%s files failed hashsum checks:\n", length); - write_dir_to_file(failed, 0, checkfile); + unsigned long failed_check = write_dir_and_filecount(checkfile, failed, "\n%s files failed hashsum checks:\n"); - snprintf(length, 10, "%d", arr[1]); + snprintf(length, 10, "%lu", not_found); write_to_file(checkfile, "\n%s files could not be found:\n", length); - write_dir_to_file(missing, 0, checkfile); + write_dir_to_file(missing, 0, checkfile, ¬_found); fclose(checkfile); - printf("\n%d files failed hashsum checks, %d files could not be found\n", arr[2], arr[1]); + printf("\n%lu files failed hashsum checks, %lu files could not be found\n", failed_check, not_found); } From 91a6f7f9e86d617cb40fa76557fa8b244daa0067 Mon Sep 17 00:00:00 2001 From: Zachareee Date: Tue, 9 Jan 2024 20:30:47 +0800 Subject: [PATCH 3/4] Revert "Starting work on #19" This reverts commit 1239d44f29cbaaf3b56983fee8c0f0fa6fc386e2. --- dir.c | 5 ++--- files.c | 12 ------------ getdent.c | 4 ++-- headers/dir.h | 2 +- headers/files.h | 3 +-- main.c | 25 ++++++++++++++++--------- 6 files changed, 22 insertions(+), 29 deletions(-) diff --git a/dir.c b/dir.c index 2105844..b8f4e34 100644 --- a/dir.c +++ b/dir.c @@ -74,11 +74,10 @@ void free_dir(dir_t *dir) { } // write the whole dir struct to checksum file -void write_dir_to_file(dir_t *dir, int level, FILE *f, int *file_count) { +void write_dir_to_file(dir_t *dir, int level, FILE *f) { for (int i = 0; i < level; i++) write_to_file(f, "%s", "|--"); if (level) write_to_file(f, "%s\n", dir->name); for (int i = 0; i < dir->num; i++) { - write_dir_to_file(dir->folder[i], level + 1, f, file_count); + write_dir_to_file(dir->folder[i], level + 1, f); } - (*file_count)++; } diff --git a/files.c b/files.c index 3bfbc33..1fc66bf 100644 --- a/files.c +++ b/files.c @@ -49,15 +49,3 @@ void write_to_file(FILE *f, char *format, char *line) { } fflush(f); } - -unsigned long write_dir_and_filecount(FILE *f, dir_t *dir, char *string) { - unsigned long count = 0; - long pos = ftell(f); - write_dir_to_file(dir, 0, f, &count); - fseek(f, pos, SEEK_SET); - char length[11] = {0}; - snprintf(length, 10, "%lu", count); - write_to_file(f, string, length); - fseek(f, 0, SEEK_END); - return count; -} \ No newline at end of file diff --git a/getdent.c b/getdent.c index d4f3e5f..fb58d77 100644 --- a/getdent.c +++ b/getdent.c @@ -109,13 +109,13 @@ void getdent(char *name) { dir_t *pass, *fail, *extras; char *dir, *src, *dst; -unsigned long *count; +unsigned int *count; define_func // iterate through directories, path is absolute path void file_iterator(char *dirl, char *srcl, char *dstl, - dir_t *passl, dir_t *faill, dir_t *extral, unsigned long *countl) { + dir_t *passl, dir_t *faill, dir_t *extral, unsigned int *countl) { pass = passl; fail = faill; extras = extral; diff --git a/headers/dir.h b/headers/dir.h index ec53842..d96d5c3 100644 --- a/headers/dir.h +++ b/headers/dir.h @@ -2,5 +2,5 @@ void add_path_to_dir(char *path, dir_t *dir); void free_dir(dir_t *dir); -void write_dir_to_file(dir_t *dir, int level, FILE *f, unsigned long *count); +void write_dir_to_file(dir_t *dir, int level, FILE *f); int path_exists(dir_t *dir, const char *path); diff --git a/headers/files.h b/headers/files.h index abdacc4..afe7afe 100644 --- a/headers/files.h +++ b/headers/files.h @@ -1,9 +1,8 @@ #include "datatypes.h" void file_iterator(char *dir, char *src, char *dst, - dir_t *pass, dir_t *fail, dir_t *extra, unsigned long *count); + dir_t *pass, dir_t *fail, dir_t *extra, unsigned int *count); char *get_line(FILE *f); int check_exists(char *path, int file); void write_to_file(FILE *f, char *format, char *line); void free_files(); -unsigned long write_dir_and_filecount(FILE *f, dir_t *dir, char *string); \ No newline at end of file diff --git a/main.c b/main.c index a688545..5c1d82a 100644 --- a/main.c +++ b/main.c @@ -64,7 +64,7 @@ int main(int argc, char **argv) { char ptr[PATH_MAX]; char *state; // 0: OK, 1: MISSING, 2: FAILED, 3: EXTRA - unsigned long count = 0, extra_files = 0; + unsigned int arr[4] = {0}; passed = calloc(1, sizeof(dir_t)); missing = calloc(1, sizeof(dir_t)); @@ -84,15 +84,17 @@ int main(int argc, char **argv) { case 0: add_path_to_dir(ptr, passed); state = "OK"; - count++; + arr[0]++; break; case -2: add_path_to_dir(ptr, missing); state = "MISSING"; + arr[1]++; break; default: add_path_to_dir(ptr, failed); state = "FAILED"; + arr[2]++; } write_to_file(checkfile, "%s\n", state); @@ -100,22 +102,27 @@ int main(int argc, char **argv) { fclose(hashfile); - file_iterator(dir, src, dst, passed, failed, extra, &extra_files); + file_iterator(dir, src, dst, passed, failed, extra, arr + 3); // creates a char array which can hold the number of files as text char length[11] = {0}; - snprintf(length, 10, "%lu", count); + + snprintf(length, 10, "%d", arr[0]); write_to_file(checkfile, "\n%s files OK\n", length); - unsigned long not_found = write_dir_and_filecount(checkfile, extra, "\n%s files were not found in the hashfile:\n"); + snprintf(length, 10, "%d", arr[3]); + write_to_file(checkfile, "\n%s files were not found in the hashfile:\n", length); + write_dir_to_file(extra, 0, checkfile); - unsigned long failed_check = write_dir_and_filecount(checkfile, failed, "\n%s files failed hashsum checks:\n"); + snprintf(length, 10, "%d", arr[2]); + write_to_file(checkfile, "\n%s files failed hashsum checks:\n", length); + write_dir_to_file(failed, 0, checkfile); - snprintf(length, 10, "%lu", not_found); + snprintf(length, 10, "%d", arr[1]); write_to_file(checkfile, "\n%s files could not be found:\n", length); - write_dir_to_file(missing, 0, checkfile, ¬_found); + write_dir_to_file(missing, 0, checkfile); fclose(checkfile); - printf("\n%lu files failed hashsum checks, %lu files could not be found\n", failed_check, not_found); + printf("\n%d files failed hashsum checks, %d files could not be found\n", arr[2], arr[1]); } From 1ff449f93f98bebc73f46d174ebfe91876fafe28 Mon Sep 17 00:00:00 2001 From: Zachareee Date: Wed, 10 Jan 2024 00:30:20 +0800 Subject: [PATCH 4/4] Added count files function to prevent duplicate files adding to count --- dir.c | 9 +++++++++ headers/dir.h | 1 + main.c | 19 +++++++++---------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/dir.c b/dir.c index b8f4e34..96b2a80 100644 --- a/dir.c +++ b/dir.c @@ -81,3 +81,12 @@ void write_dir_to_file(dir_t *dir, int level, FILE *f) { write_dir_to_file(dir->folder[i], level + 1, f); } } + +int count_files_in_dir(dir_t *dir) { + int count = 0; + for (int i = 0; i < dir->num; i++) { + count += count_files_in_dir(dir->folder[i]); + } + + return count + !dir->num; +} \ No newline at end of file diff --git a/headers/dir.h b/headers/dir.h index d96d5c3..17f3ea7 100644 --- a/headers/dir.h +++ b/headers/dir.h @@ -4,3 +4,4 @@ void add_path_to_dir(char *path, dir_t *dir); void free_dir(dir_t *dir); void write_dir_to_file(dir_t *dir, int level, FILE *f); int path_exists(dir_t *dir, const char *path); +int count_files_in_dir(dir_t *dir); \ No newline at end of file diff --git a/main.c b/main.c index 5c1d82a..f035af8 100644 --- a/main.c +++ b/main.c @@ -64,7 +64,6 @@ int main(int argc, char **argv) { char ptr[PATH_MAX]; char *state; // 0: OK, 1: MISSING, 2: FAILED, 3: EXTRA - unsigned int arr[4] = {0}; passed = calloc(1, sizeof(dir_t)); missing = calloc(1, sizeof(dir_t)); @@ -84,17 +83,14 @@ int main(int argc, char **argv) { case 0: add_path_to_dir(ptr, passed); state = "OK"; - arr[0]++; break; case -2: add_path_to_dir(ptr, missing); state = "MISSING"; - arr[1]++; break; default: add_path_to_dir(ptr, failed); state = "FAILED"; - arr[2]++; } write_to_file(checkfile, "%s\n", state); @@ -102,27 +98,30 @@ int main(int argc, char **argv) { fclose(hashfile); - file_iterator(dir, src, dst, passed, failed, extra, arr + 3); + unsigned int not_found = 0; + file_iterator(dir, src, dst, passed, failed, extra, ¬_found); // creates a char array which can hold the number of files as text char length[11] = {0}; - snprintf(length, 10, "%d", arr[0]); + snprintf(length, 10, "%d", count_files_in_dir(passed)); write_to_file(checkfile, "\n%s files OK\n", length); - snprintf(length, 10, "%d", arr[3]); + snprintf(length, 10, "%d", count_files_in_dir(extra)); write_to_file(checkfile, "\n%s files were not found in the hashfile:\n", length); write_dir_to_file(extra, 0, checkfile); - snprintf(length, 10, "%d", arr[2]); + int failed_count = count_files_in_dir(failed); + snprintf(length, 10, "%d", failed_count); write_to_file(checkfile, "\n%s files failed hashsum checks:\n", length); write_dir_to_file(failed, 0, checkfile); - snprintf(length, 10, "%d", arr[1]); + int missing_count = count_files_in_dir(missing); + snprintf(length, 10, "%d", missing_count); write_to_file(checkfile, "\n%s files could not be found:\n", length); write_dir_to_file(missing, 0, checkfile); fclose(checkfile); - printf("\n%d files failed hashsum checks, %d files could not be found\n", arr[2], arr[1]); + printf("\n%d files failed hashsum checks, %d files could not be found\n", failed_count, missing_count); }