1 /*-------------------------------------------------------------------------
4 * Common code for control data file output.
7 * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * src/common/controldata_utils.c
14 *-------------------------------------------------------------------------
20 #include "postgres_fe.h"
27 #include "catalog/pg_control.h"
28 #include "common/controldata_utils.h"
29 #include "port/pg_crc32c.h"
32 * get_controlfile(char *DataDir, const char *progname)
34 * Get controlfile values. The caller is responsible
35 * for pfreeing the result.
37 * Returns NULL if the CRC did not match.
40 get_controlfile(const char *DataDir, const char *progname)
42 ControlFileData *ControlFile;
44 char ControlFilePath[MAXPGPATH];
47 ControlFile = palloc(sizeof(ControlFileData));
48 snprintf(ControlFilePath, MAXPGPATH, "%s/global/pg_control", DataDir);
50 if ((fd = open(ControlFilePath, O_RDONLY | PG_BINARY, 0)) == -1)
53 (errcode_for_file_access(),
54 errmsg("could not open file \"%s\" for reading: %m",
58 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
59 progname, ControlFilePath, strerror(errno));
64 if (read(fd, ControlFile, sizeof(ControlFileData)) != sizeof(ControlFileData))
67 (errcode_for_file_access(),
68 errmsg("could not read file \"%s\": %m", ControlFilePath)));
71 fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
72 progname, ControlFilePath, strerror(errno));
83 offsetof(ControlFileData, crc));
86 if (!EQ_CRC32C(crc, ControlFile->crc))
92 /* Make sure the control file is valid byte order. */
93 if (ControlFile->pg_control_version % 65536 == 0 &&
94 ControlFile->pg_control_version / 65536 != 0)
96 elog(ERROR, _("byte ordering mismatch"));
98 printf(_("WARNING: possible byte ordering mismatch\n"
99 "The byte ordering used to store the pg_control file might not match the one\n"
100 "used by this program. In that case the results below would be incorrect, and\n"
101 "the PostgreSQL installation would be incompatible with this data directory.\n"));