lib/osutil: Don't remove before rename on Windows (ref #6493) (#6495)

This was needed in ancient times but not currently.
This commit is contained in:
Jakob Borg 2020-04-04 14:17:16 +02:00 committed by GitHub
parent e1324a0e23
commit 7505ea79a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 12 deletions

View File

@ -9,7 +9,6 @@ package osutil
import (
"errors"
"path/filepath"
"runtime"
"github.com/syncthing/syncthing/lib/fs"
)
@ -93,16 +92,6 @@ func (w *AtomicWriter) Close() error {
return err
}
// Remove the destination file, on Windows only. If it fails, and not due
// to the file not existing, we won't be able to complete the rename
// either. Return this error because it may be more informative. On non-
// Windows we want the atomic rename behavior so we don't attempt remove.
if runtime.GOOS == "windows" {
if err := w.fs.Remove(w.path); err != nil && !fs.IsNotExist(err) {
return err
}
}
if err := w.fs.Rename(w.next.Name(), w.path); err != nil {
w.err = err
return err

View File

@ -52,6 +52,15 @@ func TestCreateAtomicCreate(t *testing.T) {
}
func TestCreateAtomicReplace(t *testing.T) {
testCreateAtomicReplace(t, 0644)
}
func TestCreateAtomicReplaceReadOnly(t *testing.T) {
testCreateAtomicReplace(t, 0400)
}
func testCreateAtomicReplace(t *testing.T, oldPerms os.FileMode) {
t.Helper()
os.RemoveAll("testdata")
defer os.RemoveAll("testdata")
@ -59,7 +68,7 @@ func TestCreateAtomicReplace(t *testing.T) {
t.Fatal(err)
}
if err := ioutil.WriteFile("testdata/file", []byte("some old data"), 0644); err != nil {
if err := ioutil.WriteFile("testdata/file", []byte("some old data"), oldPerms); err != nil {
t.Fatal(err)
}