Made some minor modifications

This commit is contained in:
Alexei KADIR 2024-02-16 18:26:02 +01:00
parent d925445876
commit 332f5ccfd0
5 changed files with 23 additions and 29 deletions

View File

@ -14,6 +14,7 @@ Command COMMANDS[] = {
{CMD_LIST_ENTRIES, "list-entries", "", "Lists all entries in the pool", "TODO"}, {CMD_LIST_ENTRIES, "list-entries", "", "Lists all entries in the pool", "TODO"},
{CMD_CLEAR_ENTRIES, "clear-entries", "", "Clears all entries from the pool", "TODO"}, {CMD_CLEAR_ENTRIES, "clear-entries", "", "Clears all entries from the pool", "TODO"},
{CMD_SEPARATOR}, {CMD_SEPARATOR},
{CMD_SYNC, "sync", "", "Synchronizes the backing pool with the server", "TODO"},
{CMD_ADD_BACKING, "add-backing", "<entry>", "Adds a backing disk to the pool from the given entry", "TODO"}, {CMD_ADD_BACKING, "add-backing", "<entry>", "Adds a backing disk to the pool from the given entry", "TODO"},
{CMD_REMOVE_BACKING, "remove-backing", "<backing>", "Removes a backing disk from the pool", "TODO"}, {CMD_REMOVE_BACKING, "remove-backing", "<backing>", "Removes a backing disk from the pool", "TODO"},
{CMD_LIST_BACKINGS, "list-backings", "[--tree|-t]", "Lists all backing disks in the pool", "TODO"}, {CMD_LIST_BACKINGS, "list-backings", "[--tree|-t]", "Lists all backing disks in the pool", "TODO"},
@ -53,6 +54,7 @@ Command* find_command(const char* name) {
void show_help() { void show_help() {
printf("Usage: sandbox <command> [args]\n\n"); printf("Usage: sandbox <command> [args]\n\n");
printf("Commands:\n"); printf("Commands:\n");
for (size_t i = 0; i < sizeof(COMMANDS) / sizeof(COMMANDS[0]); i++) { for (size_t i = 0; i < sizeof(COMMANDS) / sizeof(COMMANDS[0]); i++) {
if (COMMANDS[i].id == CMD_SEPARATOR) { if (COMMANDS[i].id == CMD_SEPARATOR) {
printf("\n"); printf("\n");
@ -62,20 +64,11 @@ void show_help() {
printf(" %s %s\n", COMMANDS[i].name, COMMANDS[i].args); printf(" %s %s\n", COMMANDS[i].name, COMMANDS[i].args);
printf(" %s\n", COMMANDS[i].description); printf(" %s\n", COMMANDS[i].description);
} }
printf("\n"); printf("\n");
printf("For more information about a specific command, use 'sandbox help <command>'.\n"); printf("For more information about a specific command, use 'sandbox help <command>'.\n");
} }
void show_command_help(const Command* command) {
printf("Usage: sandbox %s %s\n", command->name, command->args);
printf(" %s\n", command->description);
if (command->details != NULL) {
printf("\n");
printf("%s\n", command->details);
}
}
int cmd_help(int argc, char** argv) { int cmd_help(int argc, char** argv) {
if (argc == 0) { if (argc == 0) {
show_help(); show_help();
@ -88,7 +81,13 @@ int cmd_help(int argc, char** argv) {
return 1; return 1;
} }
show_command_help(command); printf("Usage: sandbox %s %s\n", command->name, command->args);
printf(" %s\n", command->description);
if (command->details != NULL) {
printf("\n");
printf("%s\n", command->details);
}
return 0; return 0;
} }

View File

@ -4,7 +4,7 @@
typedef enum { typedef enum {
CMD_UNKNOWN, CMD_UNKNOWN,
CMD_SEPARATOR, CMD_SEPARATOR,
CMD_HELP, CMD_HELP,
CMD_VERSION, CMD_VERSION,
@ -14,6 +14,7 @@ typedef enum {
CMD_LIST_ENTRIES, CMD_LIST_ENTRIES,
CMD_CLEAR_ENTRIES, CMD_CLEAR_ENTRIES,
CMD_SYNC,
CMD_ADD_BACKING, CMD_ADD_BACKING,
CMD_REMOVE_BACKING, CMD_REMOVE_BACKING,
CMD_LIST_BACKINGS, CMD_LIST_BACKINGS,
@ -30,7 +31,7 @@ typedef struct {
const char* name; const char* name;
const char* args; const char* args;
const char* description; const char* description;
const char* details; const char* details;
} Command; } Command;
extern Command COMMANDS[]; extern Command COMMANDS[];
@ -43,10 +44,6 @@ Command* find_command(const char* name);
/// @brief Shows the help for all commands. /// @brief Shows the help for all commands.
void show_help(); void show_help();
/// @brief Shows the help for a specific command.
/// @param command The command to show the help for.
void show_command_help(const Command* command);
int cmd_help(int argc, char** argv); int cmd_help(int argc, char** argv);
int cmd_version(int argc, char** argv); int cmd_version(int argc, char** argv);

View File

@ -15,10 +15,8 @@ bool create_empty_disk(const char* path, uint64_t size) {
// Convert the size to a string // Convert the size to a string
char* size_str = format("%lu", size); char* size_str = format("%lu", size);
if (size_str == NULL) { if (size_str == NULL)
log_message(LOG_ERROR, "Failed to allocate memory for the size string.");
return false; return false;
}
// Create the disk // Create the disk
int ret = execute(NULL, &errb, "qemu-img", "create", "-f", "qcow2", path, size_str, NULL); int ret = execute(NULL, &errb, "qemu-img", "create", "-f", "qcow2", path, size_str, NULL);
@ -71,10 +69,8 @@ bool create_backed_disk(const char* path, const char* backing_disk) {
bool trim_disk(const char* path) { bool trim_disk(const char* path) {
char* tmp_path = format("%s.tmp", path); char* tmp_path = format("%s.tmp", path);
if (tmp_path == NULL) { if (tmp_path == NULL)
log_message(LOG_ERROR, "Failed to allocate memory for the temporary disk path.");
return false; return false;
}
DiskInfo info; DiskInfo info;
if (!get_disk_info(path, &info)) { if (!get_disk_info(path, &info)) {
@ -90,10 +86,8 @@ bool trim_disk(const char* path) {
if (info.backing_file != NULL) { if (info.backing_file != NULL) {
char* backing_str = format("backing_file=%s", info.backing_file); char* backing_str = format("backing_file=%s", info.backing_file);
if (backing_str == NULL) { if (backing_str == NULL) {
log_message(LOG_ERROR, "Failed to allocate memory for the backing file string.");
free_disk_info(&info);
free(tmp_path); free(tmp_path);
free_disk_info(&info);
return false; return false;
} }

View File

@ -20,11 +20,13 @@ int main(int argc, char* argv[]) {
return 1; return 1;
} }
int consumed = 2;
switch (command->id) { switch (command->id) {
case CMD_HELP: case CMD_HELP:
return cmd_help(argc - 2, argv + 2); return cmd_help(argc - consumed, argv + consumed);
case CMD_VERSION: case CMD_VERSION:
return cmd_version(argc - 2, argv + 2); return cmd_version(argc - consumed, argv + consumed);
default: default:
log_message(LOG_ERROR, "Command '%s' is not implemented.", command->name); log_message(LOG_ERROR, "Command '%s' is not implemented.", command->name);

View File

@ -23,8 +23,10 @@ char* format(const char* fmt, ...) {
va_end(args); va_end(args);
if (length <= 0) if (length <= 0) {
log_message(LOG_ERROR, "Failed to calculate the length of the formatted string.");
return NULL; return NULL;
}
// Allocate a buffer for the formatted string // Allocate a buffer for the formatted string
char* buffer = calloc(length, sizeof(char)); char* buffer = calloc(length, sizeof(char));