From e38e07ca68348fae4e7abf56b218b3bcd93cd7e5 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:00:17 +0100
Subject: [PATCH] Added some signatures to reprensent automatic entries

---
 src/entry.c   | 135 +-------------------------------------------------
 src/entry.h   |  37 +++++++-------
 src/sandbox.c |  46 ++++++++++++++++-
 src/sandbox.h |   4 ++
 4 files changed, 67 insertions(+), 155 deletions(-)

diff --git a/src/entry.c b/src/entry.c
index 55cc152..469d6ac 100644
--- a/src/entry.c
+++ b/src/entry.c
@@ -63,137 +63,4 @@ Result entry_exists(const char* entry_id, bool* out_exists) {
 	*out_exists = exists;
 
 	return SUCCESS;
-}
-
-Result add_root_entry(const char* entry_id, uint64_t size) {
-	// Check that the entry does not exist
-	bool exists;
-	Result result = entry_exists(entry_id, &exists);
-	if (result != SUCCESS)
-		return result;
-
-	if (exists) {
-		log_message(LOG_LEVEL_ERROR, "Entry '%s' already exists.", entry_id);
-		return FAILURE;
-	}
-
-	// Get the path of the entry
-	char* path;
-	result = get_entry_path(entry_id, &path);
-	if (result != SUCCESS)
-		return result;
-
-	// Create the directory
-	int return_code = mkdir(path, 0755);
-
-	// Free the path as it is no longer needed
-	free(path);
-
-	// Check if the directory was created
-	if (return_code != 0) {
-		log_message(LOG_LEVEL_ERROR, "Failed to create the entry '%s' (%s).", entry_id, strerror(errno));
-		return FAILURE;
-	}
-
-	// Get the disk path
-	char* disk_path;
-	result = get_entry_disk_path(entry_id, &disk_path);
-	if (result != SUCCESS) {
-		remove_entry(entry_id);
-		return result;
-	}
-
-	// Create the disk
-	result = create_root_disk(disk_path, size, 0644);
-
-	// Free the disk path as it is no longer needed
-	free(disk_path);
-
-	// Remove the entry if the disk could not be created
-	if (result != SUCCESS) {
-		remove_entry(entry_id);
-		return result;
-	}
-
-	return SUCCESS;
-}
-
-Result remove_entry(const char* entry_id) {
-	// Remove the disk
-	char* disk_path;
-	Result result = get_entry_disk_path(entry_id, &disk_path);
-	if (result == SUCCESS) {
-		unlink(disk_path);
-		free(disk_path);
-	}
-
-	// Get the path of the entry
-	char* path;
-	result = get_entry_path(entry_id, &path);
-	if (result != SUCCESS)
-		return result;
-
-	// Remove the directory
-	int return_code = rmdir(path);
-
-	// Free the path as it is no longer needed
-	free(path);
-
-	// Check if the directory was removed
-	if (return_code != 0) {
-		log_message(LOG_LEVEL_ERROR, "Failed to remove the entry '%s' (%s).", entry_id, strerror(errno));
-		return FAILURE;
-	}
-
-	return SUCCESS;
-}
-
-Result get_entry_disk_path(const char* entry_id, char** out_path) {
-	*out_path = NULL;
-
-	// Get the path of the entry
-	char* path;
-	Result result = get_entry_path(entry_id, &path);
-	if (result != SUCCESS)
-		return result;
-
-	// Format the disk path
-	result = format(out_path, "%s/disk", path);
-
-	// Free the path as it is no longer needed
-	free(path);
-
-	return result;
-}
-
-Result trim_entry_disk(const char* entry_id) {
-	// Get the disk path
-	char* disk_path;
-	Result result = get_entry_disk_path(entry_id, &disk_path);
-	if (result != SUCCESS)
-		return result;
-
-	// Trim the disk
-	result = trim_disk(disk_path);
-
-	// Free the disk path as it is no longer needed
-	free(disk_path);
-
-	return result;
-}
-
-Result reset_entry_disk(const char* entry_id) {
-	// Get the disk path
-	char* disk_path;
-	Result 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 as it is no longer needed
-	free(disk_path);
-
-	return result;
-}
+}
\ No newline at end of file
diff --git a/src/entry.h b/src/entry.h
index c9314c9..1028e68 100644
--- a/src/entry.h
+++ b/src/entry.h
@@ -19,52 +19,49 @@ bool is_entry_id_valid(const char* entry_id);
 /// @return The result of the operation.
 Result get_entry_path(const char* entry_id, char** out_path);
 
