Made some small modifications (backing extraction)

This commit is contained in:
Alexei KADIR 2024-02-19 20:23:26 +01:00
parent 7513bb37cc
commit a260002a6c
4 changed files with 64 additions and 12 deletions

View File

@ -396,24 +396,24 @@ result_t get_backing_parent(char** _parent, const char* backing) {
return result; 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) { if (info.backing_path == NULL) {
free_disk_info(&info); free_disk_info(&info);
free(path);
return success(); return success();
} }
// Get the backing identifier of the parent // Get the backing identifier of the parent
char* parent = strdup(basename(info.backing_path)); 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_disk_info(&info);
free(path);
if (parent == NULL)
return failure("Failed to get the backing identifier of the parent.");
*_parent = parent; *_parent = parent;
return result; return success();
} }

View File

@ -66,7 +66,7 @@ result_t list_backings(char*** _backings);
result_t get_default_backing(char** _backing); result_t get_default_backing(char** _backing);
/// @brief Sets the default backing identifier. /// @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. /// @return The result of the operation.
result_t set_default_backing(const char* backing); result_t set_default_backing(const char* backing);

View File

@ -7,6 +7,7 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <libgen.h>
#include <sys/stat.h> #include <sys/stat.h>
result_t check_container_identifier(const char* container) { result_t check_container_identifier(const char* container) {
@ -235,4 +236,49 @@ result_t list_containers(char*** _containers) {
free(pool_path); free(pool_path);
return result; return result;
} }
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();
}

View File

@ -60,4 +60,10 @@ bool container_filter(const char* file);
/// @brief Lists the containers in the pool. /// @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. /// @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. /// @return The result of the operation.
result_t list_containers(char*** _containers); 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);