Skip to content

Commit ef2abc3

Browse files
committed
Write WAL files with O_EXCL during archive-get and archive-push
1 parent 6c7ab25 commit ef2abc3

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

src/data.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
11021102
bool overwrite)
11031103
{
11041104
FILE *in = NULL;
1105-
FILE *out=NULL;
1105+
int out;
11061106
char buf[XLOG_BLCKSZ];
11071107
const char *to_path_p;
11081108
char to_path_temp[MAXPGPATH];
@@ -1142,7 +1142,13 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
11421142
{
11431143
snprintf(to_path_temp, sizeof(to_path_temp), "%s.partial", gz_to_path);
11441144

1145-
gz_out = gzopen(to_path_temp, PG_BINARY_W);
1145+
out = open(to_path_temp, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
1146+
S_IRUSR | S_IWUSR);
1147+
if (out < 0)
1148+
elog(ERROR, "Cannot open destination temporary WAL file \"%s\": %s",
1149+
to_path_temp, strerror(errno));
1150+
1151+
gz_out = gzdopen(out, PG_BINARY_W);
11461152
if (gzsetparams(gz_out, instance_config.compress_level, Z_DEFAULT_STRATEGY) != Z_OK)
11471153
elog(ERROR, "Cannot set compression level %d to file \"%s\": %s",
11481154
instance_config.compress_level, to_path_temp,
@@ -1153,9 +1159,10 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
11531159
{
11541160
snprintf(to_path_temp, sizeof(to_path_temp), "%s.partial", to_path);
11551161

1156-
out = fopen(to_path_temp, PG_BINARY_W);
1157-
if (out == NULL)
1158-
elog(ERROR, "Cannot open destination WAL file \"%s\": %s",
1162+
out = open(to_path_temp, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
1163+
S_IRUSR | S_IWUSR);
1164+
if (out < 0)
1165+
elog(ERROR, "Cannot open destination temporary WAL file \"%s\": %s",
11591166
to_path_temp, strerror(errno));
11601167
}
11611168

@@ -1191,7 +1198,7 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
11911198
else
11921199
#endif
11931200
{
1194-
if (fwrite(buf, 1, read_len, out) != read_len)
1201+
if (write(out, buf, read_len) != read_len)
11951202
{
11961203
errno_temp = errno;
11971204
unlink(to_path_temp);
@@ -1219,9 +1226,7 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
12191226
else
12201227
#endif
12211228
{
1222-
if (fflush(out) != 0 ||
1223-
fsync(fileno(out)) != 0 ||
1224-
fclose(out))
1229+
if (fsync(out) != 0 || close(out) != 0)
12251230
{
12261231
errno_temp = errno;
12271232
unlink(to_path_temp);
@@ -1262,7 +1267,7 @@ void
12621267
get_wal_file(const char *from_path, const char *to_path)
12631268
{
12641269
FILE *in = NULL;
1265-
FILE *out;
1270+
int out;
12661271
char buf[XLOG_BLCKSZ];
12671272
const char *from_path_p = from_path;
12681273
char to_path_temp[MAXPGPATH];
@@ -1312,10 +1317,11 @@ get_wal_file(const char *from_path, const char *to_path)
13121317
/* open backup file for write */
13131318
snprintf(to_path_temp, sizeof(to_path_temp), "%s.partial", to_path);
13141319

1315-
out = fopen(to_path_temp, PG_BINARY_W);
1316-
if (out == NULL)
1317-
elog(ERROR, "Cannot open destination WAL file \"%s\": %s",
1318-
to_path_temp, strerror(errno));
1320+
out = open(to_path_temp, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
1321+
S_IRUSR | S_IWUSR);
1322+
if (out < 0)
1323+
elog(ERROR, "Cannot open destination temporary WAL file \"%s\": %s",
1324+
to_path_temp, strerror(errno));
13191325

13201326
/* copy content */
13211327
for (;;)
@@ -1349,7 +1355,7 @@ get_wal_file(const char *from_path, const char *to_path)
13491355

13501356
if (read_len > 0)
13511357
{
1352-
if (fwrite(buf, 1, read_len, out) != read_len)
1358+
if (write(out, buf, read_len) != read_len)
13531359
{
13541360
errno_temp = errno;
13551361
unlink(to_path_temp);
@@ -1373,9 +1379,7 @@ get_wal_file(const char *from_path, const char *to_path)
13731379
}
13741380
}
13751381

1376-
if (fflush(out) != 0 ||
1377-
fsync(fileno(out)) != 0 ||
1378-
fclose(out))
1382+
if (fsync(out) != 0 || close(out) != 0)
13791383
{
13801384
errno_temp = errno;
13811385
unlink(to_path_temp);

0 commit comments

Comments
 (0)