PostgreSQL Source Code  git master
parse_manifest.c File Reference
#include "postgres_fe.h"
#include "common/jsonapi.h"
#include "common/parse_manifest.h"
Include dependency graph for parse_manifest.c:

Go to the source code of this file.

Data Structures

struct  JsonManifestParseState
 
struct  JsonManifestParseIncrementalState
 

Enumerations

enum  JsonManifestSemanticState {
  JM_EXPECT_TOPLEVEL_START , JM_EXPECT_TOPLEVEL_END , JM_EXPECT_TOPLEVEL_FIELD , JM_EXPECT_VERSION_VALUE ,
  JM_EXPECT_SYSTEM_IDENTIFIER_VALUE , JM_EXPECT_FILES_START , JM_EXPECT_FILES_NEXT , JM_EXPECT_THIS_FILE_FIELD ,
  JM_EXPECT_THIS_FILE_VALUE , JM_EXPECT_WAL_RANGES_START , JM_EXPECT_WAL_RANGES_NEXT , JM_EXPECT_THIS_WAL_RANGE_FIELD ,
  JM_EXPECT_THIS_WAL_RANGE_VALUE , JM_EXPECT_MANIFEST_CHECKSUM_VALUE , JM_EXPECT_EOF
}
 
enum  JsonManifestFileField {
  JMFF_PATH , JMFF_ENCODED_PATH , JMFF_SIZE , JMFF_LAST_MODIFIED ,
  JMFF_CHECKSUM_ALGORITHM , JMFF_CHECKSUM
}
 
enum  JsonManifestWALRangeField { JMWRF_TIMELINE , JMWRF_START_LSN , JMWRF_END_LSN }
 

Functions

static JsonParseErrorType json_manifest_object_start (void *state)
 
static JsonParseErrorType json_manifest_object_end (void *state)
 
static JsonParseErrorType json_manifest_array_start (void *state)
 
static JsonParseErrorType json_manifest_array_end (void *state)
 
static JsonParseErrorType json_manifest_object_field_start (void *state, char *fname, bool isnull)
 
static JsonParseErrorType json_manifest_scalar (void *state, char *token, JsonTokenType tokentype)
 
static void json_manifest_finalize_version (JsonManifestParseState *parse)
 
static void json_manifest_finalize_system_identifier (JsonManifestParseState *parse)
 
static void json_manifest_finalize_file (JsonManifestParseState *parse)
 
static void json_manifest_finalize_wal_range (JsonManifestParseState *parse)
 
static void verify_manifest_checksum (JsonManifestParseState *parse, char *buffer, size_t size, pg_cryptohash_ctx *incr_ctx)
 
static void json_manifest_parse_failure (JsonManifestParseContext *context, char *msg)
 
static int hexdecode_char (char c)
 
static bool hexdecode_string (uint8 *result, char *input, int nbytes)
 
static bool parse_xlogrecptr (XLogRecPtr *result, char *input)
 
JsonManifestParseIncrementalStatejson_parse_manifest_incremental_init (JsonManifestParseContext *context)
 
void json_parse_manifest_incremental_shutdown (JsonManifestParseIncrementalState *incstate)
 
void json_parse_manifest_incremental_chunk (JsonManifestParseIncrementalState *incstate, char *chunk, int size, bool is_last)
 
void json_parse_manifest (JsonManifestParseContext *context, char *buffer, size_t size)
 

Enumeration Type Documentation

◆ JsonManifestFileField

Enumerator
JMFF_PATH 
JMFF_ENCODED_PATH 
JMFF_SIZE 
JMFF_LAST_MODIFIED 
JMFF_CHECKSUM_ALGORITHM 
JMFF_CHECKSUM 

Definition at line 44 of file parse_manifest.c.

45 {
46  JMFF_PATH,
48  JMFF_SIZE,
JsonManifestFileField
@ JMFF_ENCODED_PATH
@ JMFF_PATH
@ JMFF_LAST_MODIFIED
@ JMFF_CHECKSUM_ALGORITHM
@ JMFF_CHECKSUM
@ JMFF_SIZE

◆ JsonManifestSemanticState

Enumerator
JM_EXPECT_TOPLEVEL_START 
JM_EXPECT_TOPLEVEL_END 
JM_EXPECT_TOPLEVEL_FIELD 
JM_EXPECT_VERSION_VALUE 
JM_EXPECT_SYSTEM_IDENTIFIER_VALUE 
JM_EXPECT_FILES_START 
JM_EXPECT_FILES_NEXT 
JM_EXPECT_THIS_FILE_FIELD 
JM_EXPECT_THIS_FILE_VALUE 
JM_EXPECT_WAL_RANGES_START 
JM_EXPECT_WAL_RANGES_NEXT 
JM_EXPECT_THIS_WAL_RANGE_FIELD 
JM_EXPECT_THIS_WAL_RANGE_VALUE 
JM_EXPECT_MANIFEST_CHECKSUM_VALUE 
JM_EXPECT_EOF 

Definition at line 22 of file parse_manifest.c.

23 {
JsonManifestSemanticState
@ JM_EXPECT_TOPLEVEL_END
@ JM_EXPECT_FILES_START
@ JM_EXPECT_SYSTEM_IDENTIFIER_VALUE
@ JM_EXPECT_TOPLEVEL_START
@ JM_EXPECT_EOF
@ JM_EXPECT_WAL_RANGES_START
@ JM_EXPECT_FILES_NEXT
@ JM_EXPECT_THIS_FILE_FIELD
@ JM_EXPECT_THIS_FILE_VALUE
@ JM_EXPECT_THIS_WAL_RANGE_VALUE
@ JM_EXPECT_VERSION_VALUE
@ JM_EXPECT_MANIFEST_CHECKSUM_VALUE
@ JM_EXPECT_THIS_WAL_RANGE_FIELD
@ JM_EXPECT_TOPLEVEL_FIELD
@ JM_EXPECT_WAL_RANGES_NEXT

◆ JsonManifestWALRangeField

Enumerator
JMWRF_TIMELINE 
JMWRF_START_LSN 
JMWRF_END_LSN 

Definition at line 57 of file parse_manifest.c.

58 {
JsonManifestWALRangeField
@ JMWRF_START_LSN
@ JMWRF_TIMELINE
@ JMWRF_END_LSN

Function Documentation

◆ hexdecode_char()

static int hexdecode_char ( char  c)
static

Definition at line 901 of file parse_manifest.c.

902 {
903  if (c >= '0' && c <= '9')
904  return c - '0';
905  if (c >= 'a' && c <= 'f')
906  return c - 'a' + 10;
907  if (c >= 'A' && c <= 'F')
908  return c - 'A' + 10;
909 
910  return -1;
911 }
char * c

Referenced by hexdecode_string().

◆ hexdecode_string()

static bool hexdecode_string ( uint8 result,
char *  input,
int  nbytes 
)
static

Definition at line 919 of file parse_manifest.c.

920 {
921  int i;
922 
923  for (i = 0; i < nbytes; ++i)
924  {
925  int n1 = hexdecode_char(input[i * 2]);
926  int n2 = hexdecode_char(input[i * 2 + 1]);
927 
928  if (n1 < 0 || n2 < 0)
929  return false;
930  result[i] = n1 * 16 + n2;
931  }
932 
933  return true;
934 }
FILE * input
int i
Definition: isn.c:73
static int hexdecode_char(char c)

References hexdecode_char(), i, and input.

Referenced by json_manifest_finalize_file(), and verify_manifest_checksum().

◆ json_manifest_array_end()

static JsonParseErrorType json_manifest_array_end ( void *  state)
static

Definition at line 379 of file parse_manifest.c.

380 {
382 
383  switch (parse->state)
384  {
388  break;
389  default:
391  "unexpected array end");
392  break;
393  }
394 
395  return JSON_SUCCESS;
396 }
@ JSON_SUCCESS
Definition: jsonapi.h:38
static void json_manifest_parse_failure(JsonManifestParseContext *context, char *msg)
static struct subre * parse(struct vars *v, int stopper, int type, struct state *init, struct state *final)
Definition: regcomp.c:715
Definition: regguts.h:323

References JM_EXPECT_FILES_NEXT, JM_EXPECT_TOPLEVEL_FIELD, JM_EXPECT_WAL_RANGES_NEXT, json_manifest_parse_failure(), JSON_SUCCESS, and parse().

Referenced by json_parse_manifest(), and json_parse_manifest_incremental_init().

◆ json_manifest_array_start()

static JsonParseErrorType json_manifest_array_start ( void *  state)
static

Definition at line 352 of file parse_manifest.c.

353 {
355 
356  switch (parse->state)
357  {
359  parse->state = JM_EXPECT_FILES_NEXT;
360  break;
363  break;
364  default:
366  "unexpected array start");
367  break;
368  }
369 
370  return JSON_SUCCESS;
371 }

References JM_EXPECT_FILES_NEXT, JM_EXPECT_FILES_START, JM_EXPECT_WAL_RANGES_NEXT, JM_EXPECT_WAL_RANGES_START, json_manifest_parse_failure(), JSON_SUCCESS, and parse().

Referenced by json_parse_manifest(), and json_parse_manifest_incremental_init().

◆ json_manifest_finalize_file()

static void json_manifest_finalize_file ( JsonManifestParseState parse)
static

Definition at line 650 of file parse_manifest.c.

651 {
653  size_t size;
654  char *ep;
655  int checksum_string_length;
656  pg_checksum_type checksum_type;
657  int checksum_length;
658  uint8 *checksum_payload;
659 
660  /* Pathname and size are required. */
661  if (parse->pathname == NULL && parse->encoded_pathname == NULL)
662  json_manifest_parse_failure(parse->context, "missing path name");
663  if (parse->pathname != NULL && parse->encoded_pathname != NULL)
665  "both path name and encoded path name");
666  if (parse->size == NULL)
667  json_manifest_parse_failure(parse->context, "missing size");
668  if (parse->algorithm == NULL && parse->checksum != NULL)
670  "checksum without algorithm");
671 
672  /* Decode encoded pathname, if that's what we have. */
673  if (parse->encoded_pathname != NULL)
674  {
675  int encoded_length = strlen(parse->encoded_pathname);
676  int raw_length = encoded_length / 2;
677 
678  parse->pathname = palloc(raw_length + 1);
679  if (encoded_length % 2 != 0 ||
680  !hexdecode_string((uint8 *) parse->pathname,
681  parse->encoded_pathname,
682  raw_length))
684  "could not decode file name");
685  parse->pathname[raw_length] = '\0';
686  pfree(parse->encoded_pathname);
687  parse->encoded_pathname = NULL;
688  }
689 
690  /* Parse size. */
691  size = strtoul(parse->size, &ep, 10);
692  if (*ep)
694  "file size is not an integer");
695 
696  /* Parse the checksum algorithm, if it's present. */
697  if (parse->algorithm == NULL)
698  checksum_type = CHECKSUM_TYPE_NONE;
699  else if (!pg_checksum_parse_type(parse->algorithm, &checksum_type))
700  context->error_cb(context, "unrecognized checksum algorithm: \"%s\"",
701  parse->algorithm);
702 
703  /* Parse the checksum payload, if it's present. */
704  checksum_string_length = parse->checksum == NULL ? 0
705  : strlen(parse->checksum);
706  if (checksum_string_length == 0)
707  {
708  checksum_length = 0;
709  checksum_payload = NULL;
710  }
711  else
712  {
713  checksum_length = checksum_string_length / 2;
714  checksum_payload = palloc(checksum_length);
715  if (checksum_string_length % 2 != 0 ||
716  !hexdecode_string(checksum_payload, parse->checksum,
717  checksum_length))
718  context->error_cb(context,
719  "invalid checksum for file \"%s\": \"%s\"",
720  parse->pathname, parse->checksum);
721  }
722 
723  /* Invoke the callback with the details we've gathered. */
724  context->per_file_cb(context, parse->pathname, size,
725  checksum_type, checksum_length, checksum_payload);
726 
727  /* Free memory we no longer need. */
728  if (parse->size != NULL)
729  {
730  pfree(parse->size);
731  parse->size = NULL;
732  }
733  if (parse->algorithm != NULL)
734  {
735  pfree(parse->algorithm);
736  parse->algorithm = NULL;
737  }
738  if (parse->checksum != NULL)
739  {
740  pfree(parse->checksum);
741  parse->checksum = NULL;
742  }
743 }
unsigned char uint8
Definition: c.h:504
bool pg_checksum_parse_type(char *name, pg_checksum_type *type)
pg_checksum_type
@ CHECKSUM_TYPE_NONE
void pfree(void *pointer)
Definition: mcxt.c:1520
void * palloc(Size size)
Definition: mcxt.c:1316
static bool hexdecode_string(uint8 *result, char *input, int nbytes)
tree context
Definition: radixtree.h:1829
static pg_noinline void Size size
Definition: slab.c:607

