PostgreSQL Source Code  git master
archive.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int RestoreArchivedFile (const char *path, const char *xlogfname, off_t expectedSize, const char *restoreCommand)
 

Function Documentation

◆ RestoreArchivedFile()

int RestoreArchivedFile ( const char *  path,
const char *  xlogfname,
off_t  expectedSize,
const char *  restoreCommand 
)

Definition at line 39 of file archive.c.

41 {
42  char xlogpath[MAXPGPATH];
43  char *xlogRestoreCmd;
44  int rc;
45  struct stat stat_buf;
46 
47  snprintf(xlogpath, MAXPGPATH, "%s/" XLOGDIR "/%s", path, xlogfname);
48 
49  xlogRestoreCmd = BuildRestoreCommand(restoreCommand, xlogpath,
50  xlogfname, NULL);
51  if (xlogRestoreCmd == NULL)
52  pg_fatal("cannot use restore_command with %%r placeholder");
53 
54  /*
55  * Execute restore_command, which should copy the missing file from
56  * archival storage.
57  */
58  rc = system(xlogRestoreCmd);
59  pfree(xlogRestoreCmd);
60 
61  if (rc == 0)
62  {
63  /*
64  * Command apparently succeeded, but let's make sure the file is
65  * really there now and has the correct size.
66  */
67  if (stat(xlogpath, &stat_buf) == 0)
68  {
69  if (expectedSize > 0 && stat_buf.st_size != expectedSize)
70  pg_fatal("unexpected file size for \"%s\": %lld instead of %lld",
71  xlogfname, (long long int) stat_buf.st_size,
72  (long long int) expectedSize);
73  else
74  {
75  int xlogfd = open(xlogpath, O_RDONLY | PG_BINARY, 0);
76 
77  if (xlogfd < 0)
78  pg_fatal("could not open file \"%s\" restored from archive: %m",
79  xlogpath);
80  else
81  return xlogfd;
82  }
83  }
84  else
85  {
86  if (errno != ENOENT)
87  pg_fatal("could not stat file \"%s\": %m",
88  xlogpath);
89  }
90  }
91 
92  /*
93  * If the failure was due to a signal, then it would be misleading to
94  * return with a failure at restoring the file. So just bail out and
95  * exit. Hard shell errors such as "command not found" are treated as
96  * fatal too.
97  */
98  if (wait_result_is_any_signal(rc, true))
99  pg_fatal("restore_command failed: %s",
100  wait_result_to_str(rc));
101 
102  /*
103  * The file is not available, so just let the caller decide what to do
104  * next.
105  */
106  pg_log_error("could not restore file \"%s\" from archive",
107  xlogfname);
108  return -1;
109 }
#define PG_BINARY
Definition: c.h:1268
char * BuildRestoreCommand(const char *restoreCommand, const char *xlogpath, const char *xlogfname, const char *lastRestartPointFname)
Definition: archive.c:39
#define pg_log_error(...)
Definition: logging.h:106
void pfree(void *pointer)
Definition: mcxt.c:1175
#define pg_fatal(...)
#define MAXPGPATH
#define snprintf
Definition: port.h:225
char * wait_result_to_str(int exitstatus)
Definition: wait_error.c:32
bool wait_result_is_any_signal(int exit_status, bool include_command_not_found)
Definition: wait_error.c:111
#define stat
Definition: win32_port.h:283
#define XLOGDIR

References BuildRestoreCommand(), MAXPGPATH, pfree(), PG_BINARY, pg_fatal, pg_log_error, snprintf, stat::st_size, stat, wait_result_is_any_signal(), wait_result_to_str(), and XLOGDIR.