@@ -31,11 +31,6 @@ typedef struct DataPage
31
31
char data [BLCKSZ ];
32
32
} DataPage ;
33
33
34
- typedef struct DataBlock
35
- {
36
- char data [BLCKSZ ];
37
- } DataBlock ;
38
-
39
34
static bool get_page_header (FILE * in , const char * fullpath , BackupPageHeader * bph ,
40
35
pg_crc32 * crc , bool use_crc32c );
41
36
@@ -1797,49 +1792,51 @@ validate_file_pages(pgFile *file, const char *fullpath, XLogRecPtr stop_lsn,
1797
1792
return is_valid ;
1798
1793
}
1799
1794
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 */
1803
1796
PageState *
1804
1797
get_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 )
1807
1799
{
1808
1800
PageState * checksum_map = NULL ;
1809
1801
FILE * in = NULL ;
1810
1802
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 ;
1813
1806
1814
1807
/* open file */
1815
1808
in = fopen (fullpath , "r+" );
1816
1809
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 ));
1820
1811
1821
1812
/* truncate up to blocks */
1822
1813
if (ftruncate (fileno (in ), n_blocks * BLCKSZ ) != 0 )
1823
1814
elog (ERROR , "Cannot truncate file to blknum %u \"%s\": %s" ,
1824
1815
n_blocks , fullpath , strerror (errno ));
1825
1816
1826
- read_buffer = pgut_malloc ( bufsize );
1817
+ setvbuf ( in , in_buf , _IOFBF , STDIO_BUFSIZE );
1827
1818
1828
1819
/* initialize array of checksums */
1829
1820
checksum_map = pgut_malloc (n_blocks * sizeof (PageState ));
1830
1821
memset (checksum_map , 0 , n_blocks * sizeof (PageState ));
1831
1822
1832
1823
for (;;)
1833
1824
{
1834
- int rc ;
1835
- int block ;
1836
1825
PageState page_st ;
1837
- size_t read_len = 0 ;
1826
+ size_t read_len = 0 ;
1838
1827
1839
- if (interrupted )
1840
- elog ( ERROR , "Interrupted during page reading" ) ;
1828
+ if (blknum >= n_blocks )
1829
+ break ;
1841
1830
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 ;
1843
1840
1844
1841
/* report error */
1845
1842
if (ferror (in ))
@@ -1849,37 +1846,34 @@ get_checksum_map(const char *fullpath, uint32 checksum_version,
1849
1846
if (read_len == 0 && feof (in ))
1850
1847
break ;
1851
1848
1852
- for ( block = 0 ; block < read_len / BLCKSZ ; block ++ )
1849
+ if ( read_len == BLCKSZ )
1853
1850
{
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 ,
1859
1852
dest_stop_lsn , & page_st ,
1860
1853
checksum_version );
1861
1854
1862
- /* we care only about valid pages */
1863
1855
if (rc == PAGE_IS_VALID )
1864
1856
{
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 ;
1869
1861
1870
- checksum_map [blknum ].checksum = page_st .checksum ;
1871
1862
checksum_map [blknum ].lsn = page_st .lsn ;
1872
1863
}
1873
1864
1874
1865
blknum ++ ;
1875
1866
}
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" );
1876
1872
}
1877
1873
1878
1874
if (in )
1879
1875
fclose (in );
1880
1876
1881
- pg_free (read_buffer );
1882
-
1883
1877
return checksum_map ;
1884
1878
}
1885
1879
@@ -1899,7 +1893,7 @@ get_lsn_map(const char *fullpath, uint32 checksum_version,
1899
1893
/* open file */
1900
1894
in = fopen (fullpath , "r+" );
1901
1895
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 ));
1903
1897
1904
1898
/* truncate up to blocks */
1905
1899
if (ftruncate (fileno (in ), n_blocks * BLCKSZ ) != 0 )
0 commit comments