Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/bin/pg_basebackup/pg_basebackup.c
Original file line number Diff line number Diff line change
Expand Up @@ -1426,12 +1426,17 @@ ReceiveArchiveStreamChunk(size_t r, char *copybuf, void *callback_data)
if (fwrite(copybuf + 1, r - 1, 1,
state->manifest_file) != 1)
{
int save_errno = errno;

/*
* If fwrite() didn't set errno, assume that the
* problem is that we're out of disk space.
*/
if (errno == 0)
errno = ENOSPC;
if (save_errno == 0)
save_errno = ENOSPC;
fclose(state->manifest_file);
state->manifest_file = NULL;
errno = save_errno;
pg_fatal("could not write to file \"%s\": %m",
state->manifest_filename);
}
Expand Down Expand Up @@ -1723,9 +1728,13 @@ ReceiveBackupManifestChunk(size_t r, char *copybuf, void *callback_data)
errno = 0;
if (fwrite(copybuf, r, 1, state->file) != 1)
{
int save_errno = errno;

/* if write didn't set errno, assume problem is no disk space */
if (errno == 0)
errno = ENOSPC;
if (save_errno == 0)
save_errno = ENOSPC;
fclose(state->file);
errno = save_errno;
pg_fatal("could not write to file \"%s\": %m", state->filename);
}
}
Expand Down
18 changes: 12 additions & 6 deletions src/bin/pg_basebackup/pg_createsubscriber.c
Original file line number Diff line number Diff line change
Expand Up @@ -1383,15 +1383,21 @@ setup_recovery(const struct LogicalRepInfo *dbinfo, const char *datadir, const c
/* Write the recovery parameters to INCLUDED_CONF_FILE */
snprintf(conf_filename, MAXPGPATH, "%s/%s", datadir,
INCLUDED_CONF_FILE);
fd = fopen(conf_filename, "w");
if (fd == NULL)
pg_fatal("could not open file \"%s\": %m", conf_filename);
fd = fopen(conf_filename, "w");
if (fd == NULL)
pg_fatal("could not open file \"%s\": %m", conf_filename);

if (fwrite(recoveryconfcontents->data, recoveryconfcontents->len, 1, fd) != 1)
pg_fatal("could not write to file \"%s\": %m", conf_filename);
if (fwrite(recoveryconfcontents->data, recoveryconfcontents->len, 1, fd) != 1)
{
int save_errno = errno;

fclose(fd);
recovery_params_set = true;
errno = save_errno;
pg_fatal("could not write to file \"%s\": %m", conf_filename);
}

fclose(fd);
recovery_params_set = true;

/* Include conditionally the recovery parameters. */
resetPQExpBuffer(recoveryconfcontents);
Expand Down
20 changes: 13 additions & 7 deletions src/bin/pg_waldump/pg_waldump.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,15 +527,21 @@ XLogRecordSaveFPWs(XLogReaderState *record, const char *savepath)
LSN_FORMAT_ARGS(record->ReadRecPtr),
rnode.spcOid, rnode.dbOid, rnode.relNumber, blk, forkname);

file = fopen(filename, PG_BINARY_W);
if (!file)
pg_fatal("could not open file \"%s\": %m", filename);
file = fopen(filename, PG_BINARY_W);
if (!file)
pg_fatal("could not open file \"%s\": %m", filename);

if (fwrite(page, BLCKSZ, 1, file) != 1)
pg_fatal("could not write file \"%s\": %m", filename);
if (fwrite(page, BLCKSZ, 1, file) != 1)
{
int save_errno = errno;

fclose(file);
errno = save_errno;
pg_fatal("could not write file \"%s\": %m", filename);
}

if (fclose(file) != 0)
pg_fatal("could not close file \"%s\": %m", filename);
if (fclose(file) != 0)
pg_fatal("could not close file \"%s\": %m", filename);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/bin/pgbench/pgbench.c
Original file line number Diff line number Diff line change
Expand Up @@ -6185,7 +6185,14 @@ process_file(const char *filename, int weight)
buf = read_file_contents(fd);

if (ferror(fd))
{
int save_errno = errno;

if (fd != stdin)
fclose(fd);
errno = save_errno;
pg_fatal("could not read file \"%s\": %m", filename);
}

if (fd != stdin)
fclose(fd);
Expand Down
9 changes: 7 additions & 2 deletions src/fe_utils/astreamer_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,14 @@ astreamer_extractor_content(astreamer *streamer, astreamer_member *member,
errno = 0;
if (len > 0 && fwrite(data, len, 1, mystreamer->file) != 1)
{
int save_errno = errno;

/* if write didn't set errno, assume problem is no disk space */
if (errno == 0)
errno = ENOSPC;
if (save_errno == 0)
save_errno = ENOSPC;
fclose(mystreamer->file);
mystreamer->file = NULL;
errno = save_errno;
pg_fatal("could not write to file \"%s\": %m",
mystreamer->filename);
}
Expand Down