Modified some signatures

This commit is contained in:
Alexei KADIR 2024-02-17 02:18:16 +01:00
parent ab244698c5
commit a2339d7515
6 changed files with 32 additions and 50 deletions

View File

@ -7,7 +7,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <json-c/json.h> #include <json-c/json.h>
Result create_empty_disk(const char* path, uint64_t size, mode_t permissions) { Result create_root_disk(const char* path, uint64_t size, mode_t permissions) {
// Convert the size to a string // Convert the size to a string
char* size_str; char* size_str;
Result result = format(&size_str, "%lu", size); Result result = format(&size_str, "%lu", size);

View File

@ -18,12 +18,12 @@ typedef struct {
char* backing_file; char* backing_file;
} DiskInfo; } DiskInfo;
/// @brief Creates an empty disk at the given path, with the given size and with the given permissions. /// @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 path The path to the disk to create.
/// @param size The size of the disk to create. /// @param size The size of the disk to create.
/// @param permissions The permissions to set on the disk. /// @param permissions The permissions to set on the disk.
/// @return The result of the operation. /// @return The result of the operation.
Result create_empty_disk(const char* path, uint64_t size, mode_t permissions); Result create_root_disk(const char* path, uint64_t size, mode_t permissions);
/// @brief Creates a backed disk at the given path, with the given backing disk and with the given permissions. /// @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 path The path to the disk to create.

View File

@ -62,34 +62,3 @@ Result entry_exists(const char* entry_id, bool* out_exists) {
return SUCCESS; return SUCCESS;
} }
Result add_entry(const char* entry_id) {
// Check if the entry already exists
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;
}
// Create the entry directory
char* path;
result = get_entry_path(entry_id, &path);
if (result != SUCCESS)
return result;
int result_code = mkdir(path, 0755);
free(path);
// Check if the operation failed
if (result_code != 0) {
log_message(LOG_LEVEL_ERROR, "Failed to create entry '%s' (%s).", entry_id, strerror(errno));
return FAILURE;
}
return SUCCESS;
}

View File

@ -25,10 +25,17 @@ Result get_entry_path(const char* entry_id, char** out_path);
/// @return The result of the operation. /// @return The result of the operation.
Result entry_exists(const char* entry_id, bool* out_exists); Result entry_exists(const char* entry_id, bool* out_exists);
/// @brief Adds an entry to the entry pool. /// @brief Adds a root entry to the entry pool.
/// @param entry_id The entry id. /// @param entry_id The entry id.
/// @param size The size of the entry, in bytes.
/// @return The result of the operation. /// @return The result of the operation.
Result add_entry(const char* entry_id); Result add_root_entry(const char* entry_id, uint64_t size);
/// @brief Adds a backed entry to the entry 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 Removes an entry from the entry pool.
/// @param entry_id The entry id. /// @param entry_id The entry id.

View File

@ -17,6 +17,9 @@ const Command COMMANDS[] = {
{}, {},
{command_add_entry, "add-entry", "<entry id> [--root|-r <size>] [--backed|-b <backing id>]", "Adds an entry to the entry pool.", {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."}, "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_add_backing, "add-backing", "<entry id> [<backing id>]", "Adds a backing disk to the backing pool, from the given entry.",
"TODO"},
}; };
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
@ -111,19 +114,20 @@ int command_add_entry(int argc, char* argv[]) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
// TODO: Parse arguments // TODO: Parse options
// Extract the entry id // TODO: Call add_root_entry or add_backed_entry depending on the options
const char* entry_id = argv[0];
// Create the entry
Result result = add_entry(entry_id);
if (result != SUCCESS)
return result;
// Add the disk to the entry
// If this fails, remove the entry
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
int command_add_backing(int argc, char* argv[]) {
if (argc < 1) {
log_message(LOG_LEVEL_ERROR, "Missing entry id.");
return EXIT_FAILURE;
}
// TODO: Call add_backing
return EXIT_SUCCESS;
}

View File

@ -17,4 +17,6 @@ int main(int argc, char* argv[]);
int command_help(int argc, char* argv[]); int command_help(int argc, char* argv[]);
int command_version(int argc, char* argv[]); int command_version(int argc, char* argv[]);
int command_add_entry(int argc, char* argv[]); int command_add_entry(int argc, char* argv[]);
int command_add_backing(int argc, char* argv[]);