PostgreSQL Source Code  git master
basic_archive.c File Reference
#include "postgres.h"
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include "archive/archive_module.h"
#include "common/int.h"
#include "miscadmin.h"
#include "storage/copydir.h"
#include "storage/fd.h"
#include "utils/guc.h"
#include "utils/memutils.h"
Include dependency graph for basic_archive.c:

Go to the source code of this file.

Macros

#define CMP_BUF_SIZE   (4096)
 

Functions

static bool basic_archive_configured (ArchiveModuleState *state)
 
static bool basic_archive_file (ArchiveModuleState *state, const char *file, const char *path)
 
static bool check_archive_directory (char **newval, void **extra, GucSource source)
 
static bool compare_files (const char *file1, const char *file2)
 
void _PG_init (void)
 
const ArchiveModuleCallbacks_PG_archive_module_init (void)
 

Variables

 PG_MODULE_MAGIC
 
static char * archive_directory = NULL
 
static const ArchiveModuleCallbacks basic_archive_callbacks
 

Macro Definition Documentation

◆ CMP_BUF_SIZE

#define CMP_BUF_SIZE   (4096)

Function Documentation

◆ _PG_archive_module_init()

const ArchiveModuleCallbacks* _PG_archive_module_init ( void  )

Definition at line 83 of file basic_archive.c.

84 {
86 }
static const ArchiveModuleCallbacks basic_archive_callbacks
Definition: basic_archive.c:50

References basic_archive_callbacks.

◆ _PG_init()

void _PG_init ( void  )

Definition at line 63 of file basic_archive.c.

64 {
65  DefineCustomStringVariable("basic_archive.archive_directory",
66  gettext_noop("Archive file destination directory."),
67  NULL,
69  "",
70  PGC_SIGHUP,
71  0,
72  check_archive_directory, NULL, NULL);
73 
74  MarkGUCPrefixReserved("basic_archive");
75 }
static char * archive_directory
Definition: basic_archive.c:43
static bool check_archive_directory(char **newval, void **extra, GucSource source)
Definition: basic_archive.c:94
#define gettext_noop(x)
Definition: c.h:1196
void DefineCustomStringVariable(const char *name, const char *short_desc, const char *long_desc, char **valueAddr, const char *bootValue, GucContext context, int flags, GucStringCheckHook check_hook, GucStringAssignHook assign_hook, GucShowHook show_hook)
Definition: guc.c:5161
void MarkGUCPrefixReserved(const char *className)
Definition: guc.c:5222
@ PGC_SIGHUP
Definition: guc.h:71

References archive_directory, check_archive_directory(), DefineCustomStringVariable(), gettext_noop, MarkGUCPrefixReserved(), and PGC_SIGHUP.

◆ basic_archive_configured()

static bool basic_archive_configured ( ArchiveModuleState state)
static

Definition at line 136 of file basic_archive.c.

137 {
138  if (archive_directory != NULL && archive_directory[0] != '\0')
139  return true;
140 
141  arch_module_check_errdetail("%s is not set.",
142  "basic_archive.archive_directory");
143  return false;
144 }
#define arch_module_check_errdetail

References arch_module_check_errdetail, and archive_directory.

◆ basic_archive_file()

static bool basic_archive_file ( ArchiveModuleState state,
const char *  file,
const char *  path 
)
static

Definition at line 152 of file basic_archive.c.

