@@ -538,17 +538,17 @@ catalog_get_backup_list(const char *instance_name, time_t requested_backup_id)
538538 * TODO this function only used once. Is it really needed?
539539 */
540540parray *
541- get_backup_filelist (pgBackup * backup )
541+ get_backup_filelist (pgBackup * backup , bool strict )
542542{
543543 parray * files = NULL ;
544544 char backup_filelist_path [MAXPGPATH ];
545545
546546 join_path_components (backup_filelist_path , backup -> root_dir , DATABASE_FILE_LIST );
547- files = dir_read_file_list (NULL , NULL , backup_filelist_path , FIO_BACKUP_HOST );
547+ files = dir_read_file_list (NULL , NULL , backup_filelist_path , FIO_BACKUP_HOST , backup -> content_crc );
548548
549549 /* redundant sanity? */
550550 if (!files )
551- elog (ERROR , "Failed to get filelist for backup %s" , base36enc (backup -> start_time ));
551+ elog (strict ? ERROR : WARNING , "Failed to get file list for backup %s" , base36enc (backup -> start_time ));
552552
553553 return files ;
554554}
@@ -1759,6 +1759,9 @@ pgBackupWriteControl(FILE *out, pgBackup *backup)
17591759 if (backup -> note )
17601760 fio_fprintf (out , "note = '%s'\n" , backup -> note );
17611761
1762+ if (backup -> content_crc != 0 )
1763+ fio_fprintf (out , "content-crc = %u\n" , backup -> content_crc );
1764+
17621765}
17631766
17641767/*
@@ -1813,33 +1816,38 @@ write_backup_filelist(pgBackup *backup, parray *files, const char *root,
18131816 parray * external_list , bool sync )
18141817{
18151818 FILE * out ;
1816- char path [MAXPGPATH ];
1817- char path_temp [MAXPGPATH ];
1819+ char control_path [MAXPGPATH ];
1820+ char control_path_temp [MAXPGPATH ];
18181821 size_t i = 0 ;
18191822 #define BUFFERSZ 1024*1024
18201823 char * buf ;
18211824 int64 backup_size_on_disk = 0 ;
18221825 int64 uncompressed_size_on_disk = 0 ;
18231826 int64 wal_size_on_disk = 0 ;
18241827
1825- join_path_components (path , backup -> root_dir , DATABASE_FILE_LIST );
1826- snprintf (path_temp , sizeof (path_temp ), "%s.tmp" , path );
1828+ join_path_components (control_path , backup -> root_dir , DATABASE_FILE_LIST );
1829+ snprintf (control_path_temp , sizeof (control_path_temp ), "%s.tmp" , control_path );
18271830
1828- out = fopen (path_temp , PG_BINARY_W );
1831+ out = fopen (control_path_temp , PG_BINARY_W );
18291832 if (out == NULL )
1830- elog (ERROR , "Cannot open file list \"%s\": %s" , path_temp ,
1833+ elog (ERROR , "Cannot open file list \"%s\": %s" , control_path_temp ,
18311834 strerror (errno ));
18321835
1833- if (chmod (path_temp , FILE_PERMISSION ) == -1 )
1834- elog (ERROR , "Cannot change mode of \"%s\": %s" , path_temp ,
1836+ if (chmod (control_path_temp , FILE_PERMISSION ) == -1 )
1837+ elog (ERROR , "Cannot change mode of \"%s\": %s" , control_path_temp ,
18351838 strerror (errno ));
18361839
18371840 buf = pgut_malloc (BUFFERSZ );
18381841 setvbuf (out , buf , _IOFBF , BUFFERSZ );
18391842
1843+ if (sync )
1844+ INIT_FILE_CRC32 (true, backup -> content_crc );
1845+
18401846 /* print each file in the list */
18411847 for (i = 0 ; i < parray_num (files ); i ++ )
18421848 {
1849+ int len = 0 ;
1850+ char line [BLCKSZ ];
18431851 pgFile * file = (pgFile * ) parray_get (files , i );
18441852 char * path = file -> path ; /* for streamed WAL files */
18451853
@@ -1873,7 +1881,7 @@ write_backup_filelist(pgBackup *backup, parray *files, const char *root,
18731881 (file -> external_dir_num && external_list ))
18741882 path = file -> rel_path ;
18751883
1876- fprintf ( out , "{\"path\":\"%s\", \"size\":\"" INT64_FORMAT "\", "
1884+ len = sprintf ( line , "{\"path\":\"%s\", \"size\":\"" INT64_FORMAT "\", "
18771885 "\"mode\":\"%u\", \"is_datafile\":\"%u\", "
18781886 "\"is_cfs\":\"%u\", \"crc\":\"%u\", "
18791887 "\"compress_alg\":\"%s\", \"external_dir_num\":\"%d\", "
@@ -1887,32 +1895,40 @@ write_backup_filelist(pgBackup *backup, parray *files, const char *root,
18871895 file -> dbOid );
18881896
18891897 if (file -> is_datafile )
1890- fprintf ( out , ",\"segno\":\"%d\"" , file -> segno );
1898+ len += sprintf ( line + len , ",\"segno\":\"%d\"" , file -> segno );
18911899
18921900 if (file -> linked )
1893- fprintf ( out , ",\"linked\":\"%s\"" , file -> linked );
1901+ len += sprintf ( line + len , ",\"linked\":\"%s\"" , file -> linked );
18941902
18951903 if (file -> n_blocks != BLOCKNUM_INVALID )
1896- fprintf (out , ",\"n_blocks\":\"%i\"" , file -> n_blocks );
1904+ len += sprintf (line + len , ",\"n_blocks\":\"%i\"" , file -> n_blocks );
1905+
1906+ sprintf (line + len , "}\n" );
18971907
1898- fprintf (out , "}\n" );
1908+ if (sync )
1909+ COMP_FILE_CRC32 (true, backup -> content_crc , line , strlen (line ));
1910+
1911+ fprintf (out , "%s" , line );
18991912 }
19001913
1914+ if (sync )
1915+ FIN_FILE_CRC32 (true, backup -> content_crc );
1916+
19011917 if (fflush (out ) != 0 )
19021918 elog (ERROR , "Cannot flush file list \"%s\": %s" ,
1903- path_temp , strerror (errno ));
1919+ control_path_temp , strerror (errno ));
19041920
19051921 if (sync && fsync (fileno (out )) < 0 )
19061922 elog (ERROR , "Cannot sync file list \"%s\": %s" ,
1907- path_temp , strerror (errno ));
1923+ control_path_temp , strerror (errno ));
19081924
19091925 if (fclose (out ) != 0 )
19101926 elog (ERROR , "Cannot close file list \"%s\": %s" ,
1911- path_temp , strerror (errno ));
1927+ control_path_temp , strerror (errno ));
19121928
1913- if (rename (path_temp , path ) < 0 )
1929+ if (rename (control_path_temp , control_path ) < 0 )
19141930 elog (ERROR , "Cannot rename file \"%s\" to \"%s\": %s" ,
1915- path_temp , path , strerror (errno ));
1931+ control_path_temp , control_path , strerror (errno ));
19161932
19171933 /* use extra variable to avoid reset of previous data_bytes value in case of error */
19181934 backup -> data_bytes = backup_size_on_disk ;
@@ -1975,6 +1991,7 @@ readBackupControlFile(const char *path)
19751991 {'s' , 0 , "primary-conninfo" , & backup -> primary_conninfo , SOURCE_FILE_STRICT },
19761992 {'s' , 0 , "external-dirs" , & backup -> external_dir_str , SOURCE_FILE_STRICT },
19771993 {'s' , 0 , "note" , & backup -> note , SOURCE_FILE_STRICT },
1994+ {'u' , 0 , "content-crc" , & backup -> content_crc , SOURCE_FILE_STRICT },
19781995 {0 }
19791996 };
19801997
@@ -2242,6 +2259,7 @@ pgBackupInit(pgBackup *backup)
22422259 backup -> root_dir = NULL ;
22432260 backup -> files = NULL ;
22442261 backup -> note = NULL ;
2262+ backup -> content_crc = 0 ;
22452263
22462264}
22472265
0 commit comments