PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pg_verifybackup.c File Reference
#include "postgres_fe.h"
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/stat.h>
#include <time.h>
#include "access/xlog_internal.h"
#include "common/logging.h"
#include "common/parse_manifest.h"
#include "fe_utils/simple_list.h"
#include "getopt_long.h"
#include "pg_verifybackup.h"
#include "pgtime.h"
Include dependency graph for pg_verifybackup.c:

Go to the source code of this file.

Data Structures

struct  tar_file
 

Macros

#define ESTIMATED_BYTES_PER_MANIFEST_LINE   100
 
#define READ_CHUNK_SIZE   (128 * 1024)
 

Typedefs

typedef struct tar_file tar_file
 

Functions

static manifest_dataparse_manifest_file (char *manifest_path)
 
static void verifybackup_version_cb (JsonManifestParseContext *context, int manifest_version)
 
static void verifybackup_system_identifier (JsonManifestParseContext *context, uint64 manifest_system_identifier)
 
static void verifybackup_per_file_cb (JsonManifestParseContext *context, const char *pathname, uint64 size, pg_checksum_type checksum_type, int checksum_length, uint8 *checksum_payload)
 
static void verifybackup_per_wal_range_cb (JsonManifestParseContext *context, TimeLineID tli, XLogRecPtr start_lsn, XLogRecPtr end_lsn)
 
static pg_noreturn void report_manifest_error (JsonManifestParseContext *context, const char *fmt,...) pg_attribute_printf(2
 
static pg_noreturn void static void verify_tar_backup (verifier_context *context, DIR *dir)
 
static void verify_plain_backup_directory (verifier_context *context, char *relpath, char *fullpath, DIR *dir)
 
static void verify_plain_backup_file (verifier_context *context, char *relpath, char *fullpath)
 
static void verify_control_file (const char *controlpath, uint64 manifest_system_identifier)
 
static void precheck_tar_backup_file (verifier_context *context, char *relpath, char *fullpath, SimplePtrList *tarfiles)
 
static void verify_tar_file (verifier_context *context, char *relpath, char *fullpath, astreamer *streamer)
 
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 *fullpath, uint8 *buffer)
 
static void parse_required_wal (verifier_context *context, char *pg_waldump_path, char *wal_directory)
 
static astreamercreate_archive_verifier (verifier_context *context, char *archive_name, Oid tblspc_oid, pg_compress_algorithm compress_algo)
 
static void progress_report (bool finished)
 
static void usage (void)
 
int main (int argc, char **argv)
 
void report_backup_error (verifier_context *context, const char *pg_restrict fmt,...)
 
void report_fatal_error (const char *pg_restrict fmt,...)
 
bool should_ignore_relpath (verifier_context *context, const char *relpath)
 

Variables

static const char * progname
 
static bool show_progress = false
 
static uint64 total_size = 0
 
static uint64 done_size = 0
 

Macro Definition Documentation

◆ ESTIMATED_BYTES_PER_MANIFEST_LINE

#define ESTIMATED_BYTES_PER_MANIFEST_LINE   100

Definition at line 42 of file pg_verifybackup.c.

◆ READ_CHUNK_SIZE

#define READ_CHUNK_SIZE   (128 * 1024)

Definition at line 47 of file pg_verifybackup.c.

Typedef Documentation

◆ tar_file

typedef struct tar_file tar_file

Function Documentation

◆ create_archive_verifier()

static astreamer * create_archive_verifier ( verifier_context context,
char *  archive_name,
Oid  tblspc_oid,
pg_compress_algorithm  compress_algo 
)
static

Definition at line 1291 of file pg_verifybackup.c.

1293{
1294 astreamer *streamer = NULL;
1295
1296 /* Should be here only for tar backup */
1297 Assert(context->format == 't');
1298
1299 /* Last step is the actual verification. */
1300 streamer = astreamer_verify_content_new(streamer, context, archive_name,
1301 tblspc_oid);
1302
1303 /* Before that we must parse the tar file. */
1304 streamer = astreamer_tar_parser_new(streamer);
1305
1306 /* Before that we must decompress, if archive is compressed. */
1307 if (compress_algo == PG_COMPRESSION_GZIP)
1308 streamer = astreamer_gzip_decompressor_new(streamer);
1309 else if (compress_algo == PG_COMPRESSION_LZ4)
1310 streamer = astreamer_lz4_decompressor_new(streamer);
1311 else if (compress_algo == PG_COMPRESSION_ZSTD)
1312 streamer = astreamer_zstd_decompressor_new(streamer);
1313
1314 return streamer;
1315}
astreamer * astreamer_gzip_decompressor_new(astreamer *next)
astreamer * astreamer_lz4_decompressor_new(astreamer *next)
astreamer * astreamer_tar_parser_new(astreamer *next)
Definition: astreamer_tar.c:93
astreamer * astreamer_verify_content_new(astreamer *next, verifier_context *context, char *archive_name, Oid tblspc_oid)
astreamer * astreamer_zstd_decompressor_new(astreamer *next)
@ PG_COMPRESSION_GZIP
Definition: compression.h:24
@ PG_COMPRESSION_LZ4
Definition: compression.h:25
@ PG_COMPRESSION_ZSTD
Definition: compression.h:26
Assert(PointerIsAligned(start, uint64))

References Assert(), astreamer_gzip_decompressor_new(), astreamer_lz4_decompressor_new(), astreamer_tar_parser_new(), astreamer_verify_content_new(), astreamer_zstd_decompressor_new(), verifier_context::format, PG_COMPRESSION_GZIP, PG_COMPRESSION_LZ4, and PG_COMPRESSION_ZSTD.

Referenced by verify_tar_backup().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 118 of file pg_verifybackup.c.