153 {
154  char destination[MAXPGPATH];
155  char temp[MAXPGPATH + 256];
156  struct stat st;
157  struct timeval tv;
158  uint64 epoch; /* milliseconds */
159 
160  ereport(DEBUG3,
161  (errmsg("archiving \"%s\" via basic_archive", file)));
162 
163  snprintf(destination, MAXPGPATH, "%s/%s", archive_directory, file);
164 
165  /*
166  * First, check if the file has already been archived. If it already
167  * exists and has the same contents as the file we're trying to archive,
168  * we can return success (after ensuring the file is persisted to disk).
169  * This scenario is possible if the server crashed after archiving the
170  * file but before renaming its .ready file to .done.
171  *
172  * If the archive file already exists but has different contents,
173  * something might be wrong, so we just fail.
174  */
175  if (stat(destination, &st) == 0)
176  {
177  if (compare_files(path, destination))
178  {
179  ereport(DEBUG3,
180  (errmsg("archive file \"%s\" already exists with identical contents",
181  destination)));
182 
183  fsync_fname(destination, false);
185 
186  return true;
187  }
188 
189  ereport(ERROR,
190  (errmsg("archive file \"%s\" already exists", destination)));
191  }
192  else if (errno != ENOENT)
193  ereport(ERROR,
195  errmsg("could not stat file \"%s\": %m", destination)));
196 
197  /*
198  * Pick a sufficiently unique name for the temporary file so that a
199  * collision is unlikely. This helps avoid problems in case a temporary
200  * file was left around after a crash or another server happens to be
201  * archiving to the same directory.
202  */
203  gettimeofday(&tv, NULL);
204  if (pg_mul_u64_overflow((uint64) 1000, (uint64) tv.tv_sec, &epoch) ||
205  pg_add_u64_overflow(epoch, (uint64) (tv.tv_usec / 1000), &epoch))
206  elog(ERROR, "could not generate temporary file name for archiving");
207 
208  snprintf(temp, sizeof(temp), "%s/%s.%s.%d." UINT64_FORMAT,
209  archive_directory, "archtemp", file, MyProcPid, epoch);
210 
211  /*
212  * Copy the file to its temporary destination. Note that this will fail
213  * if temp already exists.
214  */
215  copy_file(path, temp);
216 
217  /*
218  * Sync the temporary file to disk and move it to its final destination.
219  * Note that this will overwrite any existing file, but this is only
220  * possible if someone else created the file since the stat() above.
221  */
222  (void) durable_rename(temp, destination, ERROR);
223 
224  ereport(DEBUG1,
225  (errmsg("archived \"%s\" via basic_archive", file)));
226 
227  return true;
228 }
static bool compare_files(const char *file1, const char *file2)
#define UINT64_FORMAT
Definition: c.h:549
void copy_file(const char *fromfile, const char *tofile)
Definition: copydir.c:117
int errcode_for_file_access(void)
Definition: elog.c:882
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define DEBUG3
Definition: elog.h:28
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:782
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:756
int MyProcPid
Definition: globals.c:45
static bool pg_add_u64_overflow(uint64 a, uint64 b, uint64 *result)
Definition: int.h:380
static bool pg_mul_u64_overflow(uint64 a, uint64 b, uint64 *result)
Definition: int.h:414
#define MAXPGPATH
#define snprintf
Definition: port.h:238
#define stat
Definition: win32_port.h:284
static const unsigned __int64 epoch
int gettimeofday(struct timeval *tp, void *tzp)

References archive_directory, compare_files(), copy_file(), DEBUG1, DEBUG3, durable_rename(), elog, epoch, ereport, errcode_for_file_access(), errmsg(), ERROR, fsync_fname(), gettimeofday(), MAXPGPATH, MyProcPid, pg_add_u64_overflow(), pg_mul_u64_overflow(), snprintf, stat, and UINT64_FORMAT.

◆ check_archive_directory()

static bool check_archive_directory ( char **  newval,
void **  extra,
GucSource  source 
)
static

Definition at line 94 of file basic_archive.c.

95 {
96  struct stat st;
97 
98  /*
99  * The default value is an empty string, so we have to accept that value.
100  * Our check_configured callback also checks for this and prevents
101  * archiving from proceeding if it is still empty.
102  */
103  if (*newval == NULL || *newval[0] == '\0')
104  return true;
105 
106  /*
107  * Make sure the file paths won't be too long. The docs indicate that the
108  * file names to be archived can be up to 64 characters long.
109  */
110  if (strlen(*newval) + 64 + 2 >= MAXPGPATH)
111  {
112  GUC_check_errdetail("Archive directory too long.");
113  return false;
114  }
115 
116  /*
117  * Do a basic sanity check that the specified archive directory exists. It
118  * could be removed at some point in the future, so we still need to be
119  * prepared for it not to exist in the actual archiving logic.
120  */
121  if (stat(*newval, &st) != 0 || !S_ISDIR(st.st_mode))
122  {
123  GUC_check_errdetail("Specified archive directory does not exist.");
124  return false;
125  }
126 
127  return true;
128 }
#define newval
#define GUC_check_errdetail
Definition: guc.h:448
#define S_ISDIR(m)
Definition: win32_port.h:325