References CHECKSUM_TYPE_NONE, context, hexdecode_string(), json_manifest_parse_failure(), palloc(), parse(), pfree(), pg_checksum_parse_type(), and size.

Referenced by json_manifest_object_end().

◆ json_manifest_finalize_system_identifier()

static void json_manifest_finalize_system_identifier ( JsonManifestParseState parse)
static

Definition at line 625 of file parse_manifest.c.

626 {
628  uint64 system_identifier;
629  char *ep;
630 
631  Assert(parse->manifest_system_identifier != NULL);
632 
633  /* Parse system identifier. */
634  system_identifier = strtou64(parse->manifest_system_identifier, &ep, 10);
635  if (*ep)
637  "manifest system identifier not an integer");
638 
639  /* Invoke the callback for system identifier */
640  context->system_identifier_cb(context, system_identifier);
641 }
#define Assert(condition)
Definition: c.h:858
#define strtou64(str, endptr, base)
Definition: c.h:1298

References Assert, context, json_manifest_parse_failure(), parse(), and strtou64.

Referenced by json_manifest_scalar().

◆ json_manifest_finalize_version()

static void json_manifest_finalize_version ( JsonManifestParseState parse)
static

Definition at line 597 of file parse_manifest.c.

598 {
600  int version;
601  char *ep;
602 
603  Assert(parse->saw_version_field);
604 
605  /* Parse version. */
606  version = strtoi64(parse->manifest_version, &ep, 10);
607  if (*ep)
609  "manifest version not an integer");
610 
611  if (version != 1 && version != 2)
613  "unexpected manifest version");
614 
615  /* Invoke the callback for version */
616  context->version_cb(context, version);
617 }
#define strtoi64(str, endptr, base)
Definition: c.h:1297

