PostgreSQL Source Code  git master
pg_verifybackup.c File Reference
#include "postgres_fe.h"
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "common/hashfn.h"
#include "common/logging.h"
#include "fe_utils/simple_list.h"
#include "getopt_long.h"
#include "parse_manifest.h"
#include "lib/simplehash.h"
Include dependency graph for pg_verifybackup.c:

Go to the source code of this file.

Data Structures

struct  manifest_file
 
struct  manifest_wal_range
 
struct  parser_context
 
struct  verifier_context
 

Macros

#define ESTIMATED_BYTES_PER_MANIFEST_LINE   100
 
#define READ_CHUNK_SIZE   4096
 
#define SH_PREFIX   manifest_files
 
#define SH_ELEMENT_TYPE   manifest_file
 
#define SH_KEY_TYPE   char *
 
#define SH_KEY   pathname
 
#define SH_HASH_KEY(tb, key)   hash_string_pointer(key)
 
#define SH_EQUAL(tb, a, b)   (strcmp(a, b) == 0)
 
#define SH_SCOPE   static inline
 
#define SH_RAW_ALLOCATOR   pg_malloc0
 
#define SH_DECLARE
 
#define SH_DEFINE
 

Typedefs

typedef struct manifest_file manifest_file
 
typedef struct manifest_wal_range manifest_wal_range
 
typedef struct parser_context parser_context
 
typedef struct verifier_context verifier_context
 

Functions

static uint32 hash_string_pointer (char *s)
 
static void parse_manifest_file (char *manifest_path, manifest_files_hash **ht_p, manifest_wal_range **first_wal_range_p)
 
static void record_manifest_details_for_file (JsonManifestParseContext *context, char *pathname, size_t size, pg_checksum_type checksum_type, int checksum_length, uint8 *checksum_payload)
 
static void record_manifest_details_for_wal_range (JsonManifestParseContext *context, TimeLineID tli, XLogRecPtr start_lsn, XLogRecPtr end_lsn)
 
