From 2d1f228ad0146f77261c06deaa90f9da3c2909de Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alexe=C3=AF=20KADIR?= <alexei.kadir@gmail.com>
Date: Sat, 17 Feb 2024 12:35:33 +0100
Subject: [PATCH] Added disk resetting

---
 src/disk.c  | 18 ++++++++++++++++++
 src/disk.h  |  5 +++++
 src/entry.c | 27 +++++++++++++++++++++++++++
 3 files changed, 50 insertions(+)

diff --git a/src/disk.c b/src/disk.c
index 72bb52b..b406b0c 100644
--- a/src/disk.c
+++ b/src/disk.c
@@ -206,6 +206,24 @@ Result rebase_disk(const char* path, const char* backing_disk) {
 	return SUCCESS;
 }
 
+Result reset_disk(const char* path) {
+	// Get the backing file of the disk
+	DiskInfo info;
+	Result result = get_disk_info(path, &info);
+	if (result != SUCCESS)
+		return result;
+
+	if (info.backing_file == NULL)
+		result = create_root_disk(path, info.virtual_size, 0644);
+	else
+		result = create_backed_disk(path, info.backing_file, 0644);
+
+	// Free the disk info as it is no longer needed
+	free_disk_info(&info);
+
+	return result;
+}
+
 Result get_disk_info(const char* path, DiskInfo* out_info) {
 	// Initialize the output
 	out_info->backing_file = NULL;
diff --git a/src/disk.h b/src/disk.h
index 62a9416..fdfde2a 100644
--- a/src/disk.h
+++ b/src/disk.h
@@ -43,6 +43,11 @@ Result trim_disk(const char* path);
 /// @return The result of the operation.
 Result rebase_disk(const char* path, const char* backing_disk);
 
+/// @brief Resets a disk to its original state.
+/// @param path The path to the disk to reset.
+/// @return The result of the operation.
+Result reset_disk(const char* path);
+
 /// @brief Gathers information about a disk.
 /// @param path The path to the disk to gather information about.
 /// @param out_info The information about the disk.
diff --git a/src/entry.c b/src/entry.c
index 0632169..7785016 100644
--- a/src/entry.c
+++ b/src/entry.c
@@ -230,6 +230,33 @@ Result remove_entry(const char* entry_id) {
 	return SUCCESS;
 }
 
+Result reset_entry(const char* entry_id) {
+	// Check that it exists
+	bool exists;
+	Result result = entry_exists(entry_id, &exists);
+	if (result != SUCCESS)
+		return result;
+
+	if (!exists) {
+		log_message(LOG_LEVEL_ERROR, "The entry '%s' does not exist.", entry_id);
+		return FAILURE;
+	}
+
+	// Get the path of the disk
+	char* disk_path;
+	result = get_entry_disk_path(entry_id, &disk_path);
+	if (result != SUCCESS)
+		return result;
+
+	// Reset the disk
+	result = reset_disk(disk_path);
+
+	// Free the disk path
+	free(disk_path);
+
+	return result;
+}
+
 Result get_entry_info(const char* entry_id, EntryInfo* out_info) {
 	out_info->backing_id = NULL;
 	out_info->type = ENTRY_TYPE_UNKNOWN;