@@ -799,13 +799,17 @@ backup_non_data_file(pgFile *file, pgFile *prev_file,
799799 * and its mtime is less than parent backup start time ... */
800800 if ((pg_strcasecmp (file -> name , RELMAPPER_FILENAME ) != 0 ) &&
801801 (prev_file && file -> exists_in_prev &&
802+ file -> size == prev_file -> size &&
802803 file -> mtime <= parent_backup_time ))
803804 {
804805 /*
805806 * file could be deleted under our feets.
806807 * But then backup_non_data_file_internal will handle it safely
807808 */
808- file -> crc = fio_get_crc32 (from_fullpath , FIO_DB_HOST , false, true);
809+ if (file -> forkName != cfm )
810+ file -> crc = fio_get_crc32 (from_fullpath , FIO_DB_HOST , false, true);
811+ else
812+ file -> crc = fio_get_crc32_truncated (from_fullpath , FIO_DB_HOST , true);
809813
810814 /* ...and checksum is the same... */
811815 if (EQ_TRADITIONAL_CRC32 (file -> crc , prev_file -> crc ))
@@ -1330,7 +1334,12 @@ restore_non_data_file(parray *parent_chain, pgBackup *dest_backup,
13301334 if (already_exists )
13311335 {
13321336 /* compare checksums of already existing file and backup file */
1333- pg_crc32 file_crc = fio_get_crc32 (to_fullpath , FIO_DB_HOST , false, false);
1337+ pg_crc32 file_crc ;
1338+ if (tmp_file -> forkName == cfm &&
1339+ tmp_file -> uncompressed_size > tmp_file -> write_size )
1340+ file_crc = fio_get_crc32_truncated (to_fullpath , FIO_DB_HOST , false);
1341+ else
1342+ file_crc = fio_get_crc32 (to_fullpath , FIO_DB_HOST , false, false);
13341343
13351344 if (file_crc == tmp_file -> crc )
13361345 {
@@ -1387,10 +1396,12 @@ backup_non_data_file_internal(const char *from_fullpath,
13871396 const char * to_fullpath , pgFile * file ,
13881397 bool missing_ok )
13891398{
1390- FILE * in = NULL ;
13911399 FILE * out = NULL ;
1392- ssize_t read_len = 0 ;
1393- char * buf = NULL ;
1400+ char * errmsg = NULL ;
1401+ int rc ;
1402+ bool cut_zero_tail ;
1403+
1404+ cut_zero_tail = file -> forkName == cfm ;
13941405
13951406 INIT_FILE_CRC32 (true, file -> crc );
13961407
@@ -1412,107 +1423,44 @@ backup_non_data_file_internal(const char *from_fullpath,
14121423
14131424 /* backup remote file */
14141425 if (fio_is_remote (FIO_DB_HOST ))
1415- {
1416- char * errmsg = NULL ;
1417- int rc = fio_send_file (from_fullpath , to_fullpath , out , file , & errmsg );
1426+ rc = fio_send_file ( from_fullpath , out , cut_zero_tail , file , & errmsg );
1427+ else
1428+ rc = fio_send_file_local (from_fullpath , out , cut_zero_tail , file , & errmsg );
14181429
1419- /* handle errors */
1420- if (rc == FILE_MISSING )
1421- {
1422- /* maybe deleted, it's not error in case of backup */
1423- if (missing_ok )
1424- {
1425- elog (LOG , "File \"%s\" is not found" , from_fullpath );
1426- file -> write_size = FILE_NOT_FOUND ;
1427- goto cleanup ;
1428- }
1429- else
1430- elog (ERROR , "File \"%s\" is not found" , from_fullpath );
1431- }
1432- else if (rc == WRITE_FAILED )
1433- elog (ERROR , "Cannot write to \"%s\": %s" , to_fullpath , strerror (errno ));
1434- else if (rc != SEND_OK )
1430+ /* handle errors */
1431+ if (rc == FILE_MISSING )
1432+ {
1433+ /* maybe deleted, it's not error in case of backup */
1434+ if (missing_ok )
14351435 {
1436- if (errmsg )
1437- elog (ERROR , "%s" , errmsg );
1438- else
1439- elog (ERROR , "Cannot access remote file \"%s\"" , from_fullpath );
1436+ elog (LOG , "File \"%s\" is not found" , from_fullpath );
1437+ file -> write_size = FILE_NOT_FOUND ;
1438+ goto cleanup ;
14401439 }
1441-
1442- pg_free ( errmsg );
1440+ else
1441+ elog ( ERROR , "File \"%s\" is not found" , from_fullpath );
14431442 }
1444- /* backup local file */
1445- else
1443+ else if (rc == WRITE_FAILED )
1444+ elog (ERROR , "Cannot write to \"%s\": %s" , to_fullpath , strerror (errno ));
1445+ else if (rc != SEND_OK )
14461446 {
1447- /* open source file for read */
1448- in = fopen (from_fullpath , PG_BINARY_R );
1449- if (in == NULL )
1450- {
1451- /* maybe deleted, it's not error in case of backup */
1452- if (errno == ENOENT )
1453- {
1454- if (missing_ok )
1455- {
1456- elog (LOG , "File \"%s\" is not found" , from_fullpath );
1457- file -> write_size = FILE_NOT_FOUND ;
1458- goto cleanup ;
1459- }
1460- else
1461- elog (ERROR , "File \"%s\" is not found" , from_fullpath );
1462- }
1463-
1464- elog (ERROR , "Cannot open file \"%s\": %s" , from_fullpath ,
1465- strerror (errno ));
1466- }
1467-
1468- /* disable stdio buffering for local input/output files to avoid triple buffering */
1469- setvbuf (in , NULL , _IONBF , BUFSIZ );
1470- setvbuf (out , NULL , _IONBF , BUFSIZ );
1471-
1472- /* allocate 64kB buffer */
1473- buf = pgut_malloc (CHUNK_SIZE );
1474-
1475- /* copy content and calc CRC */
1476- for (;;)
1477- {
1478- read_len = fread (buf , 1 , CHUNK_SIZE , in );
1479-
1480- if (ferror (in ))
1481- elog (ERROR , "Cannot read from file \"%s\": %s" ,
1482- from_fullpath , strerror (errno ));
1483-
1484- if (read_len > 0 )
1485- {
1486- if (fwrite (buf , 1 , read_len , out ) != read_len )
1487- elog (ERROR , "Cannot write to file \"%s\": %s" , to_fullpath ,
1488- strerror (errno ));
1489-
1490- /* update CRC */
1491- COMP_FILE_CRC32 (true, file -> crc , buf , read_len );
1492- file -> read_size += read_len ;
1493- }
1494-
1495- if (feof (in ))
1496- break ;
1497- }
1447+ if (errmsg )
1448+ elog (ERROR , "%s" , errmsg );
1449+ else
1450+ elog (ERROR , "Cannot access remote file \"%s\"" , from_fullpath );
14981451 }
14991452
1500- file -> write_size = (int64 ) file -> read_size ;
1501-
1502- if (file -> write_size > 0 )
1503- file -> uncompressed_size = file -> write_size ;
1453+ file -> uncompressed_size = file -> read_size ;
15041454
15051455cleanup :
1456+ if (errmsg != NULL )
1457+ pg_free (errmsg );
1458+
15061459 /* finish CRC calculation and store into pgFile */
15071460 FIN_FILE_CRC32 (true, file -> crc );
15081461
1509- if (in && fclose (in ))
1510- elog (ERROR , "Cannot close the file \"%s\": %s" , from_fullpath , strerror (errno ));
1511-
15121462 if (out && fclose (out ))
15131463 elog (ERROR , "Cannot close the file \"%s\": %s" , to_fullpath , strerror (errno ));
1514-
1515- pg_free (buf );
15161464}
15171465
15181466/*
0 commit comments