diff --git a/src/backing.c b/src/backing.c new file mode 100644 index 0000000..5a4a3b9 --- /dev/null +++ b/src/backing.c @@ -0,0 +1,63 @@ +#include "backing.h" + +#include +#include +#include +#include +#include + +bool is_valid_backing_id(const char* backing_id) { + if (backing_id == NULL) + return false; + + size_t length = strlen(backing_id); + + // Check that the length is valid + if (length == 0 || length > MAX_BACKING_LENGTH) + return false; + + // Check that the backing id does not contain any slashes + for (size_t i = 0; i < length; i++) + if (backing_id[i] == '/') + return false; + + // Check that the backing id is not a reserved name + if (strcmp(backing_id, ".") == 0 || strcmp(backing_id, "..") == 0) + return false; + + return true; +} + +Result get_backing_path(const char* backing_id, char** out_path) { + *out_path = NULL; + + // Check that the backing id is valid + if (!is_valid_backing_id(backing_id)) { + log_message(LOG_LEVEL_ERROR, "Invalid backing id '%s'.", backing_id); + return FAILURE; + } + + return format(out_path, "%s/%s", BACKING_POOL_DIR, backing_id); +} + +Result backing_exists(const char* backing_id, bool* out_exists) { + *out_exists = false; + + // Get the path of the backing + char* path; + Result result = get_entry_path(backing_id, &path); + if (result != SUCCESS) + return result; + + // Check if the backing exists and is a file + struct stat st; + bool exists = stat(path, &st) == 0 && S_ISREG(st.st_mode); + + // Free the path + free(path); + + // Output the result + *out_exists = exists; + + return SUCCESS; +} diff --git a/src/backing.h b/src/backing.h new file mode 100644 index 0000000..3da1884 --- /dev/null +++ b/src/backing.h @@ -0,0 +1,27 @@ +#pragma once + +#include "utils.h" +#include "disk.h" + +#include +#include + +#define BACKING_POOL_DIR "/var/lib/sandbox/backings" +#define MAX_BACKING_LENGTH 256 + +/// @brief Checks whether the given backing id is valid. +/// @param backing_id The backing id to check. +/// @return True if the backing id is valid, false otherwise. +bool is_valid_backing_id(const char* backing_id); + +/// @brief Gets the path of the given backing. +/// @param backing_id The backing id. +/// @param out_path The pointer to the output path string. The caller is responsible for freeing the memory. +/// @return The result of the operation. +Result get_backing_path(const char* backing_id, char** out_path); + +/// @brief Checks whether the given backing exists in the pool. +/// @param backing_id The backing id. +/// @param out_exists The pointer to the output boolean. +/// @return The result of the operation. +Result backing_exists(const char* backing_id, bool* out_exists); \ No newline at end of file diff --git a/src/entry.c b/src/entry.c index 522371d..63c5276 100644 --- a/src/entry.c +++ b/src/entry.c @@ -304,6 +304,27 @@ Result list_entries(char*** out_entries) { return SUCCESS; } +Result clear_entries(void) { + char** entries; + Result result = list_entries(&entries); + if (result != SUCCESS) + return result; + + // Remove each entry + for (int i = 0; entries[i] != NULL; i++) { + result = remove_entry(entries[i]); + if (result != SUCCESS) + log_message(LOG_LEVEL_WARNING, "Failed to remove the entry '%s'.", entries[i]); + } + + // Free the entries + for (int i = 0; entries[i] != NULL; i++) + free(entries[i]); + free(entries); + + return SUCCESS; +} + Result reset_entry(const char* entry_id) { // Check that it exists bool exists; diff --git a/src/entry.h b/src/entry.h index 3731dae..d29c7e6 100644 --- a/src/entry.h +++ b/src/entry.h @@ -9,6 +9,10 @@ #define ENTRY_POOL_DIR "/var/lib/sandbox/entries" #define MAX_ENTRY_LENGTH 256 +#define ENTRY_TYPE_ROOT_STRING "ROOT" +#define ENTRY_TYPE_BACKED_STRING "BACKED" +#define ENTRY_TYPE_AUTOMATIC_STRING "AUTOMATIC" + typedef enum { ENTRY_TYPE_UNKNOWN, ENTRY_TYPE_ROOT, @@ -16,10 +20,6 @@ typedef enum { ENTRY_TYPE_AUTOMATIC } EntryType; -#define ENTRY_TYPE_ROOT_STRING "ROOT" -#define ENTRY_TYPE_BACKED_STRING "BACKED" -#define ENTRY_TYPE_AUTOMATIC_STRING "AUTOMATIC" - typedef struct { EntryType type; char* backing_id;