Started implementing backing managment

This commit is contained in:
2024-02-17 13:01:40 +01:00
parent 36a1faf948
commit fd7b056457
4 changed files with 115 additions and 4 deletions

63
src/backing.c Normal file
View File

@@ -0,0 +1,63 @@
#include "backing.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
bool is_valid_backing_id(const char* backing_id) {
if (backing_id == NULL)
return false;
size_t length = strlen(backing_id);
// Check that the length is valid
if (length == 0 || length > MAX_BACKING_LENGTH)
return false;
// Check that the backing id does not contain any slashes
for (size_t i = 0; i < length; i++)
if (backing_id[i] == '/')
return false;
// Check that the backing id is not a reserved name
if (strcmp(backing_id, ".") == 0 || strcmp(backing_id, "..") == 0)
return false;
return true;
}
Result get_backing_path(const char* backing_id, char** out_path) {
*out_path = NULL;
// Check that the backing id is valid
if (!is_valid_backing_id(backing_id)) {
log_message(LOG_LEVEL_ERROR, "Invalid backing id '%s'.", backing_id);
return FAILURE;
}
return format(out_path, "%s/%s", BACKING_POOL_DIR, backing_id);
}
Result backing_exists(const char* backing_id, bool* out_exists) {
*out_exists = false;
// Get the path of the backing
char* path;
Result result = get_entry_path(backing_id, &path);
if (result != SUCCESS)
return result;
// Check if the backing exists and is a file
struct stat st;
bool exists = stat(path, &st) == 0 && S_ISREG(st.st_mode);
// Free the path
free(path);
// Output the result
*out_exists = exists;
return SUCCESS;
}