-/// @brief Checks whether the given entry exists.
+/// @brief Checks whether the given entry exists in the pool.
 /// @param entry_id The entry id.
 /// @param out_exists The pointer to the output boolean.
 /// @return The result of the operation.
 Result entry_exists(const char* entry_id, bool* out_exists);
 
-/// @brief Adds a root entry to the entry pool.
+/// @brief Adds a root entry to the pool.
 /// @param entry_id The entry id.
-/// @param size The size of the entry, in bytes.
+/// @param size The size of the entry.
 /// @return The result of the operation.
 Result add_root_entry(const char* entry_id, uint64_t size);
 
-/// @brief Adds a backed entry to the entry pool.
+/// @brief Adds a backed entry to the pool.
 /// @param entry_id The entry id.
 /// @param backing_id The backing id.
 /// @return The result of the operation.
 Result add_backed_entry(const char* entry_id, const char* backing_id);
 
-/// @brief Removes an entry from the entry pool.
+/// @brief Adds an automatic entry to the pool. Automatically selects the latest backing disk available.
+/// @param entry_id The entry id.
+/// @return The result of the operation.
+Result add_automatic_entry(const char* entry_id);
+
+/// @brief Removes the given entry from the pool.
 /// @param entry_id The entry id.
 /// @return The result of the operation.
 Result remove_entry(const char* entry_id);
 
-/// @brief Lists the entries in the entry pool.
-/// @param out_entries The pointer to the null-terminated output entries array. The caller is responsible for freeing the memory of the array and its elements.
+/// @brief Lists the entries in the pool.
+/// @param out_entries The pointer to the output entries string. The caller is responsible for freeing the memory of the strings and the array.
 /// @return The result of the operation.
 Result list_entries(char*** out_entries);
 
-/// @brief Removes all the entries from the entry pool.
+/// @brief Clears all the entries from the pool.
 /// @return The result of the operation.
 Result clear_entries(void);
 
-/// @brief Gets the path of the disk of the given entry.
-/// @param entry_id The entry id.
-/// @param out_path The pointer to the output path string. The caller is responsible for freeing the memory.
-/// @return The result of the operation.
-Result get_entry_disk_path(const char* entry_id, char** out_path);
-
-/// @brief Trims the disk of the given entry to remove any unused space.
+/// @brief Resets an entry to its initial state. Automatic entries will be backed by the latest backing disk available.
 /// @param entry_id The entry id.
 /// @return The result of the operation.
-Result trim_entry_disk(const char* entry_id);
+Result reset_entry(const char* entry_id);
 
-/// @brief Resets the disk of the given entry to its original state.
+/// @brief Updates the backing disk of the given entry if it is an automatic entry.
 /// @param entry_id The entry id.
 /// @return The result of the operation.
