1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-09-16 09:06:18 +02:00

Add ioReadDrain().

Read all data from an IoRead object and discard it.  This is handy for calculating size, hash, etc. when the output is not needed.

Update code where a loop was used before.
This commit is contained in:
David Steele
2019-07-15 08:44:41 -04:00
parent cdb75ac8b3
commit d5654375a5
5 changed files with 54 additions and 25 deletions

View File

@@ -72,21 +72,11 @@ archivePushFile(
if (isSegment)
{
// Generate a sha1 checksum for the wal segment. ??? Probably need a function in storage for this.
// Generate a sha1 checksum for the wal segment
IoRead *read = storageReadIo(storageNewReadNP(storageLocal(), walSource));
ioFilterGroupAdd(ioReadFilterGroup(read), cryptoHashNew(HASH_TYPE_SHA1_STR));
ioReadDrain(read);
Buffer *buffer = bufNew(ioBufferSize());
ioReadOpen(read);
do
{
ioRead(read, buffer);
bufUsedZero(buffer);
}
while (!ioReadEof(read));
ioReadClose(read);
const String *walSegmentChecksum = varStr(ioFilterGroupResult(ioReadFilterGroup(read), CRYPTO_HASH_FILTER_TYPE_STR));
// If the wal segment already exists in the repo then compare checksums

View File

@@ -88,19 +88,7 @@ restoreFile(
{
read = storageReadIo(storageNewReadNP(storagePgWrite(), pgFile));
ioFilterGroupAdd(ioReadFilterGroup(read), cryptoHashNew(HASH_TYPE_SHA1_STR));
Buffer *buffer = bufNew(ioBufferSize());
CHECK(ioReadOpen(read));
do
{
ioRead(read, buffer);
bufUsedZero(buffer);
}
while (!ioReadEof(read));
ioReadClose(read);
ioReadDrain(read);
}
// If size and checksum are equal then no need to copy the file

View File

@@ -74,3 +74,39 @@ ioReadBuf(IoRead *read)
FUNCTION_TEST_RETURN(result);
}
/***********************************************************************************************************************************
Read all IO but don't store it. Useful for calculating checksums, size, etc.
***********************************************************************************************************************************/
bool
ioReadDrain(IoRead *read)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(IO_READ, read);
FUNCTION_TEST_END();
ASSERT(read != NULL);
bool result = ioReadOpen(read);
if (result)
{
MEM_CONTEXT_TEMP_BEGIN()
{
// Read IO into the buffer
Buffer *buffer = bufNew(ioBufferSize());
do
{
ioRead(read, buffer);
bufUsedZero(buffer);
}
while (!ioReadEof(read));
ioReadClose(read);
}
MEM_CONTEXT_TEMP_END();
}
FUNCTION_TEST_RETURN(result);
}

View File

@@ -14,6 +14,7 @@ Common IO functions.
Functions
***********************************************************************************************************************************/
Buffer *ioReadBuf(IoRead *read);
bool ioReadDrain(IoRead *read);
/***********************************************************************************************************************************
Getters/Setters

View File

@@ -424,6 +424,20 @@ testRun(void)
ioReadOpen(bufferRead);
TEST_RESULT_STR(strPtr(strNewBuf(ioReadBuf(bufferRead))), "a test string", "read into buffer");
// Drain read IO
// -------------------------------------------------------------------------------------------------------------------------
bufferRead = ioBufferReadNew(BUFSTRDEF("a better test string"));
ioFilterGroupAdd(ioReadFilterGroup(bufferRead), ioSizeNew());
TEST_RESULT_BOOL(ioReadDrain(bufferRead), true, "drain read io");
TEST_RESULT_UINT(varUInt64(ioFilterGroupResult(ioReadFilterGroup(bufferRead), SIZE_FILTER_TYPE_STR)), 20, "check length");
// Cannot open file
TEST_ASSIGN(
read, ioReadNewP((void *)998, .close = testIoReadClose, .open = testIoReadOpen, .read = testIoRead),
"create io read object");
TEST_RESULT_BOOL(ioReadDrain(read), false, "cannot open");
}
// *****************************************************************************************************************************