You've already forked pgbackrest
mirror of
https://github.com/pgbackrest/pgbackrest.git
synced 2025-07-17 01:12:23 +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);
|
ioWriteOpen(destination);
|
||||||
|
|
||||||
// Copy data from source to destination
|
// Copy data from source to destination
|
||||||
Buffer *buffer = bufNew(ioBufferSize());
|
ioCopy(source, destination);
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
ioRead(source, buffer);
|
|
||||||
ioWrite(destination, buffer);
|
|
||||||
bufUsedZero(buffer);
|
|
||||||
}
|
|
||||||
while (!ioReadEof(source));
|
|
||||||
|
|
||||||
// Close the source and destination
|
// Close the source and destination
|
||||||
ioReadClose(source);
|
ioReadClose(source);
|
||||||
|
@ -61,15 +61,7 @@ storagePutProcess(IoRead *source)
|
|||||||
ioWriteOpen(storageWriteIo(destination));
|
ioWriteOpen(storageWriteIo(destination));
|
||||||
|
|
||||||
// Copy data from source to destination
|
// Copy data from source to destination
|
||||||
Buffer *buffer = bufNew(ioBufferSize());
|
ioCopy(source, storageWriteIo(destination));
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
ioRead(source, buffer);
|
|
||||||
ioWrite(storageWriteIo(destination), buffer);
|
|
||||||
bufUsedZero(buffer);
|
|
||||||
}
|
|
||||||
while (!ioReadEof(source));
|
|
||||||
|
|
||||||
// Close the source and destination
|
// Close the source and destination
|
||||||
ioReadClose(source);
|
ioReadClose(source);
|
||||||
|
@ -95,6 +95,32 @@ ioReadBuf(IoRead *read)
|
|||||||
FUNCTION_TEST_RETURN(result);
|
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
|
bool
|
||||||
ioReadDrain(IoRead *read)
|
ioReadDrain(IoRead *read)
|
||||||
|
@ -9,11 +9,15 @@ Common IO functions.
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
#include <common/io/read.h>
|
#include <common/io/read.h>
|
||||||
|
#include <common/io/write.h>
|
||||||
#include <common/time.h>
|
#include <common/time.h>
|
||||||
|
|
||||||
/***********************************************************************************************************************************
|
/***********************************************************************************************************************************
|
||||||
Functions
|
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
|
// Read all IO into a buffer
|
||||||
Buffer *ioReadBuf(IoRead *read);
|
Buffer *ioReadBuf(IoRead *read);
|
||||||
|
|
||||||
|
@ -115,15 +115,7 @@ storageCopy(StorageRead *source, StorageWrite *destination)
|
|||||||
ioWriteOpen(storageWriteIo(destination));
|
ioWriteOpen(storageWriteIo(destination));
|
||||||
|
|
||||||
// Copy data from source to destination
|
// Copy data from source to destination
|
||||||
Buffer *read = bufNew(ioBufferSize());
|
ioCopy(storageReadIo(source), storageWriteIo(destination));
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
ioRead(storageReadIo(source), read);
|
|
||||||
ioWrite(storageWriteIo(destination), read);
|
|
||||||
bufUsedZero(read);
|
|
||||||
}
|
|
||||||
while (!ioReadEof(storageReadIo(source)));
|
|
||||||
|
|
||||||
// Close the source and destination files
|
// Close the source and destination files
|
||||||
ioReadClose(storageReadIo(source));
|
ioReadClose(storageReadIo(source));
|
||||||
|
@ -468,6 +468,23 @@ testRun(void)
|
|||||||
ioReadOpen(read);
|
ioReadOpen(read);
|
||||||
TEST_RESULT_STR_Z(ioReadLineParam(read, true), "1234", "read line without eof");
|
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
|
// Read IO into a buffer
|
||||||
// -------------------------------------------------------------------------------------------------------------------------
|
// -------------------------------------------------------------------------------------------------------------------------
|
||||||
ioBufferSizeSet(8);
|
ioBufferSizeSet(8);
|
||||||
|
@ -271,15 +271,7 @@ testRun(void)
|
|||||||
\
|
\
|
||||||
uint64_t benchMarkBegin = timeMSec(); \
|
uint64_t benchMarkBegin = timeMSec(); \
|
||||||
\
|
\
|
||||||
Buffer *buffer = bufNew(ioBufferSize()); \
|
ioCopy(read, write); \
|
||||||
\
|
|
||||||
do \
|
|
||||||
{ \
|
|
||||||
ioRead(read, buffer); \
|
|
||||||
ioWrite(write, buffer); \
|
|
||||||
bufUsedZero(buffer); \
|
|
||||||
} \
|
|
||||||
while (!ioReadEof(read)); \
|
|
||||||
\
|
\
|
||||||
ioReadClose(read); \
|
ioReadClose(read); \
|
||||||
ioWriteClose(write); \
|
ioWriteClose(write); \
|
||||||
|
Reference in New Issue
Block a user