From a260002a6ca600af140c757faf4cbc7ed1f15890 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexe=C3=AF=20KADIR?= Date: Mon, 19 Feb 2024 20:23:26 +0100 Subject: [PATCH] Made some small modifications (backing extraction) --- src/backing.c | 18 +++++++++--------- src/backing.h | 2 +- src/container.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- src/container.h | 8 +++++++- 4 files changed, 64 insertions(+), 12 deletions(-) diff --git a/src/backing.c b/src/backing.c index 99e87f4..400d2f3 100755 --- a/src/backing.c +++ b/src/backing.c @@ -396,24 +396,24 @@ result_t get_backing_parent(char** _parent, const char* backing) { return result; } - // If the disk is not backed, return success and a null parent + // Free the backing path as it is not needed anymore + free(path); + + // Check if the disk is backed if (info.backing_path == NULL) { free_disk_info(&info); - free(path); return success(); } // Get the backing identifier of the parent char* parent = strdup(basename(info.backing_path)); - if (parent == NULL) { - free_disk_info(&info); - free(path); - return failure("Failed to get the backing identifier of the parent."); - } + // Free the disk info as it is not needed anymore free_disk_info(&info); - free(path); + + if (parent == NULL) + return failure("Failed to get the backing identifier of the parent."); *_parent = parent; - return result; + return success(); } diff --git a/src/backing.h b/src/backing.h index 94669a4..c94d4a0 100755 --- a/src/backing.h +++ b/src/backing.h @@ -66,7 +66,7 @@ result_t list_backings(char*** _backings); result_t get_default_backing(char** _backing); /// @brief Sets the default backing identifier. -/// @param backing The backing identifier to set as the default. +/// @param backing The backing identifier to set as the default. This parameter can be NULL to remove the default backing. /// @return The result of the operation. result_t set_default_backing(const char* backing); diff --git a/src/container.c b/src/container.c index e9136e6..5e00b44 100755 --- a/src/container.c +++ b/src/container.c @@ -7,6 +7,7 @@ #include #include #include +#include #include result_t check_container_identifier(const char* container) { @@ -235,4 +236,49 @@ result_t list_containers(char*** _containers) { free(pool_path); return result; -} \ No newline at end of file +} + +result_t get_container_backing(char** _backing, const char* container) { + // Initialize the output parameters + *_backing = NULL; + + // Check that the container exists + result_t result = check_container_exists(container); + if (result != success()) + return result; + + // Get the container path + char* path; + result = get_container_path(&path, container); + if (result != success()) + return result; + + // Get the disk information + disk_info_t info; + result = get_disk_info(&info, path); + if (result != success()) { + free(path); + return result; + } + + // Free the container path as it is no longer needed + free(path); + + // Check if the disk is backed + if (info.backing_path == NULL) { + free_disk_info(&info); + success(); + } + + // Get the backing identifier + char* backing = strdup(basename(info.backing_path)); + + // Free the disk information as it is no longer needed + free_disk_info(&info); + + if (backing == NULL) + return failure("Failed to get backing identifier for container '%s'.", container); + + *_backing = backing; + return success(); +} diff --git a/src/container.h b/src/container.h index 9312b4b..ee5e35b 100755 --- a/src/container.h +++ b/src/container.h @@ -60,4 +60,10 @@ bool container_filter(const char* file); /// @brief Lists the containers in the pool. /// @param _containers The string pointer array to store the resulting array in. The caller is responsible for freeing the strings and the array. /// @return The result of the operation. -result_t list_containers(char*** _containers); \ No newline at end of file +result_t list_containers(char*** _containers); + +/// @brief Gets the backing of the given container. If the container does not exist, the call will return a failure result with an error message. +/// @param _backing The string pointer to store the resulting backing in. +/// @param container The container to get the backing of. +/// @return The result of the operation. +result_t get_container_backing(char** _backing, const char* container); \ No newline at end of file