From f92c3c6a1ec02bc3f589666223b1fdfcecb8a534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexe=C3=AF=20KADIR?= Date: Sat, 17 Feb 2024 00:57:30 +0100 Subject: [PATCH] Added disk operation signatures --- src/disk.h | 32 ++++++++++++++++++++++++++++++++ src/entry.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- src/entry.h | 4 ++++ src/sandbox.c | 20 +++----------------- 4 files changed, 85 insertions(+), 18 deletions(-) create mode 100644 src/disk.h diff --git a/src/disk.h b/src/disk.h new file mode 100644 index 0000000..82b4ee8 --- /dev/null +++ b/src/disk.h @@ -0,0 +1,32 @@ +#pragma once + +#include "utils.h" + +#include +#include +#include + +/// @brief Creates an empty disk at the given path, with the given size and with the given permissions. +/// @param path The path to the disk to create. +/// @param size The size of the disk to create. +/// @param permissions The permissions to set on the disk. +/// @return The result of the operation. +Result create_empty_disk(const char* path, uint64_t size, mode_t permissions); + +/// @brief Creates a backed disk at the given path, with the given backing disk and with the given permissions. +/// @param path The path to the disk to create. +/// @param backing_disk The disk to back the new disk with. +/// @param permissions The permissions to set on the disk. +/// @return The result of the operation. +Result create_backed_disk(const char* path, const char* backing_disk, mode_t permissions); + +/// @brief Trims a disk to remove any unused space. +/// @param path The path to the disk to trim. +/// @return The result of the operation. +Result trim_disk(const char* path); + +/// @brief Moves a disk's backing file to the given path. +/// @param path The path to the disk to move. +/// @param backing_disk The new path to the backing disk. +/// @return The result of the operation. +Result rebase_disk(const char* path, const char* backing_disk); diff --git a/src/entry.c b/src/entry.c index 24b001a..69de2eb 100644 --- a/src/entry.c +++ b/src/entry.c @@ -4,6 +4,7 @@ #include #include +#include #include bool is_entry_id_valid(const char* entry_id) { @@ -12,21 +13,30 @@ bool is_entry_id_valid(const char* entry_id) { size_t length = strlen(entry_id); + // Check that the length is valid if (length == 0 || length > MAX_ENTRY_LENGTH) return false; + // Check that the entry id does not contain any slashes for (size_t i = 0; i < length; i++) if (entry_id[i] == '/') return false; + // Check that the entry id is not a reserved name + if (strcmp(entry_id, ".") == 0 || strcmp(entry_id, "..") == 0) + return false; + return true; } Result get_entry_path(const char* entry_id, char** out_path) { *out_path = NULL; - if (!is_entry_id_valid(entry_id)) + // Check that the entry id is valid + if (!is_entry_id_valid(entry_id)) { + log_message(LOG_LEVEL_ERROR, "Invalid entry id '%s'.", entry_id); return FAILURE; + } return format(out_path, "%s/%s", ENTRY_POOL_DIR, entry_id); } @@ -34,17 +44,52 @@ Result get_entry_path(const char* entry_id, char** out_path) { Result entry_exists(const char* entry_id, bool* out_exists) { *out_exists = false; + // Get the path of the entry char* path; Result result = get_entry_path(entry_id, &path); if (result != SUCCESS) return result; + // Check if the entry exists and is a directory struct stat st; bool exists = stat(path, &st) == 0 && S_ISDIR(st.st_mode); + // Free the path free(path); + // Output the result *out_exists = exists; return SUCCESS; } + +Result add_entry(const char* entry_id) { + // Check if the entry already exists + bool exists; + Result result = entry_exists(entry_id, &exists); + if (result != SUCCESS) + return result; + + if (exists) { + log_message(LOG_LEVEL_ERROR, "Entry '%s' already exists.", entry_id); + return FAILURE; + } + + // Create the entry directory + char* path; + result = get_entry_path(entry_id, &path); + if (result != SUCCESS) + return result; + + int result_code = mkdir(path, 0755); + + free(path); + + // Check if the operation failed + if (result_code != 0) { + log_message(LOG_LEVEL_ERROR, "Failed to create entry '%s' (%s).", entry_id, strerror(errno)); + return FAILURE; + } + + return SUCCESS; +} diff --git a/src/entry.h b/src/entry.h index b9a87ef..973463a 100644 --- a/src/entry.h +++ b/src/entry.h @@ -25,3 +25,7 @@ Result get_entry_path(const char* entry_id, char** out_path); /// @return The result of the operation. Result entry_exists(const char* entry_id, bool* out_exists); +/// @brief Adds an entry to the entry pool. +/// @param entry_id The entry id. +/// @return The result of the operation. +Result add_entry(const char* entry_id); \ No newline at end of file diff --git a/src/sandbox.c b/src/sandbox.c index 67ee4d7..5fa6e74 100644 --- a/src/sandbox.c +++ b/src/sandbox.c @@ -112,24 +112,10 @@ int command_add_entry(int argc, char* argv[]) { // Extract the entry id const char* entry_id = argv[0]; - // Check if the entry id is valid - if (!is_entry_id_valid(entry_id)) { - log_message(LOG_LEVEL_ERROR, "Invalid entry id '%s'.", entry_id); - return EXIT_FAILURE; - } - - // Check if the entry already exists - bool exists; - if (entry_exists(entry_id, &exists) != SUCCESS) - return EXIT_FAILURE; - - if (exists) { - log_message(LOG_LEVEL_ERROR, "Entry '%s' already exists.", entry_id); - return EXIT_FAILURE; - } - // Create the entry - // TODO + Result result = add_entry(entry_id); + if (result != SUCCESS) + return result; // Add the disk to the entry // TODO