References Assert, context, json_manifest_parse_failure(), parse(), and strtoi64.

Referenced by json_manifest_scalar().

◆ json_manifest_finalize_wal_range()

static void json_manifest_finalize_wal_range ( JsonManifestParseState parse)
static

Definition at line 752 of file parse_manifest.c.

753 {
755  TimeLineID tli;
756  XLogRecPtr start_lsn,
757  end_lsn;
758  char *ep;
759 
760  /* Make sure all fields are present. */
761  if (parse->timeline == NULL)
762  json_manifest_parse_failure(parse->context, "missing timeline");
763  if (parse->start_lsn == NULL)
764  json_manifest_parse_failure(parse->context, "missing start LSN");
765  if (parse->end_lsn == NULL)
766  json_manifest_parse_failure(parse->context, "missing end LSN");
767 
768  /* Parse timeline. */
769  tli = strtoul(parse->timeline, &ep, 10);
770  if (*ep)
772  "timeline is not an integer");
773  if (!parse_xlogrecptr(&start_lsn, parse->start_lsn))
775  "could not parse start LSN");
776  if (!parse_xlogrecptr(&end_lsn, parse->end_lsn))
778  "could not parse end LSN");
779 
780  /* Invoke the callback with the details we've gathered. */
781  context->per_wal_range_cb(context, tli, start_lsn, end_lsn);
782 
783  /* Free memory we no longer need. */
784  if (parse->timeline != NULL)
785  {
786  pfree(parse->timeline);
787  parse->timeline = NULL;
788  }
789  if (parse->start_lsn != NULL)
790  {
791  pfree(parse->start_lsn);
792  parse->start_lsn = NULL;
793  }
794  if (parse->end_lsn != NULL)
795  {
796  pfree(parse->end_lsn);
797  parse->end_lsn = NULL;
798  }
799 }
static bool parse_xlogrecptr(XLogRecPtr *result, char *input)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
uint32 TimeLineID
Definition: xlogdefs.h:59

References context, json_manifest_parse_failure(), parse(), parse_xlogrecptr(), and pfree().

Referenced by json_manifest_object_end().

◆ json_manifest_object_end()

static JsonParseErrorType json_manifest_object_end ( void *  state)
static

Definition at line 318 of file parse_manifest.c.

319 {
321 
322  switch (parse->state)
323  {
325  parse->state = JM_EXPECT_EOF;
326  break;
329  parse->state = JM_EXPECT_FILES_NEXT;
330  break;
334  break;
335  default:
337  "unexpected object end");
338  break;
339  }
340 
341  return JSON_SUCCESS;
342 }
static void json_manifest_finalize_wal_range(JsonManifestParseState *parse)
static void json_manifest_finalize_file(JsonManifestParseState *parse)

References JM_EXPECT_EOF, JM_EXPECT_FILES_NEXT, JM_EXPECT_THIS_FILE_FIELD, JM_EXPECT_THIS_WAL_RANGE_FIELD, JM_EXPECT_TOPLEVEL_END, JM_EXPECT_WAL_RANGES_NEXT, json_manifest_finalize_file(), json_manifest_finalize_wal_range(), json_manifest_parse_failure(), JSON_SUCCESS, and parse().

Referenced by json_parse_manifest(), and json_parse_manifest_incremental_init().

◆ json_manifest_object_field_start()

static JsonParseErrorType json_manifest_object_field_start ( void *  state,
char *  fname,
bool  isnull 
)
static

Definition at line 402 of file parse_manifest.c.

