@@ -2001,13 +2001,14 @@ send_pages(ConnectionArgs* conn_arg, const char *to_fullpath, const char *from_f
20012001{
20022002 FILE * in = NULL ;
20032003 FILE * out = NULL ;
2004- int hdr_num = -1 ;
20052004 off_t cur_pos_out = 0 ;
20062005 char curr_page [BLCKSZ ];
20072006 int n_blocks_read = 0 ;
20082007 BlockNumber blknum = 0 ;
20092008 datapagemap_iterator_t * iter = NULL ;
20102009 int compressed_size = 0 ;
2010+ BackupPageHeader2 * header = NULL ;
2011+ parray * harray = NULL ;
20112012
20122013 /* stdio buffers */
20132014 char * in_buf = NULL ;
@@ -2046,6 +2047,8 @@ send_pages(ConnectionArgs* conn_arg, const char *to_fullpath, const char *from_f
20462047 setvbuf (in , in_buf , _IOFBF , STDIO_BUFSIZE );
20472048 }
20482049
2050+ harray = parray_new ();
2051+
20492052 while (blknum < file -> n_blocks )
20502053 {
20512054 PageState page_st ;
@@ -2063,17 +2066,15 @@ send_pages(ConnectionArgs* conn_arg, const char *to_fullpath, const char *from_f
20632066 if (!out )
20642067 out = open_local_file_rw (to_fullpath , & out_buf , STDIO_BUFSIZE );
20652068
2066- hdr_num ++ ;
2067-
2068- if (!* headers )
2069- * headers = (BackupPageHeader2 * ) pgut_malloc (sizeof (BackupPageHeader2 ));
2070- else
2071- * headers = (BackupPageHeader2 * ) pgut_realloc (* headers , (hdr_num + 1 ) * sizeof (BackupPageHeader2 ));
2069+ header = pgut_new0 (BackupPageHeader2 );
2070+ * header = (BackupPageHeader2 ){
2071+ .block = blknum ,
2072+ .pos = cur_pos_out ,
2073+ .lsn = page_st .lsn ,
2074+ .checksum = page_st .checksum ,
2075+ };
20722076
2073- (* headers )[hdr_num ].block = blknum ;
2074- (* headers )[hdr_num ].pos = cur_pos_out ;
2075- (* headers )[hdr_num ].lsn = page_st .lsn ;
2076- (* headers )[hdr_num ].checksum = page_st .checksum ;
2077+ parray_append (harray , header );
20772078
20782079 compressed_size = compress_and_backup_page (file , blknum , in , out , & (file -> crc ),
20792080 rc , curr_page , calg , clevel ,
@@ -2098,12 +2099,22 @@ send_pages(ConnectionArgs* conn_arg, const char *to_fullpath, const char *from_f
20982099 * Add dummy header, so we can later extract the length of last header
20992100 * as difference between their offsets.
21002101 */
2101- if (* headers )
2102+ if (parray_num ( harray ) > 0 )
21022103 {
2103- file -> n_headers = hdr_num + 1 ;
2104- * headers = (BackupPageHeader2 * ) pgut_realloc (* headers , (hdr_num + 2 ) * sizeof (BackupPageHeader2 ));
2105- (* headers )[hdr_num + 1 ].pos = cur_pos_out ;
2104+ size_t hdr_num = parray_num (harray );
2105+ size_t i ;
2106+
2107+ file -> n_headers = (int ) hdr_num ; /* is it valid? */
2108+ * headers = (BackupPageHeader2 * ) pgut_malloc0 ((hdr_num + 1 ) * sizeof (BackupPageHeader2 ));
2109+ for (i = 0 ; i < hdr_num ; i ++ )
2110+ {
2111+ header = (BackupPageHeader2 * )parray_get (harray , i );
2112+ (* headers )[i ] = * header ;
2113+ pg_free (header );
2114+ }
2115+ (* headers )[hdr_num ] = (BackupPageHeader2 ){.pos = cur_pos_out };
21062116 }
2117+ parray_free (harray );
21072118
21082119 /* cleanup */
21092120 if (in && fclose (in ))
0 commit comments