119{
120 static struct option long_options[] = {
121 {"exit-on-error", no_argument, NULL, 'e'},
122 {"ignore", required_argument, NULL, 'i'},
123 {"manifest-path", required_argument, NULL, 'm'},
124 {"format", required_argument, NULL, 'F'},
125 {"no-parse-wal", no_argument, NULL, 'n'},
126 {"progress", no_argument, NULL, 'P'},
127 {"quiet", no_argument, NULL, 'q'},
128 {"skip-checksums", no_argument, NULL, 's'},
129 {"wal-directory", required_argument, NULL, 'w'},
130 {NULL, 0, NULL, 0}
131 };
132
133 int c;
134 verifier_context context;
135 char *manifest_path = NULL;
136 bool no_parse_wal = false;
137 bool quiet = false;
138 char *wal_directory = NULL;
139 char *pg_waldump_path = NULL;
140 DIR *dir;
141
142 pg_logging_init(argv[0]);
143 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_verifybackup"));
144 progname = get_progname(argv[0]);
145
146 memset(&context, 0, sizeof(context));
147
148 if (argc > 1)
149 {
150 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
151 {
152 usage();
153 exit(0);
154 }
155 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
156 {
157 puts("pg_verifybackup (PostgreSQL) " PG_VERSION);
158 exit(0);
159 }
160 }
161
162 /*
163 * Skip certain files in the toplevel directory.
164 *
165 * Ignore the backup_manifest file, because it's not included in the
166 * backup manifest.
167 *
168 * Ignore the pg_wal directory, because those files are not included in
169 * the backup manifest either, since they are fetched separately from the
170 * backup itself, and verified via a separate mechanism.
171 *
172 * Ignore postgresql.auto.conf, recovery.signal, and standby.signal,
173 * because we expect that those files may sometimes be created or changed
174 * as part of the backup process. For example, pg_basebackup -R will
175 * modify postgresql.auto.conf and create standby.signal.
176 */
177 simple_string_list_append(&context.ignore_list, "backup_manifest");
178 simple_string_list_append(&context.ignore_list, "pg_wal");
179 simple_string_list_append(&context.ignore_list, "postgresql.auto.conf");
180 simple_string_list_append(&context.ignore_list, "recovery.signal");
181 simple_string_list_append(&context.ignore_list, "standby.signal");
182
183 while ((c = getopt_long(argc, argv, "eF:i:m:nPqsw:", long_options, NULL)) != -1)
184 {
185 switch (c)
186 {
187 case 'e':
188 context.exit_on_error = true;
189 break;
190 case 'i':
191 {
192 char *arg = pstrdup(optarg);
193
196 break;
197 }
198 case 'm':
199 manifest_path = pstrdup(optarg);
200 canonicalize_path(manifest_path);
201 break;
202 case 'F':
203 if (strcmp(optarg, "p") == 0 || strcmp(optarg, "plain") == 0)
204 context.format = 'p';
205 else if (strcmp(optarg, "t") == 0 || strcmp(optarg, "tar") == 0)
206 context.format = 't';
207 else
208 pg_fatal("invalid backup format \"%s\", must be \"plain\" or \"tar\"",
209 optarg);
210 break;
211 case 'n':
212 no_parse_wal = true;
213 break;
214 case 'P':
215 show_progress = true;
216 break;
217 case 'q':
218 quiet = true;
219 break;
220 case 's':
221 context.skip_checksums = true;
222 break;
223 case 'w':
224 wal_directory = pstrdup(optarg);
225 canonicalize_path(wal_directory);
226 break;
227 default:
228 /* getopt_long already emitted a complaint */
229 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
230 exit(1);
231 }
232 }
233
234 /* Get backup directory name */
235 if (optind >= argc)
236 {
237 pg_log_error("no backup directory specified");
238 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
239 exit(1);
240 }
241 context.backup_directory = pstrdup(argv[optind++]);
243
244 /* Complain if any arguments remain */
245 if (optind < argc)
246 {
247 pg_log_error("too many command-line arguments (first is \"%s\")",
248 argv[optind]);
249 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
250 exit(1);
251 }
252
253 /* Complain if the specified arguments conflict */
254 if (show_progress && quiet)
255 pg_fatal("cannot specify both %s and %s",
256 "-P/--progress", "-q/--quiet");
257
258 /* Unless --no-parse-wal was specified, we will need pg_waldump. */
259 if (!no_parse_wal)
260 {
261 int ret;
262
263 pg_waldump_path = pg_malloc(MAXPGPATH);
264 ret = find_other_exec(argv[0], "pg_waldump",
265 "pg_waldump (PostgreSQL) " PG_VERSION "\n",
266 pg_waldump_path);
267 if (ret < 0)
268 {
269 char full_path[MAXPGPATH];
270
271 if (find_my_exec(argv[0], full_path) < 0)
272 strlcpy(full_path, progname, sizeof(full_path));
273
274 if (ret == -1)
275 pg_fatal("program \"%s\" is needed by %s but was not found in the same directory as \"%s\"",
276 "pg_waldump", "pg_verifybackup", full_path);
277 else
278 pg_fatal("program \"%s\" was found by \"%s\" but was not the same version as %s",
279 "pg_waldump", full_path, "pg_verifybackup");
280 }
281 }
282
283 /* By default, look for the manifest in the backup directory. */
284 if (manifest_path == NULL)
285 manifest_path = psprintf("%s/backup_manifest",
286 context.backup_directory);
287
288 /* By default, look for the WAL in the backup directory, too. */
289 if (wal_directory == NULL)
290 wal_directory = psprintf("%s/pg_wal", context.backup_directory);
291
292 /*
293 * Try to read the manifest. We treat any errors encountered while parsing
294 * the manifest as fatal; there doesn't seem to be much point in trying to
295 * verify the backup directory against a corrupted manifest.
296 */
297 context.manifest = parse_manifest_file(manifest_path);
298
299 /*
300 * If the backup directory cannot be found, treat this as a fatal error.
301 */
302 dir = opendir(context.backup_directory);
303 if (dir == NULL)
304 report_fatal_error("could not open directory \"%s\": %m",
305 context.backup_directory);
306
307 /*
308 * At this point, we know that the backup directory exists, so it's now
309 * reasonable to check for files immediately inside it. Thus, before going
310 * further, if the user did not specify the backup format, check for
311 * PG_VERSION to distinguish between tar and plain format.
312 */
313 if (context.format == '\0')
314 {
315 struct stat sb;
316 char *path;
317
318 path = psprintf("%s/%s", context.backup_directory, "PG_VERSION");
319 if (stat(path, &sb) == 0)
320 context.format = 'p';
321 else if (errno != ENOENT)
322 {
323 pg_log_error("could not stat file \"%s\": %m", path);
324 exit(1);
325 }
326 else
327 {
328 /* No PG_VERSION, so assume tar format. */
329 context.format = 't';
330 }
331 pfree(path);
332 }
333
334 /*
335 * XXX: In the future, we should consider enhancing pg_waldump to read WAL
336 * files from an archive.
337 */
338 if (!no_parse_wal && context.format == 't')
339 {
340 pg_log_error("pg_waldump cannot read tar files");
341 pg_log_error_hint("You must use -n or --no-parse-wal when verifying a tar-format backup.");
342 exit(1);
343 }
344
345 /*
346 * Perform the appropriate type of verification appropriate based on the
347 * backup format. This will close 'dir'.
348 */
349 if (context.format == 'p')
350 verify_plain_backup_directory(&context, NULL, context.backup_directory,
351 dir);
352 else
353 verify_tar_backup(&context, dir);
354
355 /*
356 * The "matched" flag should now be set on every entry in the hash table.
357 * Any entries for which the bit is not set are files mentioned in the
358 * manifest that don't exist on disk (or in the relevant tar files).
359 */
361
362 /*
363 * If this is a tar-format backup, checksums were already verified above;
364 * but if it's a plain-format backup, we postpone it until this point,
365 * since the earlier checks can be performed just by knowing which files
366 * are present, without needing to read all of them.
367 */
368 if (context.format == 'p' && !context.skip_checksums)
369 verify_backup_checksums(&context);
370
371 /*
372 * Try to parse the required ranges of WAL records, unless we were told
373 * not to do so.
374 */
375 if (!no_parse_wal)
376 parse_required_wal(&context, pg_waldump_path, wal_directory);
377
378 /*
379 * If everything looks OK, tell the user this, unless we were asked to
380 * work quietly.
381 */
382 if (!context.saw_any_error && !quiet)
383 printf(_("backup successfully verified\n"));
384
385 return context.saw_any_error ? 1 : 0;
386}
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1185
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:160
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:429
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
Definition: exec.c:310
DIR * opendir(const char *)
Definition: dirent.c:33
#define _(x)
Definition: elog.c:91
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:60
#define no_argument
Definition: getopt_long.h:25
#define required_argument
Definition: getopt_long.h:26
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:2325
void pfree(void *pointer)
Definition: mcxt.c:2150
void * arg
#define pg_fatal(...)
#define MAXPGPATH
PGDLLIMPORT int optind
Definition: getopt.c:51
PGDLLIMPORT char * optarg
Definition: getopt.c:53
static void verify_backup_checksums(verifier_context *context)
static manifest_data * parse_manifest_file(char *manifest_path)
void report_fatal_error(const char *pg_restrict fmt,...)
static void verify_plain_backup_directory(verifier_context *context, char *relpath, char *fullpath, DIR *dir)
static void report_extra_backup_files(verifier_context *context)
static bool show_progress
static void parse_required_wal(verifier_context *context, char *pg_waldump_path, char *wal_directory)
static const char * progname
static pg_noreturn void static void verify_tar_backup(verifier_context *context, DIR *dir)
static void usage(void)
void canonicalize_path(char *path)
Definition: path.c:337
const char * get_progname(const char *argv0)
Definition: path.c:652
#define printf(...)
Definition: port.h:245
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:43
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
Definition: dirent.c:26
SimpleStringList ignore_list
manifest_data * manifest
#define stat
Definition: win32_port.h:274

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

