Skip to content

Commit 46ea3a5

Browse files
committed
Improve stability when backing up files.
1 parent 634a0e2 commit 46ea3a5

File tree

1 file changed

+2
-8
lines changed

1 file changed

+2
-8
lines changed

file_writer.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,18 @@ func (w *fileWriter) swap() {
117117
// We need to make sure that the log file is written to disk before closing the file.
118118
if err := w.fd.Sync(); err != nil {
119119
internal.EchoError("Failed to sync log file %s: %s.", w.name, err)
120-
return
121120
}
122121
// On the windows platform, the file must be closed before renaming the file.
123122
// We need to ignore the error that the file is closed.
124123
if err := w.fd.Close(); err != nil {
125-
e, ok := err.(*os.PathError)
126-
if !ok || e.Err != os.ErrClosed {
127-
internal.EchoError("Failed to close log file %s: %s.", w.name, err)
128-
return
129-
}
124+
internal.EchoError("Failed to close log file %s: %s.", w.name, err)
130125
}
131126
// Accurate to the nanosecond, it maximizes the assurance that file names are not duplicated.
132127
suffix := time.Now().Format("20060102150405.000000000")
133128
if err := os.Rename(w.name, w.name+"."+suffix); err != nil {
134129
internal.EchoError("Failed to rename log file %s to %s.%s: %s.", w.name, w.name, suffix, err)
135-
return
136130
}
137-
fd, err := os.OpenFile(w.name, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
131+
fd, err := os.OpenFile(w.name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
138132
if err != nil {
139133
internal.EchoError("Failed to open log file %s: %s.", w.name, err)
140134
return

0 commit comments

Comments
 (0)