@@ -1770,80 +1770,78 @@ write_backup(pgBackup *backup, bool strict)
17701770 FILE * fp = NULL ;
17711771 char path [MAXPGPATH ];
17721772 char path_temp [MAXPGPATH ];
1773- int errno_temp ;
17741773 char buf [4096 ];
17751774
17761775 join_path_components (path , backup -> root_dir , BACKUP_CONTROL_FILE );
17771776 snprintf (path_temp , sizeof (path_temp ), "%s.tmp" , path );
17781777
1779- fp = fio_fopen (path_temp , PG_BINARY_W , FIO_BACKUP_HOST );
1778+ fp = fopen (path_temp , PG_BINARY_W );
17801779 if (fp == NULL )
1781- elog (ERROR , "Cannot open configuration file \"%s\": %s" ,
1780+ elog (ERROR , "Cannot open control file \"%s\": %s" ,
17821781 path_temp , strerror (errno ));
17831782
1783+ if (chmod (path_temp , FILE_PERMISSION ) == -1 )
1784+ elog (ERROR , "Cannot change mode of \"%s\": %s" , path_temp ,
1785+ strerror (errno ));
1786+
17841787 setvbuf (fp , buf , _IOFBF , sizeof (buf ));
17851788
17861789 pgBackupWriteControl (fp , backup );
17871790
1788- if (fio_fclose (fp ))
1789- {
1790- errno_temp = errno ;
1791- fio_unlink (path_temp , FIO_BACKUP_HOST );
1791+ if (fflush (fp ) != 0 )
1792+ elog (ERROR , "Cannot flush control file \"%s\": %s" ,
1793+ path_temp , strerror (errno ));
17921794
1793- if (!strict && errno_temp == ENOSPC )
1794- return ;
1795+ if (fsync (fileno (fp )) < 0 )
1796+ elog (ERROR , "Cannot sync control file \"%s\": %s" ,
1797+ path_temp , strerror (errno ));
17951798
1796- elog ( ERROR , "Cannot write configuration file \"%s\": %s" ,
1797- path_temp , strerror ( errno_temp ));
1798- }
1799+ if ( fclose ( fp ) != 0 )
1800+ elog ( ERROR , "Cannot close control file \"%s\": %s" ,
1801+ path_temp , strerror ( errno ));
17991802
1800- if (fio_rename (path_temp , path , FIO_BACKUP_HOST ) < 0 )
1801- {
1802- errno_temp = errno ;
1803- fio_unlink (path_temp , FIO_BACKUP_HOST );
1804- elog (ERROR , "Cannot rename configuration file \"%s\" to \"%s\": %s" ,
1805- path_temp , path , strerror (errno_temp ));
1806- }
1803+ if (rename (path_temp , path ) < 0 )
1804+ elog (ERROR , "Cannot rename file \"%s\" to \"%s\": %s" ,
1805+ path_temp , path , strerror (errno ));
18071806}
18081807
18091808/*
18101809 * Output the list of files to backup catalog DATABASE_FILE_LIST
18111810 */
18121811void
18131812write_backup_filelist (pgBackup * backup , parray * files , const char * root ,
1814- parray * external_list )
1813+ parray * external_list , bool sync )
18151814{
18161815 FILE * out ;
18171816 char path [MAXPGPATH ];
18181817 char path_temp [MAXPGPATH ];
1819- int errno_temp ;
18201818 size_t i = 0 ;
18211819 #define BUFFERSZ 1024*1024
18221820 char * buf ;
1823- size_t write_len = 0 ;
18241821 int64 backup_size_on_disk = 0 ;
18251822 int64 uncompressed_size_on_disk = 0 ;
18261823 int64 wal_size_on_disk = 0 ;
18271824
18281825 join_path_components (path , backup -> root_dir , DATABASE_FILE_LIST );
18291826 snprintf (path_temp , sizeof (path_temp ), "%s.tmp" , path );
18301827
1831- out = fio_fopen (path_temp , PG_BINARY_W , FIO_BACKUP_HOST );
1828+ out = fopen (path_temp , PG_BINARY_W );
18321829 if (out == NULL )
18331830 elog (ERROR , "Cannot open file list \"%s\": %s" , path_temp ,
18341831 strerror (errno ));
18351832
1833+ if (chmod (path_temp , FILE_PERMISSION ) == -1 )
1834+ elog (ERROR , "Cannot change mode of \"%s\": %s" , path_temp ,
1835+ strerror (errno ));
1836+
18361837 buf = pgut_malloc (BUFFERSZ );
1838+ setvbuf (out , buf , _IOFBF , BUFFERSZ );
18371839
18381840 /* print each file in the list */
1839- while (i < parray_num (files ))
1841+ for (i = 0 ; i < parray_num (files ); i ++ )
18401842 {
1841- pgFile * file = (pgFile * ) parray_get (files , i );
1842- char * path = file -> path ; /* for streamed WAL files */
1843- char line [BLCKSZ ];
1844- int len = 0 ;
1845-
1846- i ++ ;
1843+ pgFile * file = (pgFile * ) parray_get (files , i );
1844+ char * path = file -> path ; /* for streamed WAL files */
18471845
18481846 if (S_ISDIR (file -> mode ))
18491847 {
@@ -1875,7 +1873,7 @@ write_backup_filelist(pgBackup *backup, parray *files, const char *root,
18751873 (file -> external_dir_num && external_list ))
18761874 path = file -> rel_path ;
18771875
1878- len = sprintf ( line , "{\"path\":\"%s\", \"size\":\"" INT64_FORMAT "\", "
1876+ fprintf ( out , "{\"path\":\"%s\", \"size\":\"" INT64_FORMAT "\", "
18791877 "\"mode\":\"%u\", \"is_datafile\":\"%u\", "
18801878 "\"is_cfs\":\"%u\", \"crc\":\"%u\", "
18811879 "\"compress_alg\":\"%s\", \"external_dir_num\":\"%d\", "
@@ -1889,59 +1887,32 @@ write_backup_filelist(pgBackup *backup, parray *files, const char *root,
18891887 file -> dbOid );
18901888
18911889 if (file -> is_datafile )
1892- len += sprintf ( line + len , ",\"segno\":\"%d\"" , file -> segno );
1890+ fprintf ( out , ",\"segno\":\"%d\"" , file -> segno );
18931891
18941892 if (file -> linked )
1895- len += sprintf ( line + len , ",\"linked\":\"%s\"" , file -> linked );
1893+ fprintf ( out , ",\"linked\":\"%s\"" , file -> linked );
18961894
18971895 if (file -> n_blocks != BLOCKNUM_INVALID )
1898- len += sprintf (line + len , ",\"n_blocks\":\"%i\"" , file -> n_blocks );
1899-
1900- len += sprintf (line + len , "}\n" );
1896+ fprintf (out , ",\"n_blocks\":\"%i\"" , file -> n_blocks );
19011897
1902- if (write_len + len >= BUFFERSZ )
1903- {
1904- /* write buffer to file */
1905- if (fio_fwrite (out , buf , write_len ) != write_len )
1906- {
1907- errno_temp = errno ;
1908- fio_unlink (path_temp , FIO_BACKUP_HOST );
1909- elog (ERROR , "Cannot write file list \"%s\": %s" ,
1910- path_temp , strerror (errno ));
1911- }
1912- /* reset write_len */
1913- write_len = 0 ;
1914- }
1915-
1916- memcpy (buf + write_len , line , len );
1917- write_len += len ;
1898+ fprintf (out , "}\n" );
19181899 }
19191900
1920- /* write what is left in the buffer to file */
1921- if (write_len > 0 )
1922- if (fio_fwrite (out , buf , write_len ) != write_len )
1923- {
1924- errno_temp = errno ;
1925- fio_unlink (path_temp , FIO_BACKUP_HOST );
1926- elog (ERROR , "Cannot write file list \"%s\": %s" ,
1927- path_temp , strerror (errno ));
1928- }
1901+ if (fflush (out ) != 0 )
1902+ elog (ERROR , "Cannot flush file list \"%s\": %s" ,
1903+ path_temp , strerror (errno ));
19291904
1930- if (fio_fflush (out ) || fio_fclose (out ))
1931- {
1932- errno_temp = errno ;
1933- fio_unlink (path_temp , FIO_BACKUP_HOST );
1934- elog (ERROR , "Cannot write file list \"%s\": %s" ,
1905+ if (sync && fsync (fileno (out )) < 0 )
1906+ elog (ERROR , "Cannot sync file list \"%s\": %s" ,
19351907 path_temp , strerror (errno ));
1936- }
19371908
1938- if (fio_rename ( path_temp , path , FIO_BACKUP_HOST ) < 0 )
1939- {
1940- errno_temp = errno ;
1941- fio_unlink ( path_temp , FIO_BACKUP_HOST );
1942- elog ( ERROR , "Cannot rename configuration file \"%s\" to \"%s\": %s" ,
1943- path_temp , path , strerror ( errno_temp ));
1944- }
1909+ if (fclose ( out ) != 0 )
1910+ elog ( ERROR , "Cannot close file list \"%s\": %s" ,
1911+ path_temp , strerror ( errno )) ;
1912+
1913+ if ( rename ( path_temp , path ) < 0 )
1914+ elog ( ERROR , "Cannot rename file \"%s\" to \"%s\": %s" ,
1915+ path_temp , path , strerror ( errno ));
19451916
19461917 /* use extra variable to avoid reset of previous data_bytes value in case of error */
19471918 backup -> data_bytes = backup_size_on_disk ;
0 commit comments