@@ -31,11 +31,6 @@ typedef struct DataPage
3131 char data [BLCKSZ ];
3232} DataPage ;
3333
34- typedef struct DataBlock
35- {
36- char data [BLCKSZ ];
37- } DataBlock ;
38-
3934static bool get_page_header (FILE * in , const char * fullpath , BackupPageHeader * bph ,
4035 pg_crc32 * crc , bool use_crc32c );
4136
@@ -1797,49 +1792,51 @@ validate_file_pages(pgFile *file, const char *fullpath, XLogRecPtr stop_lsn,
17971792 return is_valid ;
17981793}
17991794
1800- /* read local data file and construct map with block checksums
1801- * bufsize must be divisible by BLCKSZ
1802- */
1795+ /* read local data file and construct map with block checksums */
18031796PageState *
18041797get_checksum_map (const char * fullpath , uint32 checksum_version ,
1805- int n_blocks , XLogRecPtr dest_stop_lsn ,
1806- BlockNumber segmentno )
1798+ int n_blocks , XLogRecPtr dest_stop_lsn , BlockNumber segmentno )
18071799{
18081800 PageState * checksum_map = NULL ;
18091801 FILE * in = NULL ;
18101802 BlockNumber blknum = 0 ;
1811- DataBlock * read_buffer ;
1812- int bufsize = LARGE_CHUNK_SIZE ;
1803+ char read_buffer [BLCKSZ ];
1804+ char in_buf [STDIO_BUFSIZE ];
1805+ off_t cur_pos = 0 ;
18131806
18141807 /* open file */
18151808 in = fopen (fullpath , "r+" );
18161809 if (!in )
1817- elog (ERROR , "Cannot open file \"%s\": %s" , fullpath , strerror (errno ));
1818-
1819- setvbuf (in , NULL , _IONBF , BUFSIZ );
1810+ elog (ERROR , "Cannot open source file \"%s\": %s" , fullpath , strerror (errno ));
18201811
18211812 /* truncate up to blocks */
18221813 if (ftruncate (fileno (in ), n_blocks * BLCKSZ ) != 0 )
18231814 elog (ERROR , "Cannot truncate file to blknum %u \"%s\": %s" ,
18241815 n_blocks , fullpath , strerror (errno ));
18251816
1826- read_buffer = pgut_malloc ( bufsize );
1817+ setvbuf ( in , in_buf , _IOFBF , STDIO_BUFSIZE );
18271818
18281819 /* initialize array of checksums */
18291820 checksum_map = pgut_malloc (n_blocks * sizeof (PageState ));
18301821 memset (checksum_map , 0 , n_blocks * sizeof (PageState ));
18311822
18321823 for (;;)
18331824 {
1834- int rc ;
1835- int block ;
18361825 PageState page_st ;
1837- size_t read_len = 0 ;
1826+ size_t read_len = 0 ;
18381827
1839- if (interrupted )
1840- elog ( ERROR , "Interrupted during page reading" ) ;
1828+ if (blknum >= n_blocks )
1829+ break ;
18411830
1842- read_len = fread (read_buffer , 1 , bufsize , in );
1831+ if (cur_pos != blknum * BLCKSZ &&
1832+ fseek (in , blknum * BLCKSZ , SEEK_SET ))
1833+ {
1834+ elog (ERROR , "Cannot seek to offset %u in file \"%s\": %s" ,
1835+ blknum * BLCKSZ , fullpath , strerror (errno ));
1836+ }
1837+
1838+ read_len = fread (read_buffer , 1 , BLCKSZ , in );
1839+ cur_pos += read_len ;
18431840
18441841 /* report error */
18451842 if (ferror (in ))
@@ -1849,37 +1846,34 @@ get_checksum_map(const char *fullpath, uint32 checksum_version,
18491846 if (read_len == 0 && feof (in ))
18501847 break ;
18511848
1852- for ( block = 0 ; block < read_len / BLCKSZ ; block ++ )
1849+ if ( read_len == BLCKSZ )
18531850 {
1854-
1855- if (blknum >= n_blocks )
1856- elog (ERROR , "Concurrent writing to restored cluster detected" );
1857-
1858- rc = validate_one_page (read_buffer [block ].data , segmentno + blknum ,
1851+ int rc = validate_one_page (read_buffer , segmentno + blknum ,
18591852 dest_stop_lsn , & page_st ,
18601853 checksum_version );
18611854
1862- /* we care only about valid pages */
18631855 if (rc == PAGE_IS_VALID )
18641856 {
1865- // if (checksum_version)
1866- // checksum_map[blknum].checksum = ((PageHeader) read_buffer)->pd_checksum;
1867- // else
1868- // checksum_map[blknum].checksum = page_st.checksum;
1857+ if (checksum_version )
1858+ checksum_map [blknum ].checksum = ((PageHeader ) read_buffer )-> pd_checksum ;
1859+ else
1860+ checksum_map [blknum ].checksum = page_st .checksum ;
18691861
1870- checksum_map [blknum ].checksum = page_st .checksum ;
18711862 checksum_map [blknum ].lsn = page_st .lsn ;
18721863 }
18731864
18741865 blknum ++ ;
18751866 }
1867+ else
1868+ elog (WARNING , "Odd size read len %lu for blknum %u in file \"%s\"" , read_len , blknum , fullpath );
1869+
1870+ if (interrupted )
1871+ elog (ERROR , "Interrupted during page reading" );
18761872 }
18771873
18781874 if (in )
18791875 fclose (in );
18801876
1881- pg_free (read_buffer );
1882-
18831877 return checksum_map ;
18841878}
18851879
@@ -1899,7 +1893,7 @@ get_lsn_map(const char *fullpath, uint32 checksum_version,
18991893 /* open file */
19001894 in = fopen (fullpath , "r+" );
19011895 if (!in )
1902- elog (ERROR , "Cannot open file \"%s\": %s" , fullpath , strerror (errno ));
1896+ elog (ERROR , "Cannot open source file \"%s\": %s" , fullpath , strerror (errno ));
19031897
19041898 /* truncate up to blocks */
19051899 if (ftruncate (fileno (in ), n_blocks * BLCKSZ ) != 0 )
0 commit comments