2024-02-18 00:11:12 +01:00
|
|
|
#include "backing.h"
|
|
|
|
|
2024-02-18 00:28:42 +01:00
|
|
|
#include "disk.h"
|
|
|
|
#include "entry.h"
|
|
|
|
|
2024-02-18 00:11:12 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2024-02-18 00:28:42 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <sys/stat.h>
|
2024-02-18 00:11:12 +01:00
|
|
|
|
|
|
|
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
|
2024-02-18 00:28:42 +01:00
|
|
|
if (backing_identifier[0] < '0' || backing_identifier[0] > '9')
|
2024-02-18 00:11:12 +01:00
|
|
|
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;
|
|
|
|
}
|