diff --git a/src/backing.c b/src/backing.c new file mode 100644 index 0000000..55ad99f --- /dev/null +++ b/src/backing.c @@ -0,0 +1,62 @@ +#include "backing.h" + +#include +#include + +bool IsBackingIdentifierValid(const char* backing_identifier) { + if (backing_identifier == NULL) + return false; + + // Check that the identifier is not empty or too long + size_t length = strlen(backing_identifier); + if (length == 0 || length > 64) + return false; + + // Check that the identifier does not contain any slashes + for (size_t i = 0; i < length; i++) + if (backing_identifier[i] == '/') + return false; + + // Check that the identifier is not "." or ".." + if (strcmp(backing_identifier, ".") == 0 || strcmp(backing_identifier, "..") == 0) + return false; + + // Check that the identifier starts with a number + if (backing_identifier[0] >= '0' && backing_identifier[0] <= '9') + return false; + + return true; +} + +int GetBackingIndex(const char* backing_identifier) { + // Check that the identifier is valid + if (!IsBackingIdentifierValid(backing_identifier)) + return -1; + + // Get the index + return atoi(backing_identifier); +} + +Status GetBackingPoolPath(char** _backing_pool_path) { + return Format(_backing_pool_path, "/var/lib/sandbox/backings"); +} + +Status GetBackingDiskPath(const char* backing_identifier, char** _backing_path) { + // Check that the identifier is valid, as it will be used in a path + if (!IsBackingIdentifierValid(backing_identifier)) { + Log(LOG_LEVEL_ERROR, "Invalid backing identifier '%s'.", backing_identifier); + return FAILURE; + } + + // Get the backing pool path + char* backing_pool_path = NULL; + Status status = GetBackingPoolPath(&backing_pool_path); + if (status != SUCCESS) + return status; + + // Format the backing path + status = Format(_backing_path, "%s/%s", backing_pool_path, backing_identifier); + free(backing_pool_path); + + return status; +} diff --git a/src/backing.h b/src/backing.h new file mode 100644 index 0000000..97eaa9b --- /dev/null +++ b/src/backing.h @@ -0,0 +1,9 @@ +#pragma once + +#include "utils.h" + +bool IsBackingIdentifierValid(const char* backing_identifier); +int GetBackingIndex(const char* backing_identifier); + +Status GetBackingPoolPath(char** _backing_pool_path); +Status GetBackingDiskPath(const char* backing_identifier, char** _backing_path);