static void report_manifest_error (JsonManifestParseContext *context, const char *fmt,...) pg_attribute_printf(2
 
static void pg_attribute_noreturn ()
 
static void verify_backup_directory (verifier_context *context, char *relpath, char *fullpath)
 
static void verify_backup_file (verifier_context *context, char *relpath, char *fullpath)
 
static void report_extra_backup_files (verifier_context *context)
 
static void verify_backup_checksums (verifier_context *context)
 
static void verify_file_checksum (verifier_context *context, manifest_file *m, char *pathname)
 
static void parse_required_wal (verifier_context *context, char *pg_waldump_path, char *wal_directory, manifest_wal_range *first_wal_range)
 
static void report_backup_error (verifier_context *context, const char *pg_restrict fmt,...) pg_attribute_printf(2
 
static void static void report_fatal_error (const char *pg_restrict fmt,...) pg_attribute_printf(1
 
static bool should_ignore_relpath (verifier_context *context, char *relpath)
 
static void usage (void)
 
int main (int argc, char **argv)
 

Variables

static const char * progname
 

Macro Definition Documentation

◆ ESTIMATED_BYTES_PER_MANIFEST_LINE

#define ESTIMATED_BYTES_PER_MANIFEST_LINE   100

Definition at line 38 of file pg_verifybackup.c.

Referenced by parse_manifest_file().

◆ READ_CHUNK_SIZE

#define READ_CHUNK_SIZE   4096

Definition at line 43 of file pg_verifybackup.c.

Referenced by verify_file_checksum().

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 74 of file pg_verifybackup.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 75 of file pg_verifybackup.c.

◆ SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   manifest_file

Definition at line 67 of file pg_verifybackup.c.

◆ SH_EQUAL

#define SH_EQUAL (   tb,
  a,
 
)    (strcmp(a, b) == 0)

Definition at line 71 of file pg_verifybackup.c.

◆ SH_HASH_KEY

#define SH_HASH_KEY (   tb,
  key 
)    hash_string_pointer(key)

Definition at line 70 of file pg_verifybackup.c.

◆ SH_KEY

#define SH_KEY   pathname

Definition at line 69 of file pg_verifybackup.c.

◆ SH_KEY_TYPE

#define SH_KEY_TYPE   char *

Definition at line 68 of file pg_verifybackup.c.

◆ SH_PREFIX

#define SH_PREFIX   manifest_files

Definition at line 66 of file pg_verifybackup.c.

◆ SH_RAW_ALLOCATOR

#define SH_RAW_ALLOCATOR   pg_malloc0

Definition at line 73 of file pg_verifybackup.c.

◆ SH_SCOPE

#define SH_SCOPE   static inline

Definition at line 72 of file pg_verifybackup.c.

Typedef Documentation

◆ manifest_file

typedef struct manifest_file manifest_file

◆ manifest_wal_range

◆ parser_context

◆ verifier_context

Function Documentation

◆ hash_string_pointer()

static uint32 hash_string_pointer ( char *  s)
static

Definition at line 877 of file pg_verifybackup.c.

References hash_bytes().

878 {
879  unsigned char *ss = (unsigned char *) s;
880 
881  return hash_bytes(ss, strlen(s));
882 }
uint32 hash_bytes(const unsigned char *k, int keylen)
Definition: hashfn.c:146

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 158 of file pg_verifybackup.c.

References _, arg, verifier_context::backup_directory, canonicalize_path(), verifier_context::exit_on_error, find_my_exec(), find_other_exec(), fprintf, get_progname(), getopt_long(), verifier_context::ht, verifier_context::ignore_list, MAXPGPATH, no_argument, optarg, optind, parse_manifest_file(), parse_required_wal(), pg_log_fatal, pg_logging_init(), pg_malloc(), PG_TEXTDOMAIN, printf, psprintf(), pstrdup(), report_extra_backup_files(), required_argument, verifier_context::saw_any_error, set_pglocale_pgservice(), simple_string_list_append(), strlcpy(), usage(), verify_backup_checksums(), and verify_backup_directory().

159 {
160  static struct option long_options[] = {
161  {"exit-on-error", no_argument, NULL, 'e'},
162  {"ignore", required_argument, NULL, 'i'},
163  {"manifest-path", required_argument, NULL, 'm'},
164  {"no-parse-wal", no_argument, NULL, 'n'},
165  {"quiet", no_argument, NULL, 'q'},
166  {"skip-checksums", no_argument, NULL, 's'},
167  {"wal-directory", required_argument, NULL, 'w'},
168  {NULL, 0, NULL, 0}
169  };
170 
171  int c;
172  verifier_context context;
173  manifest_wal_range *first_wal_range;
174  char *manifest_path = NULL;
175  bool no_parse_wal = false;
176  bool quiet = false;
177  bool skip_checksums = false;
178  char *wal_directory = NULL;
179  char *pg_waldump_path = NULL;
180 
181  pg_logging_init(argv[0]);
182  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_verifybackup"));
183  progname = get_progname(argv[0]);
184 
185  memset(&context, 0, sizeof(context));
186 
187  if (argc > 1)
188  {
189  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
190  {
191  usage();
192  exit(0);
193  }
194  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
195  {
196  puts("pg_verifybackup (PostgreSQL) " PG_VERSION);
197  exit(0);
198  }
199  }
200 
201  /*
202  * Skip certain files in the toplevel directory.
203  *
204  * Ignore the backup_manifest file, because it's not included in the
205  * backup manifest.
206  *
207  * Ignore the pg_wal directory, because those files are not included in
208  * the backup manifest either, since they are fetched separately from the
209  * backup itself, and verified via a separate mechanism.
210  *
211  * Ignore postgresql.auto.conf, recovery.signal, and standby.signal,
212  * because we expect that those files may sometimes be created or changed
213  * as part of the backup process. For example, pg_basebackup -R will
214  * modify postgresql.auto.conf and create standby.signal.
215  */
216  simple_string_list_append(&context.ignore_list, "backup_manifest");
217  simple_string_list_append(&context.ignore_list, "pg_wal");
218  simple_string_list_append(&context.ignore_list, "postgresql.auto.conf");
219  simple_string_list_append(&context.ignore_list, "recovery.signal");
220  simple_string_list_append(&context.ignore_list, "standby.signal");
221 
222  while ((c = getopt_long(argc, argv, "ei:m:nqsw:", long_options, NULL)) != -1)
223  {
224  switch (c)
225  {
226  case 'e':
227  context.exit_on_error = true;
228  break;
229  case 'i':
230  {
231  char *arg = pstrdup(optarg);
232 
233  canonicalize_path(arg);
234  simple_string_list_append(&context.ignore_list, arg);
235  break;
236  }
237  case 'm':
238  manifest_path = pstrdup(optarg);
239  canonicalize_path(manifest_path);
240  break;
241  case 'n':
242  no_parse_wal = true;
243  break;
244  case 'q':
245  quiet = true;
246  break;
247  case 's':
248  skip_checksums = true;
249  break;
250  case 'w':
251  wal_directory = pstrdup(optarg);
252  canonicalize_path(wal_directory);
253  break;
254  default:
255  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
256  progname);
257  exit(1);
258  }
259  }
260 
261  /* Get backup directory name */
262  if (optind >= argc)
263  {
264  pg_log_fatal("no backup directory specified");
265  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
266  progname);
267  exit(1);
268  }
269  context.backup_directory = pstrdup(argv[optind++]);
271 
272  /* Complain if any arguments remain */
273  if (optind < argc)
274  {
275  pg_log_fatal("too many command-line arguments (first is \"%s\")",
276  argv[optind]);
277  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
278  progname);
279  exit(1);
280  }
281 
282  /* Unless --no-parse-wal was specified, we will need pg_waldump. */
283  if (!no_parse_wal)
284  {
285  int ret;
286 
287  pg_waldump_path = pg_malloc(MAXPGPATH);
288  ret = find_other_exec(argv[0], "pg_waldump",
289  "pg_waldump (PostgreSQL) " PG_VERSION "\n",
290  pg_waldump_path);
291  if (ret < 0)
292  {
293  char full_path[MAXPGPATH];
294 
295  if (find_my_exec(argv[0], full_path) < 0)
296  strlcpy(full_path, progname, sizeof(full_path));
297  if (ret == -1)
298  pg_log_fatal("The program \"%s\" is needed by %s but was not found in the\n"
299  "same directory as \"%s\".\n"
300  "Check your installation.",
301  "pg_waldump", "pg_verifybackup", full_path);
302  else
303  pg_log_fatal("The program \"%s\" was found by \"%s\"\n"
304  "but was not the same version as %s.\n"
305  "Check your installation.",
306  "pg_waldump", full_path, "pg_verifybackup");
307  }
308  }
309 
310  /* By default, look for the manifest in the backup directory. */
311  if (manifest_path == NULL)
312  manifest_path = psprintf("%s/backup_manifest",
313  context.backup_directory);
314 
315  /* By default, look for the WAL in the backup directory, too. */
316  if (wal_directory == NULL)
317  wal_directory = psprintf("%s/pg_wal", context.backup_directory);
318 
319  /*
320  * Try to read the manifest. We treat any errors encountered while parsing
321  * the manifest as fatal; there doesn't seem to be much point in trying to
322  * verify the backup directory against a corrupted manifest.
323  */
324  parse_manifest_file(manifest_path, &context.ht, &first_wal_range);
325 
326  /*
327  * Now scan the files in the backup directory. At this stage, we verify
328  * that every file on disk is present in the manifest and that the sizes
329  * match. We also set the "matched" flag on every manifest entry that
330  * corresponds to a file on disk.
331  */
332  verify_backup_directory(&context, NULL, context.backup_directory);
333 
334  /*
335  * The "matched" flag should now be set on every entry in the hash table.
336  * Any entries for which the bit is not set are files mentioned in the
337  * manifest that don't exist on disk.
338  */
339  report_extra_backup_files(&context);
340 
341  /*
342  * Now do the expensive work of verifying file checksums, unless we were
343  * told to skip it.
344  */
345  if (!skip_checksums)
346  verify_backup_checksums(&context);
347 
348  /*
349  * Try to parse the required ranges of WAL records, unless we were told
350  * not to do so.
351  */
352  if (!no_parse_wal)
353  parse_required_wal(&context, pg_waldump_path,
354  wal_directory, first_wal_range);
355 
356  /*
357  * If everything looks OK, tell the user this, unless we were asked to
358  * work quietly.
359  */
360  if (!context.saw_any_error && !quiet)
361  printf(_("backup successfully verified\n"));
362 
363  return context.saw_any_error ? 1 : 0;
364 }
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
Definition: exec.c:323
static const char * progname
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
const char * get_progname(const char *argv0)
Definition: path.c:453
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
char * pstrdup(const char *in)
Definition: mcxt.c:1186
void pg_logging_init(const char *argv0)
Definition: logging.c:81
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
void canonicalize_path(char *path)
Definition: path.c:254
manifest_files_hash * ht
#define printf(...)
Definition: port.h:199
#define fprintf
Definition: port.h:197
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:50
#define MAXPGPATH
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:128
char * c
static void verify_backup_directory(verifier_context *context, char *relpath, char *fullpath)
static void parse_manifest_file(char *manifest_path, manifest_files_hash **ht_p, manifest_wal_range **first_wal_range_p)
static void verify_backup_checksums(verifier_context *context)
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1177
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static void report_extra_backup_files(verifier_context *context)
static void usage(void)
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:434
char * optarg
Definition: getopt.c:52
void * arg
SimpleStringList ignore_list
#define _(x)
Definition: elog.c:88
static void parse_required_wal(verifier_context *context, char *pg_waldump_path, char *wal_directory, manifest_wal_range *first_wal_range)
#define pg_log_fatal(...)
Definition: logging.h:75

