From 5be6d618bd83e052a972ac747fec809ff1f74aa6 Mon Sep 17 00:00:00 2001
From: Brandon Rodriguez <brodriguez8774@gmail.com>
Date: Sat, 18 Nov 2017 02:57:50 -0500
Subject: [PATCH] Add basic user status display and change file read-in to
 ignore sorted.yay

---
 Main.c | 46 ++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 38 insertions(+), 8 deletions(-)

diff --git a/Main.c b/Main.c
index e2a9671..3a016ad 100644
--- a/Main.c
+++ b/Main.c
@@ -134,6 +134,9 @@ int main(int argc, char* argv[]) {
             // Take thread data and merge + write to file.
             merge_to_file(thread_arrays);
 
+            printf("\nAll operations complete.\n");
+            printf("Sorted data can found at %s/sorted.yay\n", absolute_path);
+
             // Merging done. Free memory.
             for (index = 0; index < dir_file_counter; index++) {
                 for (struct_number = 0; struct_number < thread_arrays[index]->array_count; struct_number++) {
@@ -202,9 +205,19 @@ void open_folder() {
     int index;
     int return_int;
     char* temp_string;
+    char* save_file_path;
     struct dirent* dir_struct;
     struct stat stat_buffer;
     DIR* dir_pointer;
+    pthread_attr_t attr;
+    size_t stacksize;
+
+    printf("Locating files to merge...\n");
+
+    // Save path of file to save to.
+    save_file_path = copy_string_with_buffer(absolute_path, BUFFER_SIZE);
+    strcat(save_file_path, "/");
+    strcat(save_file_path, "sorted.yay");
 
     // Iterate through directory first time, to count number of files to open/threads to make.
     dir_file_counter = 0;
@@ -223,7 +236,20 @@ void open_folder() {
                     err_sys("Failed to stat file with err %d", return_int);
                 }
                 if (S_ISREG(stat_buffer.st_mode)) {
-                    dir_file_counter++;
+
+                    // Get absolute file path.
+                    temp_string = copy_string_with_buffer(absolute_path, BUFFER_SIZE);
+                    strcat(temp_string, "/");
+                    strcat(temp_string, dir_struct->d_name);
+
+                    // Ensure that file is not save file.
+                    if (strcmp(temp_string, save_file_path) != 0) {
+                        printf("Path: %s\n", temp_string);
+                        dir_file_counter++;
+                    }
+
+                    free(temp_string);
+
                 }
             } else {
                 // End of files in directory. Closing stream.
@@ -241,9 +267,6 @@ void open_folder() {
     // Prepare to set up threads.
     thread_array = calloc(dir_file_counter, sizeof(pthread_t));
     index = 0;
-    pthread_attr_t attr;
-    size_t stacksize;
-
     pthread_attr_init(&attr);
     pthread_attr_getstacksize(&attr, &stacksize);
     if (stacksize < 8388608) {
@@ -251,6 +274,7 @@ void open_folder() {
     }
     pthread_attr_setstacksize(&attr, (stacksize * 2));
 
+    printf("Reading in files and creating sorting threads...\n");
 
     // Iterate through directory again. This time, actually create threads and hand off files.
     dir_pointer = opendir(absolute_path);
@@ -273,11 +297,13 @@ void open_folder() {
                     temp_string = copy_string_with_buffer(absolute_path, BUFFER_SIZE);
                     strcat(temp_string, "/");
                     strcat(temp_string, dir_struct->d_name);
-                    // printf("Path: %s\n", temp_string);
 
                     // Actually create threads.
-                    pthread_create(&thread_array[index], &attr, thread_read_file, (void*) copy_string(temp_string));
-                    index++;
+                    if (strcmp(temp_string, save_file_path) != 0) {
+                        // printf("Path: %s\n", temp_string);
+                        pthread_create(&thread_array[index], &attr, thread_read_file, (void*) copy_string(temp_string));
+                        index++;
+                    }
                     free(temp_string);
                 }
             } else {
@@ -292,7 +318,7 @@ void open_folder() {
     } else {
         err_msg("Directory does not have read access. Cannot view files.");
     }
-
+    free(save_file_path);
 }
 
 
@@ -464,6 +490,8 @@ void merge_to_file(thread_return_struct** thread_arrays) {
     FILE* reset_file;
     thread_return_struct* temp_dataset;
 
+    printf("Merging array data together...\n");
+
     // Get file path of file to save.
     temp_string = copy_string_with_buffer(absolute_path, BUFFER_SIZE);
     strcat(temp_string, "/");
@@ -489,6 +517,8 @@ void merge_to_file(thread_return_struct** thread_arrays) {
         free(temp_dataset);
     }
 
+    printf("Saving sorted data to file...\n");
+
     // For each record stored in the full_dataset struct, write to file.
     for (index = 0; index < full_dataset->array_count; index++) {
         write_to_file(full_dataset->data_array[index]);
-- 
GitLab