Started implementing backing managment

This commit is contained in:
Alexei KADIR 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;
}

27
src/backing.h Normal file
View File

@ -0,0 +1,27 @@
#pragma once
#include "utils.h"
#include "disk.h"
#include <stdbool.h>
#include <stdint.h>
#define BACKING_POOL_DIR "/var/lib/sandbox/backings"
#define MAX_BACKING_LENGTH 256
/// @brief Checks whether the given backing id is valid.
/// @param backing_id The backing id to check.
/// @return True if the backing id is valid, false otherwise.
bool is_valid_backing_id(const char* backing_id);
/// @brief Gets the path of the given backing.
/// @param backing_id The backing id.
/// @param out_path The pointer to the output path string. The caller is responsible for freeing the memory.
/// @return The result of the operation.
Result get_backing_path(const char* backing_id, char** out_path);
/// @brief Checks whether the given backing exists in the pool.
/// @param backing_id The backing id.
/// @param out_exists The pointer to the output boolean.
/// @return The result of the operation.
Result backing_exists(const char* backing_id, bool* out_exists);

View File

@ -304,6 +304,27 @@ Result list_entries(char*** out_entries) {
return SUCCESS; return SUCCESS;
} }
Result clear_entries(void) {
char** entries;
Result result = list_entries(&entries);
if (result != SUCCESS)
return result;
// Remove each entry
for (int i = 0; entries[i] != NULL; i++) {
result = remove_entry(entries[i]);
if (result != SUCCESS)
log_message(LOG_LEVEL_WARNING, "Failed to remove the entry '%s'.", entries[i]);
}
// Free the entries
for (int i = 0; entries[i] != NULL; i++)
free(entries[i]);
free(entries);
return SUCCESS;
}
Result reset_entry(const char* entry_id) { Result reset_entry(const char* entry_id) {
// Check that it exists // Check that it exists
bool exists; bool exists;

View File

@ -9,6 +9,10 @@
#define ENTRY_POOL_DIR "/var/lib/sandbox/entries" #define ENTRY_POOL_DIR "/var/lib/sandbox/entries"
#define MAX_ENTRY_LENGTH 256 #define MAX_ENTRY_LENGTH 256
#define ENTRY_TYPE_ROOT_STRING "ROOT"
#define ENTRY_TYPE_BACKED_STRING "BACKED"
#define ENTRY_TYPE_AUTOMATIC_STRING "AUTOMATIC"
typedef enum { typedef enum {
ENTRY_TYPE_UNKNOWN, ENTRY_TYPE_UNKNOWN,
ENTRY_TYPE_ROOT, ENTRY_TYPE_ROOT,
@ -16,10 +20,6 @@ typedef enum {
ENTRY_TYPE_AUTOMATIC ENTRY_TYPE_AUTOMATIC
} EntryType; } EntryType;
#define ENTRY_TYPE_ROOT_STRING "ROOT"
#define ENTRY_TYPE_BACKED_STRING "BACKED"
#define ENTRY_TYPE_AUTOMATIC_STRING "AUTOMATIC"
typedef struct { typedef struct {
EntryType type; EntryType type;
char* backing_id; char* backing_id;