From 4a9c38d03422ea318566ee33e0544d9aaa92fcd7 Mon Sep 17 00:00:00 2001 From: nova Date: Sun, 24 Aug 2025 19:45:08 +0200 Subject: [PATCH] handling of empty dirs --- defines.h | 3 +- dir.c | 11 ++++++- interactions.c | 3 ++ threading.c | 86 ++++++++++++++++++++++++++++---------------------- window.c | 2 ++ 5 files changed, 65 insertions(+), 40 deletions(-) diff --git a/defines.h b/defines.h index b6ad1b8..d9c2ee1 100644 --- a/defines.h +++ b/defines.h @@ -25,8 +25,7 @@ #define FILE_STATUS_HOVER 1 #define FILE_STATUS_SELECTED 2 #define FILE_STATUS_IS_REGULAR_FILE 4 -#define FILE_STATUS_DIR_EMPTY 64 /* if a directory is empty */ -#define FILE_STATUS_FILE_OPEN 128 /* only used for file previews */ +#define FILE_STATUS_FILE_OPEN 64 /* only used for file previews */ #define COLOR_UNKNOWN 0 #define COLOR_DIR 1 diff --git a/dir.c b/dir.c index 63ba4db..56b2235 100644 --- a/dir.c +++ b/dir.c @@ -314,6 +314,9 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file } void update_selected_file(){ + if (mid_content->file_name[0] == '\0') { /* only happens if the current path is either empty or inaccessible */ + return; + } if (selected_file_current >= mid_file_count) { selected_file_current = mid_file_count-1; } @@ -324,11 +327,17 @@ void update_selected_file(){ selected_file_last = selected_file_current; } void dir_set_selected_file_current(unsigned long selected_file_current){ - current_dir->selected_file_current = selected_file_current; + if (mid_content->file_name[0] != '\0') { + current_dir->selected_file_current = selected_file_current; + } } unsigned long dir_get_selected_file_current(){ + current_dir = visited_dirs; + if (mid_content->file_name[0] == '\0') { + return 0; + } char hit = 0; char *path = getcwd(NULL, 0); while(current_dir->next != NULL) { diff --git a/interactions.c b/interactions.c index db03413..18fc5ad 100644 --- a/interactions.c +++ b/interactions.c @@ -237,6 +237,9 @@ void move_left(int passes){ status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY); } void move_right(){ + if (mid_content->file_name[0] == '\0') { + return; + } if (mid_content[selected_file_current].file_type == FILE_TYPE_DIR || mid_content[selected_file_current].file_type == FILE_TYPE_SYMLINK) { if (chdir(mid_content[selected_file_current].file_name) != 0) { FAIL("move_right", "unhandled error of chdir"); diff --git a/threading.c b/threading.c index 7e8567b..761dcc4 100644 --- a/threading.c +++ b/threading.c @@ -68,35 +68,28 @@ void *thread_mid(void *data){ } free(mid_content); mid_file_count = get_dir_size(path); - if (mid_file_count > 0) { + if (mid_file_count != 0) { mid_content = malloc(mid_file_count * sizeof(file)); memset(mid_content, '\0', mid_file_count * sizeof(file)); get_dir_content(path, &mid_file_count, mid_content); } else { selected_file_current = 0; mid_content = malloc(sizeof(file)); - mid_content->status = FILE_STATUS_DIR_EMPTY; mid_content->file_type = 0; mid_content->file_size = 0; mid_content->permissions = 0; mid_content->color_pair = 0; - mid_content->file_name = "empty dir"; + mid_content->file_name = malloc(sizeof(char)); + mid_content->file_name[0] = '\0'; mid_file_count = 0; - pthread_cond_signal(&cond_rgt); - pthread_cond_signal(&cond_lft); - - free(path); - pthread_mutex_unlock(&mutex_mid); - pthread_exit(0); } pthread_mutex_lock(&mutex_selection); update_selected_file(); pthread_mutex_unlock(&mutex_selection); } - pthread_cond_signal(&cond_rgt); pthread_cond_signal(&cond_lft); @@ -150,35 +143,58 @@ void *thread_rgt(void *data){ pthread_mutex_lock(&mutex_mid); - char *path = malloc(strlen(mid_content[selected_file_current].file_name) + 1); - strcpy(path, mid_content[selected_file_current].file_name); + char *path; + if (mid_file_count != 0) { + path = malloc(strlen(mid_content[selected_file_current].file_name) + 1); + strcpy(path, mid_content[selected_file_current].file_name); + } else { + path = malloc(sizeof(char)); + path[0] = '\0'; + } file_current.file_type = mid_content[selected_file_current].file_type; file_current.file_size = mid_content[selected_file_current].file_size; file_current.status = mid_content[selected_file_current].status; pthread_mutex_unlock(&mutex_mid); - if (file_current.file_type == FILE_TYPE_DIR || file_current.file_type == FILE_TYPE_SYMLINK) { - images_clear(); + if (mid_content[selected_file_current].permissions & S_IRUSR) { + if (file_current.file_type == FILE_TYPE_DIR || file_current.file_type == FILE_TYPE_SYMLINK) { + images_clear(); - unsigned long i = 0; - for (i = 0; i < rgt_file_count; i++) { - if (rgt_content[i].file_name) { - free(rgt_content[i].file_name); + unsigned long i = 0; + for (i = 0; i < rgt_file_count; i++) { + if (rgt_content[i].file_name) { + free(rgt_content[i].file_name); + } } + free(rgt_content); + + rgt_file_count = get_dir_size(path); + rgt_content = malloc(rgt_file_count * sizeof(file)); + memset(rgt_content, '\0', rgt_file_count * sizeof(file)); + get_dir_content(path, &rgt_file_count, rgt_content); + rgt_content[0].status &= ~FILE_STATUS_FILE_OPEN; + + free(rgt_buffer); + rgt_buffer = malloc(sizeof(char)); + rgt_buffer[0] = '\0'; + } else { + + unsigned long i = 0; + for (i = 0; i < rgt_file_count; i++) { + if (rgt_content[i].file_name) { + free(rgt_content[i].file_name); + } + } + free(rgt_content); + rgt_file_count = 0; + rgt_content = malloc(sizeof(file)); + + free(rgt_buffer); + rgt_content->file_type = FILE_TYPE_OPEN_FILE; + rgt_content->status = FILE_STATUS_HOVER; + rgt_buffer = preview_file(path, file_current.file_size); } - free(rgt_content); - - rgt_file_count = get_dir_size(path); - rgt_content = malloc(rgt_file_count * sizeof(file)); - memset(rgt_content, '\0', rgt_file_count * sizeof(file)); - get_dir_content(path, &rgt_file_count, rgt_content); - rgt_content[0].status &= ~FILE_STATUS_FILE_OPEN; - - free(rgt_buffer); - rgt_buffer = malloc(sizeof(char)); - rgt_buffer[0] = '\0'; } else { - unsigned long i = 0; for (i = 0; i < rgt_file_count; i++) { if (rgt_content[i].file_name) { @@ -190,14 +206,10 @@ void *thread_rgt(void *data){ rgt_content = malloc(sizeof(file)); free(rgt_buffer); - if (file_current.status & FILE_STATUS_DIR_EMPTY) { - rgt_buffer = "empty dir"; - } else { - rgt_content->file_type = FILE_TYPE_OPEN_FILE; - rgt_content->status = FILE_STATUS_HOVER; - rgt_buffer = preview_file(path, file_current.file_size); - } + rgt_buffer = malloc(sizeof(char)); + rgt_buffer[0] = '\0'; } + free(path); pthread_mutex_unlock(&mutex_rgt); diff --git a/window.c b/window.c index be08d46..2cd26ec 100644 --- a/window.c +++ b/window.c @@ -97,6 +97,8 @@ void window_rgt(WINDOW *win){ if (rgt_file_count == 0) { if (rgt_content[0].file_type == FILE_TYPE_OPEN_FILE) { mvwaddnstr(win, 0, 0, rgt_buffer, (terminal_width/2) * terminal_width); + } else if (rgt_content->permissions & S_IRUSR) { + mvwaddstr(win, 0, 0, "not accessible"); } else { mvwaddstr(win, 0, 0, "empty"); }