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.

◆ READ_CHUNK_SIZE

#define READ_CHUNK_SIZE   4096

Definition at line 43 of file pg_verifybackup.c.

◆ 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,
  b 
)    (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 889 of file pg_verifybackup.c.

890 {
891  unsigned char *ss = (unsigned char *) s;
892 
893  return hash_bytes(ss, strlen(s));
894 }
uint32 hash_bytes(const unsigned char *k, int keylen)
Definition: hashfn.c:146

References hash_bytes().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 158 of file pg_verifybackup.c.

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 
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  /* getopt_long already emitted a complaint */
256  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
257  exit(1);
258  }
259  }
260 
261  /* Get backup directory name */
262  if (optind >= argc)
263  {
264  pg_log_error("no backup directory specified");
265  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
266  exit(1);
267  }
268  context.backup_directory = pstrdup(argv[optind++]);
270 
271  /* Complain if any arguments remain */
272  if (optind < argc)
273  {
274  pg_log_error("too many command-line arguments (first is \"%s\")",
275  argv[optind]);
276  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
277  exit(1);
278  }
279 
280  /* Unless --no-parse-wal was specified, we will need pg_waldump. */
281  if (!no_parse_wal)
282  {
283  int ret;
284 
285  pg_waldump_path = pg_malloc(MAXPGPATH);
286  ret = find_other_exec(argv[0], "pg_waldump",
287  "pg_waldump (PostgreSQL) " PG_VERSION "\n",
288  pg_waldump_path);
289  if (ret < 0)
290  {
291  char full_path[MAXPGPATH];
292 
293  if (find_my_exec(argv[0], full_path) < 0)
294  strlcpy(full_path, progname, sizeof(full_path));
295 
296  if (ret == -1)
297  pg_fatal("program \"%s\" is needed by %s but was not found in the same directory as \"%s\"",
298  "pg_waldump", "pg_verifybackup", full_path);
299  else
300  pg_fatal("program \"%s\" was found by \"%s\" but was not the same version as %s",
301  "pg_waldump", full_path, "pg_verifybackup");
302  }
303  }
304 
305  /* By default, look for the manifest in the backup directory. */
306  if (manifest_path == NULL)
307  manifest_path = psprintf("%s/backup_manifest",
308  context.backup_directory);
309 
310  /* By default, look for the WAL in the backup directory, too. */
311  if (wal_directory == NULL)
312  wal_directory = psprintf("%s/pg_wal", context.backup_directory);
313 
314  /*
315  * Try to read the manifest. We treat any errors encountered while parsing
316  * the manifest as fatal; there doesn't seem to be much point in trying to
317  * verify the backup directory against a corrupted manifest.
318  */
319  parse_manifest_file(manifest_path, &context.ht, &first_wal_range);
320 
321  /*
322  * Now scan the files in the backup directory. At this stage, we verify
323  * that every file on disk is present in the manifest and that the sizes
324  * match. We also set the "matched" flag on every manifest entry that
325  * corresponds to a file on disk.
326  */
327  verify_backup_directory(&context, NULL, context.backup_directory);
328 
329  /*
330  * The "matched" flag should now be set on every entry in the hash table.
331  * Any entries for which the bit is not set are files mentioned in the
332  * manifest that don't exist on disk.
333  */
334  report_extra_backup_files(&context);
335 
336  /*
337  * Now do the expensive work of verifying file checksums, unless we were
338  * told to skip it.
339  */
340  if (!skip_checksums)
341  verify_backup_checksums(&context);
342 
343  /*
344  * Try to parse the required ranges of WAL records, unless we were told
345  * not to do so.
346  */
347  if (!no_parse_wal)
348  parse_required_wal(&context, pg_waldump_path,
349  wal_directory, first_wal_range);
350 
351  /*
352  * If everything looks OK, tell the user this, unless we were asked to
353  * work quietly.
354  */
355  if (!context.saw_any_error && !quiet)
356  printf(_("backup successfully verified\n"));
357 
358  return context.saw_any_error ? 1 : 0;
359 }
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1223
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:140
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:446
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
Definition: exec.c:335
#define _(x)
Definition: elog.c:89
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
#define no_argument
Definition: getopt_long.h:24
#define required_argument
Definition: getopt_long.h:25
exit(1)
void pg_logging_init(const char *argv0)
Definition: logging.c:83
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_error_hint(...)
Definition: logging.h:112
char * pstrdup(const char *in)
Definition: mcxt.c:1305
void * arg
#define pg_fatal(...)
#define MAXPGPATH
PGDLLIMPORT int optind
Definition: getopt.c:50
PGDLLIMPORT char * optarg
Definition: getopt.c:52
static void verify_backup_checksums(verifier_context *context)
static void parse_manifest_file(char *manifest_path, manifest_files_hash **ht_p, manifest_wal_range **first_wal_range_p)
static void parse_required_wal(verifier_context *context, char *pg_waldump_path, char *wal_directory, manifest_wal_range *first_wal_range)
static void report_extra_backup_files(verifier_context *context)
static const char * progname
static void verify_backup_directory(verifier_context *context, char *relpath, char *fullpath)
static void usage(void)
void canonicalize_path(char *path)
Definition: path.c:264
const char * get_progname(const char *argv0)
Definition: path.c:574
#define printf(...)
Definition: port.h:231
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
char * c
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
SimpleStringList ignore_list
manifest_files_hash * ht

