You've already forked pgbackrest
							
							
				mirror of
				https://github.com/pgbackrest/pgbackrest.git
				synced 2025-10-30 23:37:45 +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:
		| @@ -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 | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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); | ||||
| } | ||||
|   | ||||
| @@ -14,6 +14,7 @@ Common IO functions. | ||||
| Functions | ||||
| ***********************************************************************************************************************************/ | ||||
| Buffer *ioReadBuf(IoRead *read); | ||||
| bool ioReadDrain(IoRead *read); | ||||
|  | ||||
| /*********************************************************************************************************************************** | ||||
| Getters/Setters | ||||
|   | ||||
| @@ -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"); | ||||
|     } | ||||
|  | ||||
|     // ***************************************************************************************************************************** | ||||
|   | ||||
		Reference in New Issue
	
	Block a user