Added some signatures to reprensent automatic entries

This commit is contained in:
Alexei KADIR 2024-02-17 12:00:17 +01:00
parent 5073ba3834
commit e38e07ca68
4 changed files with 67 additions and 155 deletions

View File

@ -64,136 +64,3 @@ Result entry_exists(const char* entry_id, bool* out_exists) {
return SUCCESS;
}
Result add_root_entry(const char* entry_id, uint64_t size) {
// Check that the entry does not exist
bool exists;
Result result = entry_exists(entry_id, &exists);
if (result != SUCCESS)
return result;
if (exists) {
log_message(LOG_LEVEL_ERROR, "Entry '%s' already exists.", entry_id);
return FAILURE;
}
// Get the path of the entry
char* path;
result = get_entry_path(entry_id, &path);
if (result != SUCCESS)
return result;
// Create the directory
int return_code = mkdir(path, 0755);
// Free the path as it is no longer needed
free(path);
// Check if the directory was created
if (return_code != 0) {
log_message(LOG_LEVEL_ERROR, "Failed to create the entry '%s' (%s).", entry_id, strerror(errno));
return FAILURE;
}
// Get the disk path
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 as it is no longer needed
free(disk_path);
// Remove the entry if the disk could not be created
if (result != SUCCESS) {
remove_entry(entry_id);
return result;
}
return SUCCESS;
}
Result remove_entry(const char* entry_id) {
// Remove the disk
char* disk_path;
Result result = get_entry_disk_path(entry_id, &disk_path);
if (result == SUCCESS) {
unlink(disk_path);
free(disk_path);
}
// Get the path of the entry
char* path;
result = get_entry_path(entry_id, &path);
if (result != SUCCESS)
return result;
// Remove the directory
int return_code = rmdir(path);
// Free the path as it is no longer needed
free(path);
// Check if the directory was removed
if (return_code != 0) {
log_message(LOG_LEVEL_ERROR, "Failed to remove the entry '%s' (%s).", entry_id, strerror(errno));
return FAILURE;
}
return SUCCESS;
}
Result get_entry_disk_path(const char* entry_id, char** out_path) {
*out_path = NULL;
// Get the path of the entry
char* path;
Result result = get_entry_path(entry_id, &path);
if (result != SUCCESS)
return result;
// Format the disk path
result = format(out_path, "%s/disk", path);
// Free the path as it is no longer needed
free(path);
return result;
}
Result trim_entry_disk(const char* entry_id) {
// Get the disk path
char* disk_path;
Result result = get_entry_disk_path(entry_id, &disk_path);
if (result != SUCCESS)
return result;
// Trim the disk
result = trim_disk(disk_path);
// Free the disk path as it is no longer needed
free(disk_path);
return result;
}
Result reset_entry_disk(const char* entry_id) {
// Get the disk path
char* disk_path;
Result result = get_entry_disk_path(entry_id, &disk_path);
if (result != SUCCESS)
return result;
// Reset the disk
result = reset_disk(disk_path);
// Free the disk path as it is no longer needed
free(disk_path);
return result;
}

View File

@ -19,52 +19,49 @@ bool is_entry_id_valid(const char* entry_id);
/// @return The result of the operation.
Result get_entry_path(const char* entry_id, char** out_path);
/// @brief Checks whether the given entry exists.
/// @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 a root entry to the entry pool.
/// @brief Adds a root entry to the pool.
/// @param entry_id The entry id.
/// @param size The size of the entry, in bytes.
/// @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 entry pool.
/// @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 Removes an entry from the entry pool.
/// @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 entry pool.
/// @param out_entries The pointer to the null-terminated output entries array. The caller is responsible for freeing the memory of the array and its elements.
/// @brief Lists the entries in the pool.
/// @param out_entries The pointer to the output entries string. The caller is responsible for freeing the memory of the strings and the array.
/// @return The result of the operation.
Result list_entries(char*** out_entries);
/// @brief Removes all the entries from the entry pool.
/// @brief Clears all the entries from the pool.
/// @return The result of the operation.
Result clear_entries(void);
/// @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 Trims the disk of the given entry to remove any unused space.
/// @brief Resets an entry to its initial state. Automatic entries will be backed by the latest backing disk available.
/// @param entry_id The entry id.
/// @return The result of the operation.
Result trim_entry_disk(const char* entry_id);
Result reset_entry(const char* entry_id);
/// @brief Resets the disk of the given entry to its original state.
/// @brief Updates the backing disk of the given entry if it is an automatic entry.
/// @param entry_id The entry id.
/// @return The result of the operation.
Result reset_entry_disk(const char* entry_id);
// TODO: Implement a function to reserve disk space to avoid running out of space while the sandbox is running.
Result update_entry(const char* entry_id);

View File

@ -17,6 +17,14 @@ const Command COMMANDS[] = {
{},
{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 is backed by the latest backing disk available."},
{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, "list-entries", "", "Lists the entries in the entry pool.",
"Lists the entries in the entry pool."},
{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.",
"Resets the entry with the given id to its initial state. Automatic entries will be backed by the latest backing disk available."},
{},
{command_add_backing, "add-backing", "<entry id> [<backing id>]", "Adds a backing disk to the backing pool, from the given entry.",
"TODO"},
@ -68,7 +76,7 @@ int command_help(int argc, char* argv[]) {
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 %s - %s\n", COMMANDS[i].name, COMMANDS[i].arguments, COMMANDS[i].description);
fprintf(stdout, "\nRun 'sandbox help [command]' for more information on a command.\n");
return EXIT_SUCCESS;
@ -121,6 +129,42 @@ int command_add_entry(int argc, char* argv[]) {
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;
}
// TODO: Call remove_entry
return EXIT_SUCCESS;
}
int command_list_entries(int argc, char* argv[]) {
// TODO: Call list_entries, and display the result in a nice way
return EXIT_SUCCESS;
}
int command_clear_entries(int argc, char* argv[]) {
// TODO: Ask for confirmation
// TODO: Call clear_entries
return EXIT_SUCCESS;
}
int command_reset_entry(int argc, char* argv[]) {
if (argc < 1) {
log_message(LOG_LEVEL_ERROR, "Missing entry id.");
return EXIT_FAILURE;
}
// TODO: Call reset_entry
return EXIT_SUCCESS;
}
int command_add_backing(int argc, char* argv[]) {
if (argc < 1) {
log_message(LOG_LEVEL_ERROR, "Missing entry id.");

View File

@ -18,5 +18,9 @@ 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_add_backing(int argc, char* argv[]);