From dcdec6747670bef4e6e353915ab5d086abc0d172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexe=C3=AF=20KADIR?= Date: Sun, 18 Feb 2024 00:28:42 +0100 Subject: [PATCH] Implemented some more functions. Next big step : AddBacking --- src/backing.c | 10 +++++++++- src/backing.h | 6 ++++++ src/sandbox.c | 22 +++++++++++++++++++--- src/utils.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/utils.h | 2 ++ 5 files changed, 85 insertions(+), 4 deletions(-) diff --git a/src/backing.c b/src/backing.c index 55ad99f..e3b06db 100644 --- a/src/backing.c +++ b/src/backing.c @@ -1,7 +1,15 @@ #include "backing.h" +#include "disk.h" +#include "entry.h" + #include #include +#include +#include +#include +#include +#include bool IsBackingIdentifierValid(const char* backing_identifier) { if (backing_identifier == NULL) @@ -22,7 +30,7 @@ bool IsBackingIdentifierValid(const char* backing_identifier) { return false; // Check that the identifier starts with a number - if (backing_identifier[0] >= '0' && backing_identifier[0] <= '9') + if (backing_identifier[0] < '0' || backing_identifier[0] > '9') return false; return true; diff --git a/src/backing.h b/src/backing.h index 97eaa9b..f189000 100644 --- a/src/backing.h +++ b/src/backing.h @@ -7,3 +7,9 @@ int GetBackingIndex(const char* backing_identifier); Status GetBackingPoolPath(char** _backing_pool_path); Status GetBackingDiskPath(const char* backing_identifier, char** _backing_path); + +Status AddBacking(const char* backing_identifier, const char* entry_identifier); +Status RemoveBacking(const char* backing_identifier); + +Status ListBackings(char*** _backings); +Status GetLatestBacking(char** _backing_identifier); \ No newline at end of file diff --git a/src/sandbox.c b/src/sandbox.c index 26c4dc8..1998cf5 100644 --- a/src/sandbox.c +++ b/src/sandbox.c @@ -1,6 +1,7 @@ #include "sandbox.h" #include "entry.h" +#include "backing.h" #include #include @@ -145,7 +146,12 @@ int CommandAddEntry(int argc, char* argv[]) { return EXIT_FAILURE; } - return AddEntry(argv[0]); + Status status = AddEntry(argv[0]); + if (status != SUCCESS) + return status; + + Log(LOG_LEVEL_INFO, "Added entry '%s'.", argv[0]); + return EXIT_SUCCESS; } int CommandRemoveEntry(int argc, char* argv[]) { @@ -157,7 +163,12 @@ int CommandRemoveEntry(int argc, char* argv[]) { return EXIT_FAILURE; } - return RemoveEntry(argv[0]); + Status status = RemoveEntry(argv[0]); + if (status != SUCCESS) + return status; + + Log(LOG_LEVEL_INFO, "Removed entry '%s'.", argv[0]); + return EXIT_SUCCESS; } int CommandListEntries(int argc, char* argv[]) { @@ -187,5 +198,10 @@ int CommandClearEntries(int argc, char* argv[]) { return EXIT_FAILURE; } - return ClearEntries(); + Status status = ClearEntries(); + if (status != SUCCESS) + return status; + + Log(LOG_LEVEL_INFO, "Cleared all entries."); + return EXIT_SUCCESS; } diff --git a/src/utils.c b/src/utils.c index 735cea8..5ed4982 100644 --- a/src/utils.c +++ b/src/utils.c @@ -35,6 +35,7 @@ void Log(LogLevel level, const char* format, ...) { case LOG_LEVEL_INFO: color = "\033[0;36m"; level_str = "INFO"; + break; case LOG_LEVEL_WARNING: color = "\033[0;33m"; level_str = "WARNING"; @@ -323,3 +324,51 @@ Status WriteFile(const char* path, const char* content) { return status; } + +Status CopyFile(const char* source_path, const char* destination_path) { + // Open the source file + int source_file = open(source_path, O_RDONLY); + if (source_file == -1) { + Log(LOG_LEVEL_ERROR, "Failed to open the source file %s (%s).", source_path, strerror(errno)); + return FAILURE; + } + + // Open the destination file + int destination_file = open(destination_path, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (destination_file == -1) { + Log(LOG_LEVEL_ERROR, "Failed to open the destination file %s (%s).", destination_path, strerror(errno)); + close(source_file); + return FAILURE; + } + + // Copy the file + char buffer[4096]; + ssize_t bytes_read; + while ((bytes_read = read(source_file, buffer, sizeof(buffer))) > 0) { + ssize_t bytes_written = 0; + while (bytes_written < bytes_read) { + ssize_t result = write(destination_file, buffer + bytes_written, bytes_read - bytes_written); + if (result == -1) { + Log(LOG_LEVEL_ERROR, "Failed to write to the destination file %s (%s).", destination_path, strerror(errno)); + close(source_file); + close(destination_file); + return FAILURE; + } + + bytes_written += result; + } + } + + if (bytes_read == -1) { + Log(LOG_LEVEL_ERROR, "Failed to read from the source file %s (%s).", source_path, strerror(errno)); + close(source_file); + close(destination_file); + return FAILURE; + } + + // Close the files + close(source_file); + close(destination_file); + + return SUCCESS; +} diff --git a/src/utils.h b/src/utils.h index a7edf1e..c9d5ea6 100644 --- a/src/utils.h +++ b/src/utils.h @@ -28,3 +28,5 @@ Status ReadFileDescriptor(int fd, char** _content); Status WriteFileDescriptor(int fd, const char* content); Status ReadFile(const char* path, char** _content); Status WriteFile(const char* path, const char* content); + +Status CopyFile(const char* source_path, const char* destination_path); \ No newline at end of file