2016-03-05 20:10:19 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* controldata_utils.c
|
|
|
|
* Common code for control data file output.
|
|
|
|
*
|
|
|
|
*
|
2018-01-03 05:30:12 +01:00
|
|
|
* Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
|
2016-03-05 20:10:19 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* src/common/controldata_utils.c
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FRONTEND
|
|
|
|
#include "postgres.h"
|
|
|
|
#else
|
|
|
|
#include "postgres_fe.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "catalog/pg_control.h"
|
|
|
|
#include "common/controldata_utils.h"
|
|
|
|
#include "port/pg_crc32c.h"
|
|
|
|
|
|
|
|
/*
|
2016-09-28 18:00:00 +02:00
|
|
|
* get_controlfile(char *DataDir, const char *progname, bool *crc_ok_p)
|
2016-03-05 20:10:19 +01:00
|
|
|
*
|
2016-09-28 18:00:00 +02:00
|
|
|
* Get controlfile values. The result is returned as a palloc'd copy of the
|
|
|
|
* control file data.
|
2016-07-26 17:23:43 +02:00
|
|
|
*
|
2016-09-28 18:00:00 +02:00
|
|
|
* crc_ok_p can be used by the caller to see whether the CRC of the control
|
|
|
|
* file data is correct.
|
2016-03-05 20:10:19 +01:00
|
|
|
*/
|
|
|
|
ControlFileData *
|
2016-09-28 18:00:00 +02:00
|
|
|
get_controlfile(const char *DataDir, const char *progname, bool *crc_ok_p)
|
2016-03-05 20:10:19 +01:00
|
|
|
{
|
2016-06-10 00:02:36 +02:00
|
|
|
ControlFileData *ControlFile;
|
|
|
|
int fd;
|
|
|
|
char ControlFilePath[MAXPGPATH];
|
|
|
|
pg_crc32c crc;
|
2018-05-18 17:52:18 +02:00
|
|
|
int r;
|
2016-03-05 20:10:19 +01:00
|
|
|
|
2016-09-28 18:00:00 +02:00
|
|
|
AssertArg(crc_ok_p);
|
|
|
|
|
2016-03-05 20:10:19 +01:00
|
|
|
ControlFile = palloc(sizeof(ControlFileData));
|
|
|
|
snprintf(ControlFilePath, MAXPGPATH, "%s/global/pg_control", DataDir);
|
|
|
|
|
|
|
|
if ((fd = open(ControlFilePath, O_RDONLY | PG_BINARY, 0)) == -1)
|
2016-03-08 00:14:20 +01:00
|
|
|
#ifndef FRONTEND
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
2016-06-10 00:02:36 +02:00
|
|
|
errmsg("could not open file \"%s\" for reading: %m",
|
|
|
|
ControlFilePath)));
|
2016-03-08 00:14:20 +01:00
|
|
|
#else
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
|
|
|
|
progname, ControlFilePath, strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
#endif
|
2016-03-05 20:10:19 +01:00
|
|
|
|
2018-05-18 17:52:18 +02:00
|
|
|
r = read(fd, ControlFile, sizeof(ControlFileData));
|
|
|
|
if (r != sizeof(ControlFileData))
|
|
|
|
{
|
|
|
|
if (r < 0)
|
2016-03-08 00:14:20 +01:00
|
|
|
#ifndef FRONTEND
|
2018-05-18 17:52:18 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not read file \"%s\": %m", ControlFilePath)));
|
2016-03-08 00:14:20 +01:00
|
|
|
#else
|
2018-05-18 17:52:18 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
|
|
|
|
progname, ControlFilePath, strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2016-03-08 00:14:20 +01:00
|
|
|
#endif
|
2018-05-18 17:52:18 +02:00
|
|
|
else
|
|
|
|
#ifndef FRONTEND
|
|
|
|
ereport(ERROR,
|
Rework error messages around file handling
Some error messages related to file handling are using the code path
context to define their state. For example, 2PC-related errors are
referring to "two-phase status files", or "relation mapping file" is
used for catalog-to-filenode mapping, however those prove to be
difficult to translate, and are not more helpful than just referring to
the path of the file being worked on. So simplify all those error
messages by just referring to files with their path used. In some
cases, like the manipulation of WAL segments, the context is actually
helpful so those are kept.
Calls to the system function read() have also been rather inconsistent
with their error handling sometimes not reporting the number of bytes
read, and some other code paths trying to use an errno which has not
been set. The in-core functions are using a more consistent pattern
with this patch, which checks for both errno if set or if an
inconsistent read is happening.
So as to care about pluralization when reading an unexpected number of
byte(s), "could not read: read %d of %zu" is used as error message, with
%d field being the output result of read() and %zu the expected size.
This simplifies the work of translators with less variations of the same
message.
Author: Michael Paquier
Reviewed-by: Álvaro Herrera
Discussion: https://postgr.es/m/20180520000522.GB1603@paquier.xyz
2018-07-18 01:01:23 +02:00
|
|
|
(errmsg("could not read file \"%s\": read %d of %zu",
|
|
|
|
ControlFilePath, r, sizeof(ControlFileData))));
|
2018-05-18 17:52:18 +02:00
|
|
|
#else
|
|
|
|
{
|
Rework error messages around file handling
Some error messages related to file handling are using the code path
context to define their state. For example, 2PC-related errors are
referring to "two-phase status files", or "relation mapping file" is
used for catalog-to-filenode mapping, however those prove to be
difficult to translate, and are not more helpful than just referring to
the path of the file being worked on. So simplify all those error
messages by just referring to files with their path used. In some
cases, like the manipulation of WAL segments, the context is actually
helpful so those are kept.
Calls to the system function read() have also been rather inconsistent
with their error handling sometimes not reporting the number of bytes
read, and some other code paths trying to use an errno which has not
been set. The in-core functions are using a more consistent pattern
with this patch, which checks for both errno if set or if an
inconsistent read is happening.
So as to care about pluralization when reading an unexpected number of
byte(s), "could not read: read %d of %zu" is used as error message, with
%d field being the output result of read() and %zu the expected size.
This simplifies the work of translators with less variations of the same
message.
Author: Michael Paquier
Reviewed-by: Álvaro Herrera
Discussion: https://postgr.es/m/20180520000522.GB1603@paquier.xyz
2018-07-18 01:01:23 +02:00
|
|
|
fprintf(stderr, _("%s: could not read file \"%s\": read %d of %zu\n"),
|
|
|
|
progname, ControlFilePath, r, sizeof(ControlFileData));
|
2018-05-18 17:52:18 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2016-03-05 20:10:19 +01:00
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* Check the CRC. */
|
|
|
|
INIT_CRC32C(crc);
|
|
|
|
COMP_CRC32C(crc,
|
2016-06-10 00:02:36 +02:00
|
|
|
(char *) ControlFile,
|
|
|
|
offsetof(ControlFileData, crc));
|
2016-03-05 20:10:19 +01:00
|
|
|
FIN_CRC32C(crc);
|
|
|
|
|
2016-09-28 18:00:00 +02:00
|
|
|
*crc_ok_p = EQ_CRC32C(crc, ControlFile->crc);
|
2016-03-05 20:10:19 +01:00
|
|
|
|
|
|
|
/* Make sure the control file is valid byte order. */
|
|
|
|
if (ControlFile->pg_control_version % 65536 == 0 &&
|
|
|
|
ControlFile->pg_control_version / 65536 != 0)
|
|
|
|
#ifndef FRONTEND
|
|
|
|
elog(ERROR, _("byte ordering mismatch"));
|
|
|
|
#else
|
|
|
|
printf(_("WARNING: possible byte ordering mismatch\n"
|
|
|
|
"The byte ordering used to store the pg_control file might not match the one\n"
|
|
|
|
"used by this program. In that case the results below would be incorrect, and\n"
|
|
|
|
"the PostgreSQL installation would be incompatible with this data directory.\n"));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ControlFile;
|
|
|
|
}
|