Discarded some code that does not work well with the needs
This commit is contained in:
163
src/backing.c
163
src/backing.c
@@ -1,163 +0,0 @@
|
||||
#include "backing.h"
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <dirent.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); // TODO: Use a setting for the backings directory
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
char** list_backings(void) {
|
||||
// Open the backings directory
|
||||
DIR* dir = opendir("/var/lib/sandbox/backings"); // TODO: Use a setting for the backings directory
|
||||
if (dir == NULL)
|
||||
return NULL;
|
||||
|
||||
// Count the number of backings
|
||||
size_t count = 0;
|
||||
struct dirent* entry;
|
||||
while ((entry = readdir(dir)) != NULL) {
|
||||
if (is_valid_backing_name(entry->d_name))
|
||||
count++;
|
||||
}
|
||||
|
||||
// Allocate the array
|
||||
char** backings = calloc(count + 1, sizeof(char*));
|
||||
if (backings == NULL) {
|
||||
closedir(dir);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Reset the directory stream
|
||||
rewinddir(dir);
|
||||
|
||||
// Fill the array
|
||||
size_t i = 0;
|
||||
while ((entry = readdir(dir)) != NULL) {
|
||||
if (is_valid_backing_name(entry->d_name)) {
|
||||
backings[i] = strdup(entry->d_name);
|
||||
if (backings[i] == NULL) {
|
||||
closedir(dir);
|
||||
|
||||
for (size_t j = 0; j < i; j++)
|
||||
free(backings[j]);
|
||||
free(backings);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// Close the directory
|
||||
closedir(dir);
|
||||
|
||||
// Terminate the array
|
||||
backings[count] = NULL;
|
||||
|
||||
return backings;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
char* get_latest_backing(void) {
|
||||
// Get the backings
|
||||
char** backings = list_backings();
|
||||
if (backings == NULL)
|
||||
return NULL;
|
||||
|
||||
// Find the latest backing
|
||||
time_t latest_ctime = 0;
|
||||
char* latest_backing = NULL;
|
||||
|
||||
for (size_t i = 0; backings[i] != NULL; i++) {
|
||||
time_t ctime = get_backing_ctime(backings[i]);
|
||||
|
||||
if (ctime >= latest_ctime) {
|
||||
latest_ctime = ctime;
|
||||
latest_backing = backings[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Duplicate the latest backing
|
||||
if (latest_backing != NULL)
|
||||
latest_backing = strdup(latest_backing);
|
||||
|
||||
// Free the backings
|
||||
for (size_t i = 0; backings[i] != NULL; i++)
|
||||
free(backings[i]);
|
||||
free(backings);
|
||||
|
||||
return latest_backing;
|
||||
}
|
Reference in New Issue
Block a user