Skip to content

Commit 6485b9f

Browse files
author
Artur Zakirov
committed
Check if restore destination exists
1 parent 38407a0 commit 6485b9f

File tree

3 files changed

+42
-26
lines changed

3 files changed

+42
-26
lines changed

dir.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,3 +725,41 @@ dir_copy_files(const char *from_root, const char *to_root)
725725
parray_walk(files, pgFileFree);
726726
parray_free(files);
727727
}
728+
729+
/*
730+
* Check if directory empty.
731+
*/
732+
bool
733+
dir_is_empty(const char *path)
734+
{
735+
DIR *dir;
736+
struct dirent *dir_ent;
737+
738+
dir = opendir(path);
739+
if (dir == NULL)
740+
{
741+
/* Directory in path doesn't exist */
742+
if (errno == ENOENT)
743+
return true;
744+
elog(ERROR, "cannot open directory \"%s\": %s", path, strerror(errno));
745+
}
746+
747+
errno = 0;
748+
while ((dir_ent = readdir(dir)))
749+
{
750+
/* Skip entries point current dir or parent dir */
751+
if (strcmp(dir_ent->d_name, ".") == 0 ||
752+
strcmp(dir_ent->d_name, "..") == 0)
753+
continue;
754+
755+
/* Directory is not empty */
756+
closedir(dir);
757+
return false;
758+
}
759+
if (errno)
760+
elog(ERROR, "cannot read directory \"%s\": %s", path, strerror(errno));
761+
762+
closedir(dir);
763+
764+
return true;
765+
}

pg_probackup.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ extern parray *dir_read_file_list(const char *root, const char *file_txt);
322322

323323
extern int dir_create_dir(const char *path, mode_t mode);
324324
extern void dir_copy_files(const char *from_root, const char *to_root);
325+
extern bool dir_is_empty(const char *path);
325326

326327
extern pgFile *pgFileNew(const char *path, bool omit_symlink);
327328
extern void pgFileDelete(pgFile *file);

restore.c

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ do_restore(time_t backup_id,
4646
int i;
4747
int base_index; /* index of base (full) backup */
4848
int ret;
49-
TimeLineID cur_tli;
5049
parray *backups;
5150

52-
parray *files;
5351
parray *timelines;
5452
pgBackup *base_backup = NULL;
5553
pgBackup *dest_backup = NULL;
@@ -85,9 +83,6 @@ do_restore(time_t backup_id,
8583
if (!backups)
8684
elog(ERROR, "cannot process any more.");
8785

88-
cur_tli = get_current_timeline(true);
89-
elog(LOG, "current instance timeline ID = %u", cur_tli);
90-
9186
if (target_tli)
9287
{
9388
elog(LOG, "target timeline ID = %u", target_tli);
@@ -148,27 +143,9 @@ do_restore(time_t backup_id,
148143
base_backup_found:
149144
base_index = i;
150145

151-
/*
152-
* Clear restore destination, but don't remove $PGDATA.
153-
* To remove symbolic link, get file list with "omit_symlink = false".
154-
*/
155-
if (!check)
156-
{
157-
elog(LOG, "----------------------------------------");
158-
elog(LOG, "clearing restore destination");
159-
160-
files = parray_new();
161-
dir_list_file(files, pgdata, false, false, false);
162-
parray_qsort(files, pgFileComparePathDesc); /* delete from leaf */
163-
164-
for (i = 0; i < parray_num(files); i++)
165-
{
166-
pgFile *file = (pgFile *) parray_get(files, i);
167-
pgFileDelete(file);
168-
}
169-
parray_walk(files, pgFileFree);
170-
parray_free(files);
171-
}
146+
/* Check if restore destination empty */
147+
if (!dir_is_empty(pgdata))
148+
elog(ERROR, "restore destination is not empty");
172149

173150
print_backup_lsn(base_backup);
174151

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy