From 188e57dc310640365f0f82a781f55aeaba16bac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexe=C3=AF=20KADIR?= Date: Sat, 17 Feb 2024 12:11:09 +0100 Subject: [PATCH] Added some signatures to handle updating entries --- src/disk.c | 18 ------------------ src/disk.h | 5 ----- src/entry.h | 32 ++++++++++++++++++++++++++++---- src/sandbox.c | 19 ++++++++++++++++--- src/sandbox.h | 1 + 5 files changed, 45 insertions(+), 30 deletions(-) diff --git a/src/disk.c b/src/disk.c index b406b0c..72bb52b 100644 --- a/src/disk.c +++ b/src/disk.c @@ -206,24 +206,6 @@ Result rebase_disk(const char* path, const char* backing_disk) { return SUCCESS; } -Result reset_disk(const char* path) { - // Get the backing file of the disk - DiskInfo info; - Result result = get_disk_info(path, &info); - if (result != SUCCESS) - return result; - - if (info.backing_file == NULL) - result = create_root_disk(path, info.virtual_size, 0644); - else - result = create_backed_disk(path, info.backing_file, 0644); - - // Free the disk info as it is no longer needed - free_disk_info(&info); - - return result; -} - Result get_disk_info(const char* path, DiskInfo* out_info) { // Initialize the output out_info->backing_file = NULL; diff --git a/src/disk.h b/src/disk.h index fdfde2a..62a9416 100644 --- a/src/disk.h +++ b/src/disk.h @@ -43,11 +43,6 @@ Result trim_disk(const char* path); /// @return The result of the operation. Result rebase_disk(const char* path, const char* backing_disk); -/// @brief Resets a disk to its original state. -/// @param path The path to the disk to reset. -/// @return The result of the operation. -Result reset_disk(const char* path); - /// @brief Gathers information about a disk. /// @param path The path to the disk to gather information about. /// @param out_info The information about the disk. diff --git a/src/entry.h b/src/entry.h index 1028e68..47d3fb1 100644 --- a/src/entry.h +++ b/src/entry.h @@ -1,12 +1,26 @@ #pragma once #include "utils.h" +#include "disk.h" #include #include #define ENTRY_POOL_DIR "/var/lib/sandbox/entries" -#define MAX_ENTRY_LENGTH 64 +#define MAX_ENTRY_LENGTH 256 + +typedef enum { + ENTRY_TYPE_ROOT, + ENTRY_TYPE_BACKED, + ENTRY_TYPE_AUTOMATIC +} EntryType; + +typedef struct { + EntryType type; + char* backing_id; + + DiskInfo disk_info; +} EntryInfo; /// @brief Checks whether the given entry id is valid. /// @param entry_id The entry id to check. @@ -56,12 +70,22 @@ Result list_entries(char*** out_entries); /// @return The result of the operation. Result clear_entries(void); -/// @brief Resets an entry to its initial state. Automatic entries will be backed by the latest backing disk available. +/// @brief Resets an entry to its initial state. /// @param entry_id The entry id. /// @return The result of the operation. Result reset_entry(const char* entry_id); -/// @brief Updates the backing disk of the given entry if it is an automatic entry. +/// @brief Updates the backing disk of the given entry to the latest available. Warning: This operation resets the entry. /// @param entry_id The entry id. /// @return The result of the operation. -Result update_entry(const char* entry_id); \ No newline at end of file +Result update_entry(const char* entry_id); + +/// @brief Gathers information about the given entry. +/// @param entry_id The entry id. +/// @param out_info The pointer to the output entry information. +/// @return The result of the operation. +Result get_entry_info(const char* entry_id, EntryInfo* out_info); + +/// @brief Frees the resources used by the given entry information. +/// @param info The entry information to free. +void free_entry_info(EntryInfo* info); diff --git a/src/sandbox.c b/src/sandbox.c index b6defbf..2f058cd 100644 --- a/src/sandbox.c +++ b/src/sandbox.c @@ -16,15 +16,17 @@ const Command COMMANDS[] = { "Prints the version of the program."}, {}, {command_add_entry, "add-entry", " [--root|-r ] [--backed|-b ]", "Adds an entry to the entry pool.", - "Adds an entry to the entry pool with the given id. The entry can either be a root entry or a backed entry. If the entry is a root entry, the size of the entry must be specified. If the entry is a backed entry, the backing id must be specified. By default, the entry is backed by the latest backing disk available."}, + "Adds an entry to the entry pool with the given id. The entry can either be a root entry or a backed entry. If the entry is a root entry, the size of the entry must be specified. If the entry is a backed entry, the backing id must be specified. By default, the entry will be an automatic entry, which will be backed by the latest backing disk available and will automatically reset to its initial state when a new backing disk is added."}, {command_remove_entry, "remove-entry", "", "Removes an entry from the entry pool.", "Removes the entry with the given id from the entry pool."}, {command_list_entries, "list-entries", "", "Lists the entries in the entry pool.", - "Lists the entries in the entry pool."}, + "Lists the entries in the entry pool, as well as their types and backing disks."}, {command_clear_entries, "clear-entries", "", "Removes all the entries from the entry pool.", "Removes all the entries from the entry pool."}, {command_reset_entry, "reset-entry", "", "Resets an entry to its initial state.", - "Resets the entry with the given id to its initial state. Automatic entries will be backed by the latest backing disk available."}, + "TODO"}, + {command_update_entry, "update-entry", "", "Updates the backing disk of the given entry to the latest available.", + "TODO"}, {}, {command_add_backing, "add-backing", " []", "Adds a backing disk to the backing pool, from the given entry.", "TODO"}, @@ -165,6 +167,17 @@ int command_reset_entry(int argc, char* argv[]) { return EXIT_SUCCESS; } +int command_update_entry(int argc, char* argv[]) { + if (argc < 1) { + log_message(LOG_LEVEL_ERROR, "Missing entry id."); + return EXIT_FAILURE; + } + + // TODO: Call update_entry + + return EXIT_SUCCESS; +} + int command_add_backing(int argc, char* argv[]) { if (argc < 1) { log_message(LOG_LEVEL_ERROR, "Missing entry id."); diff --git a/src/sandbox.h b/src/sandbox.h index dc73912..b9cdaef 100644 --- a/src/sandbox.h +++ b/src/sandbox.h @@ -22,5 +22,6 @@ int command_remove_entry(int argc, char* argv[]); int command_list_entries(int argc, char* argv[]); int command_clear_entries(int argc, char* argv[]); int command_reset_entry(int argc, char* argv[]); +int command_update_entry(int argc, char* argv[]); int command_add_backing(int argc, char* argv[]); \ No newline at end of file