Reimplemented most functions

This commit is contained in:
Alexei KADIR 2024-02-17 23:59:38 +01:00
parent 1cd34dd514
commit 487982a182
10 changed files with 577 additions and 1207 deletions

View File

@ -1,9 +0,0 @@
#include "backing.h"
#include "entry.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>

View File

@ -1,10 +0,0 @@
#pragma once
#include "utils.h"
#include "disk.h"
#include <stdbool.h>
#include <stdint.h>
#define BACKING_POOL_DIR "/var/lib/sandbox/backings"
#define MAX_BACKING_LENGTH 256

View File

@ -1,56 +1,44 @@
#include "disk.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <json-c/json.h>
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;
}

View File

@ -2,53 +2,17 @@
#include "utils.h"
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
/// @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);

File diff suppressed because it is too large Load Diff

View File

@ -1,99 +1,24 @@
#pragma once
#include "utils.h"
#include "disk.h"
#include <stdbool.h>
#include <stdint.h>
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);

View File

@ -1,85 +1,65 @@
#include "sandbox.h"
#include "utils.h"
#include "backing.h"
#include "entry.h"
#include "disk.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pwd.h>
#include <unistd.h>
#include <time.h>
#include <sys/resource.h>
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", "<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 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", "<entry id>", "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", "<entry id>", "Resets an entry to its initial state.",
"TODO"},
{command_update_entry, "update-entry", "<entry id>", "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 <command> [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 <command>'.\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;
}

View File

@ -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[]);
int CommandHelp(int argc, char* argv[]);
int CommandVersion(int argc, char* argv[]);

View File

@ -3,21 +3,21 @@
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/types.h>
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;
}

View File

@ -1,90 +1,30 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#include <stdbool.h>
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);
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);