So, in a funny plot twist, it turns out that WriteFile in Go 1.13 doesn't actually set the read only bit on Windows when called with permissions 0444 so my test was broken. With an improved test it turns out that Rename does not, in fact, overwrite a read-only file on Windows. This adds a fix for that. (Rename might get improved in Go 1.15...)
This commit is contained in:
+10
-1
@@ -9,6 +9,7 @@ package osutil
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/fs"
|
||||
)
|
||||
@@ -92,7 +93,15 @@ func (w *AtomicWriter) Close() error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := w.fs.Rename(w.next.Name(), w.path); err != nil {
|
||||
err := w.fs.Rename(w.next.Name(), w.path)
|
||||
if runtime.GOOS == "windows" && fs.IsPermission(err) {
|
||||
// On Windows, we might not be allowed to rename over the file
|
||||
// because it's read-only. Get us some write permissions and try
|
||||
// again.
|
||||
_ = w.fs.Chmod(w.path, 0644)
|
||||
err = w.fs.Rename(w.next.Name(), w.path)
|
||||
}
|
||||
if err != nil {
|
||||
w.err = err
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user