@@ -208,18 +208,15 @@ merge_backups(pgBackup *to_backup, pgBackup *from_backup)
208208 */
209209 pgBackupGetPath (to_backup , control_file , lengthof (control_file ),
210210 DATABASE_FILE_LIST );
211- to_files = dir_read_file_list (from_database_path , /* Use from_database_path
212- * so root path will be
213- * equal with 'files' */
214- control_file );
211+ to_files = dir_read_file_list (NULL , control_file );
215212 /* To delete from leaf, sort in reversed order */
216213 parray_qsort (to_files , pgFileComparePathDesc );
217214 /*
218215 * Get list of files which need to be moved.
219216 */
220217 pgBackupGetPath (from_backup , control_file , lengthof (control_file ),
221218 DATABASE_FILE_LIST );
222- files = dir_read_file_list (from_database_path , control_file );
219+ files = dir_read_file_list (NULL , control_file );
223220 /* sort by size for load balancing */
224221 parray_qsort (files , pgFileCompareSize );
225222
@@ -331,8 +328,18 @@ merge_backups(pgBackup *to_backup, pgBackup *from_backup)
331328
332329 if (parray_bsearch (files , file , pgFileComparePathDesc ) == NULL )
333330 {
331+ char to_file_path [MAXPGPATH ];
332+ char * prev_path ;
333+
334+ /* We need full path, file object has relative path */
335+ join_path_components (to_file_path , to_database_path , file -> path );
336+ prev_path = file -> path ;
337+ file -> path = to_file_path ;
338+
334339 pgFileDelete (file );
335340 elog (VERBOSE , "Deleted \"%s\"" , file -> path );
341+
342+ file -> path = prev_path ;
336343 }
337344 }
338345
@@ -378,13 +385,14 @@ merge_files(void *arg)
378385 pgBackup * from_backup = argument -> from_backup ;
379386 int i ,
380387 num_files = parray_num (argument -> files );
381- int to_root_len = strlen (argument -> to_root );
382388
383389 for (i = 0 ; i < num_files ; i ++ )
384390 {
385391 pgFile * file = (pgFile * ) parray_get (argument -> files , i );
386392 pgFile * to_file ;
387393 pgFile * * res_file ;
394+ char from_file_path [MAXPGPATH ];
395+ char * prev_file_path ;
388396
389397 if (!pg_atomic_test_set_flag (& file -> lock ))
390398 continue ;
@@ -429,19 +437,23 @@ merge_files(void *arg)
429437 continue ;
430438 }
431439
440+ /* We need to make full path, file object has relative path */
441+ join_path_components (from_file_path , argument -> from_root , file -> path );
442+ prev_file_path = file -> path ;
443+ file -> path = from_file_path ;
444+
432445 /*
433446 * Move the file. We need to decompress it and compress again if
434447 * necessary.
435448 */
436- elog (VERBOSE , "Moving file \"%s\", is_datafile %d, is_cfs %d" ,
449+ elog (VERBOSE , "Merging file \"%s\", is_datafile %d, is_cfs %d" ,
437450 file -> path , file -> is_database , file -> is_cfs );
438451
439452 if (file -> is_datafile && !file -> is_cfs )
440453 {
441- char to_path_tmp [MAXPGPATH ]; /* Path of target file */
454+ char to_file_path [MAXPGPATH ]; /* Path of target file */
442455
443- join_path_components (to_path_tmp , argument -> to_root ,
444- file -> path + to_root_len + 1 );
456+ join_path_components (to_file_path , argument -> to_root , prev_file_path );
445457
446458 /*
447459 * We need more complicate algorithm if target file should be
@@ -453,7 +465,7 @@ merge_files(void *arg)
453465 char tmp_file_path [MAXPGPATH ];
454466 char * prev_path ;
455467
456- snprintf (tmp_file_path , MAXPGPATH , "%s_tmp" , to_path_tmp );
468+ snprintf (tmp_file_path , MAXPGPATH , "%s_tmp" , to_file_path );
457469
458470 /* Start the magic */
459471
@@ -479,7 +491,7 @@ merge_files(void *arg)
479491 * need the file in directory to_root.
480492 */
481493 prev_path = to_file -> path ;
482- to_file -> path = to_path_tmp ;
494+ to_file -> path = to_file_path ;
483495 /* Decompress target file into temporary one */
484496 restore_data_file (tmp_file_path , to_file , false, false,
485497 parse_program_version (to_backup -> program_version ));
@@ -494,7 +506,7 @@ merge_files(void *arg)
494506 false,
495507 parse_program_version (from_backup -> program_version ));
496508
497- elog (VERBOSE , "Compress file and save it to the directory \"%s\"" ,
509+ elog (VERBOSE , "Compress file and save it into the directory \"%s\"" ,
498510 argument -> to_root );
499511
500512 /* Again we need to change path */
@@ -504,7 +516,7 @@ merge_files(void *arg)
504516 file -> size = pgFileSize (file -> path );
505517 /* Now we can compress the file */
506518 backup_data_file (NULL , /* We shouldn't need 'arguments' here */
507- to_path_tmp , file ,
519+ to_file_path , file ,
508520 to_backup -> start_lsn ,
509521 to_backup -> backup_mode ,
510522 to_backup -> compress_alg ,
@@ -523,7 +535,7 @@ merge_files(void *arg)
523535 else
524536 {
525537 /* We can merge in-place here */
526- restore_data_file (to_path_tmp , file ,
538+ restore_data_file (to_file_path , file ,
527539 from_backup -> backup_mode == BACKUP_MODE_DIFF_DELTA ,
528540 true,
529541 parse_program_version (from_backup -> program_version ));
@@ -532,8 +544,8 @@ merge_files(void *arg)
532544 * We need to calculate write_size, restore_data_file() doesn't
533545 * do that.
534546 */
535- file -> write_size = pgFileSize (to_path_tmp );
536- file -> crc = pgFileGetCRC (to_path_tmp , true, true, NULL );
547+ file -> write_size = pgFileSize (to_file_path );
548+ file -> crc = pgFileGetCRC (to_file_path , true, true, NULL );
537549 }
538550 }
539551 else if (strcmp (file -> name , "pg_control" ) == 0 )
@@ -548,8 +560,11 @@ merge_files(void *arg)
548560 file -> compress_alg = to_backup -> compress_alg ;
549561
550562 if (file -> write_size != BYTES_INVALID )
551- elog (LOG , "Moved file \"%s\": " INT64_FORMAT " bytes" ,
563+ elog (LOG , "Merged file \"%s\": " INT64_FORMAT " bytes" ,
552564 file -> path , file -> write_size );
565+
566+ /* Restore relative path */
567+ file -> path = prev_file_path ;
553568 }
554569
555570 /* Data files merging is successful */
0 commit comments