You've already forked pgbackrest
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:
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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)
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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));
|
||||
|
@ -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);
|
||||
|
@ -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); \
|
||||
|
Reference in New Issue
Block a user