Fix error message on short read of pg_control
authorMagnus Hagander
Fri, 18 May 2018 15:53:15 +0000 (17:53 +0200)
committerMagnus Hagander
Fri, 18 May 2018 15:53:15 +0000 (17:53 +0200)
Instead of saying "error: success", indicate that we got a working read
but it was too short.

src/backend/access/transam/xlog.c
src/common/controldata_utils.c

index 61061e52570e357cde0464c329935691651ca4c6..98abc4961f0c851b967a9b72e2af9bc4f4113b1c 100644 (file)
@@ -4311,6 +4311,7 @@ ReadControlFile(void)
 {
    pg_crc32c   crc;
    int         fd;
+   int         r;
 
    /*
     * Read data...
@@ -4324,10 +4325,17 @@ ReadControlFile(void)
                 errmsg("could not open control file \"%s\": %m",
                        XLOG_CONTROL_FILE)));
 
-   if (read(fd, ControlFile, sizeof(ControlFileData)) != sizeof(ControlFileData))
-       ereport(PANIC,
-               (errcode_for_file_access(),
-                errmsg("could not read from control file: %m")));
+   r = read(fd, ControlFile, sizeof(ControlFileData));
+   if (r != sizeof(ControlFileData))
+   {
+       if (r < 0)
+           ereport(PANIC,
+                   (errcode_for_file_access(),
+                    errmsg("could not read from control file: %m")));
+       else
+           ereport(PANIC,
+                    (errmsg("could not read from control file: read %d bytes, expected %d", r, (int) sizeof(ControlFileData))));
+   }
 
    close(fd);
 
index 5592fe7039c92bbf875b5e24d4b886f730fe2ba1..598df059e7d239fef3afa79c3d52dbf5ce8dd94a 100644 (file)
@@ -41,6 +41,7 @@ get_controlfile(char *DataDir, const char *progname)
    int         fd;
    char        ControlFilePath[MAXPGPATH];
    pg_crc32c   crc;
+   int         r;
 
    ControlFile = palloc(sizeof(ControlFileData));
    snprintf(ControlFilePath, MAXPGPATH, "%s/global/pg_control", DataDir);
@@ -59,18 +60,34 @@ get_controlfile(char *DataDir, const char *progname)
    }
 #endif
 
-   if (read(fd, ControlFile, sizeof(ControlFileData)) != sizeof(ControlFileData))
+   r = read(fd, ControlFile, sizeof(ControlFileData));
+   if (r != sizeof(ControlFileData))
+   {
+       if (r < 0)
 #ifndef FRONTEND
-       ereport(ERROR,
-               (errcode_for_file_access(),
-                errmsg("could not read file \"%s\": %m", ControlFilePath)));
+           ereport(ERROR,
+                   (errcode_for_file_access(),
+                    errmsg("could not read file \"%s\": %m", ControlFilePath)));
 #else
-   {
-       fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
-               progname, ControlFilePath, strerror(errno));
-       exit(EXIT_FAILURE);
-   }
+       {
+           fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
+                   progname, ControlFilePath, strerror(errno));
+           exit(EXIT_FAILURE);
+       }
 #endif
+       else
+#ifndef FRONTEND
+           ereport(ERROR,
+                   (errmsg("could not read file \"%s\": read %d bytes, expected %d",
+                           ControlFilePath, r, (int) sizeof(ControlFileData))));
+#else
+       {
+           fprintf(stderr, _("%s: could not read file \"%s\": read %d bytes, expected %d\n"),
+                   progname, ControlFilePath, r, (int) sizeof(ControlFileData));
+           exit(EXIT_FAILURE);
+       }
+#endif
+   }
 
    close(fd);