Made some small adjustments

This commit is contained in:
2024-02-19 16:01:53 +01:00
parent 1896e41b8a
commit 5cf1b33178
17 changed files with 1055 additions and 1637 deletions

221
src/backing.c Executable file
View File

@@ -0,0 +1,221 @@
#include "backing.h"
#include "container.h"
#include "disk.h"
#include <md5.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
result_t check_backing_identifier(const char* backing) {
// Check that the identifier is not null
if (backing == NULL)
return failure("Backing identifier cannot be null.");
// Check that the identifier is the length of a md5 hash
size_t length = strlen(backing);
if (length != MD5_DIGEST_STRING_LENGTH - 1) // -1 because the string is null-terminated
return failure("Backing identifier must be %d characters long.", MD5_DIGEST_STRING_LENGTH - 1);
// Check that the identifier only contains allowed characters
for (size_t i = 0; i < length; i++) {
char c = backing[i];
if (c >= 'a' && c <= 'f')
continue;
if (c >= '0' && c <= '9')
continue;
return failure("Backing identifier cannot contain the character '%c' at index %d.", c, i);
}
return success();
}
result_t check_backing_exists(const char* backing) {
// Get the backing path
char* path;
result_t result = get_backing_path(&path, backing);
if (result != success())
return result;
// Check that the backing exists
struct stat st;
errno = 0;
if (stat(path, &st) != 0) {
if (errno == ENOENT)
result = failure("Backing '%s' does not exist.", backing);
else
result = failure("Failed to check if backing '%s' exists (%s).", backing, strerror(errno));
free(path);
return result;
}
// Check that the backing is a file
if (!S_ISREG(st.st_mode)) {
result = failure("Backing '%s' is not a regular file.", backing);
free(path);
return result;
}
// Free the backing path
free(path);
return success();
}
result_t get_backing_pool_path(char** _path) {
return format(_path, "/var/lib/sandbox/backings");
}
result_t get_backing_path(char** _path, const char* backing) {
// Check the backing identifier
result_t result = check_backing_identifier(backing);
if (result != success())
return result;
// Get the backing pool path
char* pool_path;
result = get_backing_pool_path(&pool_path);
if (result != success())
return result;
// Format the backing path
result = format(_path, "%s/%s", pool_path, backing);
free(pool_path);
return result;
}
result_t get_backing_default_path(char** _path) {
// Get the backing pool path
char* pool_path;
result_t result = get_backing_pool_path(&pool_path);
if (result != success())
return result;
// Format the backing path
result = format(_path, "%s/default", pool_path);
free(pool_path);
return result;
}
result_t remove_backing(const char* backing) {
// Check that the backing exists
result_t result = check_backing_exists(backing);
if (result != success())
return result;
// Get the backing path
char* path;
result = get_backing_path(&path, backing);
if (result != success())
return result;
// Remove the backing
errno = 0;
remove(path);
// Check for errors during the removal
if (errno != 0) {
free(path);
return failure("Failed to remove backing '%s' (%s).", backing, strerror(errno));
}
free(path);
return success();
}
bool backing_filter(const char* file) {
// Check that a backing with the same name exists
return check_backing_exists(file) == success();
}
result_t list_backings(char*** _backings) {
// Get the backing pool path
char* pool_path;
result_t result = get_backing_pool_path(&pool_path);
if (result != success())
return result;
// List the backings
result = list_files(_backings, pool_path, &backing_filter);
free(pool_path);
return result;
}
result_t get_default_backing(char** _backing) {
// Get the backing default path
char* path;
result_t result = get_backing_default_path(&path);
if (result != success())
return result;
char* backing;
// Read the default backing
result = read_file(&backing, path);
free(path);
if (result != success())
return failure("No default backing configured.");
// Check that the backing is valid
result = check_backing_identifier(backing);
if (result != success()) {
free(backing);
return failure("Default backing '%s' is not a valid backing identifier.", backing);
}
// Check that the backing exists
result = check_backing_exists(backing);
if (result != success()) {
free(backing);
return failure("Default backing '%s' does not exist.", backing);
}
*_backing = backing;
return result;
}
result_t set_default_backing(const char* backing) {
// Get the backing default path
char* path;
result_t result = get_backing_default_path(&path);
if (result != success())
return result;
// If the backing is null, remove the default backing file
if (backing == NULL) {
errno = 0;
remove(path);
free(path);
if (errno == ENOENT)
return success();
else if (errno != 0)
return failure("Failed to remove the default backing file (%s).", strerror(errno));
return success();
} else {
// Check that the backing exists
result = check_backing_exists(backing);
if (result != success()) {
free(path);
return result;
}
// Write the default backing
result = write_file(path, backing);
free(path);
return result;
}
}