Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit61c65cc

Browse files
committed
When performing a base backup, check for read errors.
The old code didn't differentiate between a read error and aconcurrent truncation. fread reports both of these by returning 0;you have to use feof() or ferror() to distinguish between them,which this code did not do.It might be a better idea to use read() rather than fread() here,so that we can display a less-generic error message, but I'm notsure that would qualify as a back-patchable bug fix, so just dothis much for now.Jeevan Chalke, reviewed by Jeevan Ladhe and by me.Discussion:http://postgr.es/m/CA+TgmobG4ywMzL5oQq2a8YKp8x2p3p1LOMMcGqpS7aekT9+ETA@mail.gmail.com
1 parentaf28744 commit61c65cc

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

‎src/backend/replication/basebackup.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ static char *statrelpath = NULL;
9090
*/
9191
#defineTHROTTLING_FREQUENCY8
9292

93+
/*
94+
* Checks whether we encountered any error in fread(). fread() doesn't give
95+
* any clue what has happened, so we check with ferror(). Also, neither
96+
* fread() nor ferror() set errno, so we just throw a generic error.
97+
*/
98+
#defineCHECK_FREAD_ERROR(fp,filename) \
99+
do { \
100+
if (ferror(fp)) \
101+
ereport(ERROR, \
102+
(errmsg("could not read from file \"%s\"", filename))); \
103+
} while (0)
104+
93105
/* The actual number of bytes, transfer of which may cause sleep. */
94106
staticuint64throttling_sample;
95107

@@ -550,6 +562,8 @@ perform_base_backup(basebackup_options *opt)
550562
break;
551563
}
552564

565+
CHECK_FREAD_ERROR(fp,pathbuf);
566+
553567
if (len!=wal_segment_size)
554568
{
555569
CheckXLogRemoved(segno,tli);
@@ -1487,6 +1501,20 @@ sendFile(const char *readfilename, const char *tarfilename, struct stat *statbuf
14871501

14881502
if (fread(buf+BLCKSZ*i,1,BLCKSZ,fp)!=BLCKSZ)
14891503
{
1504+
/*
1505+
* If we hit end-of-file, a concurrent
1506+
* truncation must have occurred, so break out
1507+
* of this loop just as if the initial fread()
1508+
* returned 0. We'll drop through to the same
1509+
* code that handles that case. (We must fix
1510+
* up cnt first, though.)
1511+
*/
1512+
if (feof(fp))
1513+
{
1514+
cnt=BLCKSZ*i;
1515+
break;
1516+
}
1517+
14901518
ereport(ERROR,
14911519
(errcode_for_file_access(),
14921520
errmsg("could not reread block %d of file \"%s\": %m",
@@ -1538,7 +1566,7 @@ sendFile(const char *readfilename, const char *tarfilename, struct stat *statbuf
15381566
len+=cnt;
15391567
throttle(cnt);
15401568

1541-
if (len >=statbuf->st_size)
1569+
if (feof(fp)||len >=statbuf->st_size)
15421570
{
15431571
/*
15441572
* Reached end of file. The file could be longer, if it was
@@ -1549,6 +1577,8 @@ sendFile(const char *readfilename, const char *tarfilename, struct stat *statbuf
15491577
}
15501578
}
15511579

1580+
CHECK_FREAD_ERROR(fp,readfilename);
1581+
15521582
/* If the file was truncated while we were sending it, pad it with zeros */
15531583
if (len<statbuf->st_size)
15541584
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp