From a149a838d16fd4d54d7e3d21f2a8368aadefb1ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexe=C3=AF=20KADIR?= Date: Sun, 18 Feb 2024 13:20:30 +0100 Subject: [PATCH] Implemented entry resetting --- src/entry.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/entry.c b/src/entry.c index 1126f60..3b62eae 100644 --- a/src/entry.c +++ b/src/entry.c @@ -200,11 +200,13 @@ Status ListEntries(char*** _entries) { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; + // Check if the entry exists (this checks for validity) bool exists; Status status = DoesEntryExist(entry->d_name, &exists); if (status != SUCCESS || !exists) continue; + // Allocate memory for the new entry list char** new_entries = realloc(*_entries, (count + 2) * sizeof(char*)); // +2 for the new entry and the NULL terminator if (new_entries == NULL) { Log(LOG_LEVEL_ERROR, "Failed to reallocate memory for the entries list (%s).", strerror(errno)); @@ -351,6 +353,60 @@ Status RemoveEntryDisk(const char* entry_identifier) { return SUCCESS; } +Status ResetEntryDisk(const char* entry_identifier, const char* backing_identifier) { + // Check if the disk exists + bool exists; + Status status = DoesEntryDiskExist(entry_identifier, &exists); + if (status != SUCCESS) + return status; + + if (!exists) { + Log(LOG_LEVEL_ERROR, "The disk for the entry '%s' does not exist.", entry_identifier); + return FAILURE; + } + + // Get the backing disk path + char* backing_disk_path = NULL; + if (backing_identifier != NULL) { + status = GetBackingDiskPath(backing_identifier, &backing_disk_path); + if (status != SUCCESS) + return status; + } + + // Get the disk path + char* entry_disk_path = NULL; + status = GetEntryDiskPath(entry_identifier, &entry_disk_path); + if (status != SUCCESS) { + free(backing_disk_path); + return status; + } + + // Get the disk info + DiskInfo disk_info; + status = GetDiskInfo(entry_disk_path, &disk_info); + if (status != SUCCESS) { + free(backing_disk_path); + free(entry_disk_path); + return status; + } + + // If no backing disk is specified, use the current backing disk + if (backing_disk_path == NULL) + backing_disk_path = disk_info.backing_file; + + // Reset the disk + if (backing_disk_path != NULL) + status = CreateBackedDisk(entry_disk_path, backing_disk_path); + else + status = CreateRootDisk(entry_disk_path, disk_info.size); + + FreeDiskInfo(&disk_info); + free(backing_disk_path); + free(entry_disk_path); + + return status; +} + Status TrimEntryDisk(const char* entry_identifier) { // Check if the disk exists bool exists;