Started implementing the backing system

This commit is contained in:
Alexei KADIR 2024-02-18 00:11:12 +01:00
parent 0aa2cfbfa8
commit 051b95484c
2 changed files with 71 additions and 0 deletions

62
src/backing.c Normal file
View File

@ -0,0 +1,62 @@
#include "backing.h"
#include <stdlib.h>
#include <string.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;
}

9
src/backing.h Normal file
View File

@ -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);