@@ -89,7 +89,7 @@ do_compress(void* dst, size_t dst_size, void const* src, size_t src_size,
89
89
* Decompresses source into dest using algorithm. Returns the number of bytes
90
90
* decompressed in the destination buffer, or -1 if decompression fails.
91
91
*/
92
- static int32
92
+ int32
93
93
do_decompress (void * dst , size_t dst_size , void const * src , size_t src_size ,
94
94
CompressAlg alg , const char * * errormsg )
95
95
{
@@ -719,6 +719,7 @@ restore_data_file(const char *to_path, pgFile *file, bool allow_truncate,
719
719
BlockNumber blknum = 0 ,
720
720
truncate_from = 0 ;
721
721
bool need_truncate = false;
722
+ size_t rc ;
722
723
723
724
/* BYTES_INVALID allowed only in case of restoring file from DELTA backup */
724
725
if (file -> write_size != BYTES_INVALID )
@@ -750,9 +751,9 @@ restore_data_file(const char *to_path, pgFile *file, bool allow_truncate,
750
751
{
751
752
off_t write_pos ;
752
753
size_t read_len ;
753
- DataPage compressed_page ; /* used as read buffer */
754
754
DataPage page ;
755
- int32 uncompressed_size = 0 ;
755
+ int32 compressed_size ;
756
+ const char * errormsg = NULL ;
756
757
757
758
/* File didn`t changed. Nothig to copy */
758
759
if (file -> write_size == BYTES_INVALID )
@@ -789,7 +790,9 @@ restore_data_file(const char *to_path, pgFile *file, bool allow_truncate,
789
790
blknum , file -> path , strerror (errno_tmp ));
790
791
}
791
792
792
- if (header .block == 0 && header .compressed_size == 0 )
793
+ compressed_size = header .compressed_size ;
794
+
795
+ if (header .block == 0 && compressed_size )
793
796
{
794
797
elog (VERBOSE , "Skip empty block of \"%s\"" , file -> path );
795
798
continue ;
@@ -801,7 +804,7 @@ restore_data_file(const char *to_path, pgFile *file, bool allow_truncate,
801
804
802
805
blknum = header .block ;
803
806
804
- if (header . compressed_size == PageIsTruncated )
807
+ if (compressed_size == PageIsTruncated )
805
808
{
806
809
/*
807
810
* Backup contains information that this block was truncated.
@@ -812,39 +815,14 @@ restore_data_file(const char *to_path, pgFile *file, bool allow_truncate,
812
815
break ;
813
816
}
814
817
815
- Assert (header . compressed_size <= BLCKSZ );
818
+ Assert (compressed_size <= BLCKSZ );
816
819
817
820
/* read a page from file */
818
- read_len = fread (compressed_page .data , 1 ,
819
- MAXALIGN (header . compressed_size ), in );
820
- if (read_len != MAXALIGN (header . compressed_size ))
821
+ read_len = fread (page .data , 1 ,
822
+ MAXALIGN (compressed_size ), in );
823
+ if (read_len != MAXALIGN (compressed_size ))
821
824
elog (ERROR , "Cannot read block %u of \"%s\" read %zu of %d" ,
822
- blknum , file -> path , read_len , header .compressed_size );
823
-
824
- /*
825
- * if page size is smaller than BLCKSZ, decompress the page.
826
- * BUGFIX for versions < 2.0.23: if page size is equal to BLCKSZ.
827
- * we have to check, whether it is compressed or not using
828
- * page_may_be_compressed() function.
829
- */
830
- if (header .compressed_size != BLCKSZ
831
- || page_may_be_compressed (compressed_page .data , file -> compress_alg ,
832
- backup_version ))
833
- {
834
- const char * errormsg = NULL ;
835
-
836
- uncompressed_size = do_decompress (page .data , BLCKSZ ,
837
- compressed_page .data ,
838
- header .compressed_size ,
839
- file -> compress_alg , & errormsg );
840
- if (uncompressed_size < 0 && errormsg != NULL )
841
- elog (WARNING , "An error occured during decompressing block %u of file \"%s\": %s" ,
842
- blknum , file -> path , errormsg );
843
-
844
- if (uncompressed_size != BLCKSZ )
845
- elog (ERROR , "Page of file \"%s\" uncompressed to %d bytes. != BLCKSZ" ,
846
- file -> path , uncompressed_size );
847
- }
825
+ blknum , file -> path , read_len , compressed_size );
848
826
849
827
write_pos = (write_header ) ? blknum * (BLCKSZ + sizeof (header )) :
850
828
blknum * BLCKSZ ;
@@ -865,21 +843,24 @@ restore_data_file(const char *to_path, pgFile *file, bool allow_truncate,
865
843
blknum , file -> path , strerror (errno ));
866
844
}
867
845
868
- /* if we uncompressed the page - write page.data,
869
- * if page wasn't compressed -
870
- * write what we've read - compressed_page.data
846
+ /*
847
+ * if page size is smaller than BLCKSZ, decompress the page.
848
+ * BUGFIX for versions < 2.0.23: if page size is equal to BLCKSZ.
849
+ * we have to check, whether it is compressed or not using
850
+ * page_may_be_compressed() function.
871
851
*/
872
- if (uncompressed_size == BLCKSZ )
873
- {
874
- if (fio_fwrite (out , page .data , BLCKSZ ) != BLCKSZ )
875
- elog (ERROR , "Cannot write block %u of \"%s\": %s" ,
876
- blknum , file -> path , strerror (errno ));
877
- }
878
- else
852
+ rc = (compressed_size != BLCKSZ || page_may_be_compressed (page .data , file -> compress_alg , backup_version ))
853
+ ? fio_fwrite_compressed (out , page .data , compressed_size , file -> compress_alg , & errormsg )
854
+ : fio_fwrite (out , page .data , compressed_size );
855
+
856
+ if (rc != compressed_size )
879
857
{
880
- if (fio_fwrite (out , compressed_page .data , BLCKSZ ) != BLCKSZ )
881
- elog (ERROR , "Cannot write block %u of \"%s\": %s" ,
882
- blknum , file -> path , strerror (errno ));
858
+ if (errormsg != NULL )
859
+ elog (WARNING , "An error occured during decompressing block %u of file \"%s\": %s" ,
860
+ blknum , file -> path , errormsg );
861
+
862
+ elog (ERROR , "Cannot write block %u of \"%s\": %s" ,
863
+ blknum , file -> path , strerror (errno ));
883
864
}
884
865
}
885
866
0 commit comments