Started reimplementing some functions for the backing system

This commit is contained in:
2024-02-15 20:00:24 +01:00
parent 01ea511b2d
commit 2a706fb01f
3 changed files with 115 additions and 4 deletions

74
src/backing.c Normal file
View File

@@ -0,0 +1,74 @@
#include "backing.h"
#include <string.h>
#include <sys/stat.h>
bool is_valid_backing_name(const char* name) {
if (name == NULL)
return false;
size_t len = strlen(name);
if (len == 0)
return false;
for (size_t i = 0; i < len; i++)
if (name[i] == '/')
return false;
if (name[0] < '0' || name[0] > '9')
return false;
return true;
}
char* get_backing_path(const char* name) {
if (name == NULL)
return NULL;
return format("%s/%s", "/var/lib/sandbox/backings", name);
}
bool backing_exists(const char* name) {
if (name == NULL)
return false;
char* path = get_backing_path(name);
if (path == NULL)
return false;
// Check if the backing exists
struct stat st;
if (stat(path, &st) != 0) {
free(path);
return false;
}
// Free the path, as it is no longer needed
free(path);
if (!S_ISREG(st.st_mode))
return false;
return true;
}
time_t get_backing_ctime(const char* name) {
// As the name starts with the ctime, we can just convert the first part of the name to a time_t
size_t len = strlen(name);
size_t ctime_len = 0;
for (size_t i = 0; i < len; i++) {
if (name[i] < '0' || name[i] > '9')
break;
ctime_len++;
}
char* ctime_str = strndup(name, ctime_len);
if (ctime_str == NULL)
return 0;
time_t ctime = (time_t)strtoull(ctime_str, NULL, 10);
free(ctime_str);
return ctime;
}