From a059611982ee2a20728fce373fd0ea909a5488df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexe=C3=AF=20KADIR?= Date: Thu, 15 Feb 2024 00:44:40 +0100 Subject: [PATCH] Added a function to delete a directory --- src/entry.c | 31 +++++++++++++++++++++++++ src/utils.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/utils.h | 11 +++++---- 3 files changed, 103 insertions(+), 4 deletions(-) diff --git a/src/entry.c b/src/entry.c index 0c20bbf..474d2bf 100644 --- a/src/entry.c +++ b/src/entry.c @@ -70,6 +70,37 @@ bool entry_exists(const char* entry) { return true; } +bool create_entry(const char* entry) { + char* entry_path = get_entry_path(entry); + if (entry_path == NULL) + return false; + + // Create the entry directory + int result = mkdir(entry_path, 0755); + + // Free the entry path + free(entry_path); + + if (result != 0) + return false; + + return true; +} + +bool delete_entry(const char* entry) { + char* entry_path = get_entry_path(entry); + if (entry_path == NULL) + return false; + + // Delete the entry directory + bool result = delete_directory(entry_path); + + // Free the entry path + free(entry_path); + + return result; +} + uint64_t get_available_space(void) { char* entries_path = get_entries_path(); if (entries_path == NULL) diff --git a/src/utils.c b/src/utils.c index ae84449..2e13013 100755 --- a/src/utils.c +++ b/src/utils.c @@ -9,6 +9,7 @@ #include #include #include +#include char* format(const char* fmt, ...) { va_list args; @@ -269,3 +270,67 @@ int execute(char** stdout_buffer, char** stderr_buffer, const char* file, ...) { return WIFEXITED(status) ? WEXITSTATUS(status) : -1; } +bool delete_directory(const char* directory) { + // Open the directory + DIR* dir = opendir(directory); + if (dir == NULL) { + log_msg(LOG_ERROR, "Failed to open directory '%s'.", directory); + return false; + } + + // Iterate over the directory entries + struct dirent* entry; + while ((entry = readdir(dir)) != NULL) { + // Skip . and .. + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) + continue; + + // Combine the directory path with the entry name + char* entry_path = format("%s/%s", directory, entry->d_name); + if (entry_path == NULL) { + log_msg(LOG_ERROR, "Failed to allocate memory for the entry path."); + closedir(dir); + return false; + } + + // Check if the entry is a directory + struct stat statbuf; + int result = stat(entry_path, &statbuf); + if (result != 0) { + log_msg(LOG_ERROR, "Failed to get information about entry '%s'.", entry_path); + free(entry_path); + closedir(dir); + return false; + } + + if (S_ISDIR(statbuf.st_mode)) { + // Delete the directory + if (!delete_directory(entry_path)) { + free(entry_path); + closedir(dir); + return false; + } + } else { + // Delete the file + if (unlink(entry_path) != 0) { + log_msg(LOG_ERROR, "Failed to delete entry '%s'.", entry_path); + free(entry_path); + closedir(dir); + return false; + } + } + + free(entry_path); + } + + // Close the directory + closedir(dir); + + // Delete the directory + if (rmdir(directory) != 0) { + log_msg(LOG_ERROR, "Failed to delete directory '%s'.", directory); + return false; + } + + return true; +} diff --git a/src/utils.h b/src/utils.h index 2494a4f..c8df5d4 100755 --- a/src/utils.h +++ b/src/utils.h @@ -1,5 +1,8 @@ #pragma once +#include +#include + /// @brief The log levels. typedef enum { LOG_DEBUG, @@ -28,7 +31,7 @@ void log_msg(LogLevel level, const char* fmt, ...); /// @return The exit code of the command. int execute(char** stdout_buffer, char** stderr_buffer, const char* file, ...); -/// @brief Returns a list of the files in the specified directory. -/// @param directory The directory to list. -/// @return The list of files in the specified directory. The caller is responsible for freeing the returned array and strings. This function can return NULL. -char** list_files(const char* directory); +/// @brief Deletes the specified directory and all its contents. +/// @param directory The directory to delete. +/// @return true if the directory was deleted, otherwise false. +bool delete_directory(const char* directory);