Fix some BufFileRead() error reporting
authorPeter Eisentraut
Mon, 16 Jan 2023 08:20:44 +0000 (09:20 +0100)
committerPeter Eisentraut
Mon, 16 Jan 2023 08:48:09 +0000 (09:48 +0100)
Remove "%m" from error messages where errno would be bogus.  Add short
read byte counts where appropriate.

This is equivalent to what was done in
7897e3bb902c557412645b82120f4d95f7474906, but some code was apparently
developed concurrently to that and not updated accordingly.

Reviewed-by: Amit Kapila
Discussion: https://www.postgresql.org/message-id/flat/f3501945-c591-8cc3-5ef0-b72a2e0eaa9c@enterprisedb.com

src/backend/replication/backup_manifest.c
src/backend/replication/logical/worker.c

index 04ca455ace879b0d672abd7f1fb5911095662e2d..2719104487118d5cd0445e74948a1d276e6a32c1 100644 (file)
@@ -377,7 +377,8 @@ SendBackupManifest(backup_manifest_info *manifest)
        if (rc != bytes_to_read)
            ereport(ERROR,
                    (errcode_for_file_access(),
-                    errmsg("could not read from temporary file: %m")));
+                    errmsg("could not read from temporary file: read only %zu of %zu bytes",
+                           rc, bytes_to_read)));
        pq_putmessage('d', manifestbuf, bytes_to_read);
        manifest_bytes_done += bytes_to_read;
    }
index 16a4fe6d909d6d6e0e424d99587fea8c159be877..d1c7d352363a6bf9ef29ae7a0dffa36984c94f12 100644 (file)
@@ -1071,7 +1071,7 @@ apply_handle_stream_commit(StringInfo s)
    nchanges = 0;
    while (true)
    {
-       int         nbytes;
+       size_t      nbytes;
        int         len;
 
        CHECK_FOR_INTERRUPTS();
@@ -1087,8 +1087,8 @@ apply_handle_stream_commit(StringInfo s)
        if (nbytes != sizeof(len))
            ereport(ERROR,
                    (errcode_for_file_access(),
-                    errmsg("could not read from streaming transaction's changes file \"%s\": %m",
-                           path)));
+                    errmsg("could not read from streaming transaction's changes file \"%s\": read only %zu of %zu bytes",
+                           path, nbytes, sizeof(len))));
 
        if (len <= 0)
            elog(ERROR, "incorrect length %d in streaming transaction's changes file \"%s\"",
@@ -1098,11 +1098,12 @@ apply_handle_stream_commit(StringInfo s)
        buffer = repalloc(buffer, len);
 
        /* and finally read the data into the buffer */
-       if (BufFileRead(fd, buffer, len) != len)
+       nbytes = BufFileRead(fd, buffer, len);
+       if (nbytes != len)
            ereport(ERROR,
                    (errcode_for_file_access(),
-                    errmsg("could not read from streaming transaction's changes file \"%s\": %m",
-                           path)));
+                    errmsg("could not read from streaming transaction's changes file \"%s\": read only %zu of %zu bytes",
+                           path, nbytes, (size_t) len)));
 
        /* copy the buffer to the stringinfo and call apply_dispatch */
        resetStringInfo(&s2);
@@ -2710,6 +2711,7 @@ static void
 subxact_info_read(Oid subid, TransactionId xid)
 {
    char        path[MAXPGPATH];
+   size_t      nread;
    Size        len;
    BufFile    *fd;
    StreamXidHash *ent;
@@ -2742,13 +2744,12 @@ subxact_info_read(Oid subid, TransactionId xid)
    fd = BufFileOpenShared(ent->subxact_fileset, path, O_RDONLY);
 
    /* read number of subxact items */
-   if (BufFileRead(fd, &subxact_data.nsubxacts,
-                   sizeof(subxact_data.nsubxacts)) !=
-       sizeof(subxact_data.nsubxacts))
+   nread = BufFileRead(fd, &subxact_data.nsubxacts, sizeof(subxact_data.nsubxacts));
+   if (nread != sizeof(subxact_data.nsubxacts))
        ereport(ERROR,
                (errcode_for_file_access(),
-                errmsg("could not read from streaming transaction's subxact file \"%s\": %m",
-                       path)));
+                errmsg("could not read from streaming transaction's subxact file \"%s\": read only %zu of %zu bytes",
+                       path, nread, sizeof(subxact_data.nsubxacts))));
 
    len = sizeof(SubXactInfo) * subxact_data.nsubxacts;
 
@@ -2766,11 +2767,15 @@ subxact_info_read(Oid subid, TransactionId xid)
                                   sizeof(SubXactInfo));
    MemoryContextSwitchTo(oldctx);
 
-   if ((len > 0) && ((BufFileRead(fd, subxact_data.subxacts, len)) != len))
+   if (len > 0)
+   {
+       nread = BufFileRead(fd, subxact_data.subxacts, len);
+       if (nread != len)
        ereport(ERROR,
                (errcode_for_file_access(),
-                errmsg("could not read from streaming transaction's subxact file \"%s\": %m",
-                       path)));
+                errmsg("could not read from streaming transaction's subxact file \"%s\": read only %zu of %zu bytes",
+                       path, nread, len)));
+   }
 
    BufFileClose(fd);
 }