Added a function to delete a directory

This commit is contained in:
Alexei KADIR 2024-02-15 00:44:40 +01:00
parent ecb9a595ca
commit a059611982
3 changed files with 103 additions and 4 deletions

View File

@ -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)

View File

@ -9,6 +9,7 @@
#include <dirent.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
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;
}

View File

@ -1,5 +1,8 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
/// @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);