From a29be4a40574045d63f05cea474ade4efc12a76d Mon Sep 17 00:00:00 2001 From: Federico Di Pierro Date: Thu, 9 Jul 2015 10:09:17 +0200 Subject: [PATCH] semplified some code, updated some doc --- TODO | 2 +- src/fm_functions.c | 10 +--------- src/helper_functions.c | 12 +++++++----- src/main.c | 13 +++++-------- src/ui_functions.c | 2 +- 5 files changed, 15 insertions(+), 24 deletions(-) diff --git a/TODO b/TODO index b2775fb..007ddfc 100644 --- a/TODO +++ b/TODO @@ -1,4 +1,4 @@ /* TODO: * fm_functions doc - * Update doc + * semplify code */ diff --git a/src/fm_functions.c b/src/fm_functions.c index c132045..035c0d0 100644 --- a/src/fm_functions.c +++ b/src/fm_functions.c @@ -316,8 +316,6 @@ static int recursive_copy(const char *path, const struct stat *sb, int typeflag, } close(fd_to); close(fd_from); - flock(fd_to, LOCK_UN); - flock(fd_from, LOCK_UN); } } return 0; @@ -362,13 +360,7 @@ void rename_file_folders(void) static int recursive_remove(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) { - int res, fd; - - fd = open(path, O_RDONLY); - flock(fd, LOCK_EX); - res = remove(path); - close(fd); - return res; + return remove(path); } static int rmrf(const char *path) diff --git a/src/helper_functions.c b/src/helper_functions.c index e5971f1..27a5b0f 100644 --- a/src/helper_functions.c +++ b/src/helper_functions.c @@ -124,8 +124,7 @@ static thread_job_list *add_thread(thread_job_list *h, int type, const char *pat } /* - * running_h is a ptr to the currently running job. - * This function will free its memory and set it to NULL. + * This function will move thread_job_list head to head->next, and will free old head memory and set it to NULL. */ void free_running_h(void) { @@ -200,6 +199,9 @@ static void init_thread_helper(const char *temp, const char *str) } } +/* + * Helper function that will copy file_list *selected to current_th->selected_files, then will free selected. + */ static void copy_selected_files(void) { file_list *tmp = selected; @@ -213,8 +215,8 @@ static void copy_selected_files(void) } /* - * - first check if we come from a recursive call (if we're in a running th), then frees previously finished job and print its completion mesg. - * - if thread_h is not NULL, and thread_f is already assigned, the th has another job waiting for it, so we run it, else we finished our work: num_of_jobs = 0. + * First check if we come from a recursive call, then frees previously finished job and print its completion mesg. + * If thread_h is not NULL, the th has another job waiting for it, so we run it, else we finished our work: num_of_jobs = 0. */ static void *execute_thread(void *x) { @@ -222,7 +224,7 @@ static void *execute_thread(void *x) free_running_h(); print_info(thread_m.str, thread_m.line); } - if (thread_h && thread_h->f) { + if (thread_h) { thread_h->f(); return execute_thread(thread_h); } else { diff --git a/src/main.c b/src/main.c index 33940c1..beda5e0 100644 --- a/src/main.c +++ b/src/main.c @@ -166,6 +166,9 @@ static void main_loop(void) while (!quit) { c = wgetch(ps[active].fm); + pthread_mutex_lock(&lock); + stat(ps[active].nl[ps[active].curr_pos], ¤t_file_stat); + pthread_mutex_unlock(&lock); if ((c >= 'A') && (c <= 'Z')) { c = tolower(c); } @@ -180,9 +183,6 @@ static void main_loop(void) switch_hidden(); break; case 10: // enter to change dir or open a file. - pthread_mutex_lock(&lock); - stat(ps[active].nl[ps[active].curr_pos], ¤t_file_stat); - pthread_mutex_unlock(&lock); if (S_ISDIR(current_file_stat.st_mode) || S_ISLNK(current_file_stat.st_mode)) { change_dir(ps[active].nl[ps[active].curr_pos]); } else { @@ -205,7 +205,7 @@ static void main_loop(void) init_thread(NEW_FILE_TH, new_file, ps[active].my_cwd); break; case 'r': //remove file - if (strcmp(strrchr(ps[active].nl[ps[active].curr_pos], '/') + 1, "..") != 0) { + if (strcmp(strrchr(ps[active].nl[ps[active].curr_pos], '/') + 1, "..") != 0) { // check this is not ".." dir ask_user(sure, &x, 1, 'n'); if (x == 'y') { init_thread(RM_TH, remove_file, ps[active].nl[ps[active].curr_pos]); @@ -222,7 +222,7 @@ static void main_loop(void) init_thread(PASTE_TH, paste_file, ps[active].my_cwd); } break; - case 'l': + case 'l': // show helper mess trigger_show_helper_message(); break; case 's': // show stat about files (size and perms) @@ -250,9 +250,6 @@ static void main_loop(void) break; #ifdef LIBCUPS_PRESENT case 'p': // p to print - pthread_mutex_lock(&lock); - stat(ps[active].nl[ps[active].curr_pos], ¤t_file_stat); - pthread_mutex_unlock(&lock); if (S_ISREG(current_file_stat.st_mode) && !get_mimetype(ps[active].nl[ps[active].curr_pos], "x-executable")) { print_support(ps[active].nl[ps[active].curr_pos]); } diff --git a/src/ui_functions.c b/src/ui_functions.c index 26c5d4a..c6d3dd5 100644 --- a/src/ui_functions.c +++ b/src/ui_functions.c @@ -435,7 +435,7 @@ void print_info(const char *str, int i) wclrtoeol(info_win); } k = 0; - if (thread_h && thread_h->type) { + if (thread_h) { sprintf(st, "[%d/%d] %s", thread_h->num, num_of_jobs, thread_job_mesg[thread_h->type - 1]); k = strlen(st) + 1; mvwprintw(info_win, INFO_LINE, COLS - strlen(st), st);