Added resetting and trim to the entries

This commit is contained in:
Alexei KADIR 2024-02-17 02:46:21 +01:00
parent 799a362074
commit 5073ba3834
5 changed files with 80 additions and 9 deletions

View File

@ -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) {

View File

@ -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.

View File

@ -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;
}

View File

@ -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.

View File

@ -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;