diff --git a/src/backing.c b/src/backing.c deleted file mode 100644 index 96a58ad..0000000 --- a/src/backing.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "backing.h" - -#include "entry.h" - -#include -#include -#include -#include -#include diff --git a/src/backing.h b/src/backing.h deleted file mode 100644 index fbb8a1d..0000000 --- a/src/backing.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include "utils.h" -#include "disk.h" - -#include -#include - -#define BACKING_POOL_DIR "/var/lib/sandbox/backings" -#define MAX_BACKING_LENGTH 256 diff --git a/src/disk.c b/src/disk.c index 72bb52b..3464640 100644 --- a/src/disk.c +++ b/src/disk.c @@ -1,56 +1,44 @@ #include "disk.h" +#include #include #include #include #include #include -#include #include -Result create_root_disk(const char* path, uint64_t size, mode_t permissions) { +Status CreateRootDisk(const char* path, uint64_t disk_size) { // Convert the size to a string char* size_str; - Result result = format(&size_str, "%lu", size); - if (result != SUCCESS) - return result; + Status status = Format(&size_str, "%lu", disk_size); + if (status != SUCCESS) + return status; // Create the disk int exit_code; - char* stderr_buffer = NULL; - result = run_executable(&exit_code, NULL, &stderr_buffer, "qemu-img", "create", "-f", "qcow2", path, size_str, NULL); + // char* stdout; + char* stderrb; + + status = RunExecutable(&exit_code, NULL, &stderrb, "/usr/bin/qemu-img", "create", "-f", "qcow2", path, size_str, NULL); - // Free the size string free(size_str); - if (result != SUCCESS) { - free(stderr_buffer); - return result; - } + // Check if the disk was created successfully + if (status != SUCCESS) + return status; - // Check the exit code - if (exit_code != EXIT_SUCCESS) { - if (stderr_buffer == NULL) - log_message(LOG_LEVEL_ERROR, "Failed to create the disk '%s'.", path); - else { - // Remove newlines from the error message - for (size_t i = 0; i < strlen(stderr_buffer); i++) - if (stderr_buffer[i] == '\n') - stderr_buffer[i] = ' '; + if (exit_code != 0) { + if (stderrb != NULL) { + // Remove newlines from the stderr buffer + for (int i = 0; stderrb[i] != '\0'; i++) + if (stderrb[i] == '\n') + stderrb[i] = ' '; - log_message(LOG_LEVEL_ERROR, "Failed to create the disk '%s' (%s).", path, stderr_buffer); - free(stderr_buffer); - } - - return FAILURE; - } - - // Set the permissions of the disk - if (chmod(path, permissions) != 0) { - log_message(LOG_LEVEL_ERROR, "Failed to set the permissions of the disk '%s' (%s).", path, strerror(errno)); - - // Try to remove the disk if the permissions could not be set - unlink(path); + Log(LOG_LEVEL_ERROR, "Failed to create disk at '%s' (%s).", path, stderrb); + free(stderrb); + } else + Log(LOG_LEVEL_ERROR, "Failed to create disk"); return FAILURE; } @@ -58,40 +46,29 @@ Result create_root_disk(const char* path, uint64_t size, mode_t permissions) { return SUCCESS; } -Result create_backed_disk(const char* path, const char* backing_disk, mode_t permissions) { +Status CreateBackedDisk(const char* path, const char* backing_file) { // Create the disk int exit_code; - char* stderr_buffer = NULL; - Result result = run_executable(&exit_code, NULL, &stderr_buffer, "qemu-img", "create", "-f", "qcow2", "-F", "qcow2", "-b", backing_disk, path, NULL); + // char* stdout; + char* stderrb; - if (result != SUCCESS) { - free(stderr_buffer); - return result; - } + Status status = RunExecutable(&exit_code, NULL, &stderrb, "/usr/bin/qemu-img", "create", "-f", "qcow2", "-F", "qcow2", "-b", backing_file, path, NULL); - // Check the exit code - if (exit_code != EXIT_SUCCESS) { - if (stderr_buffer == NULL) - log_message(LOG_LEVEL_ERROR, "Failed to create the disk '%s'.", path); - else { - // Remove newlines from the error message - for (size_t i = 0; i < strlen(stderr_buffer); i++) - if (stderr_buffer[i] == '\n') - stderr_buffer[i] = ' '; + // Check if the disk was created successfully + if (status != SUCCESS) + return status; - log_message(LOG_LEVEL_ERROR, "Failed to create the disk '%s' (%s).", path, stderr_buffer); - free(stderr_buffer); - } + if (exit_code != 0) { + if (stderrb != NULL) { + // Remove newlines from the stderr buffer + for (int i = 0; stderrb[i] != '\0'; i++) + if (stderrb[i] == '\n') + stderrb[i] = ' '; - return FAILURE; - } - - // Set the permissions of the disk - if (chmod(path, permissions) != 0) { - log_message(LOG_LEVEL_ERROR, "Failed to set the permissions of the disk '%s' (%s).", path, strerror(errno)); - - // Try to remove the disk if the permissions could not be set - unlink(path); + Log(LOG_LEVEL_ERROR, "Failed to create disk at '%s' (%s).", path, stderrb); + free(stderrb); + } else + Log(LOG_LEVEL_ERROR, "Failed to create disk"); return FAILURE; } @@ -99,106 +76,101 @@ Result create_backed_disk(const char* path, const char* backing_disk, mode_t per return SUCCESS; } -Result trim_disk(const char* path) { - char* tmp_path; - Result result = format(&tmp_path, "%s.tmp", path); - if (result != SUCCESS) - return result; +Status TrimDisk(const char* path) { + char* tmp_path = NULL; + Status status = Format(&tmp_path, "%s.tmp", path); + if (status != SUCCESS) + return status; DiskInfo info; - result = get_disk_info(path, &info); - if (result != SUCCESS) { + status = GetDiskInfo(path, &info); + if (status != SUCCESS) { free(tmp_path); - return result; + return status; } - // Create the trimmed disk + // Create the temporary disk int exit_code; - char* stderr_buffer = NULL; + // char* stdout; + char* stderrb; - if (info.backing_file == NULL) { - // If the disk is not backed, we can simply convert it to a new disk - result = run_executable(&exit_code, NULL, &stderr_buffer, "qemu-img", "convert", "-f", "qcow2", "-O", "qcow2", path, tmp_path, NULL); - } else { - // If the disk is backed, we need to specify the backing file - char* backing_disk_arg; - result = format(&backing_disk_arg, "backing_file=%s", info.backing_file); - if (result != SUCCESS) { + if (info.backing_file != NULL) { + char* backing_file_opt = NULL; + status = Format(&backing_file_opt, "backing_file=%s", info.backing_file); + if (status != SUCCESS) { free(tmp_path); - free_disk_info(&info); - return result; + FreeDiskInfo(&info); + return status; } - result = run_executable(&exit_code, NULL, &stderr_buffer, "qemu-img", "convert", "-f", "qcow2", "-F", "qcow2", "-O", "qcow2", "-o", backing_disk_arg, path, tmp_path, NULL); + status = RunExecutable(&exit_code, NULL, &stderrb, "/usr/bin/qemu-img", "create", "-f", "qcow2", "-F", "qcow2", "-O", "qcow2", "-o", backing_file_opt, tmp_path, NULL); - free(backing_disk_arg); - } + free(backing_file_opt); + } else + status = RunExecutable(&exit_code, NULL, &stderrb, "/usr/bin/qemu-img", "create", "-f", "qcow2", "-O", "qcow2", tmp_path, NULL); - // Free the disk info as it is no longer needed - free_disk_info(&info); + FreeDiskInfo(&info); - if (result != SUCCESS) { + // Check if the disk was created successfully + if (status != SUCCESS) { free(tmp_path); - free(stderr_buffer); - return result; + return status; } - // Check the exit code - if (exit_code != EXIT_SUCCESS) { - if (stderr_buffer == NULL) - log_message(LOG_LEVEL_ERROR, "Failed to trim the disk '%s'.", path); - else { - // Remove newlines from the error message - for (size_t i = 0; i < strlen(stderr_buffer); i++) - if (stderr_buffer[i] == '\n') - stderr_buffer[i] = ' '; + if (exit_code != 0) { + if (stderrb != NULL) { + // Remove newlines from the stderr buffer + for (int i = 0; stderrb[i] != '\0'; i++) + if (stderrb[i] == '\n') + stderrb[i] = ' '; - log_message(LOG_LEVEL_ERROR, "Failed to trim the disk '%s' (%s).", path, stderr_buffer); - free(stderr_buffer); - } + Log(LOG_LEVEL_ERROR, "Failed to create temporary disk at '%s' (%s).", tmp_path, stderrb); + free(stderrb); + } else + Log(LOG_LEVEL_ERROR, "Failed to create temporary disk"); free(tmp_path); return FAILURE; } - // Replace the original disk with the trimmed disk + // Try to move the temporary disk to the original path if (rename(tmp_path, path) != 0) { - log_message(LOG_LEVEL_ERROR, "Failed to replace the disk '%s' with the trimmed disk '%s' (%s).", path, tmp_path, strerror(errno)); + Log(LOG_LEVEL_ERROR, "Failed to move the temporary disk to the original path '%s' (%s).", path, strerror(errno)); unlink(tmp_path); + free(tmp_path); return FAILURE; } - // Free the temporary path free(tmp_path); + return SUCCESS; } -Result rebase_disk(const char* path, const char* backing_disk) { - // Rebase the disk +Status RebaseDisk(const char* path, const char* backing_file) { + // Create the disk int exit_code; - char* stderr_buffer = NULL; - Result result = run_executable(&exit_code, NULL, &stderr_buffer, "qemu-img", "rebase", "-u", "-b", backing_disk, path, NULL); + // char* stdout; + char* stderrb; - if (result != SUCCESS) { - free(stderr_buffer); - return result; - } + Status status = RunExecutable(&exit_code, NULL, &stderrb, "/usr/bin/qemu-img", "rebase", "-u", "-b", backing_file, path, NULL); - // Check the exit code - if (exit_code != EXIT_SUCCESS) { - if (stderr_buffer == NULL) - log_message(LOG_LEVEL_ERROR, "Failed to rebase the disk '%s'.", path); - else { - // Remove newlines from the error message - for (size_t i = 0; i < strlen(stderr_buffer); i++) - if (stderr_buffer[i] == '\n') - stderr_buffer[i] = ' '; + // Check if the disk was created successfully + if (status != SUCCESS) + return status; - log_message(LOG_LEVEL_ERROR, "Failed to rebase the disk '%s' (%s).", path, stderr_buffer); - free(stderr_buffer); - } + if (exit_code != 0) { + if (stderrb != NULL) { + // Remove newlines from the stderr buffer + for (int i = 0; stderrb[i] != '\0'; i++) + if (stderrb[i] == '\n') + stderrb[i] = ' '; + + Log(LOG_LEVEL_ERROR, "Failed to rebase disk at '%s' (%s).", path, stderrb); + free(stderrb); + } else + Log(LOG_LEVEL_ERROR, "Failed to rebase disk"); return FAILURE; } @@ -206,88 +178,64 @@ Result rebase_disk(const char* path, const char* backing_disk) { return SUCCESS; } -Result get_disk_info(const char* path, DiskInfo* out_info) { - // Initialize the output - out_info->backing_file = NULL; - out_info->actual_size = 0; - out_info->virtual_size = 0; +Status GetDiskInfo(const char* path, DiskInfo* _info) { + *_info = (DiskInfo){0}; - // Get the information about the disk + // Get the disk info int exit_code; - char* stdout_buffer = NULL; - char* stderr_buffer = NULL; - Result result = run_executable(&exit_code, &stdout_buffer, &stderr_buffer, "qemu-img", "info", "--output", "json", path, NULL); + char* stdoutb; + char* stderrb; - if (result != SUCCESS) { - free(stdout_buffer); - free(stderr_buffer); - return result; - } + Status status = RunExecutable(&exit_code, &stdoutb, &stderrb, "/usr/bin/qemu-img", "info", "--output", "json", path, NULL); + if (status != SUCCESS) + return status; - // Check the exit code - if (exit_code != EXIT_SUCCESS) { - 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(stderr_buffer); i++) - if (stderr_buffer[i] == '\n') - stderr_buffer[i] = ' '; + if (exit_code != 0) { + if (stderrb != NULL) { + // Remove newlines from the stderr buffer + for (int i = 0; stderrb[i] != '\0'; i++) + if (stderrb[i] == '\n') + stderrb[i] = ' '; - log_message(LOG_LEVEL_ERROR, "Failed to get the information of the disk '%s' (%s).", path, stderr_buffer); - free(stderr_buffer); - } + Log(LOG_LEVEL_ERROR, "Failed to get disk info at '%s' (%s).", path, stderrb); + free(stderrb); + } else + Log(LOG_LEVEL_ERROR, "Failed to get disk info"); - free(stdout_buffer); + free(stdoutb); return FAILURE; } - - // Free the stderr buffer, as it is no longer needed - free(stderr_buffer); + free(stderrb); // Parse the JSON output - json_object* root = json_tokener_parse(stdout_buffer); + json_object* root = json_tokener_parse(stdoutb); + free(stdoutb); + if (root == NULL) { - log_message(LOG_LEVEL_ERROR, "Failed to parse the JSON output of 'qemu-img info'."); - free(stdout_buffer); + Log(LOG_LEVEL_ERROR, "Failed to parse the JSON output"); return FAILURE; } - // Free the stdout buffer, as it is no longer needed - free(stdout_buffer); + json_object* virtual_size = NULL; + json_object* actual_size = NULL; + json_object* backing_file = NULL; - // Get the virtual size - json_object* virtual_size_obj; - if (json_object_object_get_ex(root, "virtual-size", &virtual_size_obj)) - out_info->virtual_size = json_object_get_int64(virtual_size_obj); + json_object_object_get_ex(root, "virtual-size", &virtual_size); + json_object_object_get_ex(root, "actual-size", &actual_size); + json_object_object_get_ex(root, "backing-filename", &backing_file); - // Get the actual size - json_object* actual_size_obj; - if (json_object_object_get_ex(root, "actual-size", &actual_size_obj)) - out_info->actual_size = json_object_get_int64(actual_size_obj); + if (virtual_size != NULL) + _info->size = json_object_get_int64(virtual_size); + if (actual_size != NULL) + _info->allocated = json_object_get_int64(actual_size); + if (backing_file != NULL) + _info->backing_file = strdup(json_object_get_string(backing_file)); - // Get the backing file - json_object* backing_file_obj; - if (json_object_object_get_ex(root, "backing-filename", &backing_file_obj)) { - out_info->backing_file = strdup(json_object_get_string(backing_file_obj)); - - if (out_info->backing_file == NULL) { - log_message(LOG_LEVEL_ERROR, "Failed to allocate memory for the backing file path."); - json_object_put(root); - return OUT_OF_MEMORY; - } - } - - // Free the JSON object json_object_put(root); return SUCCESS; } -void free_disk_info(DiskInfo* info) { +void FreeDiskInfo(DiskInfo* info) { free(info->backing_file); - - info->backing_file = NULL; - info->actual_size = 0; - info->virtual_size = 0; } diff --git a/src/disk.h b/src/disk.h index 62a9416..2e51b55 100644 --- a/src/disk.h +++ b/src/disk.h @@ -2,53 +2,17 @@ #include "utils.h" -#include -#include -#include - -/// @brief The information about a disk. typedef struct { - /// @brief The virtual size of the disk. - uint64_t virtual_size; - - /// @brief The actual size of the disk. - uint64_t actual_size; - - /// @brief The path to the backing file of the disk. This is NULL if the disk is not backed. + uint64_t size; + uint64_t allocated; char* backing_file; } DiskInfo; -/// @brief Creates a root disk at the given path, with the given size and with the given permissions. -/// @param path The path to the disk to create. -/// @param size The size of the disk to create. -/// @param permissions The permissions to set on the disk. -/// @return The result of the operation. -Result create_root_disk(const char* path, uint64_t size, mode_t permissions); +Status CreateRootDisk(const char* path, uint64_t disk_size); +Status CreateBackedDisk(const char* path, const char* backing_file); -/// @brief Creates a backed disk at the given path, with the given backing disk and with the given permissions. -/// @param path The path to the disk to create. -/// @param backing_disk The disk to back the new disk with. -/// @param permissions The permissions to set on the disk. -/// @return The result of the operation. -Result create_backed_disk(const char* path, const char* backing_disk, mode_t permissions); +Status TrimDisk(const char* path); +Status RebaseDisk(const char* path, const char* backing_file); -/// @brief Trims a disk to remove any unused space. -/// @param path The path to the disk to trim. -/// @return The result of the operation. -Result trim_disk(const char* path); - -/// @brief Moves a disk's backing file to the given path. -/// @param path The path to the disk to move. -/// @param backing_disk The new path to the backing disk. -/// @return The result of the operation. -Result rebase_disk(const char* path, const char* backing_disk); - -/// @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. -/// @return The result of the operation. -Result get_disk_info(const char* path, DiskInfo* out_info); - -/// @brief Frees the resources used by the given disk information. -/// @param info The disk information to free. -void free_disk_info(DiskInfo* info); +Status GetDiskInfo(const char* path, DiskInfo* _info); +void FreeDiskInfo(DiskInfo* info); \ No newline at end of file diff --git a/src/entry.c b/src/entry.c index 347d92d..40faf9a 100644 --- a/src/entry.c +++ b/src/entry.c @@ -1,454 +1,337 @@ #include "entry.h" -#include "utils.h" #include "disk.h" #include #include #include -#include -#include #include +#include #include -bool is_entry_id_valid(const char* entry_id) { - if (entry_id == NULL) +bool IsEntryIdentifierValid(const char* entry_identifier) { + if (entry_identifier == NULL) return false; - size_t length = strlen(entry_id); - - // Check that the length is valid - if (length == 0 || length > MAX_ENTRY_LENGTH) + // Check that the identifier is not empty or too long + size_t length = strlen(entry_identifier); + if (length == 0 || length > 64) return false; - // Check that the entry id does not contain any slashes + // Check that the identifier does not contain any slashes for (size_t i = 0; i < length; i++) - if (entry_id[i] == '/') + if (entry_identifier[i] == '/') return false; - // Check that the entry id is not a reserved name - if (strcmp(entry_id, ".") == 0 || strcmp(entry_id, "..") == 0) + // Check that the identifier is not "." or ".." + if (strcmp(entry_identifier, ".") == 0 || strcmp(entry_identifier, "..") == 0) return false; return true; } -Result get_entry_path(const char* entry_id, char** out_path) { - *out_path = NULL; +Status GetEntryPoolPath(char** _entry_pool_path) { + return Format(_entry_pool_path, "/var/lib/sandbox/entries"); +} - // Check that the entry id is valid - if (!is_entry_id_valid(entry_id)) { - log_message(LOG_LEVEL_ERROR, "Invalid entry id '%s'.", entry_id); +Status GetEntryPath(const char* entry_identifier, char** _entry_path) { + // Check that the identifier is valid, as it will be used in a path + if (!IsEntryIdentifierValid(entry_identifier)) { + Log(LOG_LEVEL_ERROR, "Invalid entry identifier '%s'.", entry_identifier); return FAILURE; } - return format(out_path, "%s/%s", ENTRY_POOL_DIR, entry_id); + // Get the entry pool path + char* entry_pool_path = NULL; + Status status = GetEntryPoolPath(&entry_pool_path); + if (status != SUCCESS) + return status; + + // Format the entry path + status = Format(_entry_path, "%s/%s", entry_pool_path, entry_identifier); + free(entry_pool_path); + + return status; } -Result get_entry_disk_path(const char* entry_id, char** out_path) { - *out_path = NULL; +Status GetEntryDiskPath(const char* entry_identifier, char** _entry_disk_path) { + // Get the entry path + char* entry_path = NULL; + Status status = GetEntryPath(entry_identifier, &entry_path); + if (status != SUCCESS) + return status; - // Get the path of the entry - char* entry_path; - Result result = get_entry_path(entry_id, &entry_path); - if (result != SUCCESS) - return result; - - // Get the path of the disk - result = format(out_path, "%s/disk", entry_path); - - // Free the entry path + // Format the disk path + status = Format(_entry_disk_path, "%s/disk", entry_path); free(entry_path); - return result; + return status; } -Result get_entry_backing_id(const char* entry_id, char** out_backing_id) { - *out_backing_id = NULL; - - // Get the path of the disk - char* disk_path; - Result result = get_entry_disk_path(entry_id, &disk_path); - if (result != SUCCESS) - return result; - - // Get the backing file of the disk - DiskInfo info; - result = get_disk_info(disk_path, &info); - if (result != SUCCESS) { - free(disk_path); - return result; - } - - // Free the disk path - free(disk_path); - - // Check that the disk is backed - if (info.backing_file == NULL) { - free_disk_info(&info); - return SUCCESS; - } - - // Get the backing id - *out_backing_id = strdup(basename(info.backing_file)); - if (*out_backing_id == NULL) { - log_message(LOG_LEVEL_ERROR, "Failed to allocate memory for the backing id."); - free_disk_info(&info); - return OUT_OF_MEMORY; - } - - // Free the disk info - free_disk_info(&info); - - return SUCCESS; -} - -Result get_entry_type_path(const char* entry_id, char** out_path) { - *out_path = NULL; - - // Get the path of the entry - char* entry_path; - Result result = get_entry_path(entry_id, &entry_path); - if (result != SUCCESS) - return result; - - // Get the path of the type file - result = format(out_path, "%s/type", entry_path); - - // Free the entry path - free(entry_path); - - return result; -} - -Result get_entry_type(const char* entry_id, EntryType* out_type) { - // Get the path of the type file - char* type_path; - Result result = get_entry_type_path(entry_id, &type_path); - if (result != SUCCESS) - return result; - - // Read the type file - char* type; - result = read_file(type_path, &type); - if (result != SUCCESS) { - free(type_path); - return result; - } - - // Free the type path - free(type_path); - - // Check the type - if (strcmp(type, ENTRY_TYPE_ROOT_STRING) == 0) - *out_type = ENTRY_TYPE_ROOT; - else if (strcmp(type, ENTRY_TYPE_BACKED_STRING) == 0) - *out_type = ENTRY_TYPE_BACKED; - else if (strcmp(type, ENTRY_TYPE_AUTOMATIC_STRING) == 0) - *out_type = ENTRY_TYPE_AUTOMATIC; - else - *out_type = ENTRY_TYPE_UNKNOWN; - - // Free the type - free(type); - - return SUCCESS; -} - -Result entry_exists(const char* entry_id, bool* out_exists) { - *out_exists = false; - - // Get the path of the entry - char* path; - Result result = get_entry_path(entry_id, &path); - if (result != SUCCESS) - return result; +Status DoesEntryExist(const char* entry_identifier, bool* _result) { + // Get the entry path + char* entry_path = NULL; + Status status = GetEntryPath(entry_identifier, &entry_path); + if (status != SUCCESS) + return status; // Check if the entry exists and is a directory struct stat st; - bool exists = stat(path, &st) == 0 && S_ISDIR(st.st_mode); - - // Free the path - free(path); - - // Output the result - *out_exists = exists; - - return SUCCESS; -} - -Result add_entry(const char* entry_id, EntryType type) { - // Check that it does not exist - bool exists; - Result result = entry_exists(entry_id, &exists); - if (result != SUCCESS) - return result; - - if (exists) { - log_message(LOG_LEVEL_ERROR, "The entry '%s' already exists.", entry_id); - return FAILURE; - } - - // Get the path of the entry - char* entry_path; - result = get_entry_path(entry_id, &entry_path); - if (result != SUCCESS) - return result; - - // Create the directory - if (mkdir(entry_path, 0755) == -1) { - log_message(LOG_LEVEL_ERROR, "Failed to create the directory '%s' (%s).", entry_path, strerror(errno)); - - free(entry_path); - return FAILURE; - } - - // Free the entry path + *_result = stat(entry_path, &st) == 0 && S_ISDIR(st.st_mode); free(entry_path); - // Get the path of the type file - char* type_path; - result = get_entry_type_path(entry_id, &type_path); - if (result != SUCCESS) { - remove_entry(entry_id); - return result; - } + return SUCCESS; +} - // Write the type file - const char* type_string; - switch (type) { - case ENTRY_TYPE_ROOT: - type_string = ENTRY_TYPE_ROOT_STRING; - break; - case ENTRY_TYPE_BACKED: - type_string = ENTRY_TYPE_BACKED_STRING; - break; - case ENTRY_TYPE_AUTOMATIC: - type_string = ENTRY_TYPE_AUTOMATIC_STRING; - break; - default: - log_message(LOG_LEVEL_ERROR, "Invalid entry type."); - remove_entry(entry_id); - return FAILURE; - } +Status DoesEntryDiskExist(const char* entry_identifier, bool* _result) { + // Get the disk path + char* entry_disk_path = NULL; + Status status = GetEntryDiskPath(entry_identifier, &entry_disk_path); + if (status != SUCCESS) + return status; - result = write_file(type_path, type_string); - - // Free the type path - free(type_path); - - if (result != SUCCESS) { - remove_entry(entry_id); - return result; - } + // Check if the disk exists and is a regular file + struct stat st; + *_result = stat(entry_disk_path, &st) == 0 && S_ISREG(st.st_mode); + free(entry_disk_path); return SUCCESS; } -Result add_root_entry(const char* entry_id, uint64_t size) { - // Add the entry - Result result = add_entry(entry_id, ENTRY_TYPE_ROOT); - if (result != SUCCESS) - return result; - - // Get the path of the disk - 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 - free(disk_path); - - if (result != SUCCESS) { - remove_entry(entry_id); - return result; - } - - return SUCCESS; -} - -Result add_backed_entry(const char* entry_id, const char* backing_id) { - // TODO: Implement - - return FAILURE; -} - -Result add_automatic_entry(const char* entry_id) { - // TODO: Implement - - return FAILURE; -} - -Result remove_entry(const char* entry_id) { - // Check that it exists +Status AddEntry(const char* entry_identifier) { + // Check if the entry already exists bool exists; - Result result = entry_exists(entry_id, &exists); - if (result != SUCCESS) - return result; + Status status = DoesEntryExist(entry_identifier, &exists); + if (status != SUCCESS) + return status; - if (!exists) { - log_message(LOG_LEVEL_ERROR, "The entry '%s' does not exist.", entry_id); + if (exists) { + Log(LOG_LEVEL_ERROR, "The entry '%s' already exists.", entry_identifier); return FAILURE; } - // Get the path of the entry - char* entry_path; - result = get_entry_path(entry_id, &entry_path); - if (result != SUCCESS) - return result; - - // Remove all the files in the directory - DIR* dir = opendir(entry_path); - if (dir == NULL) { - log_message(LOG_LEVEL_ERROR, "Failed to open the directory '%s' (%s).", entry_path, strerror(errno)); + // Create the entry directory + char* entry_path = NULL; + status = GetEntryPath(entry_identifier, &entry_path); + if (status != SUCCESS) + return status; + if (mkdir(entry_path, 0755) != 0) { + Log(LOG_LEVEL_ERROR, "Failed to create the entry directory '%s' (%s).", entry_path, strerror(errno)); free(entry_path); return FAILURE; } + free(entry_path); + return SUCCESS; +} + +Status RemoveEntry(const char* entry_identifier) { + // Check if the entry exists + bool exists; + Status status = DoesEntryExist(entry_identifier, &exists); + if (status != SUCCESS) + return status; + + if (!exists) { + Log(LOG_LEVEL_ERROR, "The entry '%s' does not exist.", entry_identifier); + return FAILURE; + } + + // Check if the disk exists, and remove it if it does + bool disk_exists; + status = DoesEntryDiskExist(entry_identifier, &disk_exists); + if (status != SUCCESS) + return status; + + if (disk_exists) { + status = RemoveEntryDisk(entry_identifier); + if (status != SUCCESS) + return status; + } + + // Remove the entry directory + char* entry_path = NULL; + status = GetEntryPath(entry_identifier, &entry_path); + if (status != SUCCESS) + return status; + + if (rmdir(entry_path) != 0) { + Log(LOG_LEVEL_ERROR, "Failed to remove the entry directory '%s' (%s).", entry_path, strerror(errno)); + free(entry_path); + return FAILURE; + } + free(entry_path); + + return SUCCESS; +} + +Status ListEntries(char*** _entries) { + *_entries = NULL; + + // Open the entry pool directory + char* entry_pool_path = NULL; + Status status = GetEntryPoolPath(&entry_pool_path); + if (status != SUCCESS) + return status; + + DIR* dir = opendir(entry_pool_path); + if (dir == NULL) { + Log(LOG_LEVEL_ERROR, "Failed to open the entry pool directory '%s' (%s).", entry_pool_path, strerror(errno)); + free(entry_pool_path); + return FAILURE; + } + free(entry_pool_path); + + // Allocate memory for at least one entry (the NULL terminator) + *_entries = malloc(sizeof(char*)); + if (*_entries == NULL) { + Log(LOG_LEVEL_ERROR, "Failed to allocate memory for the entries list (%s).", strerror(errno)); + closedir(dir); + return FAILURE; + } + (*_entries)[0] = NULL; + + // Read the entries from the directory + size_t count = 0; struct dirent* entry; - while ((entry = readdir(dir)) != NULL) { - // Skip the current and parent directories if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; - // Get the path of the file - char* file_path; - result = format(&file_path, "%s/%s", entry_path, entry->d_name); - if (result != SUCCESS) { + char** new_entries = realloc(*_entries, (count + 2) * sizeof(char*)); // +2 for the new entry and the NULL terminator + if (new_entries == NULL) { + Log(LOG_LEVEL_ERROR, "Failed to reallocate memory for the entries list (%s).", strerror(errno)); closedir(dir); - free(entry_path); - return result; + + for (size_t i = 0; i < count; i++) + free((*_entries)[i]); + free(*_entries); + + return FAILURE; } + *_entries = new_entries; - // Remove the file - if (unlink(file_path) == -1) { - log_message(LOG_LEVEL_ERROR, "Failed to remove the file '%s' (%s).", file_path, strerror(errno)); - - free(file_path); + // Duplicate the entry name and add it to the list + (*_entries)[count] = strdup(entry->d_name); + if ((*_entries)[count] == NULL) { + Log(LOG_LEVEL_ERROR, "Failed to duplicate the entry name (%s).", strerror(errno)); closedir(dir); - free(entry_path); + + for (size_t i = 0; i < count; i++) + free((*_entries)[i]); + free(*_entries); + return FAILURE; } - // Free the entry file path - free(file_path); + // Add the NULL terminator + (*_entries)[count + 1] = NULL; + count++; } - // Close the directory - closedir(dir); - - // Remove the directory - if (rmdir(entry_path) == -1) { - log_message(LOG_LEVEL_ERROR, "Failed to remove the directory '%s' (%s).", entry_path, strerror(errno)); - - free(entry_path); - return FAILURE; - } - - // Free the entry path - free(entry_path); - - return SUCCESS; -} - -Result list_entries(char*** out_entries) { - *out_entries = malloc(sizeof(char*)); - if (*out_entries == NULL) { - log_message(LOG_LEVEL_ERROR, "Failed to allocate memory for the entries."); - return OUT_OF_MEMORY; - } - - (*out_entries)[0] = NULL; - - // Open the directory - DIR* dir = opendir(ENTRY_POOL_DIR); - if (dir == NULL) { - log_message(LOG_LEVEL_ERROR, "Failed to open the directory '%s' (%s).", ENTRY_POOL_DIR, strerror(errno)); - return FAILURE; - } - - // Read the directory - int entry_count = 0; - - struct dirent* entry; - - while ((entry = readdir(dir)) != NULL) { - // Skip the current and parent directories - if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) - continue; - - // Check that the entry exists - bool exists; - Result result = entry_exists(entry->d_name, &exists); - if (result != SUCCESS || !exists) - continue; - - // Reallocate the entries array - // TODO: More efficient reallocation - char** new_entries = realloc(*out_entries, (entry_count + 2) * sizeof(char*)); - if (new_entries == NULL) { - log_message(LOG_LEVEL_ERROR, "Failed to allocate memory for the entries."); - closedir(dir); - - for (int i = 0; i < entry_count; i++) - free((*out_entries)[i]); - free(*out_entries); - - return OUT_OF_MEMORY; - } - - *out_entries = new_entries; - - // Copy the entry name - (*out_entries)[entry_count] = strdup(entry->d_name); - if ((*out_entries)[entry_count] == NULL) { - log_message(LOG_LEVEL_ERROR, "Failed to allocate memory for the entry name."); - closedir(dir); - - for (int i = 0; i < entry_count; i++) - free((*out_entries)[i]); - free(*out_entries); - - return OUT_OF_MEMORY; - } - - // Null-terminate the array - (*out_entries)[entry_count + 1] = NULL; - - entry_count++; - } - - // Close the directory closedir(dir); return SUCCESS; } -Result clear_entries(void) { - char** entries; - Result result = list_entries(&entries); - if (result != SUCCESS) - return result; +Status ClearEntries() { + // Get the entries + char** entries = NULL; + Status status = ListEntries(&entries); + if (status != SUCCESS) + return status; - // Remove each entry - for (int i = 0; entries[i] != NULL; i++) { - result = remove_entry(entries[i]); - if (result != SUCCESS) - log_message(LOG_LEVEL_WARNING, "Failed to remove the entry '%s'.", entries[i]); + // Remove the entries + for (size_t i = 0; entries[i] != NULL; i++) { + status = RemoveEntry(entries[i]); + if (status != SUCCESS) + Log(LOG_LEVEL_WARNING, "Failed to remove the entry '%s'. Non-critical error.", entries[i]); } - // Free the entries - for (int i = 0; entries[i] != NULL; i++) + // Clean up + for (size_t i = 0; entries[i] != NULL; i++) free(entries[i]); free(entries); return SUCCESS; } + +Status AddRootEntryDisk(const char* entry_identifier, uint64_t disk_size) { + // Check if the disk already exists + bool exists; + Status status = DoesEntryDiskExist(entry_identifier, &exists); + if (status != SUCCESS) + return status; + + if (exists) { + Log(LOG_LEVEL_ERROR, "The disk for the entry '%s' already exists.", entry_identifier); + return FAILURE; + } + + // Get the disk path + char* entry_disk_path = NULL; + status = GetEntryDiskPath(entry_identifier, &entry_disk_path); + if (status != SUCCESS) + return status; + + // Create the disk + status = CreateRootDisk(entry_disk_path, disk_size); + free(entry_disk_path); + + return status; +} + +Status RemoveEntryDisk(const char* entry_identifier) { + // Check if the disk exists + bool exists; + Status status = DoesEntryDiskExist(entry_identifier, &exists); + if (status != SUCCESS) + return status; + + if (!exists) { + Log(LOG_LEVEL_ERROR, "The disk for the entry '%s' does not exist.", entry_identifier); + return FAILURE; + } + + // Get the disk path + char* entry_disk_path = NULL; + status = GetEntryDiskPath(entry_identifier, &entry_disk_path); + if (status != SUCCESS) + return status; + + // Remove the disk + if (unlink(entry_disk_path) != 0) { + Log(LOG_LEVEL_ERROR, "Failed to remove the disk for the entry '%s' (%s).", entry_identifier, strerror(errno)); + free(entry_disk_path); + return FAILURE; + } + free(entry_disk_path); + + return SUCCESS; +} + +Status TrimEntryDisk(const char* entry_identifier) { + // Check if the disk exists + bool exists; + Status status = DoesEntryDiskExist(entry_identifier, &exists); + if (status != SUCCESS) + return status; + + if (!exists) { + Log(LOG_LEVEL_ERROR, "The disk for the entry '%s' does not exist.", entry_identifier); + return FAILURE; + } + + // Get the disk path + char* entry_disk_path = NULL; + status = GetEntryDiskPath(entry_identifier, &entry_disk_path); + if (status != SUCCESS) + return status; + + // Trim the disk + status = TrimDisk(entry_disk_path); + free(entry_disk_path); + + return status; +} diff --git a/src/entry.h b/src/entry.h index 74d65b3..40b9690 100644 --- a/src/entry.h +++ b/src/entry.h @@ -1,99 +1,24 @@ #pragma once #include "utils.h" -#include "disk.h" -#include -#include +bool IsEntryIdentifierValid(const char* entry_identifier); -#define ENTRY_POOL_DIR "/var/lib/sandbox/entries" -#define MAX_ENTRY_LENGTH 256 +Status GetEntryPoolPath(char** _entry_pool_path); +Status GetEntryPath(const char* entry_identifier, char** _entry_path); +Status GetEntryDiskPath(const char* entry_identifier, char** _entry_disk_path); -#define ENTRY_TYPE_ROOT_STRING "ROOT" -#define ENTRY_TYPE_BACKED_STRING "BACKED" -#define ENTRY_TYPE_AUTOMATIC_STRING "AUTOMATIC" +Status DoesEntryExist(const char* entry_identifier, bool* _result); +Status DoesEntryDiskExist(const char* entry_identifier, bool* _result); -typedef enum { - ENTRY_TYPE_UNKNOWN, - ENTRY_TYPE_ROOT, - ENTRY_TYPE_BACKED, - ENTRY_TYPE_AUTOMATIC -} EntryType; +Status AddEntry(const char* entry_identifier); +Status RemoveEntry(const char* entry_identifier); -/// @brief Checks whether the given entry id is valid. -/// @param entry_id The entry id to check. -/// @return True if the entry id is valid, false otherwise. -bool is_entry_id_valid(const char* entry_id); +Status ListEntries(char*** _entries); +Status ClearEntries(); -/// @brief Gets the path 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_path(const char* entry_id, char** out_path); - -/// @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 Gets the backing id of the given entry. -/// @param entry_id The entry id. -/// @param out_backing_id The pointer to the output backing id string. The caller is responsible for freeing the memory. -/// @return The result of the operation. -Result get_entry_backing_id(const char* entry_id, char** out_backing_id); - -/// @brief Gets the path of the type file 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_type_path(const char* entry_id, char** out_path); - -/// @brief Gets the type of the given entry. -/// @param entry_id The entry id. -/// @param out_type The pointer to the output entry type. -/// @return The result of the operation. -Result get_entry_type(const char* entry_id, EntryType* out_type); - -/// @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 an entry directory to the pool. -/// @param entry_id The entry id. -/// @param type The type of the entry. -/// @return The result of the operation. -Result add_entry(const char* entry_id, EntryType type); - -/// @brief Adds a root entry to the pool. -/// @param entry_id The entry id. -/// @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 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 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 pool. -/// @param out_entries The pointer to the null-terminated array of entry ids. The caller is responsible for freeing the memory of the array and its elements. -/// @return The result of the operation. -Result list_entries(char*** out_entries); - -/// @brief Clears all the entries from the pool. -/// @return The result of the operation. -Result clear_entries(void); +Status AddRootEntryDisk(const char* entry_identifier, uint64_t disk_size); +Status AddBackedEntryDisk(const char* entry_identifier, const char* backing_identifier); +Status RemoveEntryDisk(const char* entry_identifier); +Status ResetEntryDisk(const char* entry_identifier, const char* backing_identifier); +Status TrimEntryDisk(const char* entry_identifier); \ No newline at end of file diff --git a/src/sandbox.c b/src/sandbox.c index 3d6e69d..5fbf797 100644 --- a/src/sandbox.c +++ b/src/sandbox.c @@ -1,85 +1,65 @@ #include "sandbox.h" -#include "utils.h" -#include "backing.h" #include "entry.h" -#include "disk.h" #include #include #include +#include #include #include -#include -#include -const Command COMMANDS[] = { - {command_help, "help", "[command]", "Prints the help message.", - "Prints the help message for the given command. If no command is given, prints the help message for all commands."}, - {command_version, "version", "", "Prints the version.", - "Prints the version of the program."}, - {}, - {command_add_entry, "add-entry", " [--root|-r ] [--backed|-b ]", "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 will be an automatic entry, which will be backed by the latest backing disk available and will automatically reset to its initial state when a new backing disk is added."}, - {command_remove_entry, "remove-entry", "", "Removes an entry from the entry pool.", - "Removes the entry with the given id from the entry pool."}, - {command_list_entries, "entries", "", "Lists the entries in the entry pool.", - "Lists the entries in the entry pool, as well as their types and backing disks."}, - {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", "", "Resets an entry to its initial state.", - "TODO"}, - {command_update_entry, "update-entry", "", "Updates the backing disk of the given entry to the latest available.", - "TODO"}, - {}, +Command COMMANDS[] = { + {CommandHelp, "help", "[command]", "Prints this help message.", + "TODO: Add details."}, + {CommandVersion, "version", NULL, "Prints the version of the program.", + "TODO: Add details."}, }; int main(int argc, char* argv[]) { - // Check that the user 'sandbox' exists - struct passwd* user = getpwnam(SANDBOX_USER); - if (user == NULL) { - log_message(LOG_LEVEL_ERROR, "User '%s' does not exist. Please check the installation.", SANDBOX_USER); + struct passwd* pw = getpwnam(SANDBOX_USER); + if (pw == NULL) { + Log(LOG_LEVEL_ERROR, "Failed to get the 'sandbox' user (%s).", strerror(errno)); return EXIT_FAILURE; } - // Check that the program is either run as root or as the user 'sandbox' - if (geteuid() != 0 && geteuid() != user->pw_uid) { - log_message(LOG_LEVEL_ERROR, "This program must be run as root or as the user '%s'.", SANDBOX_USER); + // Check if the current user is root or the 'sandbox' user + if (getuid() != 0 && getuid() != pw->pw_uid) { + Log(LOG_LEVEL_ERROR, "You must be root or the 'sandbox' user to use this program."); return EXIT_FAILURE; } - // If the program is run as root, switch to the user 'sandbox' + // Try and switch to the 'sandbox' user if we are root if (geteuid() == 0) { - if (setregid(user->pw_gid, user->pw_gid) != 0) { - log_message(LOG_LEVEL_ERROR, "Failed to switch to the user '%s'.", SANDBOX_USER); + if (setregid(pw->pw_gid, pw->pw_gid) == -1) { + Log(LOG_LEVEL_ERROR, "Failed to set the real and effective group ID to the 'sandbox' user (%s).", strerror(errno)); return EXIT_FAILURE; } - if (setreuid(user->pw_uid, user->pw_uid) != 0) { - log_message(LOG_LEVEL_ERROR, "Failed to switch to the user '%s'.", SANDBOX_USER); + if (setreuid(pw->pw_uid, pw->pw_uid) == -1) { + Log(LOG_LEVEL_ERROR, "Failed to set the real and effective user ID to the 'sandbox' user (%s).", strerror(errno)); return EXIT_FAILURE; } } - if (argc < 2) - return command_help(0, NULL); + // If there are no arguments, print the help message + if (argc == 1) + return CommandHelp(0, NULL); - size_t input_length = strlen(argv[1]); + const char* input = argv[1]; + size_t input_length = strlen(input); const Command* command = NULL; - for (int i = 0; i < ARRAY_SIZE(COMMANDS); i++) { + for (size_t i = 0; i < sizeof(COMMANDS) / sizeof(COMMANDS[0]); i++) { if (COMMANDS[i].name == NULL) continue; - // Check that the length of the input is equal or less than the length of the command name if (input_length > strlen(COMMANDS[i].name)) continue; - // Check that the input matches the command name - if (strncmp(argv[1], COMMANDS[i].name, input_length) == 0) { - // Check for multiple matches + if (strncmp(input, COMMANDS[i].name, input_length) == 0) { if (command != NULL) { - log_message(LOG_LEVEL_ERROR, "Ambiguous command '%s'.", argv[1]); + Log(LOG_LEVEL_ERROR, "Ambiguous command '%s'.", input); return EXIT_FAILURE; } @@ -87,167 +67,66 @@ int main(int argc, char* argv[]) { } } - // Check if the command is NULL (no matches) if (command == NULL) { - log_message(LOG_LEVEL_ERROR, "Unknown command '%s'.", argv[1]); + Log(LOG_LEVEL_ERROR, "Unknown command '%s'.", input); return EXIT_FAILURE; } return command->handler(argc - 2, argv + 2); } -int command_help(int argc, char* argv[]) { - // Check the number of arguments +int CommandHelp(int argc, char* argv[]) { if (argc == 0) { - fprintf(stdout, "Usage: sandbox [command] [arguments]\n\n"); + fprintf(stdout, "Usage: sandbox [arguments] [options]\n"); + fprintf(stdout, "\n"); fprintf(stdout, "Commands:\n"); + for (size_t i = 0; i < sizeof(COMMANDS) / sizeof(COMMANDS[0]); i++) { + if (COMMANDS[i].name == NULL) { + fprintf(stdout, "\n"); + continue; + } - for (int i = 0; i < ARRAY_SIZE(COMMANDS); i++) - 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", COMMANDS[i].name); + if (COMMANDS[i].arguments != NULL) + fprintf(stdout, " %s", COMMANDS[i].arguments); + fprintf(stdout, " - %s\n", COMMANDS[i].description); + } + + fprintf(stdout, "\nFor more information, run 'sandbox help '.\n"); - fprintf(stdout, "\nRun 'sandbox help [command]' for more information on a command.\n"); return EXIT_SUCCESS; } else if (argc == 1) { - // Find the command that matches the given name - const Command* command = NULL; + const char* input = argv[0]; - for (int i = 0; i < ARRAY_SIZE(COMMANDS); i++) { + for (size_t i = 0; i < sizeof(COMMANDS) / sizeof(COMMANDS[0]); i++) { if (COMMANDS[i].name == NULL) continue; - if (strcmp(argv[0], COMMANDS[i].name) == 0) { - command = &COMMANDS[i]; - break; + if (strcmp(input, COMMANDS[i].name) == 0) { + fprintf(stdout, "Usage: sandbox %s", COMMANDS[i].name); + if (COMMANDS[i].arguments != NULL) + fprintf(stdout, " %s", COMMANDS[i].arguments); + fprintf(stdout, "\n"); + fprintf(stdout, "\n"); + fprintf(stdout, "%s\n", COMMANDS[i].description); + fprintf(stdout, "\n"); + fprintf(stdout, "%s\n", COMMANDS[i].details); + + return EXIT_SUCCESS; } } - // Check if the command is NULL (no matches) - if (command == NULL) { - log_message(LOG_LEVEL_ERROR, "Unknown command '%s'.", argv[0]); - return EXIT_FAILURE; - } - - // Print the help message for the command - fprintf(stdout, "Usage: sandbox %s %s\n\n", command->name, command->arguments); - fprintf(stdout, " %s\n", command->details); - - return EXIT_SUCCESS; + Log(LOG_LEVEL_ERROR, "Unknown command '%s'.", input); + return EXIT_FAILURE; } else { - log_message(LOG_LEVEL_ERROR, "Too many arguments for 'help' command."); - return EXIT_FAILURE; - } -} - -int command_version(int argc, char* argv[]) { - fprintf(stdout, "Sandbox manager v%s\n", VERSION); - return EXIT_SUCCESS; -} - -int command_add_entry(int argc, char* argv[]) { - // Extract the primary arguments - if (argc < 1) { - log_message(LOG_LEVEL_ERROR, "Missing entry id."); + Log(LOG_LEVEL_ERROR, "Too many arguments supplied to 'help'."); return EXIT_FAILURE; } - const char* entry_id = argv[0]; - - // Extract the options - bool is_root = false; - uint64_t size = 0; - - bool is_backed = false; - const char* backing_id = NULL; - - for (int i = 1; i < argc; i++) { - if (strcmp(argv[i], "--root") == 0 || strcmp(argv[i], "-r") == 0) { - if (is_backed) { - log_message(LOG_LEVEL_ERROR, "Cannot specify both --root and --backed options."); - return EXIT_FAILURE; - } - - is_root = true; - - if (i + 1 >= argc) { - log_message(LOG_LEVEL_ERROR, "Missing size for root entry."); - return EXIT_FAILURE; - } - - Result result = parse_size(argv[i + 1], &size); - if (result != SUCCESS) - return EXIT_FAILURE; - - i++; // Consume the next argument - } else if (strcmp(argv[i], "--backed") == 0 || strcmp(argv[i], "-b") == 0) { - if (is_root) { - log_message(LOG_LEVEL_ERROR, "Cannot specify both --root and --backed options."); - return EXIT_FAILURE; - } - - is_backed = true; - - if (i + 1 >= argc) { - log_message(LOG_LEVEL_ERROR, "Missing backing id for backed entry."); - return EXIT_FAILURE; - } - - backing_id = argv[i + 1]; - i++; // Consume the next argument - } else { - log_message(LOG_LEVEL_ERROR, "Too many arguments for 'add-entry' command."); - return EXIT_FAILURE; - } - } - - Result result; - if (is_root) - result = add_root_entry(entry_id, size); - else if (is_backed) - result = add_backed_entry(entry_id, backing_id); - else - result = add_automatic_entry(entry_id); - - if (result != SUCCESS) - return EXIT_FAILURE; - 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; - } - - const char* entry_id = argv[0]; - - if (argc > 1) { - log_message(LOG_LEVEL_ERROR, "Too many arguments for 'remove-entry' command."); - return EXIT_FAILURE; - } - - Result result = remove_entry(entry_id); - if (result != SUCCESS) - return EXIT_FAILURE; - +int CommandVersion(int argc, char* argv[]) { + fprintf(stdout, "Sandbox utility v%s\n", VERSION); return EXIT_SUCCESS; } - -int command_list_entries(int argc, char* argv[]) { - return EXIT_SUCCESS; -} - -int command_clear_entries(int argc, char* argv[]) { - return EXIT_SUCCESS; -} - -int command_reset_entry(int argc, char* argv[]) { - return EXIT_SUCCESS; -} - -int command_update_entry(int argc, char* argv[]) { - return EXIT_SUCCESS; -} \ No newline at end of file diff --git a/src/sandbox.h b/src/sandbox.h index d4b6178..510a9bd 100644 --- a/src/sandbox.h +++ b/src/sandbox.h @@ -1,6 +1,6 @@ #pragma once -#define VERSION "0.0.6" +#define VERSION "0.0.9" #define SANDBOX_USER "sandbox" @@ -12,16 +12,7 @@ typedef struct { const char* details; } Command; -extern const Command COMMANDS[]; - int main(int argc, char* argv[]); -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_update_entry(int argc, char* argv[]); \ No newline at end of file +int CommandHelp(int argc, char* argv[]); +int CommandVersion(int argc, char* argv[]); \ No newline at end of file diff --git a/src/utils.c b/src/utils.c index 8951117..735cea8 100644 --- a/src/utils.c +++ b/src/utils.c @@ -3,21 +3,21 @@ #include #include #include -#include #include +#include #include #include #include -#include #include +#include -LogLevel log_level = LOG_LEVEL_WARNING; +LogLevel log_level = LOG_LEVEL_INFO; -void set_log_level(LogLevel level) { +void SetLogLevel(LogLevel level) { log_level = level; } -void log_message(LogLevel level, const char* format, ...) { +void Log(LogLevel level, const char* format, ...) { if (level < log_level) return; @@ -26,13 +26,15 @@ void log_message(LogLevel level, const char* format, ...) { const char* color; const char* level_str; - // Set the color and level_str based on the log level switch (level) { case LOG_LEVEL_DEBUG: color = "\033[0;90m"; level_str = "DEBUG"; break; + case LOG_LEVEL_INFO: + color = "\033[0;36m"; + level_str = "INFO"; case LOG_LEVEL_WARNING: color = "\033[0;33m"; level_str = "WARNING"; @@ -58,48 +60,44 @@ void log_message(LogLevel level, const char* format, ...) { va_end(args); } -Result format(char** out_string, const char* fmt, ...) { - *out_string = NULL; +Status Format(char** _string, const char* fmt, ...) { + *_string = NULL; va_list args; va_start(args, fmt); // Calculate the length of the formatted string size_t length = vsnprintf(NULL, 0, fmt, args) + 1; - va_end(args); - if (length <= 0) { - log_message(LOG_LEVEL_ERROR, "Failed to calculate the length of the formatted string (%s).", strerror(errno)); + Log(LOG_LEVEL_ERROR, "Failed to calculate the length of the formatted string."); return FAILURE; } // Allocate a buffer for the formatted string char* buffer = calloc(length, sizeof(char)); if (buffer == NULL) { - log_message(LOG_LEVEL_ERROR, "Failed to allocate memory for the formatted string (%s).", strerror(errno)); - return OUT_OF_MEMORY; + Log(LOG_LEVEL_ERROR, "Failed to allocate memory for the formatted string (%s).", strerror(errno)); + return FAILURE; } va_start(args, fmt); - // Format the string vsnprintf(buffer, length, fmt, args); va_end(args); - *out_string = buffer; + *_string = buffer; return SUCCESS; } -Result run_executable(int* out_exit_code, char** out_stdout, char** out_stderr, const char* executable, ...) { - if (out_exit_code != NULL) - *out_exit_code = -1; - - if (out_stdout != NULL) - *out_stdout = NULL; - if (out_stderr != NULL) - *out_stderr = NULL; +Status RunExecutable(int* _exit_code, char** _stdout, char** _stderr, const char* executable, ...) { + if (_exit_code != NULL) + *_exit_code = -1; + if (_stdout != NULL) + *_stdout = NULL; + if (_stderr != NULL) + *_stderr = NULL; // Count the number of arguments int argc = 1; // The first argument is the executable itself @@ -113,48 +111,44 @@ Result run_executable(int* out_exit_code, char** out_stdout, char** out_stderr, va_end(args); // Allocate an array for the arguments - char** argv = calloc(argc + 1, sizeof(char*)); + char** argv = calloc(argc + 1, sizeof(char*)); // +1 for the NULL terminator if (argv == NULL) { - log_message(LOG_LEVEL_ERROR, "Failed to allocate memory for the arguments (%s).", strerror(errno)); - return OUT_OF_MEMORY; + Log(LOG_LEVEL_ERROR, "Failed to allocate memory for the arguments array (%s).", strerror(errno)); + return FAILURE; } - // Fill the array with the arguments + // Fill the arguments array argv[0] = strdup(executable); if (argv[0] == NULL) { - log_message(LOG_LEVEL_ERROR, "Failed to duplicate the executable string (%s).", strerror(errno)); - + Log(LOG_LEVEL_ERROR, "Failed to duplicate the executable path (%s).", strerror(errno)); free(argv); - - return OUT_OF_MEMORY; + return FAILURE; } va_start(args, executable); + // Duplicate the arguments and store them in the array for (int i = 1; i < argc; i++) { argv[i] = strdup(va_arg(args, const char*)); if (argv[i] == NULL) { - log_message(LOG_LEVEL_ERROR, "Failed to duplicate the argument string (%s).", strerror(errno)); + Log(LOG_LEVEL_ERROR, "Failed to duplicate the argument n°%d (%s).", i, strerror(errno)); for (int j = 0; j < i; j++) free(argv[j]); free(argv); - va_end(args); - - return OUT_OF_MEMORY; + return FAILURE; } } va_end(args); - // Set the last element of the array to NULL argv[argc] = NULL; - // Create pipes for the stdout and stderr + // Create pipes for the standard output and error int stdout_pipe[2]; if (pipe(stdout_pipe) == -1) { - log_message(LOG_LEVEL_ERROR, "Failed to create the stdout pipe (%s).", strerror(errno)); + Log(LOG_LEVEL_ERROR, "Failed to create the standard output pipe (%s).", strerror(errno)); for (int i = 0; i < argc; i++) free(argv[i]); @@ -165,7 +159,7 @@ Result run_executable(int* out_exit_code, char** out_stdout, char** out_stderr, int stderr_pipe[2]; if (pipe(stderr_pipe) == -1) { - log_message(LOG_LEVEL_ERROR, "Failed to create the stderr pipe (%s).", strerror(errno)); + Log(LOG_LEVEL_ERROR, "Failed to create the standard error pipe (%s).", strerror(errno)); for (int i = 0; i < argc; i++) free(argv[i]); @@ -181,7 +175,7 @@ Result run_executable(int* out_exit_code, char** out_stdout, char** out_stderr, pid_t pid = fork(); if (pid == -1) { - log_message(LOG_LEVEL_ERROR, "Failed to fork the process (%s).", strerror(errno)); + Log(LOG_LEVEL_ERROR, "Failed to fork the process (%s).", strerror(errno)); for (int i = 0; i < argc; i++) free(argv[i]); @@ -196,7 +190,7 @@ Result run_executable(int* out_exit_code, char** out_stdout, char** out_stderr, } if (pid == 0) { - // Redirect the stdout and stderr to the pipes + // Redirect the standard output and error to the pipes dup2(stdout_pipe[1], STDOUT_FILENO); dup2(stderr_pipe[1], STDERR_FILENO); @@ -207,116 +201,84 @@ Result run_executable(int* out_exit_code, char** out_stdout, char** out_stderr, close(stderr_pipe[1]); // Execute the command - execvp(executable, argv); + execv(executable, argv); - // If the command failed to execute, print an error message and exit - fprintf(stderr, "Failed to execute the command '%s' (%s).\n", executable, strerror(errno)); + // If execv returns, it failed + fprintf(stderr, "Failed to execute the command (%s).\n", strerror(errno)); exit(EXIT_FAILURE); } - // Free the arguments array - for (int i = 0; i < argc; i++) - free(argv[i]); - free(argv); - // Close the unused ends of the pipes close(stdout_pipe[1]); close(stderr_pipe[1]); - // Wait for the child process to finish + // Free the arguments + for (int i = 0; i < argc; i++) + free(argv[i]); + free(argv); + + // Wait for the child process to terminate int status; waitpid(pid, &status, 0); - // Read the output from the pipes - if (out_stdout != NULL) - read_fd(stdout_pipe[0], out_stdout); - if (out_stderr != NULL) - read_fd(stderr_pipe[0], out_stderr); + // Read the standard output and error + if (_stdout != NULL) + ReadFileDescriptor(stdout_pipe[0], _stdout); + + if (_stderr != NULL) + ReadFileDescriptor(stderr_pipe[0], _stderr); // Close the pipes close(stdout_pipe[0]); close(stderr_pipe[0]); // Set the exit code - if (out_exit_code != NULL) - *out_exit_code = WEXITSTATUS(status); + if (_exit_code != NULL) + *_exit_code = WIFEXITED(status) ? WEXITSTATUS(status) : -1; return SUCCESS; } -Result read_fd(int fd, char** out_string) { - *out_string = NULL; +Status ReadFileDescriptor(int fd, char** _content) { + *_content = NULL; - // Create a buffer for the output + // Allocate a buffer for the content char buffer[4096]; size_t length = 0; - // Read the output from the file descriptor ssize_t bytes_read; while ((bytes_read = read(fd, buffer, sizeof(buffer))) > 0) { - // Reallocate the buffer - char* new_buffer = realloc(*out_string, length + bytes_read + 1); - if (new_buffer == NULL) { - log_message(LOG_LEVEL_ERROR, "Failed to reallocate memory for the output string (%s).", strerror(errno)); - - free(*out_string); - *out_string = NULL; - - return OUT_OF_MEMORY; + char* new_content = realloc(*_content, length + bytes_read + 1); // +1 for the NULL terminator + if (new_content == NULL) { + Log(LOG_LEVEL_ERROR, "Failed to reallocate memory for the file content (%s).", strerror(errno)); + free(*_content); + return FAILURE; } + *_content = new_content; - *out_string = new_buffer; - - // Copy the new data to the buffer - memcpy(*out_string + length, buffer, bytes_read); + memcpy(*_content + length, buffer, bytes_read); length += bytes_read; + + (*_content)[length] = '\0'; } - // Check if an error occurred if (bytes_read == -1) { - log_message(LOG_LEVEL_ERROR, "Failed to read from the file descriptor (%s).", strerror(errno)); - - free(*out_string); - *out_string = NULL; - + Log(LOG_LEVEL_ERROR, "Failed to read from the file descriptor (%s).", strerror(errno)); + free(*_content); return FAILURE; } - // Null-terminate the string - if (length > 0) - (*out_string)[length] = '\0'; - return SUCCESS; } -Result read_file(const char* path, char** out_string) { - *out_string = NULL; - - // Open the file - int fd = open(path, O_RDONLY); - if (fd == -1) { - log_message(LOG_LEVEL_ERROR, "Failed to open the file '%s' (%s).", path, strerror(errno)); - return FAILURE; - } - - // Read the file - Result result = read_fd(fd, out_string); - - // Close the file - close(fd); - - return result; -} - -Result write_fd(int fd, const char* string) { - // Write the string to the file descriptor - size_t length = strlen(string); +Status WriteFileDescriptor(int fd, const char* content) { + size_t length = strlen(content); ssize_t bytes_written = 0; while (bytes_written < length) { - ssize_t result = write(fd, string + bytes_written, length - bytes_written); + ssize_t result = write(fd, content + bytes_written, length - bytes_written); if (result == -1) { - log_message(LOG_LEVEL_ERROR, "Failed to write to the file descriptor (%s).", strerror(errno)); + Log(LOG_LEVEL_ERROR, "Failed to write to the file descriptor (%s).", strerror(errno)); return FAILURE; } @@ -326,141 +288,38 @@ Result write_fd(int fd, const char* string) { return SUCCESS; } -Result write_file(const char* path, const char* string) { +Status ReadFile(const char* path, char** _content) { + *_content = NULL; + // Open the file - int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (fd == -1) { - log_message(LOG_LEVEL_ERROR, "Failed to open the file '%s' (%s).", path, strerror(errno)); + int file = open(path, O_RDONLY); + if (file == -1) { + Log(LOG_LEVEL_ERROR, "Failed to open the file %s (%s).", path, strerror(errno)); return FAILURE; } - // Write the string to the file - Result result = write_fd(fd, string); + // Read the file + Status status = ReadFileDescriptor(file, _content); // Close the file - close(fd); + close(file); - return result; + return status; } -Result copy_file(const char* src_path, const char* dst_path, mode_t mode) { - // Open the source file - int src_fd = open(src_path, O_RDONLY); - if (src_fd == -1) { - log_message(LOG_LEVEL_ERROR, "Failed to open the source file '%s' (%s).", src_path, strerror(errno)); +Status WriteFile(const char* path, const char* content) { + // Open the file + int file = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (file == -1) { + Log(LOG_LEVEL_ERROR, "Failed to open the file %s (%s).", path, strerror(errno)); return FAILURE; } - // Open the destination file - int dst_fd = open(dst_path, O_WRONLY | O_CREAT | O_TRUNC, mode); - if (dst_fd == -1) { - log_message(LOG_LEVEL_ERROR, "Failed to open the destination file '%s' (%s).", dst_path, strerror(errno)); + // Write the content + Status status = WriteFileDescriptor(file, content); - close(src_fd); - return FAILURE; - } + // Close the file + close(file); - // Copy the file - char buffer[4096]; - ssize_t bytes_read; - - while ((bytes_read = read(src_fd, buffer, sizeof(buffer))) > 0) { - ssize_t bytes_written = 0; - - while (bytes_written < bytes_read) { - ssize_t result = write(dst_fd, buffer + bytes_written, bytes_read - bytes_written); - if (result == -1) { - log_message(LOG_LEVEL_ERROR, "Failed to write to the destination file '%s' (%s).", dst_path, strerror(errno)); - - close(src_fd); - close(dst_fd); - return FAILURE; - } - - bytes_written += result; - } - } - - // Check if an error occurred - if (bytes_read == -1) { - log_message(LOG_LEVEL_ERROR, "Failed to read from the source file '%s' (%s).", src_path, strerror(errno)); - - close(src_fd); - close(dst_fd); - return FAILURE; - } - - // Close the files - close(src_fd); - close(dst_fd); - - return SUCCESS; -} - -Result format_size(uint64_t size, char** out_string) { - *out_string = NULL; - - // Determine the unit - const char* unit; - double value; - - if (size < 1024ULL) { - unit = "B"; - value = size; - } else if (size < 1024ULL * 1024) { - unit = "KiB"; - value = (double)size / 1024; - } else if (size < 1024ULL * 1024 * 1024) { - unit = "MiB"; - value = (double)size / (1024 * 1024); - } else if (size < 1024ULL * 1024 * 1024 * 1024) { - unit = "GiB"; - value = (double)size / (1024 * 1024 * 1024); - } else if (size < 1024ULL * 1024 * 1024 * 1024 * 1024) { - unit = "TiB"; - value = (double)size / (1024 * 1024 * 1024 * 1024); - } else { - unit = "PiB"; - value = (double)size / (1024 * 1024 * 1024 * 1024 * 1024); - } - - // Format the string - return format(out_string, "%.2f%s", value, unit); -} - -Result parse_size(const char* string, uint64_t* out_size) { - // Parse the size - char* endptr; - uint64_t size = strtoull(string, &endptr, 10); - - if (endptr == string) { - log_message(LOG_LEVEL_ERROR, "Failed to parse the size '%s'.", string); - return FAILURE; - } - - // Determine the unit - if (*endptr == '\0') { - // No unit - *out_size = size; - } else if (strcmp(endptr, "K") == 0 || strcmp(endptr, "KB") == 0 || strcmp(endptr, "KiB") == 0) { - // Kilobytes - *out_size = size * 1024; - } else if (strcmp(endptr, "M") == 0 || strcmp(endptr, "MB") == 0 || strcmp(endptr, "MiB") == 0) { - // Megabytes - *out_size = size * 1024 * 1024; - } else if (strcmp(endptr, "G") == 0 || strcmp(endptr, "GB") == 0 || strcmp(endptr, "GiB") == 0) { - // Gigabytes - *out_size = size * 1024 * 1024 * 1024; - } else if (strcmp(endptr, "T") == 0 || strcmp(endptr, "TB") == 0 || strcmp(endptr, "TiB") == 0) { - // Terabytes - *out_size = size * 1024 * 1024 * 1024 * 1024; - } else if (strcmp(endptr, "P") == 0 || strcmp(endptr, "PB") == 0 || strcmp(endptr, "PiB") == 0) { - // Petabytes - *out_size = size * 1024 * 1024 * 1024 * 1024 * 1024; - } else { - log_message(LOG_LEVEL_ERROR, "Unknown unit '%s'.", endptr); - return FAILURE; - } - - return SUCCESS; + return status; } diff --git a/src/utils.h b/src/utils.h index a0c10c9..a7edf1e 100644 --- a/src/utils.h +++ b/src/utils.h @@ -1,90 +1,30 @@ #pragma once -#include #include -#include - -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#include typedef enum { SUCCESS, - FAILURE, - OUT_OF_MEMORY, -} Result; + FAILURE +} Status; typedef enum { LOG_LEVEL_DEBUG, + LOG_LEVEL_INFO, LOG_LEVEL_WARNING, LOG_LEVEL_ERROR, } LogLevel; extern LogLevel log_level; -/// @brief Sets the log level. -/// @param level The log level to set. -void set_log_level(LogLevel level); +void SetLogLevel(LogLevel level); +void Log(LogLevel level, const char* format, ...); -/// @brief Logs a message. -/// @param level The log level. -/// @param format The format string. -/// @param ... The format arguments. -void log_message(LogLevel level, const char* format, ...); +Status Format(char** _string, const char* fmt, ...); -/// @brief Formats a string. -/// @param out_string The pointer to the output string. The caller is responsible for freeing the memory. -/// @param fmt The format string. -/// @param ... The format arguments. -/// @return The result of the operation. -Result format(char** out_string, const char* fmt, ...); +Status RunExecutable(int* _exit_code, char** _stdout, char** _stderr, const char* executable, ...); -/// @brief Runs an external executable with the given arguments, and waits for it to finish, then returns the exit code and the output. -/// @param out_exit_code The pointer to the output exit code. This argument can be NULL if the exit code is not needed. -/// @param out_stdout The pointer to the output stdout string. This argument can be NULL if the output is not needed. The caller is responsible for freeing the memory. -/// @param out_stderr The pointer to the output stderr string. This argument can be NULL if the output is not needed. The caller is responsible for freeing the memory. -/// @param executable The path of the executable to run. -/// @param ... The arguments of the executable. The last argument must be NULL. Note: No need to include the executable path in the arguments. -/// @return The result of the operation. -Result run_executable(int* out_exit_code, char** out_stdout, char** out_stderr, const char* executable, ...); - -/// @brief Reads the contents of a file descriptor into a string. -/// @param fd The file descriptor to read from. -/// @param out_string The pointer to the output string. The caller is responsible for freeing the memory. -/// @return The result of the operation. -Result read_fd(int fd, char** out_string); - -/// @brief Reads the contents of a file into a string. -/// @param path The path of the file to read. -/// @param out_string The pointer to the output string. The caller is responsible for freeing the memory. -/// @return The result of the operation. -Result read_file(const char* path, char** out_string); - -/// @brief Writes the given string to a file descriptor. -/// @param fd The file descriptor to write to. -/// @param string The string to write. -/// @return The result of the operation. -Result write_fd(int fd, const char* string); - -/// @brief Writes the given string to a file. -/// @param path The path of the file to write. -/// @param string The string to write. -/// @return The result of the operation. -Result write_file(const char* path, const char* string); - -/// @brief Copies a file. -/// @param src_path The path of the source file. -/// @param dst_path The path of the destination file. -/// @param mode The mode of the destination file. -/// @return The result of the operation. -Result copy_file(const char* src_path, const char* dst_path, mode_t mode); - -/// @brief Converts a size in bytes to a human-readable string. -/// @param size The size in bytes. -/// @param out_string The pointer to the output string. The caller is responsible for freeing the memory. -/// @return The result of the operation. -Result format_size(uint64_t size, char** out_string); - -/// @brief Parses a size from a human-readable string. -/// @param string The string to parse. -/// @param out_size The pointer to the output size. -/// @return The result of the operation. -Result parse_size(const char* string, uint64_t* out_size); \ No newline at end of file +Status ReadFileDescriptor(int fd, char** _content); +Status WriteFileDescriptor(int fd, const char* content); +Status ReadFile(const char* path, char** _content); +Status WriteFile(const char* path, const char* content);