mirror of
https://gittea.dev/nova/th.git
synced 2025-10-21 10:20:15 -04:00
recursive file deletion
This commit is contained in:
28
dir.c
28
dir.c
@@ -5,6 +5,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "sorting.h"
|
#include "sorting.h"
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
@@ -25,6 +26,7 @@ linked_dir *current_dir;
|
|||||||
unsigned long get_dir_size(char *path);
|
unsigned long get_dir_size(char *path);
|
||||||
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content);
|
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content);
|
||||||
void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file *dir_content);
|
void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file *dir_content);
|
||||||
|
char recursive_delete(file current_file);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -370,3 +372,29 @@ void dir_init(){
|
|||||||
current_dir = visited_dirs;
|
current_dir = visited_dirs;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char recursive_delete(file current_file){
|
||||||
|
if (current_file.file_type & (FILE_TYPE_SYMLINK | FILE_TYPE_DIR)) {
|
||||||
|
unsigned int file_modifiers_tmp = file_modifiers;
|
||||||
|
file_modifiers |= FILE_MODIFIERS_HIDDEN_FILES;
|
||||||
|
unsigned long current_file_count = get_dir_size(current_file.file_name);
|
||||||
|
if (current_file_count != 0) {
|
||||||
|
file *current_dir = malloc(current_file_count * sizeof(file));
|
||||||
|
memset(current_dir, '\0', current_file_count * sizeof(file));
|
||||||
|
get_dir_content(current_file.file_name, ¤t_file_count, current_dir);
|
||||||
|
chdir(current_file.file_name);
|
||||||
|
unsigned long i;
|
||||||
|
for (i = 0; i < current_file_count; i++) {
|
||||||
|
recursive_delete(current_dir[i]);
|
||||||
|
free(current_dir[i].file_name);
|
||||||
|
}
|
||||||
|
free(current_dir);
|
||||||
|
chdir("..");
|
||||||
|
}
|
||||||
|
remove(current_file.file_name);
|
||||||
|
file_modifiers = file_modifiers_tmp;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
remove(current_file.file_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
1
dir.h
1
dir.h
@@ -10,3 +10,4 @@ char update_selected_file();
|
|||||||
void dir_set_selected_file_current(unsigned long selected_file_current);
|
void dir_set_selected_file_current(unsigned long selected_file_current);
|
||||||
unsigned long dir_get_selected_file_current();
|
unsigned long dir_get_selected_file_current();
|
||||||
void dir_init();
|
void dir_init();
|
||||||
|
char recursive_delete(file current_file);
|
||||||
|
@@ -433,32 +433,22 @@ void delete(){
|
|||||||
|
|
||||||
if (ch == 'y' || ch == 'Y') {
|
if (ch == 'y' || ch == 'Y') {
|
||||||
/* TODO(2025-06-30T02:27:06) IMPORTANT: this really fucks up when the file has a quotation mark in its name */
|
/* TODO(2025-06-30T02:27:06) IMPORTANT: this really fucks up when the file has a quotation mark in its name */
|
||||||
int error;
|
|
||||||
if (hits) {
|
if (hits) {
|
||||||
int j = 2;
|
|
||||||
for (i = 0; i < mid_file_count; i++) {
|
for (i = 0; i < mid_file_count; i++) {
|
||||||
if (mid_content[i].status & FILE_STATUS_SELECTED) {
|
if (mid_content[i].status & FILE_STATUS_SELECTED) {
|
||||||
error = remove(mid_content[i].file_name);
|
recursive_delete(mid_content[i]);
|
||||||
if (error != 0) {
|
|
||||||
mvaddstr(terminal_height-j, 0, "could not delete: " );
|
|
||||||
mvaddstr(terminal_height-j, strlen("could not delete: "), mid_content[i].file_name);
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(btm_buffer);
|
free(btm_buffer);
|
||||||
btm_buffer = concat("deleted: ", file_str);
|
btm_buffer = concat("deleted: ", file_str);
|
||||||
} else {
|
} else {
|
||||||
free(btm_buffer);
|
free(btm_buffer);
|
||||||
error = remove(mid_content[selected_file_current].file_name);
|
if (mid_content[selected_file_current].file_type & (FILE_TYPE_SYMLINK | FILE_TYPE_DIR)) {
|
||||||
if (error != 0) {
|
recursive_delete(mid_content[selected_file_current]);
|
||||||
mvaddstr(terminal_height-2, 0, "could not delete: " );
|
}
|
||||||
mvaddstr(terminal_height-2, strlen("could not delete: "), mid_content[i].file_name);
|
remove(mid_content[selected_file_current].file_name);
|
||||||
btm_buffer = " ";
|
btm_buffer = concat("deleted: \"", mid_content[selected_file_current].file_name);
|
||||||
} else {
|
btm_buffer = concat(btm_buffer, "\"");
|
||||||
btm_buffer = concat("deleted: \"", mid_content[selected_file_current].file_name);
|
|
||||||
btm_buffer = concat(btm_buffer, "\"");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
/*system(cmd);*/
|
/*system(cmd);*/
|
||||||
|
Reference in New Issue
Block a user