Skip to content

Commit

Permalink
Merge pull request #20 from Zachareee/dev
Browse files Browse the repository at this point in the history
Fix file tally
  • Loading branch information
Zachareee authored Jan 9, 2024
2 parents 0740f67 + 1ff449f commit 90bb965
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
9 changes: 9 additions & 0 deletions dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions headers/dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
19 changes: 9 additions & 10 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -84,45 +83,45 @@ 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);
}

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, &not_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);
}

0 comments on commit 90bb965

Please sign in to comment.