@@ -1102,7 +1102,7 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
1102
1102
bool overwrite )
1103
1103
{
1104
1104
FILE * in = NULL ;
1105
- FILE * out = NULL ;
1105
+ int out ;
1106
1106
char buf [XLOG_BLCKSZ ];
1107
1107
const char * to_path_p ;
1108
1108
char to_path_temp [MAXPGPATH ];
@@ -1142,7 +1142,13 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
1142
1142
{
1143
1143
snprintf (to_path_temp , sizeof (to_path_temp ), "%s.partial" , gz_to_path );
1144
1144
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 );
1146
1152
if (gzsetparams (gz_out , instance_config .compress_level , Z_DEFAULT_STRATEGY ) != Z_OK )
1147
1153
elog (ERROR , "Cannot set compression level %d to file \"%s\": %s" ,
1148
1154
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,
1153
1159
{
1154
1160
snprintf (to_path_temp , sizeof (to_path_temp ), "%s.partial" , to_path );
1155
1161
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" ,
1159
1166
to_path_temp , strerror (errno ));
1160
1167
}
1161
1168
@@ -1191,7 +1198,7 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
1191
1198
else
1192
1199
#endif
1193
1200
{
1194
- if (fwrite ( buf , 1 , read_len , out ) != read_len )
1201
+ if (write ( out , buf , read_len ) != read_len )
1195
1202
{
1196
1203
errno_temp = errno ;
1197
1204
unlink (to_path_temp );
@@ -1219,9 +1226,7 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
1219
1226
else
1220
1227
#endif
1221
1228
{
1222
- if (fflush (out ) != 0 ||
1223
- fsync (fileno (out )) != 0 ||
1224
- fclose (out ))
1229
+ if (fsync (out ) != 0 || close (out ) != 0 )
1225
1230
{
1226
1231
errno_temp = errno ;
1227
1232
unlink (to_path_temp );
@@ -1262,7 +1267,7 @@ void
1262
1267
get_wal_file (const char * from_path , const char * to_path )
1263
1268
{
1264
1269
FILE * in = NULL ;
1265
- FILE * out ;
1270
+ int out ;
1266
1271
char buf [XLOG_BLCKSZ ];
1267
1272
const char * from_path_p = from_path ;
1268
1273
char to_path_temp [MAXPGPATH ];
@@ -1312,10 +1317,11 @@ get_wal_file(const char *from_path, const char *to_path)
1312
1317
/* open backup file for write */
1313
1318
snprintf (to_path_temp , sizeof (to_path_temp ), "%s.partial" , to_path );
1314
1319
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 ));
1319
1325
1320
1326
/* copy content */
1321
1327
for (;;)
@@ -1349,7 +1355,7 @@ get_wal_file(const char *from_path, const char *to_path)
1349
1355
1350
1356
if (read_len > 0 )
1351
1357
{
1352
- if (fwrite ( buf , 1 , read_len , out ) != read_len )
1358
+ if (write ( out , buf , read_len ) != read_len )
1353
1359
{
1354
1360
errno_temp = errno ;
1355
1361
unlink (to_path_temp );
@@ -1373,9 +1379,7 @@ get_wal_file(const char *from_path, const char *to_path)
1373
1379
}
1374
1380
}
1375
1381
1376
- if (fflush (out ) != 0 ||
1377
- fsync (fileno (out )) != 0 ||
1378
- fclose (out ))
1382
+ if (fsync (out ) != 0 || close (out ) != 0 )
1379
1383
{
1380
1384
errno_temp = errno ;
1381
1385
unlink (to_path_temp );
0 commit comments