References _, arg, canonicalize_path(), exit(), find_my_exec(), find_other_exec(), get_progname(), getopt_long(), MAXPGPATH, no_argument, optarg, optind, parse_manifest_file(), parse_required_wal(), pg_fatal, pg_log_error, pg_log_error_hint, pg_logging_init(), pg_malloc(), PG_TEXTDOMAIN, printf, progname, psprintf(), pstrdup(), report_extra_backup_files(), required_argument, set_pglocale_pgservice(), simple_string_list_append(), strlcpy(), usage(), verify_backup_checksums(), and verify_backup_directory().

◆ 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 367 of file pg_verifybackup.c.

369 {
370  int fd;
371  struct stat statbuf;
372  off_t estimate;
373  uint32 initial_size;
374  manifest_files_hash *ht;
375  char *buffer;
376  int rc;
377  parser_context private_context;
378  JsonManifestParseContext context;
379 
380  /* Open the manifest file. */
381  if ((fd = open(manifest_path, O_RDONLY | PG_BINARY, 0)) < 0)
382  report_fatal_error("could not open file \"%s\": %m", manifest_path);
383 
384  /* Figure out how big the manifest is. */
385  if (fstat(fd, &statbuf) != 0)
386  report_fatal_error("could not stat file \"%s\": %m", manifest_path);
387 
388  /* Guess how large to make the hash table based on the manifest size. */
389  estimate = statbuf.st_size / ESTIMATED_BYTES_PER_MANIFEST_LINE;
390  initial_size = Min(PG_UINT32_MAX, Max(estimate, 256));
391 
392  /* Create the hash table. */
393  ht = manifest_files_create(initial_size, NULL);
394 
395  /*
396  * Slurp in the whole file.
397  *
398  * This is not ideal, but there's currently no easy way to get
399  * pg_parse_json() to perform incremental parsing.
400  */
401  buffer = pg_malloc(statbuf.st_size);
402  rc = read(fd, buffer, statbuf.st_size);
403  if (rc != statbuf.st_size)
404  {
405  if (rc < 0)
406  report_fatal_error("could not read file \"%s\": %m",
407  manifest_path);
408  else
409  report_fatal_error("could not read file \"%s\": read %d of %lld",
410  manifest_path, rc, (long long int) statbuf.st_size);
411  }
412 
413  /* Close the manifest file. */
414  close(fd);
415 
416  /* Parse the manifest. */
417  private_context.ht = ht;
418  private_context.first_wal_range = NULL;
419  private_context.last_wal_range = NULL;
420  context.private_data = &private_context;
424  json_parse_manifest(&context, buffer, statbuf.st_size);
425 
426  /* Done with the buffer. */
427  pfree(buffer);
428 
429  /* Return the file hash table and WAL range list we constructed. */
430  *ht_p = ht;
431  *first_wal_range_p = private_context.first_wal_range;
432 }
unsigned int uint32
Definition: c.h:452
#define Min(x, y)
Definition: c.h:997
#define PG_UINT32_MAX
Definition: c.h:536
#define Max(x, y)
Definition: c.h:991
#define PG_BINARY
Definition: c.h:1279
#define close(a)
Definition: win32.h:12
#define read(a, b, c)
Definition: win32.h:13
void pfree(void *pointer)
Definition: mcxt.c:1175
void json_parse_manifest(JsonManifestParseContext *context, char *buffer, size_t size)
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 ESTIMATED_BYTES_PER_MANIFEST_LINE
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 static void report_fatal_error(const char *pg_restrict fmt,...) pg_attribute_printf(1
static int fd(const char *x, int i)
Definition: preproc-init.c:105
json_manifest_perwalrange_callback perwalrange_cb
json_manifest_perfile_callback perfile_cb
json_manifest_error_callback error_cb
manifest_files_hash * ht
manifest_wal_range * last_wal_range
manifest_wal_range * first_wal_range
#define fstat
Definition: win32_port.h:282

References close, JsonManifestParseContext::error_cb, ESTIMATED_BYTES_PER_MANIFEST_LINE, fd(), parser_context::first_wal_range, fstat, 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::st_size.

Referenced by main().

◆ 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 801 of file pg_verifybackup.c.

803 {
804  manifest_wal_range *this_wal_range = first_wal_range;
805 
806  while (this_wal_range != NULL)
807  {
808  char *pg_waldump_cmd;
809 
810  pg_waldump_cmd = psprintf("\"%s\" --quiet --path=\"%s\" --timeline=%u --start=%X/%X --end=%X/%X\n",
811  pg_waldump_path, wal_directory, this_wal_range->tli,
812  LSN_FORMAT_ARGS(this_wal_range->start_lsn),
813  LSN_FORMAT_ARGS(this_wal_range->end_lsn));
814  if (system(pg_waldump_cmd) != 0)
815  report_backup_error(context,
816  "WAL parsing failed for timeline %u",
817  this_wal_range->tli);
818 
819  this_wal_range = this_wal_range->next;
820  }
821 }
static void report_backup_error(verifier_context *context, const char *pg_restrict fmt,...) pg_attribute_printf(2
struct manifest_wal_range * next
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43

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

Referenced by main().

◆ 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 456 of file pg_verifybackup.c.

460 {
461  parser_context *pcxt = context->private_data;
462  manifest_files_hash *ht = pcxt->ht;
463  manifest_file *m;
464  bool found;
465 
466  /* Make a new entry in the hash table for this file. */
467  m = manifest_files_insert(ht, pathname, &found);
468  if (found)
469  report_fatal_error("duplicate path name in backup manifest: \"%s\"",
470  pathname);
471 
472  /* Initialize the entry. */
473  m->size = size;
474  m->checksum_type = checksum_type;
475  m->checksum_length = checksum_length;
476  m->checksum_payload = checksum_payload;
477  m->matched = false;
478  m->bad = false;
479 }
uint8 * checksum_payload
pg_checksum_type checksum_type

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().

◆ 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 485 of file pg_verifybackup.c.

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

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

Referenced by parse_manifest_file().

◆ report_backup_error()

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

Definition at line 830 of file pg_verifybackup.c.

831 {
832  va_list ap;
833 
834  va_start(ap, fmt);
836  va_end(ap);
837 
838  context->saw_any_error = true;
839  if (context->exit_on_error)
840  exit(1);
841 }
#define gettext(x)
Definition: c.h:1188
va_end(args)
static void const char * fmt
va_start(args, fmt)
void pg_log_generic_v(enum pg_log_level level, enum pg_log_part part, const char *pg_restrict fmt, va_list ap)
Definition: logging.c:216
@ PG_LOG_PRIMARY
Definition: logging.h:67
@ PG_LOG_ERROR
Definition: logging.h:43

References exit(), verifier_context::exit_on_error, fmt, gettext, PG_LOG_ERROR, pg_log_generic_v(), PG_LOG_PRIMARY, verifier_context::saw_any_error, va_end(), and va_start().

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

◆ report_extra_backup_files()

static void report_extra_backup_files ( verifier_context context)
static

Definition at line 654 of file pg_verifybackup.c.

655 {
656  manifest_files_iterator it;
657  manifest_file *m;
658 
659  manifest_files_start_iterate(context->ht, &it);
660  while ((m = manifest_files_iterate(context->ht, &it)) != NULL)
661  if (!m->matched && !should_ignore_relpath(context, m->pathname))
662  report_backup_error(context,
663  "\"%s\" is present in the manifest but not on disk",
664  m->pathname);
665 }
static bool should_ignore_relpath(verifier_context *context, char *relpath)

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

Referenced by main().

◆ report_fatal_error()

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

Definition at line 847 of file pg_verifybackup.c.

848 {
849  va_list ap;
850 
851  va_start(ap, fmt);
853  va_end(ap);
854 
855  exit(1);
856 }

References exit(), fmt, gettext, PG_LOG_ERROR, pg_log_generic_v(), PG_LOG_PRIMARY, va_end(), and va_start().

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

◆ report_manifest_error()

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

Definition at line 441 of file pg_verifybackup.c.

442 {
443  va_list ap;
444 
445  va_start(ap, fmt);
447  va_end(ap);
448 
449  exit(1);
450 }

References exit(), fmt, gettext, PG_LOG_ERROR, pg_log_generic_v(), PG_LOG_PRIMARY, va_end(), and va_start().

Referenced by parse_manifest_file().

◆ should_ignore_relpath()

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

Definition at line 866 of file pg_verifybackup.c.

867 {
868  SimpleStringListCell *cell;
869 
870  for (cell = context->ignore_list.head; cell != NULL; cell = cell->next)
871  {
872  char *r = relpath;
873  char *v = cell->val;
874 
875  while (*v != '\0' && *r == *v)
876  ++r, ++v;
877 
878  if (*v == '\0' && (*r == '\0' || *r == '/'))
879  return true;
880  }
881 
882  return false;
883 }
#define relpath(rnode, forknum)
Definition: relpath.h:87
char val[FLEXIBLE_ARRAY_MEMBER]
Definition: simple_list.h:37
struct SimpleStringListCell * next
Definition: simple_list.h:34
SimpleStringListCell * head
Definition: simple_list.h:42

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().

◆ usage()

static void usage ( void  )
static

Definition at line 900 of file pg_verifybackup.c.

901 {
902  printf(_("%s verifies a backup against the backup manifest.\n\n"), progname);
903  printf(_("Usage:\n %s [OPTION]... BACKUPDIR\n\n"), progname);
904  printf(_("Options:\n"));
905  printf(_(" -e, --exit-on-error exit immediately on error\n"));
906  printf(_(" -i, --ignore=RELATIVE_PATH ignore indicated path\n"));
907  printf(_(" -m, --manifest-path=PATH use specified path for manifest\n"));
908  printf(_(" -n, --no-parse-wal do not try to parse WAL files\n"));
909  printf(_(" -q, --quiet do not print any output, except for errors\n"));
910  printf(_(" -s, --skip-checksums skip checksum verification\n"));
911  printf(_(" -w, --wal-directory=PATH use specified path for WAL files\n"));
912  printf(_(" -V, --version output version information, then exit\n"));
913  printf(_(" -?, --help show this help, then exit\n"));
914  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
915  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
916 }

References _, printf, and progname.

Referenced by main().

◆ verify_backup_checksums()

static void verify_backup_checksums ( verifier_context context)
static

Definition at line 673 of file pg_verifybackup.c.

674 {
675  manifest_files_iterator it;
676  manifest_file *m;
677 
678  manifest_files_start_iterate(context->ht, &it);
679  while ((m = manifest_files_iterate(context->ht, &it)) != NULL)
680  {
681  if (m->matched && !m->bad && m->checksum_type != CHECKSUM_TYPE_NONE &&
682  !should_ignore_relpath(context, m->pathname))
683  {
684  char *fullpath;
685 
686  /* Compute the full pathname to the target file. */
687  fullpath = psprintf("%s/%s", context->backup_directory,
688  m->pathname);
689 
690  /* Do the actual checksum verification. */
691  verify_file_checksum(context, m, fullpath);
692 
693  /* Avoid leaking memory. */
694  pfree(fullpath);
695  }
696  }
697 }
@ CHECKSUM_TYPE_NONE
static void verify_file_checksum(verifier_context *context, manifest_file *m, char *pathname)

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().

◆ verify_backup_directory()

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

Definition at line 518 of file pg_verifybackup.c.

520 {
521  DIR *dir;
522  struct dirent *dirent;
523 
524  dir = opendir(fullpath);
525  if (dir == NULL)
526  {
527  /*
528  * If even the toplevel backup directory cannot be found, treat this
529  * as a fatal error.
530  */
531  if (relpath == NULL)
532  report_fatal_error("could not open directory \"%s\": %m", fullpath);
533 
534  /*
535  * Otherwise, treat this as a non-fatal error, but ignore any further
536  * errors related to this path and anything beneath it.
537  */
538  report_backup_error(context,
539  "could not open directory \"%s\": %m", fullpath);
541 
542  return;
543  }
544 
545  while (errno = 0, (dirent = readdir(dir)) != NULL)
546  {
547  char *filename = dirent->d_name;
548  char *newfullpath = psprintf("%s/%s", fullpath, filename);
549  char *newrelpath;
550 
551  /* Skip "." and ".." */
552  if (filename[0] == '.' && (filename[1] == '\0'
553  || strcmp(filename, "..") == 0))
554  continue;
555 
556  if (relpath == NULL)
557  newrelpath = pstrdup(filename);
558  else
559  newrelpath = psprintf("%s/%s", relpath, filename);
560 
561  if (!should_ignore_relpath(context, newrelpath))
562  verify_backup_file(context, newrelpath, newfullpath);
563 
564  pfree(newfullpath);
565  pfree(newrelpath);
566  }
567 
568  if (closedir(dir))
569  {
570  report_backup_error(context,
571  "could not close directory \"%s\": %m", fullpath);
572  return;
573  }
574 }
int closedir(DIR *)
Definition: dirent.c:123
struct dirent * readdir(DIR *)
Definition: dirent.c:78
DIR * opendir(const char *)
Definition: dirent.c:33
static char * filename
Definition: pg_dumpall.c:94
static void verify_backup_file(verifier_context *context, char *relpath, char *fullpath)
Definition: dirent.c:26
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15

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

Referenced by main(), and verify_backup_file().

◆ verify_backup_file()

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

Definition at line 583 of file pg_verifybackup.c.

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

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

Referenced by verify_backup_directory().

◆ verify_file_checksum()

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

Definition at line 703 of file pg_verifybackup.c.

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

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().

Variable Documentation

◆ progname

const char* progname
static

Definition at line 152 of file pg_verifybackup.c.

Referenced by main(), and usage().