Files
linuxinstall/src/backing.c

71 lines
1.8 KiB
C
Raw Normal View History

#include "backing.h"
#include "disk.h"
#include "entry.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <unistd.h>
#include <libgen.h>
#include <sys/stat.h>
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;
}