mirror of
https://gittea.dev/nova/th.git
synced 2025-10-21 10:20:15 -04:00
fixed some race conditions and improved parsing storage size
This commit is contained in:
2
config.h
2
config.h
@@ -105,7 +105,7 @@ static const char size_unit[] = { 'B', 'K', 'M', 'G', 'T', 'P' }; /* this define
|
||||
|
||||
static const char clipboard_cmd[] = "xsel -ib --trim"; /* assembles the following shell cmd: echo "hovered_file" | clipboard_cmd */
|
||||
static const char ui_btm_text_storage_left[] = "total free";
|
||||
static const char ui_btm_current_dir_size[] = "sum of dir";
|
||||
static const char ui_btm_current_dir_size[] = "sum of dir,";
|
||||
|
||||
/* {{{ */
|
||||
static const unsigned long binding_count = sizeof(key_binding) / sizeof(binding);
|
||||
|
2
main.c
2
main.c
@@ -112,6 +112,7 @@ int main(){
|
||||
threading_free();
|
||||
free(start_path);
|
||||
|
||||
/*
|
||||
if (threading) {
|
||||
pthread_join(thread_l, NULL);
|
||||
pthread_join(thread_r, NULL);
|
||||
@@ -119,6 +120,7 @@ int main(){
|
||||
pthread_join(thread_t, NULL);
|
||||
pthread_join(thread_b, NULL);
|
||||
}
|
||||
*/
|
||||
|
||||
delwin(win_l);
|
||||
delwin(win_m);
|
||||
|
133
threading.c
133
threading.c
@@ -54,6 +54,7 @@ void *thread_mid(){
|
||||
while(!(status & STATUS_QUIT_PROGRAM)){
|
||||
pthread_mutex_lock(&mutex_mid);
|
||||
pthread_cond_wait(&cond_mid, &mutex_mid);
|
||||
unsigned int local_status = status;
|
||||
|
||||
char *path;
|
||||
if((path=getcwd(NULL, 0)) == NULL) {
|
||||
@@ -63,7 +64,7 @@ void *thread_mid(){
|
||||
} else {
|
||||
|
||||
|
||||
if (status & STATUS_RELOAD_DIRECTORY) {
|
||||
if (local_status & STATUS_RELOAD_DIRECTORY) {
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i < mid_file_count; i++) {
|
||||
free(mid_content[i].file_name);
|
||||
@@ -106,6 +107,7 @@ void *thread_lft(){
|
||||
while(!(status & STATUS_QUIT_PROGRAM)){
|
||||
pthread_mutex_lock(&mutex_lft);
|
||||
pthread_cond_wait(&cond_lft, &mutex_lft);
|
||||
unsigned int local_status = status;
|
||||
|
||||
char *path;
|
||||
if((path=getcwd(NULL, 0)) == NULL) {
|
||||
@@ -117,7 +119,7 @@ void *thread_lft(){
|
||||
path[strrchr(path, '/')-path] = '\0';
|
||||
path[0] = '/';
|
||||
|
||||
if (status & STATUS_RELOAD_DIRECTORY) {
|
||||
if (local_status & STATUS_RELOAD_DIRECTORY) {
|
||||
lft_file_count = get_dir_size(path);
|
||||
free(lft_content);
|
||||
lft_content = malloc(lft_file_count * sizeof(file));
|
||||
@@ -241,14 +243,86 @@ void *thread_top(){
|
||||
pthread_exit(0);
|
||||
}
|
||||
void *thread_btm(){
|
||||
char *path = malloc(sizeof(char));
|
||||
char *ui_btm_right_block = malloc(sizeof(char));
|
||||
unsigned int ui_btm_right_block_size = 0;
|
||||
|
||||
while(!(status & STATUS_QUIT_PROGRAM)){
|
||||
pthread_mutex_lock(&mutex_btm);
|
||||
pthread_cond_wait(&cond_btm, &mutex_btm);
|
||||
unsigned int local_status = status;
|
||||
|
||||
|
||||
int buffer_width = terminal_width;
|
||||
if (local_status & (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY)) {
|
||||
/*{{{ parse storage info */
|
||||
pthread_mutex_lock(&mutex_mid);
|
||||
unsigned long i;
|
||||
float total_dir_size = 0;
|
||||
for(i = 0; i < mid_file_count; i++) {
|
||||
if (!(mid_content[i].file_type & (FILE_TYPE_DIR))) {
|
||||
total_dir_size += mid_content[i].file_size;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&mutex_mid);
|
||||
|
||||
|
||||
free(path);
|
||||
free(ui_btm_right_block);
|
||||
|
||||
path = getcwd(NULL, 0);
|
||||
struct statvfs fs;
|
||||
statvfs(path, &fs);
|
||||
|
||||
float disk_size_free = fs.f_bsize * fs.f_bavail;
|
||||
|
||||
float parsed_number[2] = { 0 };
|
||||
|
||||
|
||||
|
||||
char size_index[2] = { 0 };
|
||||
if (total_dir_size > 1) {
|
||||
size_index[0] = -1;
|
||||
while (total_dir_size > 1 && size_index[0] < size_unit_count) {
|
||||
parsed_number[0]=total_dir_size;
|
||||
size_index[0]++;
|
||||
total_dir_size /= 1024;
|
||||
}
|
||||
} else {
|
||||
size_index[0] = 0;
|
||||
}
|
||||
|
||||
if (disk_size_free > 1) {
|
||||
size_index[1] = -1;
|
||||
while (disk_size_free > 1 && size_index[1] < size_unit_count) {
|
||||
parsed_number[1]=disk_size_free;
|
||||
size_index[1]++;
|
||||
disk_size_free /= 1024;
|
||||
}
|
||||
} else {
|
||||
size_index[1] = 0;
|
||||
}
|
||||
if (size_index[0] > 0 && size_index[1] > 0) {
|
||||
ui_btm_right_block_size = snprintf(NULL, 0, "%0.2lf%c %s %0.2lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left)+1;
|
||||
ui_btm_right_block = malloc(ui_btm_right_block_size);
|
||||
sprintf(ui_btm_right_block, "%0.2lf%c %s %0.2lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left);
|
||||
} else if (size_index[0] <= 0 && size_index[1] > 0) {
|
||||
ui_btm_right_block_size = snprintf(NULL, 0, "%0.0lf%c %s %0.2lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left)+1;
|
||||
ui_btm_right_block = malloc(ui_btm_right_block_size);
|
||||
sprintf(ui_btm_right_block, "%0.0lf%c %s %0.2lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left);
|
||||
} else if (size_index[0] > 0 && size_index[1] <= 0) {
|
||||
ui_btm_right_block_size = snprintf(NULL, 0, "%0.2lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left)+1;
|
||||
ui_btm_right_block = malloc(ui_btm_right_block_size);
|
||||
sprintf(ui_btm_right_block, "%0.2lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left);
|
||||
} else if (size_index[0] <= 0 && size_index[1] <= 0) {
|
||||
ui_btm_right_block_size = snprintf(NULL, 0, "%0.0lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left)+1;
|
||||
ui_btm_right_block = malloc(ui_btm_right_block_size);
|
||||
sprintf(ui_btm_right_block, "%0.0lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left);
|
||||
}
|
||||
/*}}}*/
|
||||
}
|
||||
|
||||
free(btm_buffer);
|
||||
int buffer_width = terminal_width;
|
||||
btm_buffer = malloc(buffer_width);
|
||||
memset(btm_buffer, ' ', buffer_width);
|
||||
btm_buffer[buffer_width] = '\0';
|
||||
@@ -264,58 +338,7 @@ void *thread_btm(){
|
||||
btm_buffer[9] = (mid_content[selected_file_current].permissions & S_IXOTH) ? 'x' : '-';
|
||||
|
||||
|
||||
char *path;
|
||||
path=getcwd(NULL, 0);
|
||||
struct statvfs fs;
|
||||
statvfs(path, &fs);
|
||||
|
||||
float disk_size_free = fs.f_bsize * fs.f_bavail;
|
||||
float parsed_number = 0;
|
||||
int offset_back = buffer_width - strlen(ui_btm_text_storage_left);
|
||||
strcpy(btm_buffer + offset_back, ui_btm_text_storage_left);
|
||||
char *float_str = malloc(buffer_width / 4);
|
||||
|
||||
char size_index = -1;
|
||||
|
||||
pthread_mutex_lock(&mutex_mid);
|
||||
unsigned long i;
|
||||
unsigned long total_dir_size = 0;
|
||||
for(i = 0; i < mid_file_count; i++) {
|
||||
if (!(mid_content[i].file_type & (FILE_TYPE_DIR))) {
|
||||
total_dir_size += mid_content[i].file_size;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&mutex_mid);
|
||||
|
||||
do {
|
||||
parsed_number=disk_size_free;
|
||||
disk_size_free /= 1024;
|
||||
size_index++;
|
||||
} while (disk_size_free > 1 && size_index < size_unit_count);
|
||||
|
||||
|
||||
snprintf(float_str, 10, "%0.2lf", parsed_number);
|
||||
|
||||
btm_buffer[offset_back - 2] = size_unit[(unsigned)size_index];
|
||||
offset_back -= strlen(float_str) + 2;
|
||||
memcpy(btm_buffer + offset_back, float_str, strlen(float_str));
|
||||
|
||||
parsed_number = 0;
|
||||
size_index = -1;
|
||||
do {
|
||||
parsed_number=total_dir_size;
|
||||
total_dir_size /= 1024;
|
||||
size_index++;
|
||||
} while (total_dir_size > 1 && size_index < size_unit_count);
|
||||
|
||||
offset_back -= strlen(ui_btm_current_dir_size) + 5;
|
||||
memcpy(btm_buffer + offset_back, ui_btm_current_dir_size, strlen(ui_btm_current_dir_size));
|
||||
|
||||
snprintf(float_str, 10, "%0.2lf", parsed_number);
|
||||
btm_buffer[offset_back - 2] = size_unit[(unsigned)size_index];
|
||||
offset_back -= strlen(float_str) + 2;
|
||||
memcpy(btm_buffer + offset_back, float_str, strlen(float_str));
|
||||
free(path);
|
||||
memcpy(btm_buffer + buffer_width - ui_btm_right_block_size, ui_btm_right_block, ui_btm_right_block_size);
|
||||
|
||||
pthread_mutex_unlock(&mutex_btm);
|
||||
}
|
||||
|
Reference in New Issue
Block a user