diff --git a/doc/xml/release.xml b/doc/xml/release.xml index 8329bd4f0..6d5b39fc2 100644 --- a/doc/xml/release.xml +++ b/doc/xml/release.xml @@ -18,6 +18,14 @@ Automatically generate constants for command and option names. + + + + + + + Migrate backupRegExp() to C. + diff --git a/src/Makefile b/src/Makefile index f17ffd895..f8e476380 100644 --- a/src/Makefile +++ b/src/Makefile @@ -224,6 +224,9 @@ command/archive/push/protocol.o: command/archive/push/protocol.c command/archive command/archive/push/push.o: command/archive/push/push.c command/archive/common.h command/archive/push/file.h command/archive/push/protocol.h command/command.h command/control/control.h common/assert.h common/crypto/common.h common/debug.h common/error.auto.h common/error.h common/fork.h common/ini.h common/io/filter/filter.h common/io/filter/group.h common/io/read.h common/io/write.h common/lock.h common/log.h common/logLevel.h common/memContext.h common/stackTrace.h common/time.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/string.h common/type/stringList.h common/type/variant.h common/type/variantList.h common/wait.h config/config.auto.h config/config.h config/define.auto.h config/define.h config/exec.h info/infoArchive.h info/infoPg.h postgres/interface.h protocol/client.h protocol/command.h protocol/helper.h protocol/parallel.h protocol/parallelJob.h protocol/server.h storage/fileRead.h storage/fileWrite.h storage/helper.h storage/info.h storage/storage.h $(CC) $(CFLAGS) -c command/archive/push/push.c -o command/archive/push/push.o +command/backup/common.o: command/backup/common.c command/backup/common.h common/assert.h common/debug.h common/error.auto.h common/error.h common/log.h common/logLevel.h common/memContext.h common/stackTrace.h common/type/buffer.h common/type/convert.h common/type/string.h + $(CC) $(CFLAGS) -c command/backup/common.c -o command/backup/common.o + command/command.o: command/command.c common/assert.h common/debug.h common/error.auto.h common/error.h common/lock.h common/log.h common/logLevel.h common/memContext.h common/stackTrace.h common/time.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/string.h common/type/stringList.h common/type/variant.h common/type/variantList.h config/config.auto.h config/config.h config/define.auto.h config/define.h version.h $(CC) $(CFLAGS) -c command/command.c -o command/command.o diff --git a/src/command/backup/common.c b/src/command/backup/common.c new file mode 100644 index 000000000..0f48f9c9e --- /dev/null +++ b/src/command/backup/common.c @@ -0,0 +1,76 @@ +/*********************************************************************************************************************************** +Common Functions and Definitions for Backup and Expire Commands +***********************************************************************************************************************************/ +#include "command/backup/common.h" +#include "common/debug.h" +#include "common/log.h" + +/*********************************************************************************************************************************** +Constants +***********************************************************************************************************************************/ +#define DATE_TIME_REGEX "[0-9]{8}\\-[0-9]{6}" + +/*********************************************************************************************************************************** +Returns an anchored regex string for filtering backups based on the type (at least one type is required to be true) +***********************************************************************************************************************************/ +String * +backupRegExp(BackupRegExpParam param) +{ + FUNCTION_LOG_BEGIN(logLevelTrace); + FUNCTION_LOG_PARAM(BOOL, param.full); + FUNCTION_LOG_PARAM(BOOL, param.differential); + FUNCTION_LOG_PARAM(BOOL, param.incremental); + FUNCTION_LOG_END(); + + ASSERT(param.full || param.differential || param.incremental); + + String *result = NULL; + + // Start the expression with the anchor, date/time regexp and full backup indicator + result = strNew("^" DATE_TIME_REGEX "F"); + + // Add the diff and/or incr expressions if requested + if (param.differential || param.incremental) + { + // If full requested then diff/incr is optional + if (param.full) + { + strCat(result, "(\\_"); + } + // Else diff/incr is required + else + { + strCat(result, "\\_"); + } + + // Append date/time regexp for diff/incr + strCat(result, DATE_TIME_REGEX); + + // Filter on both diff/incr + if (param.differential && param.incremental) + { + strCat(result, "(D|I)"); + } + // Else just diff + else if (param.differential) + { + strCatChr(result, 'D'); + } + // Else just incr + else + { + strCatChr(result, 'I'); + } + + // If full requested then diff/incr is optional + if (param.full) + { + strCat(result, "){0,1}"); + } + } + + // Append the end anchor + strCat(result, "$"); + + FUNCTION_LOG_RETURN(STRING, result); +} diff --git a/src/command/backup/common.h b/src/command/backup/common.h new file mode 100644 index 000000000..2d37cc656 --- /dev/null +++ b/src/command/backup/common.h @@ -0,0 +1,26 @@ +/*********************************************************************************************************************************** +Common Functions and Definitions for Backup and Expire Commands +***********************************************************************************************************************************/ +#ifndef COMMAND_BACKUP_COMMON_H +#define COMMAND_BACKUP_COMMON_H + +#include + +#include "common/type/string.h" + +/*********************************************************************************************************************************** +Returns an anchored regex string for filtering backups based on the type (at least one type is required to be true) +***********************************************************************************************************************************/ +typedef struct BackupRegExpParam +{ + bool full; + bool differential; + bool incremental; +} BackupRegExpParam; + +#define backupRegExpP(...) \ + backupRegExp((BackupRegExpParam){__VA_ARGS__}) + +String *backupRegExp(BackupRegExpParam param); + +#endif diff --git a/test/define.yaml b/test/define.yaml index 7224bad50..5a6786fce 100644 --- a/test/define.yaml +++ b/test/define.yaml @@ -661,6 +661,13 @@ unit: command/archive/push/protocol: full command/archive/push/push: full + # ---------------------------------------------------------------------------------------------------------------------------- + - name: backup-common + total: 1 + + coverage: + command/backup/common: full + # ---------------------------------------------------------------------------------------------------------------------------- - name: command total: 1 diff --git a/test/src/module/command/backupCommonTest.c b/test/src/module/command/backupCommonTest.c new file mode 100644 index 000000000..cb4275322 --- /dev/null +++ b/test/src/module/command/backupCommonTest.c @@ -0,0 +1,104 @@ +/*********************************************************************************************************************************** +Test Common Functions and Definitions for Backup and Expire Commands +***********************************************************************************************************************************/ +#include "common/harnessConfig.h" +#include "storage/driver/posix/storage.h" + +#include + +/*********************************************************************************************************************************** +Test Run +***********************************************************************************************************************************/ +void +testRun(void) +{ + FUNCTION_HARNESS_VOID(); + + // ***************************************************************************************************************************** + if (testBegin("backupRegExp()")) + { + String *full = strNew("20181119-152138F"); + String *incr = strNew("20181119-152138F_20181119-152152I"); + String *diff = strNew("20181119-152138F_20181119-152152D"); + + // ------------------------------------------------------------------------------------------------------------------------- + TEST_ERROR( + backupRegExpP(0), + AssertError, "assertion 'param.full || param.differential || param.incremental' failed"); + + // ------------------------------------------------------------------------------------------------------------------------- + String *filter = backupRegExpP(.full = true); + TEST_RESULT_STR(strPtr(filter), "^[0-9]{8}\\-[0-9]{6}F$", "full backup regex with anchors"); + TEST_RESULT_BOOL(regExpMatchOne(filter, incr), false, " does not exactly match incr"); + TEST_RESULT_BOOL(regExpMatchOne(filter, diff), false, " does not exactly match diff"); + TEST_RESULT_BOOL(regExpMatchOne(filter, full), true, " exactly matches full"); + + // ------------------------------------------------------------------------------------------------------------------------- + filter = backupRegExpP(.full = true, .incremental = true); + TEST_RESULT_STR( + strPtr(filter), + "^[0-9]{8}\\-[0-9]{6}F(\\_[0-9]{8}\\-[0-9]{6}I){0,1}$", "full and optional incr backup regex with anchors"); + TEST_RESULT_BOOL(regExpMatchOne(filter, incr), true, " match incr"); + TEST_RESULT_BOOL(regExpMatchOne(filter, diff), false, " does not match diff"); + TEST_RESULT_BOOL(regExpMatchOne(filter, full), true, " match full"); + TEST_RESULT_BOOL( + regExpMatchOne( + filter, strNew("12341234-123123F_12341234-123123IG")), false, " does not match with trailing character"); + TEST_RESULT_BOOL( + regExpMatchOne( + filter, strNew("A12341234-123123F_12341234-123123I")), false, " does not match with leading character"); + + // ------------------------------------------------------------------------------------------------------------------------- + filter = backupRegExpP(.full = true, .differential = true); + TEST_RESULT_STR( + strPtr(filter), + "^[0-9]{8}\\-[0-9]{6}F(\\_[0-9]{8}\\-[0-9]{6}D){0,1}$", "full and optional diff backup regex with anchors"); + TEST_RESULT_BOOL(regExpMatchOne(filter, incr), false, " does not match incr"); + TEST_RESULT_BOOL(regExpMatchOne(filter, diff), true, " match diff"); + TEST_RESULT_BOOL(regExpMatchOne(filter, full), true, " match full"); + + // ------------------------------------------------------------------------------------------------------------------------- + filter = backupRegExpP(.full = true, .incremental = true, .differential = true); + TEST_RESULT_STR( + strPtr(filter), + "^[0-9]{8}\\-[0-9]{6}F(\\_[0-9]{8}\\-[0-9]{6}(D|I)){0,1}$", "full, optional diff and incr backup regex with anchors"); + TEST_RESULT_BOOL(regExpMatchOne(filter, incr), true, " match incr"); + TEST_RESULT_BOOL(regExpMatchOne(filter, diff), true, " match diff"); + TEST_RESULT_BOOL(regExpMatchOne(filter, full), true, " match full"); + + // ------------------------------------------------------------------------------------------------------------------------- + filter = backupRegExpP(.incremental = true, .differential = true); + TEST_RESULT_STR( + strPtr(filter), + "^[0-9]{8}\\-[0-9]{6}F\\_[0-9]{8}\\-[0-9]{6}(D|I)$", "diff and incr backup regex with anchors"); + TEST_RESULT_BOOL(regExpMatchOne(filter, incr), true, " match incr"); + TEST_RESULT_BOOL(regExpMatchOne(filter, diff), true, " match diff"); + TEST_RESULT_BOOL(regExpMatchOne(filter, full), false, " does not match full"); + TEST_RESULT_BOOL( + regExpMatchOne( + filter, strNew("12341234-123123F_12341234-123123DG")), false, " does not match with trailing character"); + TEST_RESULT_BOOL( + regExpMatchOne( + filter, strNew("A12341234-123123F_12341234-123123I")), false, " does not match with leading character"); + + // ------------------------------------------------------------------------------------------------------------------------- + filter = backupRegExpP(.incremental = true); + TEST_RESULT_STR( + strPtr(filter), + "^[0-9]{8}\\-[0-9]{6}F\\_[0-9]{8}\\-[0-9]{6}I$", "incr backup regex with anchors"); + TEST_RESULT_BOOL(regExpMatchOne(filter, incr), true, " match incr"); + TEST_RESULT_BOOL(regExpMatchOne(filter, diff), false, " does not match diff"); + TEST_RESULT_BOOL(regExpMatchOne(filter, full), false, " does not match full"); + + // ------------------------------------------------------------------------------------------------------------------------- + filter = backupRegExpP(.differential = true); + TEST_RESULT_STR( + strPtr(filter), + "^[0-9]{8}\\-[0-9]{6}F\\_[0-9]{8}\\-[0-9]{6}D$", "diff backup regex with anchors"); + TEST_RESULT_BOOL(regExpMatchOne(filter, incr), false, " does not match incr"); + TEST_RESULT_BOOL(regExpMatchOne(filter, diff), true, " match diff"); + TEST_RESULT_BOOL(regExpMatchOne(filter, full), false, " does not match full"); + } + + FUNCTION_HARNESS_RESULT_VOID(); +}
Automatically generate constants for command and option names.
Migrate backupRegExp() to C.
backupRegExp()