From 332f5ccfd0b15416c67ee7c3bb2c694c2b83d23d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alexe=C3=AF=20KADIR?= <alexei.kadir@gmail.com>
Date: Fri, 16 Feb 2024 18:26:02 +0100
Subject: [PATCH] Made some minor modifications

---
 src/command.c | 21 ++++++++++-----------
 src/command.h |  9 +++------
 src/disk.c    | 12 +++---------
 src/sandbox.c |  6 ++++--
 src/utils.c   |  4 +++-
 5 files changed, 23 insertions(+), 29 deletions(-)

diff --git a/src/command.c b/src/command.c
index 35b9abc..3ce4f7a 100644
--- a/src/command.c
+++ b/src/command.c
@@ -14,6 +14,7 @@ Command COMMANDS[] = {
 	{CMD_LIST_ENTRIES, "list-entries", "", "Lists all entries in the pool", "TODO"},
 	{CMD_CLEAR_ENTRIES, "clear-entries", "", "Clears all entries from the pool", "TODO"},
 	{CMD_SEPARATOR},
+	{CMD_SYNC, "sync", "", "Synchronizes the backing pool with the server", "TODO"},
 	{CMD_ADD_BACKING, "add-backing", "<entry>", "Adds a backing disk to the pool from the given entry", "TODO"},
 	{CMD_REMOVE_BACKING, "remove-backing", "<backing>", "Removes a backing disk from the pool", "TODO"},
 	{CMD_LIST_BACKINGS, "list-backings", "[--tree|-t]", "Lists all backing disks in the pool", "TODO"},
@@ -53,6 +54,7 @@ Command* find_command(const char* name) {
 void show_help() {
 	printf("Usage: sandbox <command> [args]\n\n");
 	printf("Commands:\n");
+
 	for (size_t i = 0; i < sizeof(COMMANDS) / sizeof(COMMANDS[0]); i++) {
 		if (COMMANDS[i].id == CMD_SEPARATOR) {
 			printf("\n");
@@ -62,20 +64,11 @@ void show_help() {
 		printf("    %s %s\n", COMMANDS[i].name, COMMANDS[i].args);
 		printf("        %s\n", COMMANDS[i].description);
 	}
+
 	printf("\n");
 	printf("For more information about a specific command, use 'sandbox help <command>'.\n");
 }
 
-void show_command_help(const Command* command) {
-	printf("Usage: sandbox %s %s\n", command->name, command->args);
-	printf("   %s\n", command->description);
-
-	if (command->details != NULL) {
-		printf("\n");
-		printf("%s\n", command->details);
-	}
-}
-
 int cmd_help(int argc, char** argv) {
 	if (argc == 0) {
 		show_help();
@@ -88,7 +81,13 @@ int cmd_help(int argc, char** argv) {
 		return 1;
 	}
 
-	show_command_help(command);
+	printf("Usage: sandbox %s %s\n", command->name, command->args);
+	printf("   %s\n", command->description);
+
+	if (command->details != NULL) {
+		printf("\n");
+		printf("%s\n", command->details);
+	}
 	return 0;
 }
 
diff --git a/src/command.h b/src/command.h
index 7f83c74..048606b 100644
--- a/src/command.h
+++ b/src/command.h
@@ -4,7 +4,7 @@
 
 typedef enum {
 	CMD_UNKNOWN,
-    CMD_SEPARATOR,
+	CMD_SEPARATOR,
 
 	CMD_HELP,
 	CMD_VERSION,
@@ -14,6 +14,7 @@ typedef enum {
 	CMD_LIST_ENTRIES,
 	CMD_CLEAR_ENTRIES,
 
+	CMD_SYNC,
 	CMD_ADD_BACKING,
 	CMD_REMOVE_BACKING,
 	CMD_LIST_BACKINGS,
@@ -30,7 +31,7 @@ typedef struct {
 	const char* name;
 	const char* args;
 	const char* description;
-    const char* details;
+	const char* details;
 } Command;
 
 extern Command COMMANDS[];
@@ -43,10 +44,6 @@ Command* find_command(const char* name);
 /// @brief Shows the help for all commands.
 void show_help();
 
-/// @brief Shows the help for a specific command.
-/// @param command The command to show the help for.
-void show_command_help(const Command* command);
-
 int cmd_help(int argc, char** argv);
 
 int cmd_version(int argc, char** argv);
\ No newline at end of file
diff --git a/src/disk.c b/src/disk.c
index ece9114..a811c4d 100644
--- a/src/disk.c
+++ b/src/disk.c
@@ -15,10 +15,8 @@ bool create_empty_disk(const char* path, uint64_t size) {
 
 	// Convert the size to a string
 	char* size_str = format("%lu", size);
-	if (size_str == NULL) {
-		log_message(LOG_ERROR, "Failed to allocate memory for the size string.");
+	if (size_str == NULL)
 		return false;
-	}
 
 	// Create the disk
 	int ret = execute(NULL, &errb, "qemu-img", "create", "-f", "qcow2", path, size_str, NULL);
@@ -71,10 +69,8 @@ bool create_backed_disk(const char* path, const char* backing_disk) {
 
 bool trim_disk(const char* path) {
 	char* tmp_path = format("%s.tmp", path);
-	if (tmp_path == NULL) {
-		log_message(LOG_ERROR, "Failed to allocate memory for the temporary disk path.");
+	if (tmp_path == NULL)
 		return false;
-	}
 
 	DiskInfo info;
 	if (!get_disk_info(path, &info)) {
@@ -90,10 +86,8 @@ bool trim_disk(const char* path) {
 	if (info.backing_file != NULL) {
 		char* backing_str = format("backing_file=%s", info.backing_file);
 		if (backing_str == NULL) {
-			log_message(LOG_ERROR, "Failed to allocate memory for the backing file string.");
-
-			free_disk_info(&info);
 			free(tmp_path);
+			free_disk_info(&info);
 			return false;
 		}
 
diff --git a/src/sandbox.c b/src/sandbox.c
index 6dfe6c7..41b2aac 100644
--- a/src/sandbox.c
+++ b/src/sandbox.c
@@ -20,11 +20,13 @@ int main(int argc, char* argv[]) {
 		return 1;
 	}
 
+	int consumed = 2;
+
 	switch (command->id) {
 	case CMD_HELP:
-		return cmd_help(argc - 2, argv + 2);
+		return cmd_help(argc - consumed, argv + consumed);
 	case CMD_VERSION:
-		return cmd_version(argc - 2, argv + 2);
+		return cmd_version(argc - consumed, argv + consumed);
 
 	default:
 		log_message(LOG_ERROR, "Command '%s' is not implemented.", command->name);
diff --git a/src/utils.c b/src/utils.c
index 27950f4..46c170e 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -23,8 +23,10 @@ char* format(const char* fmt, ...) {
 
 	va_end(args);
 
-	if (length <= 0)
+	if (length <= 0) {
+		log_message(LOG_ERROR, "Failed to calculate the length of the formatted string.");
 		return NULL;
+	}
 
 	// Allocate a buffer for the formatted string
 	char* buffer = calloc(length, sizeof(char));