403 {
405 
406  switch (parse->state)
407  {
409 
410  /*
411  * Inside toplevel object. The version indicator should always be
412  * the first field.
413  */
414  if (!parse->saw_version_field)
415  {
416  if (strcmp(fname, "PostgreSQL-Backup-Manifest-Version") != 0)
418  "expected version indicator");
420  parse->saw_version_field = true;
421  break;
422  }
423 
424  /* Is this the system identifier? */
425  if (strcmp(fname, "System-Identifier") == 0)
426  {
428  break;
429  }
430 
431  /* Is this the list of files? */
432  if (strcmp(fname, "Files") == 0)
433  {
434  parse->state = JM_EXPECT_FILES_START;
435  break;
436  }
437 
438  /* Is this the list of WAL ranges? */
439  if (strcmp(fname, "WAL-Ranges") == 0)
440  {
442  break;
443  }
444 
445  /* Is this the manifest checksum? */
446  if (strcmp(fname, "Manifest-Checksum") == 0)
447  {
449  break;
450  }
451 
452  /* It's not a field we recognize. */
454  "unrecognized top-level field");
455  break;
456 
458  /* Inside object for one file; which key have we got? */
459  if (strcmp(fname, "Path") == 0)
460  parse->file_field = JMFF_PATH;
461  else if (strcmp(fname, "Encoded-Path") == 0)
462  parse->file_field = JMFF_ENCODED_PATH;
463  else if (strcmp(fname, "Size") == 0)
464  parse->file_field = JMFF_SIZE;
465  else if (strcmp(fname, "Last-Modified") == 0)
466  parse->file_field = JMFF_LAST_MODIFIED;
467  else if (strcmp(fname, "Checksum-Algorithm") == 0)
468  parse->file_field = JMFF_CHECKSUM_ALGORITHM;
469  else if (strcmp(fname, "Checksum") == 0)
470  parse->file_field = JMFF_CHECKSUM;
471  else
473  "unexpected file field");
475  break;
476 
478  /* Inside object for one file; which key have we got? */
479  if (strcmp(fname, "Timeline") == 0)
480  parse->wal_range_field = JMWRF_TIMELINE;
481  else if (strcmp(fname, "Start-LSN") == 0)
482  parse->wal_range_field = JMWRF_START_LSN;
483  else if (strcmp(fname, "End-LSN") == 0)
484  parse->wal_range_field = JMWRF_END_LSN;
485  else
487  "unexpected WAL range field");
489  break;
490 
491  default:
493  "unexpected object field");
494  break;
495  }
496 
497  pfree(fname);
498 
499  return JSON_SUCCESS;
500 }

References JM_EXPECT_FILES_START, JM_EXPECT_MANIFEST_CHECKSUM_VALUE, JM_EXPECT_SYSTEM_IDENTIFIER_VALUE, JM_EXPECT_THIS_FILE_FIELD, JM_EXPECT_THIS_FILE_VALUE, JM_EXPECT_THIS_WAL_RANGE_FIELD, JM_EXPECT_THIS_WAL_RANGE_VALUE, JM_EXPECT_TOPLEVEL_FIELD, JM_EXPECT_VERSION_VALUE, JM_EXPECT_WAL_RANGES_START, JMFF_CHECKSUM, JMFF_CHECKSUM_ALGORITHM, JMFF_ENCODED_PATH, JMFF_LAST_MODIFIED, JMFF_PATH, JMFF_SIZE, JMWRF_END_LSN, JMWRF_START_LSN, JMWRF_TIMELINE, json_manifest_parse_failure(), JSON_SUCCESS, parse(), and pfree().

Referenced by json_parse_manifest(), and json_parse_manifest_incremental_init().

◆ json_manifest_object_start()

static JsonParseErrorType json_manifest_object_start ( void *  state)
static

Definition at line 277 of file parse_manifest.c.

278 {
280 
281  switch (parse->state)
282  {
285  break;
288  parse->pathname = NULL;
289  parse->encoded_pathname = NULL;
290  parse->size = NULL;
291  parse->algorithm = NULL;
292  parse->checksum = NULL;
293  break;
296  parse->timeline = NULL;
297  parse->start_lsn = NULL;
298  parse->end_lsn = NULL;
299  break;
300  default:
302  "unexpected object start");
303  break;
304  }
305 
306  return JSON_SUCCESS;
307 }

References JM_EXPECT_FILES_NEXT, JM_EXPECT_THIS_FILE_FIELD, JM_EXPECT_THIS_WAL_RANGE_FIELD, JM_EXPECT_TOPLEVEL_FIELD, JM_EXPECT_TOPLEVEL_START, JM_EXPECT_WAL_RANGES_NEXT, json_manifest_parse_failure(), JSON_SUCCESS, and parse().

Referenced by json_parse_manifest(), and json_parse_manifest_incremental_init().

◆ json_manifest_parse_failure()

◆ json_manifest_scalar()

static JsonParseErrorType json_manifest_scalar ( void *  state,
char *  token,
JsonTokenType  tokentype 
)
static

Definition at line 518 of file parse_manifest.c.

519 {
521 
522  switch (parse->state)
523  {
525  parse->manifest_version = token;
528  break;
529 
531  parse->manifest_system_identifier = token;
534  break;
535 
537  switch (parse->file_field)
538  {
539  case JMFF_PATH:
540  parse->pathname = token;
541  break;
542  case JMFF_ENCODED_PATH:
543  parse->encoded_pathname = token;
544  break;
545  case JMFF_SIZE:
546  parse->size = token;
547  break;
548  case JMFF_LAST_MODIFIED:
549  pfree(token); /* unused */
550  break;
552  parse->algorithm = token;
553  break;
554  case JMFF_CHECKSUM:
555  parse->checksum = token;
556  break;
557  }
559  break;
560 
562  switch (parse->wal_range_field)
563  {
564  case JMWRF_TIMELINE:
565  parse->timeline = token;
566  break;
567  case JMWRF_START_LSN:
568  parse->start_lsn = token;
569  break;
570  case JMWRF_END_LSN:
571  parse->end_lsn = token;
572  break;
573  }
575  break;
576 
578  parse->state = JM_EXPECT_TOPLEVEL_END;
579  parse->manifest_checksum = token;
580  break;
581 
582  default:
583  json_manifest_parse_failure(parse->context, "unexpected scalar");
584  break;
585  }
586 
587  return JSON_SUCCESS;
588 }
#define token
Definition: indent_globs.h:126
static void json_manifest_finalize_version(JsonManifestParseState *parse)
static void json_manifest_finalize_system_identifier(JsonManifestParseState *parse)

