diff --git a/src/sandbox.c b/src/sandbox.c index 9774ebf..f5c3016 100644 --- a/src/sandbox.c +++ b/src/sandbox.c @@ -35,10 +35,12 @@ Command COMMANDS[] = { {CommandTrimDisk, "trim-disk", "", "Trims the disk of an entry.", "TODO: Add details."}, {}, - {CommandAddBacking, "add-backing", " ", "Adds a new backing to the sandbox.", + {CommandAddBacking, "add-backing", " ", "Adds a new backing to the sandbox.", "TODO: Add details."}, {CommandRemoveBacking, "remove-backing", "", "Removes a backing from the sandbox.", "TODO: Add details."}, + {CommandListBackings, "list-backings", NULL, "Lists all the backings in the sandbox.", + "TODO: Add details."}, }; int main(int argc, char* argv[]) { @@ -242,9 +244,9 @@ int CommandListEntries(int argc, char* argv[]) { if (status != SUCCESS) 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 - 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 @@ -541,8 +543,25 @@ int CommandAddBacking(int argc, char* argv[]) { } // Extract the backing identifier and entry identifier - const char* backing_id = argv[0]; - const char* entry_id = argv[1]; + const char* entry_id = argv[0]; + 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 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); 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; +} diff --git a/src/sandbox.h b/src/sandbox.h index e875ca0..a81831a 100644 --- a/src/sandbox.h +++ b/src/sandbox.h @@ -29,3 +29,4 @@ int CommandTrimDisk(int argc, char* argv[]); int CommandAddBacking(int argc, char* argv[]); int CommandRemoveBacking(int argc, char* argv[]); +int CommandListBackings(int argc, char* argv[]);