Implemented some more functions

This commit is contained in:
2024-02-15 12:50:30 +01:00
parent a059611982
commit f64f39c9c8
8 changed files with 306 additions and 29 deletions

View File

@@ -7,6 +7,7 @@
#include <time.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -270,11 +271,32 @@ int execute(char** stdout_buffer, char** stderr_buffer, const char* file, ...) {
return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
}
bool delete_directory(const char* directory) {
bool create_directory(const char* directory, mode_t mode, uid_t uid, gid_t gid) {
// Create the directory
if (mkdir(directory, mode) != 0) {
log_msg(LOG_ERROR, "Failed to create directory '%s' (%s).", directory, strerror(errno));
return false;
}
// Change the owner of the directory
if (chown(directory, uid, gid) != 0) {
log_msg(LOG_ERROR, "Failed to change the owner of directory '%s' (%s).", directory, strerror(errno));
return false;
}
return true;
}
bool delete_directory(const char* directory, int level) {
if (level > MAX_RECURSION_LEVEL) {
log_msg(LOG_ERROR, "Too many levels of recursion when deleting directory '%s'.", directory);
return false;
}
// Open the directory
DIR* dir = opendir(directory);
if (dir == NULL) {
log_msg(LOG_ERROR, "Failed to open directory '%s'.", directory);
log_msg(LOG_ERROR, "Failed to open directory '%s' (%s).", directory, strerror(errno));
return false;
}
@@ -297,7 +319,7 @@ bool delete_directory(const char* 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);
log_msg(LOG_ERROR, "Failed to get information about file '%s' (%s).", entry_path, strerror(errno));
free(entry_path);
closedir(dir);
return false;
@@ -305,7 +327,9 @@ bool delete_directory(const char* directory) {
if (S_ISDIR(statbuf.st_mode)) {
// Delete the directory
if (!delete_directory(entry_path)) {
if (!delete_directory(entry_path, level + 1)) {
log_msg(LOG_ERROR, "Failed to delete directory '%s'.", entry_path);
free(entry_path);
closedir(dir);
return false;
@@ -313,7 +337,8 @@ bool delete_directory(const char* directory) {
} else {
// Delete the file
if (unlink(entry_path) != 0) {
log_msg(LOG_ERROR, "Failed to delete entry '%s'.", entry_path);
log_msg(LOG_ERROR, "Failed to delete file '%s' (%s).", entry_path, strerror(errno));
free(entry_path);
closedir(dir);
return false;
@@ -328,7 +353,7 @@ bool delete_directory(const char* directory) {
// Delete the directory
if (rmdir(directory) != 0) {
log_msg(LOG_ERROR, "Failed to delete directory '%s'.", directory);
log_msg(LOG_ERROR, "Failed to delete directory '%s' (%s).", directory, strerror(errno));
return false;
}