References JM_EXPECT_MANIFEST_CHECKSUM_VALUE, JM_EXPECT_SYSTEM_IDENTIFIER_VALUE, JM_EXPECT_THIS_FILE_FIELD, JM_EXPECT_THIS_FILE_VALUE, JM_EXPECT_THIS_WAL_RANGE_FIELD, JM_EXPECT_THIS_WAL_RANGE_VALUE, JM_EXPECT_TOPLEVEL_END, JM_EXPECT_TOPLEVEL_FIELD, JM_EXPECT_VERSION_VALUE, JMFF_CHECKSUM, JMFF_CHECKSUM_ALGORITHM, JMFF_ENCODED_PATH, JMFF_LAST_MODIFIED, JMFF_PATH, JMFF_SIZE, JMWRF_END_LSN, JMWRF_START_LSN, JMWRF_TIMELINE, json_manifest_finalize_system_identifier(), json_manifest_finalize_version(), json_manifest_parse_failure(), JSON_SUCCESS, parse(), pfree(), and token.

Referenced by json_parse_manifest(), and json_parse_manifest_incremental_init().

◆ json_parse_manifest()

void json_parse_manifest ( JsonManifestParseContext context,
char *  buffer,
size_t  size 
)

Definition at line 228 of file parse_manifest.c.

230 {
231  JsonLexContext *lex;
232  JsonParseErrorType json_error;
235 
236  /* Set up our private parsing context. */
237  parse.context = context;
239  parse.saw_version_field = false;
240 
241  /* Create a JSON lexing context. */
242  lex = makeJsonLexContextCstringLen(NULL, buffer, size, PG_UTF8, true);
243 
244  /* Set up semantic actions. */
245  sem.semstate = &parse;
251  sem.object_field_end = NULL;
252  sem.array_element_start = NULL;
253  sem.array_element_end = NULL;
255 
256  /* Run the actual JSON parser. */
257  json_error = pg_parse_json(lex, &sem);
258  if (json_error != JSON_SUCCESS)
260  if (parse.state != JM_EXPECT_EOF)
261  json_manifest_parse_failure(context, "manifest ended unexpectedly");
262 
263  /* Verify the manifest checksum. */
264  verify_manifest_checksum(&parse, buffer, size, NULL);
265 
266  freeJsonLexContext(lex);
267 }
char * json_errdetail(JsonParseErrorType error, JsonLexContext *lex)
Definition: jsonapi.c:2096
JsonParseErrorType pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
Definition: jsonapi.c:521
JsonLexContext * makeJsonLexContextCstringLen(JsonLexContext *lex, char *json, int len, int encoding, bool need_escapes)
Definition: jsonapi.c:326
void freeJsonLexContext(JsonLexContext *lex)
Definition: jsonapi.c:482
JsonParseErrorType
Definition: jsonapi.h:37
static JsonParseErrorType json_manifest_array_start(void *state)
static JsonParseErrorType json_manifest_object_field_start(void *state, char *fname, bool isnull)
static JsonParseErrorType json_manifest_object_end(void *state)
static JsonParseErrorType json_manifest_object_start(void *state)
static JsonParseErrorType json_manifest_scalar(void *state, char *token, JsonTokenType tokentype)
static JsonParseErrorType json_manifest_array_end(void *state)
static void verify_manifest_checksum(JsonManifestParseState *parse, char *buffer, size_t size, pg_cryptohash_ctx *incr_ctx)
@ PG_UTF8
Definition: pg_wchar.h:232
json_struct_action array_end
Definition: jsonapi.h:138
json_struct_action object_start
Definition: jsonapi.h:135
json_ofield_action object_field_start
Definition: jsonapi.h:139
json_aelem_action array_element_start
Definition: jsonapi.h:141
json_scalar_action scalar
Definition: jsonapi.h:143
void * semstate
Definition: jsonapi.h:134
json_aelem_action array_element_end
Definition: jsonapi.h:142
json_struct_action array_start
Definition: jsonapi.h:137
json_struct_action object_end
Definition: jsonapi.h:136
json_ofield_action object_field_end
Definition: jsonapi.h:140
JsonSemAction sem

