1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-07-15 01:04:37 +02:00

Add ioCopy().

Functionality to copy from IoRead to IoWrite is frequently used so centralize it. This also simplifies coverage testing in places where a loop was required before.
This commit is contained in:
David Steele
2022-01-09 13:19:43 -05:00
parent 47954774c6
commit 5f78a5fc18
7 changed files with 51 additions and 36 deletions

View File

@ -157,15 +157,7 @@ storageGetProcess(IoWrite *destination)
ioWriteOpen(destination);
// Copy data from source to destination
Buffer *buffer = bufNew(ioBufferSize());
do
{
ioRead(source, buffer);
ioWrite(destination, buffer);
bufUsedZero(buffer);
}
while (!ioReadEof(source));
ioCopy(source, destination);
// Close the source and destination
ioReadClose(source);

View File

@ -61,15 +61,7 @@ storagePutProcess(IoRead *source)
ioWriteOpen(storageWriteIo(destination));
// Copy data from source to destination
Buffer *buffer = bufNew(ioBufferSize());
do
{
ioRead(source, buffer);
ioWrite(storageWriteIo(destination), buffer);
bufUsedZero(buffer);
}
while (!ioReadEof(source));
ioCopy(source, storageWriteIo(destination));
// Close the source and destination
ioReadClose(source);

View File

@ -95,6 +95,32 @@ ioReadBuf(IoRead *read)
FUNCTION_TEST_RETURN(result);
}
/**********************************************************************************************************************************/
void
ioCopy(IoRead *const source, IoWrite *const destination)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(IO_READ, source);
FUNCTION_TEST_PARAM(IO_WRITE, destination);
FUNCTION_TEST_END();
MEM_CONTEXT_TEMP_BEGIN()
{
Buffer *const buffer = bufNew(ioBufferSize());
do
{
ioRead(source, buffer);
ioWrite(destination, buffer);
bufUsedZero(buffer);
}
while (!ioReadEof(source));
}
MEM_CONTEXT_TEMP_END();
FUNCTION_TEST_RETURN_VOID();
}
/**********************************************************************************************************************************/
bool
ioReadDrain(IoRead *read)

View File

@ -9,11 +9,15 @@ Common IO functions.
#include <stddef.h>
#include <common/io/read.h>
#include <common/io/write.h>
#include <common/time.h>
/***********************************************************************************************************************************
Functions
***********************************************************************************************************************************/
// Copy data from source to destination (both must be open and neither will be closed)
void ioCopy(IoRead *source, IoWrite *destination);
// Read all IO into a buffer
Buffer *ioReadBuf(IoRead *read);

View File

@ -115,15 +115,7 @@ storageCopy(StorageRead *source, StorageWrite *destination)
ioWriteOpen(storageWriteIo(destination));
// Copy data from source to destination
Buffer *read = bufNew(ioBufferSize());
do
{
ioRead(storageReadIo(source), read);
ioWrite(storageWriteIo(destination), read);
bufUsedZero(read);
}
while (!ioReadEof(storageReadIo(source)));
ioCopy(storageReadIo(source), storageWriteIo(destination));
// Close the source and destination files
ioReadClose(storageReadIo(source));

View File

@ -468,6 +468,23 @@ testRun(void)
ioReadOpen(read);
TEST_RESULT_STR_Z(ioReadLineParam(read, true), "1234", "read line without eof");
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("ioCopy()");
ioBufferSizeSet(4);
bufferRead = ioBufferReadNew(BUFSTRDEF("a test string"));
ioReadOpen(bufferRead);
buffer = bufNew(0);
IoWrite *bufferWrite = ioBufferWriteNew(buffer);
ioWriteOpen(bufferWrite);
TEST_RESULT_VOID(ioCopy(bufferRead, bufferWrite), "copy buffer");
TEST_RESULT_VOID(ioWriteClose(bufferWrite), "close write");
TEST_RESULT_STR_Z(strNewBuf(buffer), "a test string", "check buffer");
// Read IO into a buffer
// -------------------------------------------------------------------------------------------------------------------------
ioBufferSizeSet(8);

View File

@ -271,15 +271,7 @@ testRun(void)
\
uint64_t benchMarkBegin = timeMSec(); \
\
Buffer *buffer = bufNew(ioBufferSize()); \
\
do \
{ \
ioRead(read, buffer); \
ioWrite(write, buffer); \
bufUsedZero(buffer); \
} \
while (!ioReadEof(read)); \
ioCopy(read, write); \
\
ioReadClose(read); \
ioWriteClose(write); \