Started writing the management program.
This commit is contained in:
66
src/backing.c
Normal file
66
src/backing.c
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "backing.h"
|
||||
|
||||
#include "utils.h"
|
||||
#include "sandbox.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
char* get_backings_path(void) {
|
||||
return format("%s/%s", MASTER_DIRECTORY, BACKINGS_DIRECTORY);
|
||||
}
|
||||
|
||||
bool is_valid_backing_name(const char* name) {
|
||||
if (name == NULL)
|
||||
return false;
|
||||
|
||||
// Check that the name is not empty
|
||||
size_t length = strlen(name);
|
||||
if (length == 0)
|
||||
return false;
|
||||
|
||||
// Check that the name is a number (the number is the timestamp of the backing disk creation)
|
||||
for (size_t i = 0; i < length; i++)
|
||||
if (name[i] < '0' || name[i] > '9')
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
char* get_backing_path(const char* backing) {
|
||||
char* backings_path = get_backings_path();
|
||||
if (backings_path == NULL)
|
||||
return NULL;
|
||||
|
||||
// Combine the backings path with the backing name
|
||||
char* backing_path = format("%s/%s", backings_path, backing);
|
||||
|
||||
// Free the backings path
|
||||
free(backings_path);
|
||||
|
||||
return backing_path;
|
||||
}
|
||||
|
||||
bool backing_exists(const char* backing) {
|
||||
char* backing_path = get_backing_path(backing);
|
||||
if (backing_path == NULL)
|
||||
return false;
|
||||
|
||||
// Check if the backing path exists
|
||||
struct stat statbuf;
|
||||
int result = stat(backing_path, &statbuf);
|
||||
|
||||
// Free the backing path
|
||||
free(backing_path);
|
||||
|
||||
if (result != 0)
|
||||
return false;
|
||||
|
||||
if (!S_ISREG(statbuf.st_mode))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
Reference in New Issue
Block a user