References GUC_check_errdetail, MAXPGPATH, newval, S_ISDIR, stat::st_mode, and stat.

Referenced by _PG_init().

◆ compare_files()

static bool compare_files ( const char *  file1,
const char *  file2 
)
static

Definition at line 236 of file basic_archive.c.

237 {
238 #define CMP_BUF_SIZE (4096)
239  char buf1[CMP_BUF_SIZE];
240  char buf2[CMP_BUF_SIZE];
241  int fd1;
242  int fd2;
243  bool ret = true;
244 
245  fd1 = OpenTransientFile(file1, O_RDONLY | PG_BINARY);
246  if (fd1 < 0)
247  ereport(ERROR,
249  errmsg("could not open file \"%s\": %m", file1)));
250 
251  fd2 = OpenTransientFile(file2, O_RDONLY | PG_BINARY);
252  if (fd2 < 0)
253  ereport(ERROR,
255  errmsg("could not open file \"%s\": %m", file2)));
256 
257  for (;;)
258  {
259  int nbytes = 0;
260  int buf1_len = 0;
261  int buf2_len = 0;
262 
263  while (buf1_len < CMP_BUF_SIZE)
264  {
265  nbytes = read(fd1, buf1 + buf1_len, CMP_BUF_SIZE - buf1_len);
266  if (nbytes < 0)
267  ereport(ERROR,
269  errmsg("could not read file \"%s\": %m", file1)));
270  else if (nbytes == 0)
271  break;
272 
273  buf1_len += nbytes;
274  }
275 
276  while (buf2_len < CMP_BUF_SIZE)
277  {
278  nbytes = read(fd2, buf2 + buf2_len, CMP_BUF_SIZE - buf2_len);
279  if (nbytes < 0)
280  ereport(ERROR,
282  errmsg("could not read file \"%s\": %m", file2)));
283  else if (nbytes == 0)
284  break;
285 
286  buf2_len += nbytes;
287  }
288 
289  if (buf1_len != buf2_len || memcmp(buf1, buf2, buf1_len) != 0)
290  {
291  ret = false;
292  break;
293  }
294  else if (buf1_len == 0)
295  break;
296  }
297 
298  if (CloseTransientFile(fd1) != 0)
299  ereport(ERROR,
301  errmsg("could not close file \"%s\": %m", file1)));
302 
303  if (CloseTransientFile(fd2) != 0)
304  ereport(ERROR,
306  errmsg("could not close file \"%s\": %m", file2)));
307 
308  return ret;
309 }
#define CMP_BUF_SIZE
#define PG_BINARY
Definition: c.h:1273
int CloseTransientFile(int fd)
Definition: fd.c:2809
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2633
#define read(a, b, c)
Definition: win32.h:13

References CloseTransientFile(), CMP_BUF_SIZE, ereport, errcode_for_file_access(), errmsg(), ERROR, OpenTransientFile(), PG_BINARY, and read.

Referenced by basic_archive_file().

Variable Documentation

◆ archive_directory

char* archive_directory = NULL
static

Definition at line 43 of file basic_archive.c.

Referenced by _PG_init(), basic_archive_configured(), and basic_archive_file().

◆ basic_archive_callbacks

const ArchiveModuleCallbacks basic_archive_callbacks
static
Initial value:
= {
.startup_cb = NULL,
.check_configured_cb = basic_archive_configured,
.archive_file_cb = basic_archive_file,
.shutdown_cb = NULL
}
static bool basic_archive_file(ArchiveModuleState *state, const char *file, const char *path)
static bool basic_archive_configured(ArchiveModuleState *state)

Definition at line 50 of file basic_archive.c.

Referenced by _PG_archive_module_init().

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 41 of file basic_archive.c.