diff --git a/src/disk.c b/src/disk.c index 06762e6..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; @@ -215,29 +233,36 @@ Result get_disk_info(const char* path, DiskInfo* out_info) { // Get the information about the disk int exit_code; char* stdout_buffer = NULL; - Result result = run_executable(&exit_code, &stdout_buffer, NULL, "qemu-img", "info", "--output", "json", path, NULL); + char* stderr_buffer = NULL; + Result result = run_executable(&exit_code, &stdout_buffer, &stderr_buffer, "qemu-img", "info", "--output", "json", path, NULL); + if (result != SUCCESS) { free(stdout_buffer); + free(stderr_buffer); return result; } // Check the exit code if (exit_code != EXIT_SUCCESS) { - if (stdout_buffer == NULL) - log_message(LOG_LEVEL_ERROR, "Failed to get information about the disk '%s'.", path); + if (stderr_buffer == NULL) + log_message(LOG_LEVEL_ERROR, "Failed to get the information of the disk '%s'.", path); else { // Remove newlines from the error message - for (size_t i = 0; i < strlen(stdout_buffer); i++) - if (stdout_buffer[i] == '\n') - stdout_buffer[i] = ' '; + for (size_t i = 0; i < strlen(stderr_buffer); i++) + if (stderr_buffer[i] == '\n') + stderr_buffer[i] = ' '; - log_message(LOG_LEVEL_ERROR, "Failed to get information about the disk '%s' (%s).", path, stdout_buffer); - free(stdout_buffer); + log_message(LOG_LEVEL_ERROR, "Failed to get the information of the disk '%s' (%s).", path, stderr_buffer); + free(stderr_buffer); } + free(stdout_buffer); return FAILURE; } + // Free the stderr buffer, as it is no longer needed + free(stderr_buffer); + // Parse the JSON output json_object* root = json_tokener_parse(stdout_buffer); if (root == 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 8a9ab01..55cc152 100644 --- a/src/entry.c +++ b/src/entry.c @@ -165,3 +165,35 @@ Result get_entry_disk_path(const char* entry_id, char** out_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; +} diff --git a/src/entry.h b/src/entry.h index 85d31ec..c9314c9 100644 --- a/src/entry.h +++ b/src/entry.h @@ -57,4 +57,14 @@ Result clear_entries(void); /// @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. +/// @param entry_id The entry id. +/// @return The result of the operation. +Result trim_entry_disk(const char* entry_id); + +/// @brief Resets the disk of the given entry to its original state. +/// @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 diff --git a/src/sandbox.c b/src/sandbox.c index 04a30ab..7ee5e84 100644 --- a/src/sandbox.c +++ b/src/sandbox.c @@ -127,7 +127,6 @@ int command_add_backing(int argc, char* argv[]) { return EXIT_FAILURE; } - trim_disk("test2"); // TODO: Call add_backing return EXIT_SUCCESS;