References JsonSemAction::array_element_end, JsonSemAction::array_element_start, JsonSemAction::array_end, JsonSemAction::array_start, context, freeJsonLexContext(), JM_EXPECT_EOF, JM_EXPECT_TOPLEVEL_START, json_errdetail(), json_manifest_array_end(), json_manifest_array_start(), json_manifest_object_end(), json_manifest_object_field_start(), json_manifest_object_start(), json_manifest_parse_failure(), json_manifest_scalar(), JSON_SUCCESS, makeJsonLexContextCstringLen(), JsonSemAction::object_end, JsonSemAction::object_field_end, JsonSemAction::object_field_start, JsonSemAction::object_start, parse(), pg_parse_json(), PG_UTF8, JsonSemAction::scalar, sem, JsonSemAction::semstate, size, and verify_manifest_checksum().

Referenced by load_backup_manifest(), and parse_manifest_file().

◆ json_parse_manifest_incremental_chunk()

void json_parse_manifest_incremental_chunk ( JsonManifestParseIncrementalState incstate,
char *  chunk,
int  size,
bool  is_last 
)

Definition at line 185 of file parse_manifest.c.

188 {
190  expected;
193 
194  res = pg_parse_json_incremental(&(incstate->lex), &(incstate->sem),
195  chunk, size, is_last);
196 
197  expected = is_last ? JSON_SUCCESS : JSON_INCOMPLETE;
198 
199  if (res != expected)
201  json_errdetail(res, &(incstate->lex)));
202 
203  if (is_last && parse->state != JM_EXPECT_EOF)
204  json_manifest_parse_failure(context, "manifest ended unexpectedly");
205 
206  if (!is_last)
207  {
208  if (pg_cryptohash_update(incstate->manifest_ctx,
209  (uint8 *) chunk, size) < 0)
210  context->error_cb(context, "could not update checksum of manifest");
211  }
212  else
213  {
215  }
216 }
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
Definition: cryptohash.c:136
uint64 chunk
JsonParseErrorType pg_parse_json_incremental(JsonLexContext *lex, JsonSemAction *sem, char *json, int len, bool is_last)
Definition: jsonapi.c:649
@ JSON_INCOMPLETE
Definition: jsonapi.h:39
pg_cryptohash_ctx * manifest_ctx

References chunk, context, JM_EXPECT_EOF, json_errdetail(), JSON_INCOMPLETE, json_manifest_parse_failure(), JSON_SUCCESS, JsonManifestParseIncrementalState::lex, JsonManifestParseIncrementalState::manifest_ctx, parse(), pg_cryptohash_update(), pg_parse_json_incremental(), res, JsonManifestParseIncrementalState::sem, JsonSemAction::semstate, size, and verify_manifest_checksum().

Referenced by AppendIncrementalManifestData(), FinalizeIncrementalManifest(), load_backup_manifest(), and parse_manifest_file().

◆ json_parse_manifest_incremental_init()

JsonManifestParseIncrementalState* json_parse_manifest_incremental_init ( JsonManifestParseContext context)

Definition at line 129 of file parse_manifest.c.

130 {
133  pg_cryptohash_ctx *manifest_ctx;
134 
135  incstate = palloc(sizeof(JsonManifestParseIncrementalState));
137 
138  parse->context = context;
140  parse->saw_version_field = false;
141 
142  makeJsonLexContextIncremental(&(incstate->lex), PG_UTF8, true);
143 
144  incstate->sem.semstate = parse;
150  incstate->sem.object_field_end = NULL;
151  incstate->sem.array_element_start = NULL;
152  incstate->sem.array_element_end = NULL;
153  incstate->sem.scalar = json_manifest_scalar;
154 
155  manifest_ctx = pg_cryptohash_create(PG_SHA256);
156  if (manifest_ctx == NULL)
157  context->error_cb(context, "out of memory");
158  if (pg_cryptohash_init(manifest_ctx) < 0)
159  context->error_cb(context, "could not initialize checksum of manifest");
160  incstate->manifest_ctx = manifest_ctx;
161 
162  return incstate;
163 }
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:100
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:74
@ PG_SHA256
Definition: cryptohash.h:24
JsonLexContext * makeJsonLexContextIncremental(JsonLexContext *lex, int encoding, bool need_escapes)
Definition: jsonapi.c:366

References JsonSemAction::array_element_end, JsonSemAction::array_element_start, JsonSemAction::array_end, JsonSemAction::array_start, context, JM_EXPECT_TOPLEVEL_START, json_manifest_array_end(), json_manifest_array_start(), json_manifest_object_end(), json_manifest_object_field_start(), json_manifest_object_start(), json_manifest_scalar(), JsonManifestParseIncrementalState::lex, makeJsonLexContextIncremental(), JsonManifestParseIncrementalState::manifest_ctx, JsonSemAction::object_end, JsonSemAction::object_field_end, JsonSemAction::object_field_start, JsonSemAction::object_start, palloc(), parse(), pg_cryptohash_create(), pg_cryptohash_init(), PG_SHA256, PG_UTF8, JsonSemAction::scalar, JsonManifestParseIncrementalState::sem, and JsonSemAction::semstate.

