@@ -716,14 +716,14 @@ copy_file(const char *from_root, const char *to_root, pgFile *file)
716
716
* Show error during work with compressed file
717
717
*/
718
718
static const char *
719
- get_gz_error (gzFile gzf )
719
+ get_gz_error (gzFile gzf , int errnum )
720
720
{
721
- int errnum ;
721
+ int gz_errnum ;
722
722
const char * errmsg ;
723
723
724
- errmsg = gzerror (gzf , & errnum );
725
- if (errnum == Z_ERRNO )
726
- return strerror (errno );
724
+ errmsg = gzerror (gzf , & gz_errnum );
725
+ if (gz_errnum == Z_ERRNO )
726
+ return strerror (errnum );
727
727
else
728
728
return errmsg ;
729
729
}
@@ -765,6 +765,8 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
765
765
FILE * out ;
766
766
char buf [XLOG_BLCKSZ ];
767
767
const char * to_path_p = to_path ;
768
+ char to_path_temp [MAXPGPATH ];
769
+ int errno_temp ;
768
770
769
771
#ifdef HAVE_LIBZ
770
772
char gz_to_path [MAXPGPATH ];
@@ -786,10 +788,12 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
786
788
if (!overwrite && fileExists (gz_to_path ))
787
789
elog (ERROR , "WAL segment \"%s\" already exists." , gz_to_path );
788
790
789
- gz_out = gzopen (gz_to_path , "wb" );
791
+ snprintf (to_path_temp , sizeof (to_path_temp ), "%s.temp" , gz_to_path );
792
+
793
+ gz_out = gzopen (to_path_temp , "wb" );
790
794
if (gzsetparams (gz_out , compress_level , Z_DEFAULT_STRATEGY ) != Z_OK )
791
795
elog (ERROR , "Cannot set compression level %d to file \"%s\": %s" ,
792
- compress_level , gz_to_path , get_gz_error (gz_out ));
796
+ compress_level , to_path_temp , get_gz_error (gz_out , errno ));
793
797
794
798
to_path_p = gz_to_path ;
795
799
}
@@ -799,10 +803,12 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
799
803
if (!overwrite && fileExists (to_path ))
800
804
elog (ERROR , "WAL segment \"%s\" already exists." , to_path );
801
805
802
- out = fopen (to_path , "w" );
806
+ snprintf (to_path_temp , sizeof (to_path_temp ), "%s.temp" , to_path );
807
+
808
+ out = fopen (to_path_temp , "w" );
803
809
if (out == NULL )
804
810
elog (ERROR , "Cannot open destination WAL file \"%s\": %s" ,
805
- to_path , strerror (errno ));
811
+ to_path_temp , strerror (errno ));
806
812
}
807
813
808
814
/* copy content */
@@ -814,9 +820,11 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
814
820
815
821
if (ferror (in ))
816
822
{
817
- unlink (to_path_p );
818
- elog (ERROR , "Cannot read source WAL file \"%s\": %s" ,
819
- from_path , strerror (errno ));
823
+ errno_temp = errno ;
824
+ unlink (to_path_temp );
825
+ elog (ERROR ,
826
+ "Cannot read source WAL file \"%s\": %s" ,
827
+ from_path , strerror (errno_temp ));
820
828
}
821
829
822
830
if (read_len > 0 )
@@ -826,19 +834,21 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
826
834
{
827
835
if (gzwrite (gz_out , buf , read_len ) != read_len )
828
836
{
829
- unlink (to_path_p );
837
+ errno_temp = errno ;
838
+ unlink (to_path_temp );
830
839
elog (ERROR , "Cannot write to compressed WAL file \"%s\": %s" ,
831
- gz_to_path , get_gz_error (gz_out ));
840
+ to_path_temp , get_gz_error (gz_out , errno_temp ));
832
841
}
833
842
}
834
843
else
835
844
#endif
836
845
{
837
846
if (fwrite (buf , 1 , read_len , out ) != read_len )
838
847
{
839
- unlink (to_path_p );
848
+ errno_temp = errno ;
849
+ unlink (to_path_temp );
840
850
elog (ERROR , "Cannot write to WAL file \"%s\": %s" ,
841
- to_path , strerror (errno ));
851
+ to_path_temp , strerror (errno_temp ));
842
852
}
843
853
}
844
854
}
@@ -852,11 +862,11 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
852
862
{
853
863
if (gzclose (gz_out ) != 0 )
854
864
{
855
- unlink (to_path_p );
865
+ errno_temp = errno ;
866
+ unlink (to_path_temp );
856
867
elog (ERROR , "Cannot close compressed WAL file \"%s\": %s" ,
857
- gz_to_path , get_gz_error (gz_out ));
868
+ to_path_temp , get_gz_error (gz_out , errno_temp ));
858
869
}
859
- elog (INFO , "WAL file compressed to \"%s\"" , gz_to_path );
860
870
}
861
871
else
862
872
#endif
@@ -865,21 +875,36 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
865
875
fsync (fileno (out )) != 0 ||
866
876
fclose (out ))
867
877
{
868
- unlink (to_path_p );
878
+ errno_temp = errno ;
879
+ unlink (to_path_temp );
869
880
elog (ERROR , "Cannot write WAL file \"%s\": %s" ,
870
- to_path , strerror (errno ));
881
+ to_path_temp , strerror (errno_temp ));
871
882
}
872
883
}
873
884
874
885
if (fclose (in ))
875
886
{
876
- unlink (to_path_p );
887
+ errno_temp = errno ;
888
+ unlink (to_path_temp );
877
889
elog (ERROR , "Cannot close source WAL file \"%s\": %s" ,
878
- from_path , strerror (errno ));
890
+ from_path , strerror (errno_temp ));
879
891
}
880
892
881
893
/* update file permission. */
882
- copy_meta (from_path , to_path_p , true);
894
+ copy_meta (from_path , to_path_temp , true);
895
+
896
+ if (rename (to_path_temp , to_path_p ) < 0 )
897
+ {
898
+ errno_temp = errno ;
899
+ unlink (to_path_temp );
900
+ elog (ERROR , "Cannot rename WAL file \"%s\" to \"%s\": %s" ,
901
+ to_path_temp , to_path_p , strerror (errno_temp ));
902
+ }
903
+
904
+ #ifdef HAVE_LIBZ
905
+ if (is_compress )
906
+ elog (INFO , "WAL file compressed to \"%s\"" , gz_to_path );
907
+ #endif
883
908
}
884
909
885
910
/*
@@ -892,6 +917,8 @@ get_wal_file(const char *from_path, const char *to_path)
892
917
FILE * out ;
893
918
char buf [XLOG_BLCKSZ ];
894
919
const char * from_path_p = from_path ;
920
+ char to_path_temp [MAXPGPATH ];
921
+ int errno_temp ;
895
922
bool is_decompress = false;
896
923
897
924
#ifdef HAVE_LIBZ
@@ -935,10 +962,12 @@ get_wal_file(const char *from_path, const char *to_path)
935
962
}
936
963
937
964
/* open backup file for write */
938
- out = fopen (to_path , "w" );
965
+ snprintf (to_path_temp , sizeof (to_path_temp ), "%s.temp" , to_path );
966
+
967
+ out = fopen (to_path_temp , "w" );
939
968
if (out == NULL )
940
969
elog (ERROR , "Cannot open destination WAL file \"%s\": %s" ,
941
- to_path , strerror (errno ));
970
+ to_path_temp , strerror (errno ));
942
971
943
972
/* copy content */
944
973
for (;;)
@@ -951,9 +980,10 @@ get_wal_file(const char *from_path, const char *to_path)
951
980
read_len = gzread (gz_in , buf , sizeof (buf ));
952
981
if (read_len != sizeof (buf ) && !gzeof (gz_in ))
953
982
{
954
- unlink (to_path );
983
+ errno_temp = errno ;
984
+ unlink (to_path_temp );
955
985
elog (ERROR , "Cannot read compressed WAL file \"%s\": %s" ,
956
- gz_from_path , get_gz_error (gz_in ));
986
+ gz_from_path , get_gz_error (gz_in , errno_temp ));
957
987
}
958
988
}
959
989
else
@@ -962,19 +992,21 @@ get_wal_file(const char *from_path, const char *to_path)
962
992
read_len = fread (buf , 1 , sizeof (buf ), in );
963
993
if (ferror (in ))
964
994
{
965
- unlink (to_path );
995
+ errno_temp = errno ;
996
+ unlink (to_path_temp );
966
997
elog (ERROR , "Cannot read source WAL file \"%s\": %s" ,
967
- from_path , strerror (errno ));
998
+ from_path , strerror (errno_temp ));
968
999
}
969
1000
}
970
1001
971
1002
if (read_len > 0 )
972
1003
{
973
1004
if (fwrite (buf , 1 , read_len , out ) != read_len )
974
1005
{
975
- unlink (to_path );
976
- elog (ERROR , "Cannot write to WAL file \"%s\": %s" , to_path ,
977
- strerror (errno ));
1006
+ errno_temp = errno ;
1007
+ unlink (to_path_temp );
1008
+ elog (ERROR , "Cannot write to WAL file \"%s\": %s" , to_path_temp ,
1009
+ strerror (errno_temp ));
978
1010
}
979
1011
}
980
1012
@@ -997,35 +1029,50 @@ get_wal_file(const char *from_path, const char *to_path)
997
1029
fsync (fileno (out )) != 0 ||
998
1030
fclose (out ))
999
1031
{
1000
- unlink (to_path );
1032
+ errno_temp = errno ;
1033
+ unlink (to_path_temp );
1001
1034
elog (ERROR , "Cannot write WAL file \"%s\": %s" ,
1002
- to_path , strerror (errno ));
1035
+ to_path_temp , strerror (errno_temp ));
1003
1036
}
1004
1037
1005
1038
#ifdef HAVE_LIBZ
1006
1039
if (is_decompress )
1007
1040
{
1008
1041
if (gzclose (gz_in ) != 0 )
1009
1042
{
1010
- unlink (to_path );
1043
+ errno_temp = errno ;
1044
+ unlink (to_path_temp );
1011
1045
elog (ERROR , "Cannot close compressed WAL file \"%s\": %s" ,
1012
- gz_from_path , get_gz_error (gz_in ));
1046
+ gz_from_path , get_gz_error (gz_in , errno_temp ));
1013
1047
}
1014
- elog (INFO , "WAL file decompressed from \"%s\"" , gz_from_path );
1015
1048
}
1016
1049
else
1017
1050
#endif
1018
1051
{
1019
1052
if (fclose (in ))
1020
1053
{
1021
- unlink (to_path );
1054
+ errno_temp = errno ;
1055
+ unlink (to_path_temp );
1022
1056
elog (ERROR , "Cannot close source WAL file \"%s\": %s" ,
1023
- from_path , strerror (errno ));
1057
+ from_path , strerror (errno_temp ));
1024
1058
}
1025
1059
}
1026
1060
1027
1061
/* update file permission. */
1028
- copy_meta (from_path_p , to_path , true);
1062
+ copy_meta (from_path_p , to_path_temp , true);
1063
+
1064
+ if (rename (to_path_temp , to_path ) < 0 )
1065
+ {
1066
+ errno_temp = errno ;
1067
+ unlink (to_path_temp );
1068
+ elog (ERROR , "Cannot rename WAL file \"%s\" to \"%s\": %s" ,
1069
+ to_path_temp , to_path , strerror (errno_temp ));
1070
+ }
1071
+
1072
+ #ifdef HAVE_LIBZ
1073
+ if (is_decompress )
1074
+ elog (INFO , "WAL file decompressed from \"%s\"" , gz_from_path );
1075
+ #endif
1029
1076
}
1030
1077
1031
1078
/*
0 commit comments