1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-01-18 04:58:51 +02:00

Update repo-ls command to work better with files.

If the command was passed a file it would return no results since it was originally intended to list files when passed a path.

However, as a general purpose command working directly with files makes sense.
This commit is contained in:
David Steele 2020-03-09 16:54:07 -04:00
parent 5e1291a29f
commit 948835fb84
2 changed files with 44 additions and 7 deletions

View File

@ -37,8 +37,8 @@ storageListRenderCallback(void *data, const StorageInfo *info)
StorageListRenderCallbackData *listData = (StorageListRenderCallbackData *)data;
// Skip . path when output is text
if (info->type == storageTypePath && strEq(info->name, DOT_STR) && !listData->json)
// Skip . path if it is not first when json output
if (info->type == storageTypePath && strEq(info->name, DOT_STR) && (!listData->first || !listData->json))
{
FUNCTION_TEST_RETURN_VOID();
return;
@ -150,9 +150,26 @@ storageListRender(IoWrite *write)
if (data.json)
ioWrite(data.write, BRACEL_BUF);
storageInfoListP(
storageRepo(), path, storageListRenderCallback, &data, .sortOrder = sortOrder, .expression = cfgOptionStr(cfgOptFilter),
.recurse = cfgOptionBool(cfgOptRecurse));
// Check if this is a file
StorageInfo info = storageInfoP(storageRepo(), path, .ignoreMissing = true);
if (info.exists && info.type == storageTypeFile)
{
info.name = DOT_STR;
storageListRenderCallback(&data, &info);
}
// Else try to list the path
else
{
// The path will always be reported as existing so we don't get different results from storage that does not support paths
if (data.json)
storageListRenderCallback(&data, &(StorageInfo){.type = storageTypePath, .name = DOT_STR});
// List content of the path
storageInfoListP(
storageRepo(), path, storageListRenderCallback, &data, .sortOrder = sortOrder, .expression = cfgOptionStr(cfgOptFilter),
.recurse = cfgOptionBool(cfgOptRecurse));
}
if (data.json)
ioWrite(data.write, BRACER_BUF);

View File

@ -37,7 +37,12 @@ testRun(void)
output = bufNew(0);
cfgOptionSet(cfgOptOutput, cfgSourceParam, VARSTRDEF("json"));
TEST_RESULT_VOID(storageListRender(ioBufferWriteNew(output)), "missing directory (json)");
TEST_RESULT_STR_Z(strNewBuf(output), "{}", " check output");
TEST_RESULT_STR_Z(
strNewBuf(output),
"{"
"\".\":{\"type\":\"path\"}"
"}",
" check output");
// Empty directory
// -------------------------------------------------------------------------------------------------------------------------
@ -76,7 +81,7 @@ testRun(void)
output = bufNew(0);
cfgOptionSet(cfgOptOutput, cfgSourceParam, VARSTRDEF("json"));
TEST_RESULT_VOID(storageListRender(ioBufferWriteNew(output)), "path and file (text)");
TEST_RESULT_VOID(storageListRender(ioBufferWriteNew(output)), "path and file (json)");
TEST_RESULT_STR_Z(
strNewBuf(output),
"{"
@ -150,6 +155,21 @@ testRun(void)
output = bufNew(0);
TEST_ERROR(storageListRender(ioBufferWriteNew(output)), ParamInvalidError, "only one path may be specified");
// File
// -------------------------------------------------------------------------------------------------------------------------
argList = strLstNew();
strLstAdd(argList, strNewFmt("--repo-path=%s/repo/aaa", testPath()));
strLstAddZ(argList, "--output=json");
harnessCfgLoad(cfgCmdRepoLs, argList);
TEST_RESULT_VOID(storageListRender(ioBufferWriteNew(output)), "file (json)");
TEST_RESULT_STR_Z(
strNewBuf(output),
"{"
"\".\":{\"type\":\"file\",\"size\":8,\"time\":1578671569}"
"}",
" check output");
}
FUNCTION_HARNESS_RESULT_VOID();