Skip to content

Commit e330230

Browse files
committed
PGPRO-1184: unlink WAL file on error
1 parent c5e4259 commit e330230

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

src/data.c

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -733,17 +733,25 @@ get_gz_error(gzFile gzf)
733733
* Copy file attributes
734734
*/
735735
static void
736-
copy_meta(const char *from_path, const char *to_path)
736+
copy_meta(const char *from_path, const char *to_path, bool unlink_on_error)
737737
{
738738
struct stat st;
739739

740740
if (stat(from_path, &st) == -1)
741+
{
742+
if (unlink_on_error)
743+
unlink(to_path);
741744
elog(ERROR, "Cannot stat file \"%s\": %s",
742745
from_path, strerror(errno));
746+
}
743747

744748
if (chmod(to_path, st.st_mode) == -1)
749+
{
750+
if (unlink_on_error)
751+
unlink(to_path);
745752
elog(ERROR, "Cannot change mode of file \"%s\": %s",
746753
to_path, strerror(errno));
754+
}
747755
}
748756

749757
/*
@@ -797,24 +805,33 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress)
797805
read_len = fread(buf, 1, sizeof(buf), in);
798806

799807
if (ferror(in))
808+
{
809+
unlink(to_path_p);
800810
elog(ERROR, "Cannot read source WAL file \"%s\": %s",
801811
from_path, strerror(errno));
812+
}
802813

803814
if (read_len > 0)
804815
{
805816
#ifdef HAVE_LIBZ
806817
if (is_compress)
807818
{
808819
if (gzwrite(gz_out, buf, read_len) != read_len)
820+
{
821+
unlink(to_path_p);
809822
elog(ERROR, "Cannot write to compressed WAL file \"%s\": %s",
810823
gz_to_path, get_gz_error(gz_out));
824+
}
811825
}
812826
else
813827
#endif
814828
{
815829
if (fwrite(buf, 1, read_len, out) != read_len)
830+
{
831+
unlink(to_path_p);
816832
elog(ERROR, "Cannot write to WAL file \"%s\": %s",
817833
to_path, strerror(errno));
834+
}
818835
}
819836
}
820837

@@ -826,8 +843,11 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress)
826843
if (is_compress)
827844
{
828845
if (gzclose(gz_out) != 0)
846+
{
847+
unlink(to_path_p);
829848
elog(ERROR, "Cannot close compressed WAL file \"%s\": %s",
830849
gz_to_path, get_gz_error(gz_out));
850+
}
831851
elog(INFO, "WAL file compressed to \"%s\"", gz_to_path);
832852
}
833853
else
@@ -836,16 +856,22 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress)
836856
if (fflush(out) != 0 ||
837857
fsync(fileno(out)) != 0 ||
838858
fclose(out))
859+
{
860+
unlink(to_path_p);
839861
elog(ERROR, "Cannot write WAL file \"%s\": %s",
840862
to_path, strerror(errno));
863+
}
841864
}
842865

843866
if (fclose(in))
867+
{
868+
unlink(to_path_p);
844869
elog(ERROR, "Cannot close source WAL file \"%s\": %s",
845870
from_path, strerror(errno));
871+
}
846872

847873
/* update file permission. */
848-
copy_meta(from_path, to_path_p);
874+
copy_meta(from_path, to_path_p, true);
849875
}
850876

851877
/*
@@ -916,23 +942,32 @@ get_wal_file(const char *from_path, const char *to_path)
916942
{
917943
read_len = gzread(gz_in, buf, sizeof(buf));
918944
if (read_len != sizeof(buf) && !gzeof(gz_in))
945+
{
946+
unlink(to_path);
919947
elog(ERROR, "Cannot read compressed WAL file \"%s\": %s",
920948
gz_from_path, get_gz_error(gz_in));
949+
}
921950
}
922951
else
923952
#endif
924953
{
925954
read_len = fread(buf, 1, sizeof(buf), in);
926955
if (ferror(in))
956+
{
957+
unlink(to_path);
927958
elog(ERROR, "Cannot read source WAL file \"%s\": %s",
928959
from_path, strerror(errno));
960+
}
929961
}
930962

931963
if (read_len > 0)
932964
{
933965
if (fwrite(buf, 1, read_len, out) != read_len)
966+
{
967+
unlink(to_path);
934968
elog(ERROR, "Cannot write to WAL file \"%s\": %s", to_path,
935969
strerror(errno));
970+
}
936971
}
937972

938973
/* Check for EOF */
@@ -953,27 +988,36 @@ get_wal_file(const char *from_path, const char *to_path)
953988
if (fflush(out) != 0 ||
954989
fsync(fileno(out)) != 0 ||
955990
fclose(out))
991+
{
992+
unlink(to_path);
956993
elog(ERROR, "Cannot write WAL file \"%s\": %s",
957994
to_path, strerror(errno));
995+
}
958996

959997
#ifdef HAVE_LIBZ
960998
if (is_decompress)
961999
{
9621000
if (gzclose(gz_in) != 0)
1001+
{
1002+
unlink(to_path);
9631003
elog(ERROR, "Cannot close compressed WAL file \"%s\": %s",
9641004
gz_from_path, get_gz_error(gz_in));
1005+
}
9651006
elog(INFO, "WAL file decompressed from \"%s\"", gz_from_path);
9661007
}
9671008
else
9681009
#endif
9691010
{
9701011
if (fclose(in))
1012+
{
1013+
unlink(to_path);
9711014
elog(ERROR, "Cannot close source WAL file \"%s\": %s",
9721015
from_path, strerror(errno));
1016+
}
9731017
}
9741018

9751019
/* update file permission. */
976-
copy_meta(from_path_p, to_path);
1020+
copy_meta(from_path_p, to_path, true);
9771021
}
9781022

9791023
/*

0 commit comments

Comments
 (0)