Referenced by CreateIncrementalBackupInfo(), load_backup_manifest(), and parse_manifest_file().

◆ json_parse_manifest_incremental_shutdown()

void json_parse_manifest_incremental_shutdown ( JsonManifestParseIncrementalState incstate)

Definition at line 169 of file parse_manifest.c.

170 {
171  pfree(incstate->sem.semstate);
172  freeJsonLexContext(&(incstate->lex));
173  /* incstate->manifest_ctx has already been freed */
174  pfree(incstate);
175 }

References freeJsonLexContext(), JsonManifestParseIncrementalState::lex, pfree(), JsonManifestParseIncrementalState::sem, and JsonSemAction::semstate.

Referenced by FinalizeIncrementalManifest(), load_backup_manifest(), and parse_manifest_file().

◆ parse_xlogrecptr()

static bool parse_xlogrecptr ( XLogRecPtr result,
char *  input 
)
static

Definition at line 940 of file parse_manifest.c.

941 {
942  uint32 hi;
943  uint32 lo;
944 
945  if (sscanf(input, "%X/%X", &hi, &lo) != 2)
946  return false;
947  *result = ((uint64) hi) << 32 | lo;
948  return true;
949 }
unsigned int uint32
Definition: c.h:506

References input.

Referenced by json_manifest_finalize_wal_range().

◆ verify_manifest_checksum()

static void verify_manifest_checksum ( JsonManifestParseState parse,
char *  buffer,
size_t  size,
pg_cryptohash_ctx incr_ctx 
)
static

Definition at line 813 of file parse_manifest.c.

815 {
817  size_t i;
818  size_t number_of_newlines = 0;
819  size_t ultimate_newline = 0;
820  size_t penultimate_newline = 0;
821  pg_cryptohash_ctx *manifest_ctx;
822  uint8 manifest_checksum_actual[PG_SHA256_DIGEST_LENGTH];
823  uint8 manifest_checksum_expected[PG_SHA256_DIGEST_LENGTH];
824 
825  /* Find the last two newlines in the file. */
826  for (i = 0; i < size; ++i)
827  {
828  if (buffer[i] == '\n')
829  {
830  ++number_of_newlines;
831  penultimate_newline = ultimate_newline;
832  ultimate_newline = i;
833  }
834  }
835 
836  /*
837  * Make sure that the last newline is right at the end, and that there are
838  * at least two lines total. We need this to be true in order for the
839  * following code, which computes the manifest checksum, to work properly.
840  */
841  if (number_of_newlines < 2)
843  "expected at least 2 lines");
844  if (ultimate_newline != size - 1)
846  "last line not newline-terminated");
847 
848  /* Checksum the rest. */
849  if (incr_ctx == NULL)
850  {
851  manifest_ctx = pg_cryptohash_create(PG_SHA256);
852  if (manifest_ctx == NULL)
853  context->error_cb(context, "out of memory");
854  if (pg_cryptohash_init(manifest_ctx) < 0)
855  context->error_cb(context, "could not initialize checksum of manifest");
856  }
857  else
858  {
859  manifest_ctx = incr_ctx;
860  }
861  if (pg_cryptohash_update(manifest_ctx, (uint8 *) buffer, penultimate_newline + 1) < 0)
862  context->error_cb(context, "could not update checksum of manifest");
863  if (pg_cryptohash_final(manifest_ctx, manifest_checksum_actual,
864  sizeof(manifest_checksum_actual)) < 0)
865  context->error_cb(context, "could not finalize checksum of manifest");
866 
867  /* Now verify it. */
868  if (parse->manifest_checksum == NULL)
869  context->error_cb(parse->context, "manifest has no checksum");
870  if (strlen(parse->manifest_checksum) != PG_SHA256_DIGEST_LENGTH * 2 ||
871  !hexdecode_string(manifest_checksum_expected, parse->manifest_checksum,
873  context->error_cb(context, "invalid manifest checksum: \"%s\"",
874  parse->manifest_checksum);
875  if (memcmp(manifest_checksum_actual, manifest_checksum_expected,
877  context->error_cb(context, "manifest checksum mismatch");
878  pg_cryptohash_free(manifest_ctx);
879 }
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:238
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
Definition: cryptohash.c:172
#define PG_SHA256_DIGEST_LENGTH
Definition: sha2.h:23

References context, hexdecode_string(), i, json_manifest_parse_failure(), parse(), pg_cryptohash_create(), pg_cryptohash_final(), pg_cryptohash_free(), pg_cryptohash_init(), pg_cryptohash_update(), PG_SHA256, PG_SHA256_DIGEST_LENGTH, and size.

Referenced by json_parse_manifest(), and json_parse_manifest_incremental_chunk().