@@ -89,7 +89,7 @@ do_compress(void* dst, size_t dst_size, void const* src, size_t src_size,
8989 * Decompresses source into dest using algorithm. Returns the number of bytes
9090 * decompressed in the destination buffer, or -1 if decompression fails.
9191 */
92- static int32
92+ int32
9393do_decompress (void * dst , size_t dst_size , void const * src , size_t src_size ,
9494 CompressAlg alg , const char * * errormsg )
9595{
@@ -1047,9 +1047,9 @@ restore_data_file_internal(FILE *in, FILE *out, pgFile *file, uint32 backup_vers
10471047 {
10481048 off_t write_pos ;
10491049 size_t read_len ;
1050- DataPage compressed_page ; /* used as read buffer */
10511050 DataPage page ;
1052- int32 uncompressed_size = 0 ;
1051+ int32 compressed_size = 0 ;
1052+ bool is_compressed = false;
10531053
10541054 /* check for interrupt */
10551055 if (interrupted || thread_interrupted )
@@ -1090,14 +1090,16 @@ restore_data_file_internal(FILE *in, FILE *out, pgFile *file, uint32 backup_vers
10901090 * n_blocks attribute was available only in DELTA backups.
10911091 * File truncate in PAGE and PTRACK happened on the fly when
10921092 * special value PageIsTruncated is encountered.
1093- * It is inefficient.
1093+ * It was inefficient.
10941094 *
10951095 * Nowadays every backup type has n_blocks, so instead
10961096 * writing and then truncating redundant data, writing
10971097 * is not happening in the first place.
10981098 * TODO: remove in 3.0.0
10991099 */
1100- if (header .compressed_size == PageIsTruncated )
1100+ compressed_size = header .compressed_size ;
1101+
1102+ if (compressed_size == PageIsTruncated )
11011103 {
11021104 /*
11031105 * Block header contains information that this block was truncated.
@@ -1124,16 +1126,15 @@ restore_data_file_internal(FILE *in, FILE *out, pgFile *file, uint32 backup_vers
11241126 if (nblocks > 0 && blknum >= nblocks )
11251127 break ;
11261128
1127- if (header . compressed_size > BLCKSZ )
1129+ if (compressed_size > BLCKSZ )
11281130 elog (ERROR , "Size of a blknum %i exceed BLCKSZ" , blknum );
11291131
11301132 /* read a page from file */
1131- read_len = fread (compressed_page .data , 1 ,
1132- MAXALIGN (header .compressed_size ), in );
1133+ read_len = fread (page .data , 1 , MAXALIGN (compressed_size ), in );
11331134
1134- if (read_len != MAXALIGN (header . compressed_size ))
1135+ if (read_len != MAXALIGN (compressed_size ))
11351136 elog (ERROR , "Cannot read block %u of \"%s\", read %zu of %d" ,
1136- blknum , from_fullpath , read_len , header . compressed_size );
1137+ blknum , from_fullpath , read_len , compressed_size );
11371138
11381139 /*
11391140 * if page size is smaller than BLCKSZ, decompress the page.
@@ -1142,23 +1143,10 @@ restore_data_file_internal(FILE *in, FILE *out, pgFile *file, uint32 backup_vers
11421143 * page_may_be_compressed() function.
11431144 */
11441145 if (header .compressed_size != BLCKSZ
1145- || page_may_be_compressed (compressed_page .data , file -> compress_alg ,
1146+ || page_may_be_compressed (page .data , file -> compress_alg ,
11461147 backup_version ))
11471148 {
1148- const char * errormsg = NULL ;
1149-
1150- uncompressed_size = do_decompress (page .data , BLCKSZ ,
1151- compressed_page .data ,
1152- header .compressed_size ,
1153- file -> compress_alg , & errormsg );
1154-
1155- if (uncompressed_size < 0 && errormsg != NULL )
1156- elog (WARNING , "An error occured during decompressing block %u of file \"%s\": %s" ,
1157- blknum , from_fullpath , errormsg );
1158-
1159- if (uncompressed_size != BLCKSZ )
1160- elog (ERROR , "Page of file \"%s\" uncompressed to %d bytes. != BLCKSZ" ,
1161- from_fullpath , uncompressed_size );
1149+ is_compressed = true;
11621150 }
11631151
11641152 write_pos = blknum * BLCKSZ ;
@@ -1170,19 +1158,21 @@ restore_data_file_internal(FILE *in, FILE *out, pgFile *file, uint32 backup_vers
11701158 elog (ERROR , "Cannot seek block %u of \"%s\": %s" ,
11711159 blknum , to_fullpath , strerror (errno ));
11721160
1173- /* if we uncompressed the page - write page.data,
1174- * if page wasn't compressed -
1175- * write what we've read - compressed_page.data
1161+ /* If page is compressed and restore is in remote mode, send compressed
1162+ * page to the remote side.
11761163 */
1177- if (uncompressed_size == BLCKSZ )
1164+ if (is_compressed )
11781165 {
1179- if (fio_fwrite (out , page .data , BLCKSZ ) != BLCKSZ )
1180- elog (ERROR , "Cannot write block %u of \"%s\": %s" ,
1181- blknum , to_fullpath , strerror (errno ));
1166+ ssize_t rc ;
1167+ rc = fio_fwrite_compressed (out , page .data , compressed_size , file -> compress_alg );
1168+
1169+ if (!fio_is_remote_file (out ) && rc != BLCKSZ )
1170+ elog (ERROR , "Cannot write block %u of \"%s\": %s, size: %u" ,
1171+ blknum , to_fullpath , strerror (errno ), compressed_size );
11821172 }
11831173 else
11841174 {
1185- if (fio_fwrite (out , compressed_page .data , BLCKSZ ) != BLCKSZ )
1175+ if (fio_fwrite (out , page .data , BLCKSZ ) != BLCKSZ )
11861176 elog (ERROR , "Cannot write block %u of \"%s\": %s" ,
11871177 blknum , to_fullpath , strerror (errno ));
11881178 }
0 commit comments