◆ parse_manifest_file()

static void parse_manifest_file ( char *  manifest_path,
manifest_files_hash **  ht_p,
manifest_wal_range **  first_wal_range_p 
)
static

Definition at line 372 of file pg_verifybackup.c.

References close, JsonManifestParseContext::error_cb, ESTIMATED_BYTES_PER_MANIFEST_LINE, fd(), parser_context::first_wal_range, parser_context::ht, json_parse_manifest(), parser_context::last_wal_range, Max, Min, JsonManifestParseContext::perfile_cb, JsonManifestParseContext::perwalrange_cb, pfree(), PG_BINARY, pg_malloc(), PG_UINT32_MAX, JsonManifestParseContext::private_data, read, record_manifest_details_for_file(), record_manifest_details_for_wal_range(), report_fatal_error(), report_manifest_error(), and stat.

Referenced by main().

374 {
375  int fd;
376  struct stat statbuf;
377  off_t estimate;
378  uint32 initial_size;
379  manifest_files_hash *ht;
380  char *buffer;
381  int rc;
382  parser_context private_context;
383  JsonManifestParseContext context;
384 
385  /* Open the manifest file. */
386  if ((fd = open(manifest_path, O_RDONLY | PG_BINARY, 0)) < 0)
387  report_fatal_error("could not open file \"%s\": %m", manifest_path);
388 
389  /* Figure out how big the manifest is. */
390  if (fstat(fd, &statbuf) != 0)
391  report_fatal_error("could not stat file \"%s\": %m", manifest_path);
392 
393  /* Guess how large to make the hash table based on the manifest size. */
394  estimate = statbuf.st_size / ESTIMATED_BYTES_PER_MANIFEST_LINE;
395  initial_size = Min(PG_UINT32_MAX, Max(estimate, 256));
396 
397  /* Create the hash table. */
398  ht = manifest_files_create(initial_size, NULL);
399 
400  /*
401  * Slurp in the whole file.
402  *
403  * This is not ideal, but there's currently no easy way to get
404  * pg_parse_json() to perform incremental parsing.
405  */
406  buffer = pg_malloc(statbuf.st_size);
407  rc = read(fd, buffer, statbuf.st_size);
408  if (rc != statbuf.st_size)
409  {
410  if (rc < 0)
411  report_fatal_error("could not read file \"%s\": %m",
412  manifest_path);
413  else
414  report_fatal_error("could not read file \"%s\": read %d of %zu",
415  manifest_path, rc, (size_t) statbuf.st_size);
416  }
417 
418  /* Close the manifest file. */
419  close(fd);
420 
421  /* Parse the manifest. */
422  private_context.ht = ht;
423  private_context.first_wal_range = NULL;
424  private_context.last_wal_range = NULL;
425  context.private_data = &private_context;
429  json_parse_manifest(&context, buffer, statbuf.st_size);
430 
431  /* Done with the buffer. */
432  pfree(buffer);
433 
434  /* Return the file hash table and WAL range list we constructed. */
435  *ht_p = ht;
436  *first_wal_range_p = private_context.first_wal_range;
437 }
static void static void report_fatal_error(const char *pg_restrict fmt,...) pg_attribute_printf(1
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define ESTIMATED_BYTES_PER_MANIFEST_LINE
json_manifest_error_callback error_cb
manifest_wal_range * first_wal_range
#define Min(x, y)
Definition: c.h:920
void json_parse_manifest(JsonManifestParseContext *context, char *buffer, size_t size)
#define PG_UINT32_MAX
Definition: c.h:451
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1233
void pfree(void *pointer)
Definition: mcxt.c:1056
manifest_wal_range * last_wal_range
manifest_files_hash * ht
unsigned int uint32
Definition: c.h:367
#define stat(a, b)
Definition: win32_port.h:255
static void report_manifest_error(JsonManifestParseContext *context, const char *fmt,...) pg_attribute_printf(2
#define Max(x, y)
Definition: c.h:914
json_manifest_perwalrange_callback perwalrange_cb
static void record_manifest_details_for_file(JsonManifestParseContext *context, char *pathname, size_t size, pg_checksum_type checksum_type, int checksum_length, uint8 *checksum_payload)
#define close(a)
Definition: win32.h:12
#define read(a, b, c)
Definition: win32.h:13
json_manifest_perfile_callback perfile_cb
static void record_manifest_details_for_wal_range(JsonManifestParseContext *context, TimeLineID tli, XLogRecPtr start_lsn, XLogRecPtr end_lsn)

◆ parse_required_wal()

static void parse_required_wal ( verifier_context context,
char *  pg_waldump_path,
char *  wal_directory,
manifest_wal_range first_wal_range 
)
static

Definition at line 787 of file pg_verifybackup.c.

References manifest_wal_range::end_lsn, manifest_wal_range::next, psprintf(), report_backup_error(), manifest_wal_range::start_lsn, and manifest_wal_range::tli.

Referenced by main().

789 {
790  manifest_wal_range *this_wal_range = first_wal_range;
791 
792  while (this_wal_range != NULL)
793  {
794  char *pg_waldump_cmd;
795 
796  pg_waldump_cmd = psprintf("\"%s\" --quiet --path=\"%s\" --timeline=%u --start=%X/%X --end=%X/%X\n",
797  pg_waldump_path, wal_directory, this_wal_range->tli,
798  (uint32) (this_wal_range->start_lsn >> 32),
799  (uint32) this_wal_range->start_lsn,
800  (uint32) (this_wal_range->end_lsn >> 32),
801  (uint32) this_wal_range->end_lsn);
802  if (system(pg_waldump_cmd) != 0)
803  report_backup_error(context,
804  "WAL parsing failed for timeline %u",
805  this_wal_range->tli);
806 
807  this_wal_range = this_wal_range->next;
808  }
809 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
struct manifest_wal_range * next
unsigned int uint32
Definition: c.h:367
static void report_backup_error(verifier_context *context, const char *pg_restrict fmt,...) pg_attribute_printf(2

◆ pg_attribute_noreturn()

static void pg_attribute_noreturn ( )

◆ record_manifest_details_for_file()

static void record_manifest_details_for_file ( JsonManifestParseContext context,
char *  pathname,
size_t  size,
pg_checksum_type  checksum_type,
int  checksum_length,
uint8 checksum_payload 
)
static

Definition at line 461 of file pg_verifybackup.c.

References manifest_file::bad, manifest_file::checksum_length, manifest_file::checksum_payload, manifest_file::checksum_type, parser_context::ht, manifest_file::matched, JsonManifestParseContext::private_data, report_fatal_error(), and manifest_file::size.

Referenced by parse_manifest_file().

465 {
466  parser_context *pcxt = context->private_data;
467  manifest_files_hash *ht = pcxt->ht;
468  manifest_file *m;
469  bool found;
470 
471  /* Make a new entry in the hash table for this file. */
472  m = manifest_files_insert(ht, pathname, &found);
473  if (found)
474  report_fatal_error("duplicate pathname in backup manifest: \"%s\"",
475  pathname);
476 
477  /* Initialize the entry. */
478  m->size = size;
479  m->checksum_type = checksum_type;
480  m->checksum_length = checksum_length;
481  m->checksum_payload = checksum_payload;
482  m->matched = false;
483  m->bad = false;
484 }
pg_checksum_type checksum_type
static void static void report_fatal_error(const char *pg_restrict fmt,...) pg_attribute_printf(1
manifest_files_hash * ht
uint8 * checksum_payload

◆ record_manifest_details_for_wal_range()

static void record_manifest_details_for_wal_range ( JsonManifestParseContext context,
TimeLineID  tli,
XLogRecPtr  start_lsn,
XLogRecPtr  end_lsn 
)
static

Definition at line 490 of file pg_verifybackup.c.

References manifest_wal_range::end_lsn, parser_context::first_wal_range, parser_context::last_wal_range, manifest_wal_range::next, palloc(), manifest_wal_range::prev, JsonManifestParseContext::private_data, range(), manifest_wal_range::start_lsn, and manifest_wal_range::tli.

Referenced by parse_manifest_file().

493 {
494  parser_context *pcxt = context->private_data;
496 
497  /* Allocate and initialize a struct describing this WAL range. */
498  range = palloc(sizeof(manifest_wal_range));
499  range->tli = tli;
500  range->start_lsn = start_lsn;
501  range->end_lsn = end_lsn;
502  range->prev = pcxt->last_wal_range;
503  range->next = NULL;
504 
505  /* Add it to the end of the list. */
506  if (pcxt->first_wal_range == NULL)
507  pcxt->first_wal_range = range;
508  else
509  pcxt->last_wal_range->next = range;
510  pcxt->last_wal_range = range;
511 }
manifest_wal_range * first_wal_range
struct manifest_wal_range * prev
struct manifest_wal_range * next
manifest_wal_range * last_wal_range
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:416
void * palloc(Size size)
Definition: mcxt.c:949

◆ report_backup_error()

static void report_backup_error ( verifier_context context,
const char *pg_restrict  fmt,
  ... 
)
static

Definition at line 818 of file pg_verifybackup.c.

References verifier_context::exit_on_error, gettext, PG_LOG_ERROR, pg_log_generic_v(), and verifier_context::saw_any_error.

Referenced by parse_required_wal(), report_extra_backup_files(), verify_backup_directory(), verify_backup_file(), and verify_file_checksum().

819 {
820  va_list ap;
821 
822  va_start(ap, fmt);
824  va_end(ap);
825 
826  context->saw_any_error = true;
827  if (context->exit_on_error)
828  exit(1);
829 }
#define gettext(x)
Definition: c.h:1143
void pg_log_generic_v(enum pg_log_level level, const char *pg_restrict fmt, va_list ap)
Definition: logging.c:189

◆ report_extra_backup_files()

static void report_extra_backup_files ( verifier_context context)
static

Definition at line 659 of file pg_verifybackup.c.

References verifier_context::ht, manifest_file::matched, manifest_file::pathname, report_backup_error(), and should_ignore_relpath().

Referenced by main().

660 {
661  manifest_files_iterator it;
662  manifest_file *m;
663 
664  manifest_files_start_iterate(context->ht, &it);
665  while ((m = manifest_files_iterate(context->ht, &it)) != NULL)
666  if (!m->matched && !should_ignore_relpath(context, m->pathname))
667  report_backup_error(context,
668  "\"%s\" is present in the manifest but not on disk",
669  m->pathname);
670 }
manifest_files_hash * ht
static void report_backup_error(verifier_context *context, const char *pg_restrict fmt,...) pg_attribute_printf(2
static bool should_ignore_relpath(verifier_context *context, char *relpath)

◆ report_fatal_error()

static void report_fatal_error ( const char *pg_restrict  fmt,
  ... 
)
static

Definition at line 835 of file pg_verifybackup.c.

References gettext, PG_LOG_FATAL, and pg_log_generic_v().

Referenced by parse_manifest_file(), record_manifest_details_for_file(), and verify_backup_directory().

836 {
837  va_list ap;
838 
839  va_start(ap, fmt);
841  va_end(ap);
842 
843  exit(1);
844 }
#define gettext(x)
Definition: c.h:1143
void pg_log_generic_v(enum pg_log_level level, const char *pg_restrict fmt, va_list ap)
Definition: logging.c:189

◆ report_manifest_error()

static void report_manifest_error ( JsonManifestParseContext context,
const char *  fmt,
  ... 
)
static

Definition at line 446 of file pg_verifybackup.c.

References gettext, PG_LOG_FATAL, and pg_log_generic_v().

Referenced by parse_manifest_file().

447 {
448  va_list ap;
449 
450  va_start(ap, fmt);
452  va_end(ap);
453 
454  exit(1);
455 }
#define gettext(x)
Definition: c.h:1143
void pg_log_generic_v(enum pg_log_level level, const char *pg_restrict fmt, va_list ap)
Definition: logging.c:189

◆ should_ignore_relpath()

static bool should_ignore_relpath ( verifier_context context,
char *  relpath 
)
static

Definition at line 854 of file pg_verifybackup.c.

References SimpleStringList::head, verifier_context::ignore_list, SimpleStringListCell::next, relpath, and SimpleStringListCell::val.

Referenced by report_extra_backup_files(), verify_backup_checksums(), and verify_backup_directory().

855 {
856  SimpleStringListCell *cell;
857 
858  for (cell = context->ignore_list.head; cell != NULL; cell = cell->next)
859  {
860  char *r = relpath;
861  char *v = cell->val;
862 
863  while (*v != '\0' && *r == *v)
864  ++r, ++v;
865 
866  if (*v == '\0' && (*r == '\0' || *r == '/'))
867  return true;
868  }
869 
870  return false;
871 }
struct SimpleStringListCell * next
Definition: simple_list.h:34
SimpleStringListCell * head
Definition: simple_list.h:42
char val[FLEXIBLE_ARRAY_MEMBER]
Definition: simple_list.h:37
#define relpath(rnode, forknum)
Definition: relpath.h:87
SimpleStringList ignore_list

◆ usage()

static void usage ( void  )
static

Definition at line 888 of file pg_verifybackup.c.

References _, printf, and progname.

Referenced by main().

889 {
890  printf(_("%s verifies a backup against the backup manifest.\n\n"), progname);
891  printf(_("Usage:\n %s [OPTION]... BACKUPDIR\n\n"), progname);
892  printf(_("Options:\n"));
893  printf(_(" -e, --exit-on-error exit immediately on error\n"));
894  printf(_(" -i, --ignore=RELATIVE_PATH ignore indicated path\n"));
895  printf(_(" -m, --manifest-path=PATH use specified path for manifest\n"));
896  printf(_(" -n, --no-parse-wal do not try to parse WAL files\n"));
897  printf(_(" -q, --quiet do not print any output, except for errors\n"));
898  printf(_(" -s, --skip-checksums skip checksum verification\n"));
899  printf(_(" -w, --wal-directory=PATH use specified path for WAL files\n"));
900  printf(_(" -V, --version output version information, then exit\n"));
901  printf(_(" -?, --help show this help, then exit\n"));
902  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
903  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
904 }
static const char * progname
#define printf(...)
Definition: port.h:199
#define _(x)
Definition: elog.c:88

◆ verify_backup_checksums()

static void verify_backup_checksums ( verifier_context context)
static

Definition at line 678 of file pg_verifybackup.c.

References verifier_context::backup_directory, manifest_file::bad, manifest_file::checksum_type, CHECKSUM_TYPE_NONE, verifier_context::ht, manifest_file::matched, manifest_file::pathname, pfree(), psprintf(), should_ignore_relpath(), and verify_file_checksum().

Referenced by main().

679 {
680  manifest_files_iterator it;
681  manifest_file *m;
682 
683  manifest_files_start_iterate(context->ht, &it);
684  while ((m = manifest_files_iterate(context->ht, &it)) != NULL)
685  {
686  if (m->matched && !m->bad && m->checksum_type != CHECKSUM_TYPE_NONE &&
687  !should_ignore_relpath(context, m->pathname))
688  {
689  char *fullpath;
690 
691  /* Compute the full pathname to the target file. */
692  fullpath = psprintf("%s/%s", context->backup_directory,
693  m->pathname);
694 
695  /* Do the actual checksum verification. */
696  verify_file_checksum(context, m, fullpath);
697 
698  /* Avoid leaking memory. */
699  pfree(fullpath);
700  }
701  }
702 }
pg_checksum_type checksum_type
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
manifest_files_hash * ht
void pfree(void *pointer)
Definition: mcxt.c:1056
static void verify_file_checksum(verifier_context *context, manifest_file *m, char *pathname)
static bool should_ignore_relpath(verifier_context *context, char *relpath)

◆ verify_backup_directory()

static void verify_backup_directory ( verifier_context context,
char *  relpath,
char *  fullpath 
)
static

Definition at line 523 of file pg_verifybackup.c.

References closedir(), dirent::d_name, filename, verifier_context::ignore_list, opendir(), pfree(), psprintf(), pstrdup(), readdir(), report_backup_error(), report_fatal_error(), should_ignore_relpath(), simple_string_list_append(), and verify_backup_file().

Referenced by main(), and verify_backup_file().

525 {
526  DIR *dir;
527  struct dirent *dirent;
528 
529  dir = opendir(fullpath);
530  if (dir == NULL)
531  {
532  /*
533  * If even the toplevel backup directory cannot be found, treat this
534  * as a fatal error.
535  */
536  if (relpath == NULL)
537  report_fatal_error("could not open directory \"%s\": %m", fullpath);
538 
539  /*
540  * Otherwise, treat this as a non-fatal error, but ignore any further
541  * errors related to this path and anything beneath it.
542  */
543  report_backup_error(context,
544  "could not open directory \"%s\": %m", fullpath);
546 
547  return;
548  }
549 
550  while (errno = 0, (dirent = readdir(dir)) != NULL)
551  {
552  char *filename = dirent->d_name;
553  char *newfullpath = psprintf("%s/%s", fullpath, filename);
554  char *newrelpath;
555 
556  /* Skip "." and ".." */
557  if (filename[0] == '.' && (filename[1] == '\0'
558  || strcmp(filename, "..") == 0))
559  continue;
560 
561  if (relpath == NULL)
562  newrelpath = pstrdup(filename);
563  else
564  newrelpath = psprintf("%s/%s", relpath, filename);
565 
566  if (!should_ignore_relpath(context, newrelpath))
567  verify_backup_file(context, newrelpath, newfullpath);
568 
569  pfree(newfullpath);
570  pfree(newrelpath);
571  }
572 
573  if (closedir(dir))
574  {
575  report_backup_error(context,
576  "could not close directory \"%s\": %m", fullpath);
577  return;
578  }
579 }
static void static void report_fatal_error(const char *pg_restrict fmt,...) pg_attribute_printf(1
char * pstrdup(const char *in)
Definition: mcxt.c:1186
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
int closedir(DIR *)
Definition: dirent.c:113
Definition: dirent.h:9
void pfree(void *pointer)
Definition: mcxt.c:1056
Definition: dirent.c:25
static void verify_backup_file(verifier_context *context, char *relpath, char *fullpath)
DIR * opendir(const char *)
Definition: dirent.c:33
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
struct dirent * readdir(DIR *)
Definition: dirent.c:77
static char * filename
Definition: pg_dumpall.c:90
static void report_backup_error(verifier_context *context, const char *pg_restrict fmt,...) pg_attribute_printf(2
#define relpath(rnode, forknum)
Definition: relpath.h:87
SimpleStringList ignore_list
char d_name[MAX_PATH]
Definition: dirent.h:14
static bool should_ignore_relpath(verifier_context *context, char *relpath)

◆ verify_backup_file()

static void verify_backup_file ( verifier_context context,
char *  relpath,
char *  fullpath 
)
static

Definition at line 588 of file pg_verifybackup.c.

References verifier_context::ht, verifier_context::ignore_list, report_backup_error(), S_ISDIR, S_ISREG, simple_string_list_append(), stat, and verify_backup_directory().

Referenced by verify_backup_directory().

589 {
590  struct stat sb;
591  manifest_file *m;
592 
593  if (stat(fullpath, &sb) != 0)
594  {
595  report_backup_error(context,
596  "could not stat file or directory \"%s\": %m",
597  relpath);
598 
599  /*
600  * Suppress further errors related to this path name and, if it's a
601  * directory, anything underneath it.
602  */
604 
605  return;
606  }
607 
608  /* If it's a directory, just recurse. */
609  if (S_ISDIR(sb.st_mode))
610  {
611  verify_backup_directory(context, relpath, fullpath);
612  return;
613  }
614 
615  /* If it's not a directory, it should be a plain file. */
616  if (!S_ISREG(sb.st_mode))
617  {
618  report_backup_error(context,
619  "\"%s\" is not a file or directory",
620  relpath);
621  return;
622  }
623 
624  /* Check whether there's an entry in the manifest hash. */
625  m = manifest_files_lookup(context->ht, relpath);
626  if (m == NULL)
627  {
628  report_backup_error(context,
629  "\"%s\" is present on disk but not in the manifest",
630  relpath);
631  return;
632  }
633 
634  /* Flag this entry as having been encountered in the filesystem. */
635  m->matched = true;
636 
637  /* Check that the size matches. */
638  if (m->size != sb.st_size)
639  {
640  report_backup_error(context,
641  "\"%s\" has size %zu on disk but size %zu in the manifest",
642  relpath, (size_t) sb.st_size, m->size);
643  m->bad = true;
644  }
645 
646  /*
647  * We don't verify checksums at this stage. We first finish verifying that
648  * we have the expected set of files with the expected sizes, and only
649  * afterwards verify the checksums. That's because computing checksums may
650  * take a while, and we'd like to report more obvious problems quickly.
651  */
652 }
manifest_files_hash * ht
static void verify_backup_directory(verifier_context *context, char *relpath, char *fullpath)
#define S_ISREG(m)
Definition: win32_port.h:299
#define stat(a, b)
Definition: win32_port.h:255
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
#define S_ISDIR(m)
Definition: win32_port.h:296
static void report_backup_error(verifier_context *context, const char *pg_restrict fmt,...) pg_attribute_printf(2
#define relpath(rnode, forknum)
Definition: relpath.h:87
SimpleStringList ignore_list

◆ verify_file_checksum()

static void verify_file_checksum ( verifier_context context,
manifest_file m,
char *  pathname 
)
static

Definition at line 708 of file pg_verifybackup.c.

References manifest_file::checksum_length, manifest_file::checksum_payload, manifest_file::checksum_type, close, fd(), manifest_file::pathname, PG_BINARY, pg_checksum_final(), pg_checksum_init(), PG_CHECKSUM_MAX_LENGTH, pg_checksum_update(), read, READ_CHUNK_SIZE, relpath, report_backup_error(), and manifest_file::size.

Referenced by verify_backup_checksums().

710 {
711  pg_checksum_context checksum_ctx;
712  char *relpath = m->pathname;
713  int fd;
714  int rc;
715  size_t bytes_read = 0;
716  uint8 buffer[READ_CHUNK_SIZE];
717  uint8 checksumbuf[PG_CHECKSUM_MAX_LENGTH];
718  int checksumlen;
719 
720  /* Open the target file. */
721  if ((fd = open(fullpath, O_RDONLY | PG_BINARY, 0)) < 0)
722  {
723  report_backup_error(context, "could not open file \"%s\": %m",
724  relpath);
725  return;
726  }
727 
728  /* Initialize checksum context. */
729  pg_checksum_init(&checksum_ctx, m->checksum_type);
730 
731  /* Read the file chunk by chunk, updating the checksum as we go. */
732  while ((rc = read(fd, buffer, READ_CHUNK_SIZE)) > 0)
733  {
734  bytes_read += rc;
735  pg_checksum_update(&checksum_ctx, buffer, rc);
736  }
737  if (rc < 0)
738  report_backup_error(context, "could not read file \"%s\": %m",
739  relpath);
740 
741  /* Close the file. */
742  if (close(fd) != 0)
743  {
744  report_backup_error(context, "could not close file \"%s\": %m",
745  relpath);
746  return;
747  }
748 
749  /* If we didn't manage to read the whole file, bail out now. */
750  if (rc < 0)
751  return;
752 
753  /*
754  * Double-check that we read the expected number of bytes from the file.
755  * Normally, a file size mismatch would be caught in verify_backup_file
756  * and this check would never be reached, but this provides additional
757  * safety and clarity in the event of concurrent modifications or
758  * filesystem misbehavior.
759  */
760  if (bytes_read != m->size)
761  {
762  report_backup_error(context,
763  "file \"%s\" should contain %zu bytes, but read %zu bytes",
764  relpath, m->size, bytes_read);
765  return;
766  }
767 
768  /* Get the final checksum. */
769  checksumlen = pg_checksum_final(&checksum_ctx, checksumbuf);
770 
771  /* And check it against the manifest. */
772  if (checksumlen != m->checksum_length)
773  report_backup_error(context,
774  "file \"%s\" has checksum of length %d, but expected %d",
775  relpath, m->checksum_length, checksumlen);
776  else if (memcmp(checksumbuf, m->checksum_payload, checksumlen) != 0)
777  report_backup_error(context,
778  "checksum mismatch for file \"%s\"",
779  relpath);
780 }
pg_checksum_type checksum_type
unsigned char uint8
Definition: c.h:365
void pg_checksum_update(pg_checksum_context *context, const uint8 *input, size_t len)
#define PG_CHECKSUM_MAX_LENGTH
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1233
void pg_checksum_init(pg_checksum_context *context, pg_checksum_type type)
#define READ_CHUNK_SIZE
int pg_checksum_final(pg_checksum_context *context, uint8 *output)
uint8 * checksum_payload
static void report_backup_error(verifier_context *context, const char *pg_restrict fmt,...) pg_attribute_printf(2
#define relpath(rnode, forknum)
Definition: relpath.h:87
#define close(a)
Definition: win32.h:12
#define read(a, b, c)
Definition: win32.h:13

Variable Documentation

◆ progname

const char* progname
static

Definition at line 152 of file pg_verifybackup.c.

Referenced by usage().