Added a function to delete a directory
This commit is contained in:
parent
ecb9a595ca
commit
a059611982
31
src/entry.c
31
src/entry.c
@ -70,6 +70,37 @@ bool entry_exists(const char* entry) {
|
|||||||
return true;
|
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) {
|
uint64_t get_available_space(void) {
|
||||||
char* entries_path = get_entries_path();
|
char* entries_path = get_entries_path();
|
||||||
if (entries_path == NULL)
|
if (entries_path == NULL)
|
||||||
|
65
src/utils.c
65
src/utils.c
@ -9,6 +9,7 @@
|
|||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
char* format(const char* fmt, ...) {
|
char* format(const char* fmt, ...) {
|
||||||
va_list args;
|
va_list args;
|
||||||
@ -269,3 +270,67 @@ int execute(char** stdout_buffer, char** stderr_buffer, const char* file, ...) {
|
|||||||
return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
|
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;
|
||||||
|
}
|
||||||
|
11
src/utils.h
11
src/utils.h
@ -1,5 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
/// @brief The log levels.
|
/// @brief The log levels.
|
||||||
typedef enum {
|
typedef enum {
|
||||||
LOG_DEBUG,
|
LOG_DEBUG,
|
||||||
@ -28,7 +31,7 @@ void log_msg(LogLevel level, const char* fmt, ...);
|
|||||||
/// @return The exit code of the command.
|
/// @return The exit code of the command.
|
||||||
int execute(char** stdout_buffer, char** stderr_buffer, const char* file, ...);
|
int execute(char** stdout_buffer, char** stderr_buffer, const char* file, ...);
|
||||||
|
|
||||||
/// @brief Returns a list of the files in the specified directory.
|
/// @brief Deletes the specified directory and all its contents.
|
||||||
/// @param directory The directory to list.
|
/// @param directory The directory to delete.
|
||||||
/// @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.
|
/// @return true if the directory was deleted, otherwise false.
|
||||||
char** list_files(const char* directory);
|
bool delete_directory(const char* directory);
|
||||||
|
Loading…
Reference in New Issue
Block a user