@@ -23,7 +23,7 @@ static pgBackup* get_closest_backup(timelineInfo *tlinfo);
2323static pgBackup * get_oldest_backup (timelineInfo * tlinfo );
2424static const char * backupModes [] = {"" , "PAGE" , "PTRACK" , "DELTA" , "FULL" };
2525static pgBackup * readBackupControlFile (const char * path );
26- static void create_backup_dir (pgBackup * backup , const char * backup_instance_path );
26+ static int create_backup_dir (pgBackup * backup , const char * backup_instance_path );
2727
2828static bool backup_lock_exit_hook_registered = false;
2929static parray * locks = NULL ;
@@ -976,6 +976,7 @@ catalog_get_backup_list(InstanceState *instanceState, time_t requested_backup_id
976976 }
977977 else if (strcmp (base36enc (backup -> start_time ), data_ent -> d_name ) != 0 )
978978 {
979+ /* TODO there is no such guarantees */
979980 elog (WARNING , "backup ID in control file \"%s\" doesn't match name of the backup folder \"%s\"" ,
980981 base36enc (backup -> start_time ), backup_conf_path );
981982 }
@@ -1421,21 +1422,33 @@ get_multi_timeline_parent(parray *backup_list, parray *tli_list,
14211422 return NULL ;
14221423}
14231424
1424- /* Create backup directory in $BACKUP_PATH
1425- * Note, that backup_id attribute is updated,
1426- * so it is possible to get diffrent values in
1425+ /*
1426+ * Create backup directory in $BACKUP_PATH
1427+ * (with proposed backup->backup_id)
1428+ * and initialize this directory.
1429+ * If creation of directory fails, then
1430+ * backup_id will be cleared (set to INVALID_BACKUP_ID).
1431+ * It is possible to get diffrent values in
14271432 * pgBackup.start_time and pgBackup.backup_id.
14281433 * It may be ok or maybe not, so it's up to the caller
14291434 * to fix it or let it be.
14301435 */
14311436void
1432- pgBackupCreateDir (pgBackup * backup , InstanceState * instanceState , time_t start_time )
1437+ pgBackupInitDir (pgBackup * backup , const char * backup_instance_path )
14331438{
1434- int i ;
1435- parray * subdirs = parray_new ();
1436- parray * backups ;
1437- pgBackup * target_backup ;
1439+ int i ;
1440+ char temp [MAXPGPATH ];
1441+ parray * subdirs ;
14381442
1443+ /* Try to create backup directory at first */
1444+ if (create_backup_dir (backup , backup_instance_path ) != 0 )
1445+ {
1446+ /* Clear backup_id as indication of error */
1447+ backup -> backup_id = INVALID_BACKUP_ID ;
1448+ return ;
1449+ }
1450+
1451+ subdirs = parray_new ();
14391452 parray_append (subdirs , pg_strdup (DATABASE_DIR ));
14401453
14411454 /* Add external dirs containers */
@@ -1447,38 +1460,13 @@ pgBackupCreateDir(pgBackup *backup, InstanceState *instanceState, time_t start_t
14471460 false);
14481461 for (i = 0 ; i < parray_num (external_list ); i ++ )
14491462 {
1450- char temp [MAXPGPATH ];
14511463 /* Numeration of externaldirs starts with 1 */
14521464 makeExternalDirPathByNum (temp , EXTERNAL_DIR , i + 1 );
14531465 parray_append (subdirs , pg_strdup (temp ));
14541466 }
14551467 free_dir_list (external_list );
14561468 }
14571469
1458- /* Get list of all backups*/
1459- backups = catalog_get_backup_list (instanceState , INVALID_BACKUP_ID );
1460- if (parray_num (backups ) > 0 )
1461- {
1462- target_backup = (pgBackup * ) parray_get (backups , 0 );
1463- if (start_time > target_backup -> backup_id )
1464- {
1465- backup -> backup_id = start_time ;
1466- create_backup_dir (backup , instanceState -> instance_backup_subdir_path );
1467- }
1468- else
1469- {
1470- elog (ERROR , "Cannot create directory for older backup" );
1471- }
1472- }
1473- else
1474- {
1475- backup -> backup_id = start_time ;
1476- create_backup_dir (backup , instanceState -> instance_backup_subdir_path );
1477- }
1478-
1479- if (backup -> backup_id == 0 )
1480- elog (ERROR , "Cannot create backup directory: %s" , strerror (errno ));
1481-
14821470 backup -> database_dir = pgut_malloc (MAXPGPATH );
14831471 join_path_components (backup -> database_dir , backup -> root_dir , DATABASE_DIR );
14841472
@@ -1488,10 +1476,8 @@ pgBackupCreateDir(pgBackup *backup, InstanceState *instanceState, time_t start_t
14881476 /* create directories for actual backup files */
14891477 for (i = 0 ; i < parray_num (subdirs ); i ++ )
14901478 {
1491- char path [MAXPGPATH ];
1492-
1493- join_path_components (path , backup -> root_dir , parray_get (subdirs , i ));
1494- fio_mkdir (FIO_BACKUP_HOST , path , DIR_PERMISSION , false);
1479+ join_path_components (temp , backup -> root_dir , parray_get (subdirs , i ));
1480+ fio_mkdir (FIO_BACKUP_HOST , temp , DIR_PERMISSION , false);
14951481 }
14961482
14971483 free_dir_list (subdirs );
@@ -1500,33 +1486,26 @@ pgBackupCreateDir(pgBackup *backup, InstanceState *instanceState, time_t start_t
15001486/*
15011487 * Create root directory for backup,
15021488 * update pgBackup.root_dir if directory creation was a success
1489+ * Return values (same as dir_create_dir()):
1490+ * 0 - ok
1491+ * -1 - error (warning message already emitted)
15031492 */
1504- void
1493+ int
15051494create_backup_dir (pgBackup * backup , const char * backup_instance_path )
15061495{
1507- int attempts = 10 ;
1496+ int rc ;
1497+ char path [MAXPGPATH ];
15081498
1509- while (attempts -- )
1510- {
1511- int rc ;
1512- char path [MAXPGPATH ];
1513-
1514- join_path_components (path , backup_instance_path , base36enc (backup -> backup_id ));
1499+ join_path_components (path , backup_instance_path , base36enc (backup -> backup_id ));
15151500
1516- rc = fio_mkdir (FIO_BACKUP_HOST , path , DIR_PERMISSION , true);
1517-
1518- if (rc == 0 )
1519- {
1520- backup -> root_dir = pgut_strdup (path );
1521- return ;
1522- }
1523- else
1524- {
1525- elog (WARNING , "Cannot create directory \"%s\": %s" , path , strerror (errno ));
1526- sleep (1 );
1527- }
1528- }
1501+ /* TODO: add wrapper for remote mode */
1502+ rc = fio_mkdir (FIO_BACKUP_HOST , path , DIR_PERMISSION , true);
15291503
1504+ if (rc == 0 )
1505+ backup -> root_dir = pgut_strdup (path );
1506+ else
1507+ elog (WARNING , "Cannot create directory \"%s\": %s" , path , strerror (errno ));
1508+ return rc ;
15301509}
15311510
15321511/*
0 commit comments