lib/osutil: Preserve perms in AtomicWriter (fixes #tbd) (#6885)

This commit is contained in:
Simon Frei
2020-08-10 18:48:37 +02:00
committed by GitHub
parent f2f1a28206
commit fd8bea6179
2 changed files with 18 additions and 1 deletions
+12 -1
View File
@@ -93,7 +93,12 @@ func (w *AtomicWriter) Close() error {
return err
}
err := w.fs.Rename(w.next.Name(), w.path)
info, err := w.fs.Lstat(w.path)
if err != nil && !fs.IsNotExist(err) {
w.err = err
return err
}
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
@@ -105,6 +110,12 @@ func (w *AtomicWriter) Close() error {
w.err = err
return err
}
if info != nil {
if err := w.fs.Chmod(w.path, info.Mode()); err != nil {
w.err = err
return err
}
}
// fsync the directory too
if fd, err := w.fs.Open(filepath.Dir(w.next.Name())); err == nil {