1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-03-03 14:52:21 +02:00

Add backup type conversion functions.

Convert back and forth between the string and enum representations of backup types.
This commit is contained in:
David Steele 2019-08-18 20:09:44 -04:00
parent 3df075bf40
commit 8aa1e552b0
4 changed files with 101 additions and 1 deletions

View File

@ -12,6 +12,10 @@ Constants
***********************************************************************************************************************************/
#define DATE_TIME_REGEX "[0-9]{8}\\-[0-9]{6}"
STRING_EXTERN(BACKUP_TYPE_FULL_STR, BACKUP_TYPE_FULL);
STRING_EXTERN(BACKUP_TYPE_DIFF_STR, BACKUP_TYPE_DIFF);
STRING_EXTERN(BACKUP_TYPE_INCR_STR, BACKUP_TYPE_INCR);
/***********************************************************************************************************************************
Returns an anchored regex string for filtering backups based on the type (at least one type is required to be true)
***********************************************************************************************************************************/
@ -76,3 +80,63 @@ backupRegExp(BackupRegExpParam param)
FUNCTION_LOG_RETURN(STRING, result);
}
/***********************************************************************************************************************************
Convert text backup type to an enum and back
***********************************************************************************************************************************/
BackupType
backupType(const String *type)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, type);
FUNCTION_TEST_END();
ASSERT(type != NULL);
BackupType result;
if (strEq(type, BACKUP_TYPE_FULL_STR))
result = backupTypeFull;
else if (strEq(type, BACKUP_TYPE_DIFF_STR))
result = backupTypeDiff;
else if (strEq(type, BACKUP_TYPE_INCR_STR))
result = backupTypeIncr;
else
THROW_FMT(AssertError, "invalid backup type '%s'", strPtr(type));
FUNCTION_TEST_RETURN(result);
}
const String *backupTypeStr(BackupType type)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(ENUM, type);
FUNCTION_TEST_END();
ASSERT(type <= backupTypeIncr);
const String *result = NULL;
switch (type)
{
case backupTypeFull:
{
result = BACKUP_TYPE_FULL_STR;
break;
}
case backupTypeDiff:
{
result = BACKUP_TYPE_DIFF_STR;
break;
}
case backupTypeIncr:
{
result = BACKUP_TYPE_INCR_STR;
break;
}
}
FUNCTION_TEST_RETURN(result);
}

View File

@ -8,6 +8,23 @@ Common Functions and Definitions for Backup and Expire Commands
#include "common/type/string.h"
/***********************************************************************************************************************************
Backup type enum and contants
***********************************************************************************************************************************/
typedef enum
{
backupTypeFull,
backupTypeDiff,
backupTypeIncr,
} BackupType;
#define BACKUP_TYPE_FULL "full"
STRING_DECLARE(BACKUP_TYPE_FULL_STR);
#define BACKUP_TYPE_DIFF "diff"
STRING_DECLARE(BACKUP_TYPE_DIFF_STR);
#define BACKUP_TYPE_INCR "incr"
STRING_DECLARE(BACKUP_TYPE_INCR_STR);
/***********************************************************************************************************************************
Returns an anchored regex string for filtering backups based on the type (at least one type is required to be true)
***********************************************************************************************************************************/
@ -23,4 +40,10 @@ typedef struct BackupRegExpParam
String *backupRegExp(BackupRegExpParam param);
/***********************************************************************************************************************************
Convert text backup type to an enum and back
***********************************************************************************************************************************/
BackupType backupType(const String *type);
const String *backupTypeStr(BackupType type);
#endif

View File

@ -594,7 +594,7 @@ unit:
# ----------------------------------------------------------------------------------------------------------------------------
- name: backup-common
total: 2
total: 3
coverage:
command/backup/common: full

View File

@ -254,5 +254,18 @@ testRun(void)
TEST_ERROR(ioWrite(write, buffer), AssertError, "should not be possible to see two misaligned pages in a row");
}
// *****************************************************************************************************************************
if (testBegin("backupType() and backupTypeStr()"))
{
TEST_RESULT_UINT(backupType(strNew("full")), backupTypeFull, "backup type full");
TEST_RESULT_UINT(backupType(strNew("diff")), backupTypeDiff, "backup type diff");
TEST_RESULT_UINT(backupType(strNew("incr")), backupTypeIncr, "backup type incr");
TEST_ERROR(backupType(strNew("bogus")), AssertError, "invalid backup type 'bogus'");
TEST_RESULT_STR(strPtr(backupTypeStr(backupTypeFull)), "full", "backup type str full");
TEST_RESULT_STR(strPtr(backupTypeStr(backupTypeDiff)), "diff", "backup type str diff");
TEST_RESULT_STR(strPtr(backupTypeStr(backupTypeIncr)), "incr", "backup type str incr");
}
FUNCTION_HARNESS_RESULT_VOID();
}