-Result reset_entry_disk(const char* entry_id);
-
-// TODO: Implement a function to reserve disk space to avoid running out of space while the sandbox is running.
\ No newline at end of file
+Result update_entry(const char* entry_id);
\ No newline at end of file
diff --git a/src/sandbox.c b/src/sandbox.c
index 7ee5e84..b6defbf 100644
--- a/src/sandbox.c
+++ b/src/sandbox.c
@@ -17,6 +17,14 @@ const Command COMMANDS[] = {
 	{},
 	{command_add_entry, "add-entry", "<entry id> [--root|-r <size>] [--backed|-b <backing id>]", "Adds an entry to the entry pool.",
 	 "Adds an entry to the entry pool with the given id. The entry can either be a root entry or a backed entry. If the entry is a root entry, the size of the entry must be specified. If the entry is a backed entry, the backing id must be specified. By default, the entry is backed by the latest backing disk available."},
+	{command_remove_entry, "remove-entry", "<entry id>", "Removes an entry from the entry pool.",
+	 "Removes the entry with the given id from the entry pool."},
+	{command_list_entries, "list-entries", "", "Lists the entries in the entry pool.",
+	 "Lists the entries in the entry pool."},
+	{command_clear_entries, "clear-entries", "", "Removes all the entries from the entry pool.",
+	 "Removes all the entries from the entry pool."},
+	{command_reset_entry, "reset-entry", "<entry id>", "Resets an entry to its initial state.",
+	 "Resets the entry with the given id to its initial state. Automatic entries will be backed by the latest backing disk available."},
 	{},
 	{command_add_backing, "add-backing", "<entry id> [<backing id>]", "Adds a backing disk to the backing pool, from the given entry.",
 	 "TODO"},
@@ -68,7 +76,7 @@ int command_help(int argc, char* argv[]) {
 			if (COMMANDS[i].name == NULL)
 				printf("\n");
 			else
-				fprintf(stdout, "	%s %s	- %s\n", COMMANDS[i].name, COMMANDS[i].arguments, COMMANDS[i].description);
+				fprintf(stdout, "	%s %s - %s\n", COMMANDS[i].name, COMMANDS[i].arguments, COMMANDS[i].description);
 
 		fprintf(stdout, "\nRun 'sandbox help [command]' for more information on a command.\n");
 		return EXIT_SUCCESS;
@@ -121,6 +129,42 @@ int command_add_entry(int argc, char* argv[]) {
 	return EXIT_SUCCESS;
 }
 
+int command_remove_entry(int argc, char* argv[]) {
+	if (argc < 1) {
+		log_message(LOG_LEVEL_ERROR, "Missing entry id.");
+		return EXIT_FAILURE;
+	}
+
+	// TODO: Call remove_entry
+
+	return EXIT_SUCCESS;
+}
+
+int command_list_entries(int argc, char* argv[]) {
+	// TODO: Call list_entries, and display the result in a nice way
+
+	return EXIT_SUCCESS;
+}
+
+int command_clear_entries(int argc, char* argv[]) {
+	// TODO: Ask for confirmation
+
+	// TODO: Call clear_entries
+
+	return EXIT_SUCCESS;
+}
+
+int command_reset_entry(int argc, char* argv[]) {
+	if (argc < 1) {
+		log_message(LOG_LEVEL_ERROR, "Missing entry id.");
+		return EXIT_FAILURE;
+	}
+
+	// TODO: Call reset_entry
+
+	return EXIT_SUCCESS;
+}
+
 int command_add_backing(int argc, char* argv[]) {
 	if (argc < 1) {
 		log_message(LOG_LEVEL_ERROR, "Missing entry id.");
diff --git a/src/sandbox.h b/src/sandbox.h
index 4fbc2e5..dc73912 100644
--- a/src/sandbox.h
+++ b/src/sandbox.h
@@ -18,5 +18,9 @@ int command_help(int argc, char* argv[]);
 int command_version(int argc, char* argv[]);
 
 int command_add_entry(int argc, char* argv[]);
+int command_remove_entry(int argc, char* argv[]);
+int command_list_entries(int argc, char* argv[]);
+int command_clear_entries(int argc, char* argv[]);
+int command_reset_entry(int argc, char* argv[]);
 
 int command_add_backing(int argc, char* argv[]);
\ No newline at end of file