PostgreSQL Source Code  git master
fe_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 43 of file fe_archive.c.

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

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