◆ parse_manifest_file()

static manifest_data * parse_manifest_file ( char *  manifest_path)
static

Definition at line 392 of file pg_verifybackup.c.

393{
394 int fd;
395 struct stat statbuf;
396 off_t estimate;
397 uint32 initial_size;
398 manifest_files_hash *ht;
399 char *buffer;
400 int rc;
402 manifest_data *result;
403
404 int chunk_size = READ_CHUNK_SIZE;
405
406 /* Open the manifest file. */
407 if ((fd = open(manifest_path, O_RDONLY | PG_BINARY, 0)) < 0)
408 report_fatal_error("could not open file \"%s\": %m", manifest_path);
409
410 /* Figure out how big the manifest is. */
411 if (fstat(fd, &statbuf) != 0)
412 report_fatal_error("could not stat file \"%s\": %m", manifest_path);
413
414 /* Guess how large to make the hash table based on the manifest size. */
415 estimate = statbuf.st_size / ESTIMATED_BYTES_PER_MANIFEST_LINE;
416 initial_size = Min(PG_UINT32_MAX, Max(estimate, 256));
417
418 /* Create the hash table. */
419 ht = manifest_files_create(initial_size, NULL);
420
421 result = pg_malloc0(sizeof(manifest_data));
422 result->files = ht;
423 context.private_data = result;
429
430 /*
431 * Parse the file, in chunks if necessary.
432 */
433 if (statbuf.st_size <= chunk_size)
434 {
435 buffer = pg_malloc(statbuf.st_size);
436 rc = read(fd, buffer, statbuf.st_size);
437 if (rc != statbuf.st_size)
438 {
439 if (rc < 0)
440 pg_fatal("could not read file \"%s\": %m", manifest_path);
441 else
442 pg_fatal("could not read file \"%s\": read %d of %lld",
443 manifest_path, rc, (long long int) statbuf.st_size);
444 }
445
446 /* Close the manifest file. */
447 close(fd);
448
449 /* Parse the manifest. */
450 json_parse_manifest(&context, buffer, statbuf.st_size);
451 }
452 else
453 {
454 int bytes_left = statbuf.st_size;
456
457 inc_state = json_parse_manifest_incremental_init(&context);
458
459 buffer = pg_malloc(chunk_size + 1);
460
461 while (bytes_left > 0)
462 {
463 int bytes_to_read = chunk_size;
464
465 /*
466 * Make sure that the last chunk is sufficiently large. (i.e. at
467 * least half the chunk size) so that it will contain fully the
468 * piece at the end with the checksum.
469 */
470 if (bytes_left < chunk_size)
471 bytes_to_read = bytes_left;
472 else if (bytes_left < 2 * chunk_size)
473 bytes_to_read = bytes_left / 2;
474 rc = read(fd, buffer, bytes_to_read);
475 if (rc != bytes_to_read)
476 {
477 if (rc < 0)
478 pg_fatal("could not read file \"%s\": %m", manifest_path);
479 else
480 pg_fatal("could not read file \"%s\": read %lld of %lld",
481 manifest_path,
482 (long long int) (statbuf.st_size + rc - bytes_left),
483 (long long int) statbuf.st_size);
484 }
485 bytes_left -= rc;
486 json_parse_manifest_incremental_chunk(inc_state, buffer, rc,
487 bytes_left == 0);
488 }
489
490 /* Release the incremental state memory */
492
493 close(fd);
494 }
495
496 /* Done with the buffer. */
497 pfree(buffer);
498
499 return result;
500}
#define Min(x, y)
Definition: c.h:975
#define PG_UINT32_MAX
Definition: c.h:561
#define Max(x, y)
Definition: c.h:969
#define PG_BINARY
Definition: c.h:1244
uint32_t uint32
Definition: c.h:502
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
#define close(a)
Definition: win32.h:12
#define read(a, b, c)
Definition: win32.h:13
void json_parse_manifest(JsonManifestParseContext *context, const char *buffer, size_t size)
JsonManifestParseIncrementalState * json_parse_manifest_incremental_init(JsonManifestParseContext *context)
void json_parse_manifest_incremental_shutdown(JsonManifestParseIncrementalState *incstate)
void json_parse_manifest_incremental_chunk(JsonManifestParseIncrementalState *incstate, const char *chunk, size_t size, bool is_last)
static void verifybackup_version_cb(JsonManifestParseContext *context, int manifest_version)
static void verifybackup_system_identifier(JsonManifestParseContext *context, uint64 manifest_system_identifier)
static void verifybackup_per_wal_range_cb(JsonManifestParseContext *context, TimeLineID tli, XLogRecPtr start_lsn, XLogRecPtr end_lsn)
#define ESTIMATED_BYTES_PER_MANIFEST_LINE
#define READ_CHUNK_SIZE
static pg_noreturn void report_manifest_error(JsonManifestParseContext *context, const char *fmt,...) pg_attribute_printf(2
static void verifybackup_per_file_cb(JsonManifestParseContext *context, const char *pathname, uint64 size, pg_checksum_type checksum_type, int checksum_length, uint8 *checksum_payload)
static int fd(const char *x, int i)
Definition: preproc-init.c:105
json_manifest_per_wal_range_callback per_wal_range_cb
json_manifest_system_identifier_callback system_identifier_cb
json_manifest_error_callback error_cb
json_manifest_per_file_callback per_file_cb
json_manifest_version_callback version_cb
manifest_files_hash * files
Definition: load_manifest.h:59
#define fstat
Definition: win32_port.h:273

References close, JsonManifestParseContext::error_cb, ESTIMATED_BYTES_PER_MANIFEST_LINE, fd(), manifest_data::files, fstat, json_parse_manifest(), json_parse_manifest_incremental_chunk(), json_parse_manifest_incremental_init(), json_parse_manifest_incremental_shutdown(), Max, Min, JsonManifestParseContext::per_file_cb, JsonManifestParseContext::per_wal_range_cb, pfree(), PG_BINARY, pg_fatal, pg_malloc(), pg_malloc0(), PG_UINT32_MAX, JsonManifestParseContext::private_data, read, READ_CHUNK_SIZE, report_fatal_error(), report_manifest_error(), stat::st_size, JsonManifestParseContext::system_identifier_cb, verifybackup_per_file_cb(), verifybackup_per_wal_range_cb(), verifybackup_system_identifier(), verifybackup_version_cb(), and JsonManifestParseContext::version_cb.

Referenced by main().

◆ parse_required_wal()

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

Definition at line 1200 of file pg_verifybackup.c.

1202{
1203 manifest_data *manifest = context->manifest;
1204 manifest_wal_range *this_wal_range = manifest->first_wal_range;
1205
1206 while (this_wal_range != NULL)
1207 {
1208 char *pg_waldump_cmd;
1209
1210 pg_waldump_cmd = psprintf("\"%s\" --quiet --path=\"%s\" --timeline=%u --start=%X/%X --end=%X/%X\n",
1211 pg_waldump_path, wal_directory, this_wal_range->tli,
1212 LSN_FORMAT_ARGS(this_wal_range->start_lsn),
1213 LSN_FORMAT_ARGS(this_wal_range->end_lsn));
1214 fflush(NULL);
1215 if (system(pg_waldump_cmd) != 0)
1216 report_backup_error(context,
1217 "WAL parsing failed for timeline %u",
1218 this_wal_range->tli);
1219
1220 this_wal_range = this_wal_range->next;
1221 }
1222}
static bool manifest
void report_backup_error(verifier_context *context, const char *pg_restrict fmt,...)
XLogRecPtr end_lsn
Definition: load_manifest.h:48
struct manifest_wal_range * next
Definition: load_manifest.h:49
XLogRecPtr start_lsn
Definition: load_manifest.h:47
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43

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

Referenced by main().

◆ precheck_tar_backup_file()

static void precheck_tar_backup_file ( verifier_context context,
char *  relpath,
char *  fullpath,
SimplePtrList tarfiles 
)
static

Definition at line 881 of file pg_verifybackup.c.

883{
884 struct stat sb;
885 Oid tblspc_oid = InvalidOid;
886 pg_compress_algorithm compress_algorithm;
887 tar_file *tar;
888 char *suffix = NULL;
889
890 /* Should be tar format backup */
891 Assert(context->format == 't');
892
893 /* Get file information */
894 if (stat(fullpath, &sb) != 0)
895 {
896 report_backup_error(context,
897 "could not stat file or directory \"%s\": %m",
898 relpath);
899 return;
900 }
901
902 /* In a tar format backup, we expect only plain files. */
903 if (!S_ISREG(sb.st_mode))
904 {
905 report_backup_error(context,
906 "\"%s\" is not a plain file",
907 relpath);
908 return;
909 }
910
911 /*
912 * We expect tar files for backing up the main directory, tablespace, and
913 * pg_wal directory.
914 *
915 * pg_basebackup writes the main data directory to an archive file named
916 * base.tar, the pg_wal directory to pg_wal.tar, and the tablespace
917 * directory to <tablespaceoid>.tar, each followed by a compression type
918 * extension such as .gz, .lz4, or .zst.
919 */
920 if (strncmp("base", relpath, 4) == 0)
921 suffix = relpath + 4;
922 else if (strncmp("pg_wal", relpath, 6) == 0)
923 suffix = relpath + 6;
924 else
925 {
926 /* Expected a <tablespaceoid>.tar file here. */
927 uint64 num = strtoul(relpath, &suffix, 10);
928
929 /*
930 * Report an error if we didn't consume at least one character, if the
931 * result is 0, or if the value is too large to be a valid OID.
932 */
933 if (suffix == NULL || num <= 0 || num > OID_MAX)
934 {
935 report_backup_error(context,
936 "file \"%s\" is not expected in a tar format backup",
937 relpath);
938 return;
939 }
940 tblspc_oid = (Oid) num;
941 }
942
943 /* Now, check the compression type of the tar */
944 if (strcmp(suffix, ".tar") == 0)
945 compress_algorithm = PG_COMPRESSION_NONE;
946 else if (strcmp(suffix, ".tgz") == 0)
947 compress_algorithm = PG_COMPRESSION_GZIP;
948 else if (strcmp(suffix, ".tar.gz") == 0)
949 compress_algorithm = PG_COMPRESSION_GZIP;
950 else if (strcmp(suffix, ".tar.lz4") == 0)
951 compress_algorithm = PG_COMPRESSION_LZ4;
952 else if (strcmp(suffix, ".tar.zst") == 0)
953 compress_algorithm = PG_COMPRESSION_ZSTD;
954 else
955 {
956 report_backup_error(context,
957 "file \"%s\" is not expected in a tar format backup",
958 relpath);
959 return;
960 }
961
962 /*
963 * Ignore WALs, as reading and verification will be handled through
964 * pg_waldump.
965 */
966 if (strncmp("pg_wal", relpath, 6) == 0)
967 return;
968
969 /*
970 * Append the information to the list for complete verification at a later
971 * stage.
972 */
973 tar = pg_malloc(sizeof(tar_file));
974 tar->relpath = pstrdup(relpath);
975 tar->tblspc_oid = tblspc_oid;
976 tar->compress_algorithm = compress_algorithm;
977
978 simple_ptr_list_append(tarfiles, tar);
979
980 /* Update statistics for progress report, if necessary */
981 if (show_progress)
982 total_size += sb.st_size;
983}
uint64_t uint64
Definition: c.h:503
pg_compress_algorithm
Definition: compression.h:22
@ PG_COMPRESSION_NONE
Definition: compression.h:23
static uint64 total_size
#define InvalidOid
Definition: postgres_ext.h:35
unsigned int Oid
Definition: postgres_ext.h:30
#define OID_MAX
Definition: postgres_ext.h:38
#define relpath(rlocator, forknum)
Definition: relpath.h:150
void simple_ptr_list_append(SimplePtrList *list, void *ptr)
Definition: simple_list.c:162
char * relpath
pg_compress_algorithm compress_algorithm
#define S_ISREG(m)
Definition: win32_port.h:318

References Assert(), tar_file::compress_algorithm, verifier_context::format, InvalidOid, OID_MAX, PG_COMPRESSION_GZIP, PG_COMPRESSION_LZ4, PG_COMPRESSION_NONE, PG_COMPRESSION_ZSTD, pg_malloc(), pstrdup(), tar_file::relpath, relpath, report_backup_error(), S_ISREG, show_progress, simple_ptr_list_append(), stat::st_mode, stat::st_size, stat, tar_file::tblspc_oid, and total_size.

Referenced by verify_tar_backup().

◆ progress_report()

static void progress_report ( bool  finished)
static

Definition at line 1327 of file pg_verifybackup.c.

1328{
1330 pg_time_t now;
1331 int percent_size = 0;
1332 char totalsize_str[32];
1333 char donesize_str[32];
1334
1335 if (!show_progress)
1336 return;
1337
1338 now = time(NULL);
1339 if (now == last_progress_report && !finished)
1340 return; /* Max once per second */
1341
1343 percent_size = total_size ? (int) ((done_size * 100 / total_size)) : 0;
1344
1345 snprintf(totalsize_str, sizeof(totalsize_str), UINT64_FORMAT,
1346 total_size / 1024);
1347 snprintf(donesize_str, sizeof(donesize_str), UINT64_FORMAT,
1348 done_size / 1024);
1349
1350 fprintf(stderr,
1351 _("%*s/%s kB (%d%%) verified"),
1352 (int) strlen(totalsize_str),
1353 donesize_str, totalsize_str, percent_size);
1354
1355 /*
1356 * Stay on the same line if reporting to a terminal and we're not done
1357 * yet.
1358 */
1359 fputc((!finished && isatty(fileno(stderr))) ? '\r' : '\n', stderr);
1360}
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1609
#define UINT64_FORMAT
Definition: c.h:521
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
static pg_time_t last_progress_report
Definition: pg_amcheck.c:147
static uint64 done_size
int64 pg_time_t
Definition: pgtime.h:23
#define snprintf
Definition: port.h:239

References _, done_size, fprintf, last_progress_report, now(), show_progress, snprintf, total_size, and UINT64_FORMAT.

Referenced by verify_backup_checksums(), verify_file_checksum(), verify_tar_backup(), and verify_tar_file().

◆ report_backup_error()

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

Definition at line 1231 of file pg_verifybackup.c.

1232{
1233 va_list ap;
1234
1235 va_start(ap, fmt);
1237 va_end(ap);
1238
1239 context->saw_any_error = true;
1240 if (context->exit_on_error)
1241 exit(1);
1242}
#define gettext(x)
Definition: c.h:1150
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:219
@ PG_LOG_PRIMARY
Definition: logging.h:67
@ PG_LOG_ERROR
Definition: logging.h:43

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

Referenced by member_compute_checksum(), member_verify_checksum(), member_verify_header(), parse_required_wal(), precheck_tar_backup_file(), report_extra_backup_files(), verify_file_checksum(), verify_plain_backup_directory(), verify_plain_backup_file(), verify_tar_backup(), and verify_tar_file().

◆ report_extra_backup_files()

static void report_extra_backup_files ( verifier_context context)
static

Definition at line 1039 of file pg_verifybackup.c.

1040{
1041 manifest_data *manifest = context->manifest;
1042 manifest_files_iterator it;
1043 manifest_file *m;
1044
1045 manifest_files_start_iterate(manifest->files, &it);
1046 while ((m = manifest_files_iterate(manifest->files, &it)) != NULL)
1047 if (!m->matched && !should_ignore_relpath(context, m->pathname))
1048 report_backup_error(context,
1049 "\"%s\" is present in the manifest but not on disk",
1050 m->pathname);
1051}
bool should_ignore_relpath(verifier_context *context, const char *relpath)
const char * pathname
Definition: load_manifest.h:25

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

Referenced by main().

◆ report_fatal_error()

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

Definition at line 1248 of file pg_verifybackup.c.

1249{
1250 va_list ap;
1251
1252 va_start(ap, fmt);
1254 va_end(ap);
1255
1256 exit(1);
1257}

References gettext, PG_LOG_ERROR, pg_log_generic_v(), and PG_LOG_PRIMARY.

Referenced by main(), member_verify_control_data(), parse_manifest_file(), verify_control_file(), and verifybackup_per_file_cb().

◆ report_manifest_error()

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

Definition at line 509 of file pg_verifybackup.c.

510{
511 va_list ap;
512
513 va_start(ap, fmt);
515 va_end(ap);
516
517 exit(1);
518}

References gettext, PG_LOG_ERROR, pg_log_generic_v(), and PG_LOG_PRIMARY.

Referenced by parse_manifest_file().

◆ should_ignore_relpath()

bool should_ignore_relpath ( verifier_context context,
const char *  relpath 
)

Definition at line 1267 of file pg_verifybackup.c.

1268{
1270
1271 for (cell = context->ignore_list.head; cell != NULL; cell = cell->next)
1272 {
1273 const char *r = relpath;
1274 char *v = cell->val;
1275
1276 while (*v != '\0' && *r == *v)
1277 ++r, ++v;
1278
1279 if (*v == '\0' && (*r == '\0' || *r == '/'))
1280 return true;
1281 }
1282
1283 return false;
1284}
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 member_verify_header(), report_extra_backup_files(), verify_backup_checksums(), verify_plain_backup_directory(), and verify_tar_backup().

◆ usage()

static void usage ( void  )
static

Definition at line 1366 of file pg_verifybackup.c.

1367{
1368 printf(_("%s verifies a backup against the backup manifest.\n\n"), progname);
1369 printf(_("Usage:\n %s [OPTION]... BACKUPDIR\n\n"), progname);
1370 printf(_("Options:\n"));
1371 printf(_(" -e, --exit-on-error exit immediately on error\n"));
1372 printf(_(" -F, --format=p|t backup format (plain, tar)\n"));
1373 printf(_(" -i, --ignore=RELATIVE_PATH ignore indicated path\n"));
1374 printf(_(" -m, --manifest-path=PATH use specified path for manifest\n"));
1375 printf(_(" -n, --no-parse-wal do not try to parse WAL files\n"));
1376 printf(_(" -P, --progress show progress information\n"));
1377 printf(_(" -q, --quiet do not print any output, except for errors\n"));
1378 printf(_(" -s, --skip-checksums skip checksum verification\n"));
1379 printf(_(" -w, --wal-directory=PATH use specified path for WAL files\n"));
1380 printf(_(" -V, --version output version information, then exit\n"));
1381 printf(_(" -?, --help show this help, then exit\n"));
1382 printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1383 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
1384}

References _, printf, and progname.

Referenced by main().

◆ verify_backup_checksums()

static void verify_backup_checksums ( verifier_context context)
static

Definition at line 1059 of file pg_verifybackup.c.

1060{
1061 manifest_data *manifest = context->manifest;
1062 manifest_files_iterator it;
1063 manifest_file *m;
1064 uint8 *buffer;
1065
1066 progress_report(false);
1067
1068 buffer = pg_malloc(READ_CHUNK_SIZE * sizeof(uint8));
1069
1070 manifest_files_start_iterate(manifest->files, &it);
1071 while ((m = manifest_files_iterate(manifest->files, &it)) != NULL)
1072 {
1073 if (should_verify_checksum(m) &&
1074 !should_ignore_relpath(context, m->pathname))
1075 {
1076 char *fullpath;
1077
1078 /* Compute the full pathname to the target file. */
1079 fullpath = psprintf("%s/%s", context->backup_directory,
1080 m->pathname);
1081
1082 /* Do the actual checksum verification. */
1083 verify_file_checksum(context, m, fullpath, buffer);
1084
1085 /* Avoid leaking memory. */
1086 pfree(fullpath);
1087 }
1088 }
1089
1090 pfree(buffer);
1091
1092 progress_report(true);
1093}
uint8_t uint8
Definition: c.h:500
static void progress_report(bool finished)
static void verify_file_checksum(verifier_context *context, manifest_file *m, char *fullpath, uint8 *buffer)
#define should_verify_checksum(m)

References verifier_context::backup_directory, manifest, verifier_context::manifest, manifest_file::pathname, pfree(), pg_malloc(), progress_report(), psprintf(), READ_CHUNK_SIZE, should_ignore_relpath(), should_verify_checksum, and verify_file_checksum().

Referenced by main().

◆ verify_control_file()

static void verify_control_file ( const char *  controlpath,
uint64  manifest_system_identifier 
)
static

Definition at line 755 of file pg_verifybackup.c.

756{
757 ControlFileData *control_file;
758 bool crc_ok;
759
760 pg_log_debug("reading \"%s\"", controlpath);
761 control_file = get_controlfile_by_exact_path(controlpath, &crc_ok);
762
763 /* Control file contents not meaningful if CRC is bad. */
764 if (!crc_ok)
765 report_fatal_error("%s: CRC is incorrect", controlpath);
766
767 /* Can't interpret control file if not current version. */
768 if (control_file->pg_control_version != PG_CONTROL_VERSION)
769 report_fatal_error("%s: unexpected control file version",
770 controlpath);
771
772 /* System identifiers should match. */
773 if (manifest_system_identifier != control_file->system_identifier)
774 report_fatal_error("%s: manifest system identifier is %" PRIu64 ", but control file has %" PRIu64,
775 controlpath,
776 manifest_system_identifier,
777 control_file->system_identifier);
778
779 /* Release memory. */
780 pfree(control_file);
781}
ControlFileData * get_controlfile_by_exact_path(const char *ControlFilePath, bool *crc_ok_p)
#define pg_log_debug(...)
Definition: logging.h:133
#define PG_CONTROL_VERSION
Definition: pg_control.h:25
uint32 pg_control_version
Definition: pg_control.h:125
uint64 system_identifier
Definition: pg_control.h:110

References get_controlfile_by_exact_path(), pfree(), PG_CONTROL_VERSION, ControlFileData::pg_control_version, pg_log_debug, report_fatal_error(), and ControlFileData::system_identifier.

Referenced by verify_plain_backup_file().

◆ verify_file_checksum()

static void verify_file_checksum ( verifier_context context,
manifest_file m,
char *  fullpath,
uint8 buffer 
)
static

Definition at line 1099 of file pg_verifybackup.c.

1101{
1102 pg_checksum_context checksum_ctx;
1103 const char *relpath = m->pathname;
1104 int fd;
1105 int rc;
1106 uint64 bytes_read = 0;
1107 uint8 checksumbuf[PG_CHECKSUM_MAX_LENGTH];
1108 int checksumlen;
1109
1110 /* Open the target file. */
1111 if ((fd = open(fullpath, O_RDONLY | PG_BINARY, 0)) < 0)
1112 {
1113 report_backup_error(context, "could not open file \"%s\": %m",
1114 relpath);
1115 return;
1116 }
1117
1118 /* Initialize checksum context. */
1119 if (pg_checksum_init(&checksum_ctx, m->checksum_type) < 0)
1120 {
1121 report_backup_error(context, "could not initialize checksum of file \"%s\"",
1122 relpath);
1123 close(fd);
1124 return;
1125 }
1126
1127 /* Read the file chunk by chunk, updating the checksum as we go. */
1128 while ((rc = read(fd, buffer, READ_CHUNK_SIZE)) > 0)
1129 {
1130 bytes_read += rc;
1131 if (pg_checksum_update(&checksum_ctx, buffer, rc) < 0)
1132 {
1133 report_backup_error(context, "could not update checksum of file \"%s\"",
1134 relpath);
1135 close(fd);
1136 return;
1137 }
1138
1139 /* Report progress */
1140 done_size += rc;
1141 progress_report(false);
1142 }
1143 if (rc < 0)
1144 report_backup_error(context, "could not read file \"%s\": %m",
1145 relpath);
1146
1147 /* Close the file. */
1148 if (close(fd) != 0)
1149 {
1150 report_backup_error(context, "could not close file \"%s\": %m",
1151 relpath);
1152 return;
1153 }
1154
1155 /* If we didn't manage to read the whole file, bail out now. */
1156 if (rc < 0)
1157 return;
1158
1159 /*
1160 * Double-check that we read the expected number of bytes from the file.
1161 * Normally, mismatches would be caught in verify_plain_backup_file and
1162 * this check would never be reached, but this provides additional safety
1163 * and clarity in the event of concurrent modifications or filesystem
1164 * misbehavior.
1165 */
1166 if (bytes_read != m->size)
1167 {
1168 report_backup_error(context,
1169 "file \"%s\" should contain %" PRIu64 " bytes, but read %" PRIu64,
1170 relpath, m->size, bytes_read);
1171 return;
1172 }
1173
1174 /* Get the final checksum. */
1175 checksumlen = pg_checksum_final(&checksum_ctx, checksumbuf);
1176 if (checksumlen < 0)
1177 {
1178 report_backup_error(context,
1179 "could not finalize checksum of file \"%s\"",
1180 relpath);
1181 return;
1182 }
1183
1184 /* And check it against the manifest. */
1185 if (checksumlen != m->checksum_length)
1186 report_backup_error(context,
1187 "file \"%s\" has checksum of length %d, but expected %d",
1188 relpath, m->checksum_length, checksumlen);
1189 else if (memcmp(checksumbuf, m->checksum_payload, checksumlen) != 0)
1190 report_backup_error(context,
1191 "checksum mismatch for file \"%s\"",
1192 relpath);
1193}
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
uint8 * checksum_payload
Definition: load_manifest.h:29
pg_checksum_type checksum_type
Definition: load_manifest.h:27

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

Referenced by verify_backup_checksums().

◆ verify_plain_backup_directory()

static void verify_plain_backup_directory ( verifier_context context,
char *  relpath,
char *  fullpath,
DIR dir 
)
static

Definition at line 616 of file pg_verifybackup.c.

618{
619 struct dirent *dirent;
620
621 /* Open the directory unless the caller did it. */
622 if (dir == NULL && ((dir = opendir(fullpath)) == NULL))
623 {
624 report_backup_error(context,
625 "could not open directory \"%s\": %m", fullpath);
627
628 return;
629 }
630
631 while (errno = 0, (dirent = readdir(dir)) != NULL)
632 {
633 char *filename = dirent->d_name;
634 char *newfullpath = psprintf("%s/%s", fullpath, filename);
635 char *newrelpath;
636
637 /* Skip "." and ".." */
638 if (filename[0] == '.' && (filename[1] == '\0'
639 || strcmp(filename, "..") == 0))
640 continue;
641
642 if (relpath == NULL)
643 newrelpath = pstrdup(filename);
644 else
645 newrelpath = psprintf("%s/%s", relpath, filename);
646
647 if (!should_ignore_relpath(context, newrelpath))
648 verify_plain_backup_file(context, newrelpath, newfullpath);
649
650 pfree(newfullpath);
651 pfree(newrelpath);
652 }
653
654 if (closedir(dir))
655 {
656 report_backup_error(context,
657 "could not close directory \"%s\": %m", fullpath);
658 return;
659 }
660}
int closedir(DIR *)
Definition: dirent.c:127
struct dirent * readdir(DIR *)
Definition: dirent.c:78
static char * filename
Definition: pg_dumpall.c:123
static void verify_plain_backup_file(verifier_context *context, char *relpath, char *fullpath)
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(), should_ignore_relpath(), simple_string_list_append(), and verify_plain_backup_file().

Referenced by main(), and verify_plain_backup_file().

◆ verify_plain_backup_file()

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

Definition at line 669 of file pg_verifybackup.c.

671{
672 struct stat sb;
673 manifest_file *m;
674
675 if (stat(fullpath, &sb) != 0)
676 {
677 report_backup_error(context,
678 "could not stat file or directory \"%s\": %m",
679 relpath);
680
681 /*
682 * Suppress further errors related to this path name and, if it's a
683 * directory, anything underneath it.
684 */
686
687 return;
688 }
689
690 /* If it's a directory, just recurse. */
691 if (S_ISDIR(sb.st_mode))
692 {
693 verify_plain_backup_directory(context, relpath, fullpath, NULL);
694 return;
695 }
696
697 /* If it's not a directory, it should be a plain file. */
698 if (!S_ISREG(sb.st_mode))
699 {
700 report_backup_error(context,
701 "\"%s\" is not a file or directory",
702 relpath);
703 return;
704 }
705
706 /* Check whether there's an entry in the manifest hash. */
707 m = manifest_files_lookup(context->manifest->files, relpath);
708 if (m == NULL)
709 {
710 report_backup_error(context,
711 "\"%s\" is present on disk but not in the manifest",
712 relpath);
713 return;
714 }
715
716 /* Flag this entry as having been encountered in the filesystem. */
717 m->matched = true;
718
719 /* Check that the size matches. */
720 if (m->size != sb.st_size)
721 {
722 report_backup_error(context,
723 "\"%s\" has size %llu on disk but size %llu in the manifest",
724 relpath, (unsigned long long) sb.st_size,
725 (unsigned long long) m->size);
726 m->bad = true;
727 }
728
729 /*
730 * Validate the manifest system identifier, not available in manifest
731 * version 1.
732 */
733 if (context->manifest->version != 1 &&
734 strcmp(relpath, XLOG_CONTROL_FILE) == 0)
736
737 /* Update statistics for progress report, if necessary */
738 if (show_progress && !context->skip_checksums &&
740 total_size += m->size;
741
742 /*
743 * We don't verify checksums at this stage. We first finish verifying that
744 * we have the expected set of files with the expected sizes, and only
745 * afterwards verify the checksums. That's because computing checksums may
746 * take a while, and we'd like to report more obvious problems quickly.
747 */
748}
static void verify_control_file(const char *controlpath, uint64 manifest_system_identifier)
uint64 system_identifier
Definition: load_manifest.h:58
#define S_ISDIR(m)
Definition: win32_port.h:315
#define XLOG_CONTROL_FILE

References manifest_data::files, verifier_context::ignore_list, verifier_context::manifest, relpath, report_backup_error(), S_ISDIR, S_ISREG, should_verify_checksum, show_progress, simple_string_list_append(), verifier_context::skip_checksums, stat::st_mode, stat::st_size, stat, manifest_data::system_identifier, total_size, verify_control_file(), verify_plain_backup_directory(), manifest_data::version, and XLOG_CONTROL_FILE.

Referenced by verify_plain_backup_directory().

◆ verify_tar_backup()

static void verify_tar_backup ( verifier_context context,
DIR dir 
)
static

Definition at line 790 of file pg_verifybackup.c.

791{
792 struct dirent *dirent;
793 SimplePtrList tarfiles = {NULL, NULL};
794 SimplePtrListCell *cell;
795
796 Assert(context->format != 'p');
797
798 progress_report(false);
799
800 /* First pass: scan the directory for tar files. */
801 while (errno = 0, (dirent = readdir(dir)) != NULL)
802 {
803 char *filename = dirent->d_name;
804
805 /* Skip "." and ".." */
806 if (filename[0] == '.' && (filename[1] == '\0'
807 || strcmp(filename, "..") == 0))
808 continue;
809
810 /*
811 * Unless it's something we should ignore, perform prechecks and add
812 * it to the list.
813 */
814 if (!should_ignore_relpath(context, filename))
815 {
816 char *fullpath;
817
818 fullpath = psprintf("%s/%s", context->backup_directory, filename);
819 precheck_tar_backup_file(context, filename, fullpath, &tarfiles);
820 pfree(fullpath);
821 }
822 }
823
824 if (closedir(dir))
825 {
826 report_backup_error(context,
827 "could not close directory \"%s\": %m",
828 context->backup_directory);
829 return;
830 }
831
832 /* Second pass: Perform the final verification of the tar contents. */
833 for (cell = tarfiles.head; cell != NULL; cell = cell->next)
834 {
835 tar_file *tar = (tar_file *) cell->ptr;
836 astreamer *streamer;
837 char *fullpath;
838
839 /*
840 * Prepares the archive streamer stack according to the tar
841 * compression format.
842 */
843 streamer = create_archive_verifier(context,
844 tar->relpath,
845 tar->tblspc_oid,
846 tar->compress_algorithm);
847
848 /* Compute the full pathname to the target file. */
849 fullpath = psprintf("%s/%s", context->backup_directory,
850 tar->relpath);
851
852 /* Invoke the streamer for reading, decompressing, and verifying. */
853 verify_tar_file(context, tar->relpath, fullpath, streamer);
854
855 /* Cleanup. */
856 pfree(tar->relpath);
857 pfree(tar);
858 pfree(fullpath);
859
860 astreamer_finalize(streamer);
861 astreamer_free(streamer);
862 }
863 simple_ptr_list_destroy(&tarfiles);
864
865 progress_report(true);
866}
static void astreamer_free(astreamer *streamer)
Definition: astreamer.h:153
static void astreamer_finalize(astreamer *streamer)
Definition: astreamer.h:145
static void verify_tar_file(verifier_context *context, char *relpath, char *fullpath, astreamer *streamer)
static astreamer * create_archive_verifier(verifier_context *context, char *archive_name, Oid tblspc_oid, pg_compress_algorithm compress_algo)
static void precheck_tar_backup_file(verifier_context *context, char *relpath, char *fullpath, SimplePtrList *tarfiles)
void simple_ptr_list_destroy(SimplePtrList *list)
Definition: simple_list.c:181
struct SimplePtrListCell * next
Definition: simple_list.h:48
SimplePtrListCell * head
Definition: simple_list.h:54

References Assert(), astreamer_finalize(), astreamer_free(), verifier_context::backup_directory, closedir(), tar_file::compress_algorithm, create_archive_verifier(), dirent::d_name, filename, verifier_context::format, SimplePtrList::head, SimplePtrListCell::next, pfree(), precheck_tar_backup_file(), progress_report(), psprintf(), SimplePtrListCell::ptr, readdir(), tar_file::relpath, report_backup_error(), should_ignore_relpath(), simple_ptr_list_destroy(), tar_file::tblspc_oid, and verify_tar_file().

Referenced by main().

◆ verify_tar_file()

static void verify_tar_file ( verifier_context context,
char *  relpath,
char *  fullpath,
astreamer streamer 
)
static

Definition at line 993 of file pg_verifybackup.c.

995{
996 int fd;
997 int rc;
998 char *buffer;
999
1000 pg_log_debug("reading \"%s\"", fullpath);
1001
1002 /* Open the target file. */
1003 if ((fd = open(fullpath, O_RDONLY | PG_BINARY, 0)) < 0)
1004 {
1005 report_backup_error(context, "could not open file \"%s\": %m",
1006 relpath);
1007 return;
1008 }
1009
1010 buffer = pg_malloc(READ_CHUNK_SIZE * sizeof(uint8));
1011
1012 /* Perform the reads */
1013 while ((rc = read(fd, buffer, READ_CHUNK_SIZE)) > 0)
1014 {
1015 astreamer_content(streamer, NULL, buffer, rc, ASTREAMER_UNKNOWN);
1016
1017 /* Report progress */
1018 done_size += rc;
1019 progress_report(false);
1020 }
1021
1022 pg_free(buffer);
1023
1024 if (rc < 0)
1025 report_backup_error(context, "could not read file \"%s\": %m",
1026 relpath);
1027
1028 /* Close the file. */
1029 if (close(fd) != 0)
1030 report_backup_error(context, "could not close file \"%s\": %m",
1031 relpath);
1032}
static void astreamer_content(astreamer *streamer, astreamer_member *member, const char *data, int len, astreamer_archive_context context)
Definition: astreamer.h:135
@ ASTREAMER_UNKNOWN
Definition: astreamer.h:64
void pg_free(void *ptr)
Definition: fe_memutils.c:105

References astreamer_content(), ASTREAMER_UNKNOWN, close, done_size, fd(), PG_BINARY, pg_free(), pg_log_debug, pg_malloc(), progress_report(), read, READ_CHUNK_SIZE, relpath, and report_backup_error().

Referenced by verify_tar_backup().

◆ verifybackup_per_file_cb()

static void verifybackup_per_file_cb ( JsonManifestParseContext context,
const char *  pathname,
uint64  size,
pg_checksum_type  checksum_type,
int  checksum_length,
uint8 checksum_payload 
)
static

Definition at line 550 of file pg_verifybackup.c.

554{
556 manifest_files_hash *ht = manifest->files;
557 manifest_file *m;
558 bool found;
559
560 /* Make a new entry in the hash table for this file. */
561 m = manifest_files_insert(ht, pathname, &found);
562 if (found)
563 report_fatal_error("duplicate path name in backup manifest: \"%s\"",
564 pathname);
565
566 /* Initialize the entry. */
567 m->size = size;
568 m->checksum_type = checksum_type;
569 m->checksum_length = checksum_length;
570 m->checksum_payload = checksum_payload;
571 m->matched = false;
572 m->bad = false;
573}

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

Referenced by parse_manifest_file().

◆ verifybackup_per_wal_range_cb()

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

Definition at line 579 of file pg_verifybackup.c.

582{
585
586 /* Allocate and initialize a struct describing this WAL range. */
588 range->tli = tli;
589 range->start_lsn = start_lsn;
590 range->end_lsn = end_lsn;
591 range->prev = manifest->last_wal_range;
592 range->next = NULL;
593
594 /* Add it to the end of the list. */
595 if (manifest->first_wal_range == NULL)
596 manifest->first_wal_range = range;
597 else
598 manifest->last_wal_range->next = range;
599 manifest->last_wal_range = range;
600}
void * palloc(Size size)
Definition: mcxt.c:1943
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:412

References manifest, palloc(), JsonManifestParseContext::private_data, and range().

Referenced by parse_manifest_file().

◆ verifybackup_system_identifier()

static void verifybackup_system_identifier ( JsonManifestParseContext context,
uint64  manifest_system_identifier 
)
static

Definition at line 537 of file pg_verifybackup.c.

539{
541
542 /* Validation will be at the later stage */
543 manifest->system_identifier = manifest_system_identifier;
544}

References manifest, and JsonManifestParseContext::private_data.

Referenced by parse_manifest_file().

◆ verifybackup_version_cb()

static void verifybackup_version_cb ( JsonManifestParseContext context,
int  manifest_version 
)
static

Definition at line 524 of file pg_verifybackup.c.

526{
528
529 /* Validation will be at the later stage */
530 manifest->version = manifest_version;
531}

References manifest, and JsonManifestParseContext::private_data.

Referenced by parse_manifest_file().

Variable Documentation

◆ done_size

uint64 done_size = 0
static

Definition at line 112 of file pg_verifybackup.c.

Referenced by progress_report(), verify_file_checksum(), and verify_tar_file().

◆ progname

const char* progname
static

Definition at line 105 of file pg_verifybackup.c.

Referenced by main(), and usage().

◆ show_progress

bool show_progress = false
static

◆ total_size

uint64 total_size = 0
static