diff --git a/src/backing.c b/src/backing.c index f49967b..96a58ad 100644 --- a/src/backing.c +++ b/src/backing.c @@ -7,60 +7,3 @@ #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 index b7d284c..fbb8a1d 100644 --- a/src/backing.h +++ b/src/backing.h @@ -8,36 +8,3 @@ #define BACKING_POOL_DIR "/var/lib/sandbox/backings" #define MAX_BACKING_LENGTH 256 - -/// @brief Checks whether the given backing disk id is valid. -/// @param backing_id The backing disk id to check. -/// @return True if the backing disk id is valid, false otherwise. -bool is_valid_backing_id(const char* backing_id); - -/// @brief Gets the path of the given backing disk. -/// @param backing_id The backing disk 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 disk exists in the pool. -/// @param backing_id The backing disk 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); - -/// @brief Adds a backing disk to the pool from an entry. -/// @param backing_id The backing disk id. -/// @param entry_id The entry id. -/// @return The result of the operation. -Result add_backing_from_entry(const char* backing_id, const char* entry_id); - -/// @brief Removes a backing disk from the pool, and removes any backing disks that uses it. -/// @param backing_id The backing disk id. -/// @return The result of the operation. -Result remove_backing(const char* backing_id); - -/// @brief Lists the backing disks in the pool. -/// @param out_backings The pointer to the null-terminated array of backing disk ids. The caller is responsible for freeing the memory of the array and its elements. -/// @return The result of the operation. -Result list_backings(char*** out_backings, size_t* out_count); 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.c b/src/entry.c index 82711ea..347d92d 100644 --- a/src/entry.c +++ b/src/entry.c @@ -63,6 +63,46 @@ Result get_entry_disk_path(const char* entry_id, char** out_path) { return result; } +Result get_entry_backing_id(const char* entry_id, char** out_backing_id) { + *out_backing_id = NULL; + + // Get the path of the disk + char* disk_path; + Result result = get_entry_disk_path(entry_id, &disk_path); + if (result != SUCCESS) + return result; + + // Get the backing file of the disk + DiskInfo info; + result = get_disk_info(disk_path, &info); + if (result != SUCCESS) { + free(disk_path); + return result; + } + + // Free the disk path + free(disk_path); + + // Check that the disk is backed + if (info.backing_file == NULL) { + free_disk_info(&info); + return SUCCESS; + } + + // Get the backing id + *out_backing_id = strdup(basename(info.backing_file)); + if (*out_backing_id == NULL) { + log_message(LOG_LEVEL_ERROR, "Failed to allocate memory for the backing id."); + free_disk_info(&info); + return OUT_OF_MEMORY; + } + + // Free the disk info + free_disk_info(&info); + + return SUCCESS; +} + Result get_entry_type_path(const char* entry_id, char** out_path) { *out_path = NULL; @@ -412,30 +452,3 @@ Result clear_entries(void) { return SUCCESS; } - -Result reset_entry(const char* entry_id) { - // Check that it exists - bool exists; - Result result = entry_exists(entry_id, &exists); - if (result != SUCCESS) - return result; - - if (!exists) { - log_message(LOG_LEVEL_ERROR, "The entry '%s' does not exist.", entry_id); - return FAILURE; - } - - // Get the path of the disk - char* disk_path; - result = get_entry_disk_path(entry_id, &disk_path); - if (result != SUCCESS) - return result; - - // Reset the disk - result = reset_disk(disk_path); - - // Free the disk path - free(disk_path); - - return result; -} diff --git a/src/entry.h b/src/entry.h index 71a8e88..74d65b3 100644 --- a/src/entry.h +++ b/src/entry.h @@ -37,6 +37,12 @@ Result get_entry_path(const char* entry_id, char** out_path); /// @return The result of the operation. Result get_entry_disk_path(const char* entry_id, char** out_path); +/// @brief Gets the backing id of the given entry. +/// @param entry_id The entry id. +/// @param out_backing_id The pointer to the output backing id string. The caller is responsible for freeing the memory. +/// @return The result of the operation. +Result get_entry_backing_id(const char* entry_id, char** out_backing_id); + /// @brief Gets the path of the type file of the given entry. /// @param entry_id The entry id. /// @param out_path The pointer to the output path string. The caller is responsible for freeing the memory. @@ -91,18 +97,3 @@ Result list_entries(char*** out_entries); /// @brief Clears all the entries from the pool. /// @return The result of the operation. Result clear_entries(void); - -/// @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 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); - -/// @brief Checks whether the given entry needs to be updated, and updates it if needed. -/// @param entry_id The entry id. -/// @return The result of the operation. -Result check_update_entry(const char* entry_id); diff --git a/src/sandbox.c b/src/sandbox.c index 3667420..3d6e69d 100644 --- a/src/sandbox.c +++ b/src/sandbox.c @@ -1,6 +1,7 @@ #include "sandbox.h" #include "utils.h" +#include "backing.h" #include "entry.h" #include "disk.h" @@ -9,6 +10,7 @@ #include #include #include +#include #include const Command COMMANDS[] = { @@ -30,8 +32,6 @@ const Command COMMANDS[] = { {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"}, }; int main(int argc, char* argv[]) { @@ -197,7 +197,7 @@ int command_add_entry(int argc, char* argv[]) { backing_id = argv[i + 1]; i++; // Consume the next argument } else { - log_message(LOG_LEVEL_ERROR, "Unknown option '%s'.", argv[i]); + log_message(LOG_LEVEL_ERROR, "Too many arguments for 'add-entry' command."); return EXIT_FAILURE; } } @@ -224,8 +224,8 @@ int command_remove_entry(int argc, char* argv[]) { const char* entry_id = argv[0]; - for (int i = 1; i < argc; i++) { - log_message(LOG_LEVEL_ERROR, "Unknown option '%s'.", argv[i]); + if (argc > 1) { + log_message(LOG_LEVEL_ERROR, "Too many arguments for 'remove-entry' command."); return EXIT_FAILURE; } @@ -237,33 +237,17 @@ int command_remove_entry(int argc, char* argv[]) { } int command_list_entries(int argc, char* argv[]) { - // TODO: Call list_entries, and display the result in a nice way - return EXIT_SUCCESS; } int command_clear_entries(int argc, char* argv[]) { - // TODO: Ask for confirmation - - // TODO: Call clear_entries - return EXIT_SUCCESS; } int command_reset_entry(int argc, char* argv[]) { - // TODO: Call reset_entry - return EXIT_SUCCESS; } int command_update_entry(int argc, char* argv[]) { - // TODO: Call update_entry - return EXIT_SUCCESS; -} - -int command_add_backing(int argc, char* argv[]) { - // TODO: Call add_backing - - return EXIT_SUCCESS; -} +} \ No newline at end of file diff --git a/src/sandbox.h b/src/sandbox.h index 6158962..d4b6178 100644 --- a/src/sandbox.h +++ b/src/sandbox.h @@ -24,6 +24,4 @@ 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 +int command_update_entry(int argc, char* argv[]); \ No newline at end of file