Added automatic backing id generation

This commit is contained in:
Alexei KADIR 2024-02-18 15:33:25 +01:00
parent 5613fbdcab
commit c9f76746b4
2 changed files with 49 additions and 5 deletions

View File

@ -35,10 +35,12 @@ Command COMMANDS[] = {
{CommandTrimDisk, "trim-disk", "<entry id>", "Trims the disk of an entry.", {CommandTrimDisk, "trim-disk", "<entry id>", "Trims the disk of an entry.",
"TODO: Add details."}, "TODO: Add details."},
{}, {},
{CommandAddBacking, "add-backing", "<backing id> <entry id>", "Adds a new backing to the sandbox.", {CommandAddBacking, "add-backing", "<entry id> <description>", "Adds a new backing to the sandbox.",
"TODO: Add details."}, "TODO: Add details."},
{CommandRemoveBacking, "remove-backing", "<backing id>", "Removes a backing from the sandbox.", {CommandRemoveBacking, "remove-backing", "<backing id>", "Removes a backing from the sandbox.",
"TODO: Add details."}, "TODO: Add details."},
{CommandListBackings, "list-backings", NULL, "Lists all the backings in the sandbox.",
"TODO: Add details."},
}; };
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
@ -242,9 +244,9 @@ int CommandListEntries(int argc, char* argv[]) {
if (status != SUCCESS) if (status != SUCCESS)
continue; continue;
fprintf(stdout, "%zu | %-*s | %-10s | %s%s\n", i, (int)max_length, entries[i], size_fmt, info.backing_identifier == NULL ? "No backing" : "Backed by ", info.backing_identifier == NULL ? "" : info.backing_identifier); fprintf(stdout, "%-*s | %-10s | %s%s\n", (int)max_length, entries[i], size_fmt, info.backing_identifier == NULL ? "No backing" : "Backed by ", info.backing_identifier == NULL ? "" : info.backing_identifier);
} else } else
fprintf(stdout, "%zu | %-*s | %s\n", i, (int)max_length, entries[i], "No disk "); fprintf(stdout, "%-*s | %s\n", (int)max_length, entries[i], "No disk ");
} }
// Free the entries // Free the entries
@ -541,8 +543,25 @@ int CommandAddBacking(int argc, char* argv[]) {
} }
// Extract the backing identifier and entry identifier // Extract the backing identifier and entry identifier
const char* backing_id = argv[0]; const char* entry_id = argv[0];
const char* entry_id = argv[1]; const char* backing_description = argv[1];
// Generate the backing id by incrementing the latest backing index
char* latest_backing;
Status status = GetLatestBacking(&latest_backing);
if (status != SUCCESS)
return EXIT_FAILURE;
int backing_index = 0;
if (latest_backing != NULL) {
backing_index = GetBackingIndex(latest_backing) + 1;
free(latest_backing);
}
char* backing_id;
status = Format(&backing_id, "%d - %s", backing_index, backing_description);
if (status != SUCCESS)
return EXIT_FAILURE;
// Add the backing // Add the backing
Status status = AddBacking(backing_id, entry_id); Status status = AddBacking(backing_id, entry_id);
@ -573,3 +592,27 @@ int CommandRemoveBacking(int argc, char* argv[]) {
Log(LOG_LEVEL_INFO, "Successfully removed backing '%s'.", backing_id); Log(LOG_LEVEL_INFO, "Successfully removed backing '%s'.", backing_id);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
int CommandListBackings(int argc, char* argv[]) {
if (argc > 0) {
Log(LOG_LEVEL_ERROR, "Too many arguments supplied to command 'list-backings'.");
return EXIT_FAILURE;
}
// List the backings
char** backings = NULL;
Status status = ListBackings(&backings);
if (status != SUCCESS)
return EXIT_FAILURE;
// Print the backings
for (size_t i = 0; backings[i] != NULL; i++)
fprintf(stdout, "%s\n", backings[i]);
// Free the backings
for (size_t i = 0; backings[i] != NULL; i++)
free(backings[i]);
free(backings);
return EXIT_SUCCESS;
}

View File

@ -29,3 +29,4 @@ int CommandTrimDisk(int argc, char* argv[]);
int CommandAddBacking(int argc, char* argv[]); int CommandAddBacking(int argc, char* argv[]);
int CommandRemoveBacking(int argc, char* argv[]); int CommandRemoveBacking(int argc, char* argv[]);
int CommandListBackings(int argc, char* argv[]);