/*********************************************************************************************************************************** Archive Info Handler ***********************************************************************************************************************************/ #include #include #include #include #include #include "common/debug.h" #include "common/format/cipherBlockFormat.h" #include "common/format/format.h" #include "common/ini.h" #include "common/io/bufferWrite.h" #include "common/io/io.h" #include "common/log.h" #include "common/type/convert.h" #include "info/infoArchive.intern.h" #include "info/infoPg.h" #include "postgres/interface.h" #include "postgres/version.h" #include "storage/helper.h" /*********************************************************************************************************************************** Constants ***********************************************************************************************************************************/ STRING_EXTERN(INFO_ARCHIVE_PATH_FILE_STR, INFO_ARCHIVE_PATH_FILE); STRING_EXTERN(INFO_ARCHIVE_PATH_FILE_COPY_STR, INFO_ARCHIVE_PATH_FILE_COPY); /*********************************************************************************************************************************** Object type ***********************************************************************************************************************************/ struct InfoArchive { InfoArchivePub pub; // Publicly accessible variables }; /*********************************************************************************************************************************** Internal constructor ***********************************************************************************************************************************/ static InfoArchive * infoArchiveNewInternal(void) { FUNCTION_TEST_VOID(); InfoArchive *this = OBJ_NEW_ALLOC(); *this = (InfoArchive) { .pub = { .memContext = memContextCurrent(), }, }; FUNCTION_TEST_RETURN(INFO_ARCHIVE, this); } /**********************************************************************************************************************************/ FN_EXTERN InfoArchive * infoArchiveNew( const unsigned int pgVersion, const uint64_t pgSystemId, const unsigned int format, const CipherSpecMap *const cipherSpecMapSub) { FUNCTION_LOG_BEGIN(logLevelDebug); FUNCTION_LOG_PARAM(UINT, pgVersion); FUNCTION_LOG_PARAM(UINT64, pgSystemId); FUNCTION_LOG_PARAM(UINT, format); FUNCTION_LOG_PARAM(CIPHER_SPEC_MAP, cipherSpecMapSub); FUNCTION_LOG_END(); ASSERT(pgVersion > 0 && pgSystemId > 0); InfoArchive *this; OBJ_NEW_BASE_BEGIN(InfoArchive, .childQty = MEM_CONTEXT_QTY_MAX) { this = infoArchiveNewInternal(); // Initialize the pg data this->pub.infoPg = infoPgNew(infoPgArchive, format, NULL); infoCipherSpecMapSet(infoPgInfo(this->pub.infoPg), cipherSpecMapSub); infoArchivePgSet(this, pgVersion, pgSystemId); } OBJ_NEW_END(); FUNCTION_LOG_RETURN(INFO_ARCHIVE, this); } /**********************************************************************************************************************************/ FN_EXTERN InfoArchive * infoArchiveNewLoad(IoRead *const read, const CipherSpec *const cipherSpec) { FUNCTION_LOG_BEGIN(logLevelDebug); FUNCTION_LOG_PARAM(IO_READ, read); FUNCTION_LOG_PARAM(CIPHER_SPEC, cipherSpec); FUNCTION_LOG_END(); ASSERT(read != NULL); InfoArchive *this; OBJ_NEW_BASE_BEGIN(InfoArchive, .childQty = MEM_CONTEXT_QTY_MAX) { this = infoArchiveNewInternal(); this->pub.infoPg = infoPgNewLoad(read, infoPgArchive, cipherSpec, NULL, NULL); } OBJ_NEW_END(); FUNCTION_LOG_RETURN(INFO_ARCHIVE, this); } /**********************************************************************************************************************************/ FN_EXTERN void infoArchiveCipherRotate(InfoArchive *const this, const CipherSpec *const cipherSpec, const time_t rotateTime) { FUNCTION_LOG_BEGIN(logLevelDebug); FUNCTION_LOG_PARAM(INFO_ARCHIVE, this); FUNCTION_LOG_PARAM(CIPHER_SPEC, cipherSpec); FUNCTION_LOG_PARAM(TIME, rotateTime); FUNCTION_LOG_END(); ASSERT(this != NULL); ASSERT(infoArchiveFormat(this) >= REPOSITORY_FORMAT_6); MEM_CONTEXT_TEMP_BEGIN() { // The next key id follows the current key id, or is the first key id when there is no current key const String *const idCurrent = cipherSpecMapIdCurrent(infoArchiveCipherSpecMap(this)); const String *const id = strNewFmt("%u", idCurrent == NULL ? 1 : cvtZToUInt(strZ(idCurrent)) + 1); Info *const info = infoPgInfo(infoArchivePg(this)); infoCipherSpecAdd(info, id, cipherSpec); infoCipherRotateTimeSet(info, rotateTime); } MEM_CONTEXT_TEMP_END(); FUNCTION_LOG_RETURN_VOID(); } /**********************************************************************************************************************************/ const String * infoArchiveIdHistoryMatch( const InfoArchive *const this, const unsigned int historyId, const unsigned int pgVersion, const uint64_t pgSystemId) { FUNCTION_LOG_BEGIN(logLevelTrace); FUNCTION_LOG_PARAM(INFO_ARCHIVE, this); FUNCTION_LOG_PARAM(UINT, historyId); FUNCTION_LOG_PARAM(UINT, pgVersion); FUNCTION_LOG_PARAM(UINT64, pgSystemId); FUNCTION_LOG_END(); ASSERT(this != NULL); String *archiveId = NULL; const InfoPg *const infoPg = infoArchivePg(this); // Search the history list, from newest to oldest for (unsigned int pgIdx = 0; pgIdx < infoPgDataTotal(infoPg); pgIdx++) { const InfoPgData pgDataArchive = infoPgData(infoPg, pgIdx); // If there is an exact match with the history, system and version then get the archiveId and stop if (historyId == pgDataArchive.id && pgSystemId == pgDataArchive.systemId && pgVersion == pgDataArchive.version) { archiveId = infoPgArchiveId(infoPg, pgIdx); break; } } // If there was not an exact match, then search for the first matching database system-id and version if (archiveId == NULL) { for (unsigned int pgIdx = 0; pgIdx < infoPgDataTotal(infoPg); pgIdx++) { const InfoPgData pgDataArchive = infoPgData(infoPg, pgIdx); if (pgSystemId == pgDataArchive.systemId && pgVersion == pgDataArchive.version) { archiveId = infoPgArchiveId(infoPg, pgIdx); break; } } } // If the archive id has not been found, then error if (archiveId == NULL) { THROW_FMT( ArchiveMismatchError, "unable to retrieve the archive id for database version '%s' and system-id '%" PRIu64 "'", strZ(pgVersionToStr(pgVersion)), pgSystemId); } FUNCTION_LOG_RETURN(STRING, archiveId); } /*********************************************************************************************************************************** Save to file ***********************************************************************************************************************************/ static void infoArchiveSave(InfoArchive *const this, IoWrite *const write) { FUNCTION_LOG_BEGIN(logLevelDebug); FUNCTION_LOG_PARAM(INFO_ARCHIVE, this); FUNCTION_LOG_PARAM(IO_WRITE, write); FUNCTION_LOG_END(); ASSERT(this != NULL); ASSERT(write != NULL); MEM_CONTEXT_TEMP_BEGIN() { infoPgSave(infoArchivePg(this), write, NULL, NULL); } MEM_CONTEXT_TEMP_END(); FUNCTION_LOG_RETURN_VOID(); } /**********************************************************************************************************************************/ FN_EXTERN InfoArchive * infoArchivePgSet(InfoArchive *const this, const unsigned int pgVersion, const uint64_t pgSystemId) { FUNCTION_LOG_BEGIN(logLevelDebug); FUNCTION_LOG_PARAM(INFO_ARCHIVE, this); FUNCTION_LOG_PARAM(UINT, pgVersion); FUNCTION_LOG_PARAM(UINT64, pgSystemId); FUNCTION_LOG_END(); ASSERT(this != NULL); this->pub.infoPg = infoPgSet(infoArchivePg(this), pgVersion, pgSystemId, 0); FUNCTION_LOG_RETURN(INFO_ARCHIVE, this); } /**********************************************************************************************************************************/ FN_EXTERN void infoArchiveFormatSet(InfoArchive *const this, const unsigned int format) { FUNCTION_LOG_BEGIN(logLevelDebug); FUNCTION_LOG_PARAM(INFO_ARCHIVE, this); FUNCTION_LOG_PARAM(UINT, format); FUNCTION_LOG_END(); ASSERT(this != NULL); infoFormatSet(infoPgInfo(infoArchivePg(this)), format); FUNCTION_LOG_RETURN_VOID(); } /**********************************************************************************************************************************/ FN_EXTERN void infoArchiveCipherPriorClear(InfoArchive *const this) { FUNCTION_TEST_BEGIN(); FUNCTION_TEST_PARAM(INFO_ARCHIVE, this); FUNCTION_TEST_END(); ASSERT(this != NULL); infoCipherPriorSet(infoPgInfo(infoArchivePg(this)), false); FUNCTION_TEST_RETURN_VOID(); } /**********************************************************************************************************************************/ // Create from a read of the file static void * infoArchiveLoadFileNew(IoRead *const read, const CipherSpec *const cipherSpec, const bool prior) { FUNCTION_TEST_BEGIN(); FUNCTION_TEST_PARAM(IO_READ, read); FUNCTION_TEST_PARAM(CIPHER_SPEC, cipherSpec); FUNCTION_TEST_PARAM(BOOL, prior); FUNCTION_TEST_END(); InfoArchive *const result = infoArchiveNewLoad(read, cipherSpec); infoCipherPriorSet(infoPgInfo(infoArchivePg(result)), prior); FUNCTION_TEST_RETURN(INFO_ARCHIVE, result); } FN_EXTERN InfoArchive * infoArchiveLoadFile( const Storage *const storage, const String *const fileName, const CipherSpec *const cipherSpec) { FUNCTION_LOG_BEGIN(logLevelDebug); FUNCTION_LOG_PARAM(STORAGE, storage); FUNCTION_LOG_PARAM(STRING, fileName); FUNCTION_LOG_PARAM(CIPHER_SPEC, cipherSpec); FUNCTION_LOG_END(); ASSERT(storage != NULL); ASSERT(fileName != NULL); ASSERT(cipherSpec != NULL); InfoArchive *result = NULL; TRY_BEGIN() { result = infoLoadFile(storage, fileName, cipherSpec, infoArchiveLoadFileNew); } CATCH_ANY() { THROWP_FMT( errorType(), "%s\n" "HINT: archive.info cannot be opened but is required to push/get WAL segments.\n" "HINT: is archive_command configured correctly in postgresql.conf?\n" "HINT: has a stanza-create been performed?\n" "HINT: use --no-archive-check to disable archive checks during backup if you have an alternate archiving scheme.", errorMessage()); } TRY_END(); FUNCTION_LOG_RETURN(INFO_ARCHIVE, result); } /**********************************************************************************************************************************/ FN_EXTERN void infoArchiveSaveFile( InfoArchive *const infoArchive, const Storage *const storage, const String *const fileName, const CipherSpec *const cipherSpec) { FUNCTION_LOG_BEGIN(logLevelDebug); FUNCTION_LOG_PARAM(INFO_ARCHIVE, infoArchive); FUNCTION_LOG_PARAM(STORAGE, storage); FUNCTION_LOG_PARAM(STRING, fileName); FUNCTION_LOG_PARAM(CIPHER_SPEC, cipherSpec); FUNCTION_LOG_END(); ASSERT(infoArchive != NULL); ASSERT(storage != NULL); ASSERT(fileName != NULL); ASSERT(cipherSpec != NULL); ASSERT(!infoArchiveCipherPrior(infoArchive) || cipherSpecPrior(cipherSpec) != NULL); MEM_CONTEXT_TEMP_BEGIN() { // Write output into a buffer since it needs to be saved to storage twice Buffer *const buffer = bufNew(ioBufferSize()); IoWrite *const write = ioBufferWriteNew(buffer); cipherBlockFormatFilterGroupWriteAddP( ioWriteFilterGroup(write), infoArchiveCipherPrior(infoArchive) ? cipherSpecPrior(cipherSpec) : cipherSpec, infoArchiveFormat(infoArchive)); infoArchiveSave(infoArchive, write); // Save the file and make a copy storagePutP(storageNewWriteP(storage, fileName), buffer); storagePutP(storageNewWriteP(storage, strNewFmt("%s" INFO_COPY_EXT, strZ(fileName))), buffer); } MEM_CONTEXT_TEMP_END(); FUNCTION_LOG_RETURN_VOID(); }