From 96b522d5218cefe380213e68141980b507e8c765 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexe=C3=AF=20KADIR?= Date: Thu, 15 Feb 2024 20:58:50 +0100 Subject: [PATCH] Added some more signatures and implemented more functions --- src/backing.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/entry.h | 27 +++++++++++++++ src/sandbox.c | 16 ++++++++- 3 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 src/entry.h diff --git a/src/backing.c b/src/backing.c index d51781a..29240e6 100644 --- a/src/backing.c +++ b/src/backing.c @@ -1,6 +1,12 @@ #include "backing.h" +#include "utils.h" + +#include #include +#include +#include +#include #include 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; +} \ No newline at end of file diff --git a/src/entry.h b/src/entry.h new file mode 100644 index 0000000..1fce9ea --- /dev/null +++ b/src/entry.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include +#include + +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); \ No newline at end of file diff --git a/src/sandbox.c b/src/sandbox.c index 03418e2..6ca111b 100644 --- a/src/sandbox.c +++ b/src/sandbox.c @@ -2,12 +2,26 @@ #include "utils.h" -#include "disk.h" +#include "backing.h" #include #include #include 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; } \ No newline at end of file