Added some more signatures and implemented more functions

This commit is contained in:
Alexei KADIR 2024-02-15 20:58:50 +01:00
parent 2a706fb01f
commit 96b522d521
3 changed files with 132 additions and 2 deletions

View File

@ -1,6 +1,12 @@
#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) {
@ -25,7 +31,7 @@ char* get_backing_path(const char* name) {
if (name == NULL)
return NULL;
return format("%s/%s", "/var/lib/sandbox/backings", name);
return format("%s/%s", "/var/lib/sandbox/backings", name); // TODO: Use a setting for the backings directory
}
bool backing_exists(const char* name) {
@ -52,6 +58,57 @@ bool backing_exists(const char* name) {
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);
@ -68,7 +125,39 @@ time_t get_backing_ctime(const char* name) {
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;
}

27
src/entry.h Normal file
View File

@ -0,0 +1,27 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
bool is_valid_entry_name(const char* name);
char* get_entry_path(const char* name);
bool entry_exists(const char* name);
char* get_entry_disk_path(const char* name);
char** list_entries(void);
time_t get_entry_mtime(const char* name);
char* find_oldest_entry(void);
bool create_entry(const char* name);
bool delete_entry(const char* name);
uint64_t get_available_size(void);
void reserve_space(uint64_t size);

View File

@ -2,12 +2,26 @@
#include "utils.h"
#include "disk.h"
#include "backing.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
char** backings = list_backings();
if (backings == NULL) {
logmsg(LOG_ERROR, "Failed to list backings.");
return 1;
}
for (size_t i = 0; backings[i] != NULL; i++) {
printf("%s\n", backings[i]);
free(backings[i]);
}
free(backings);
return 0;
}