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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

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 {

View File

@ -110,4 +110,10 @@ func testCreateAtomicReplace(t *testing.T, oldPerms os.FileMode) {
if !bytes.Equal(bs, []byte("hello")) {
t.Error("incorrect data")
}
if info, err := os.Stat(testfile); err != nil {
t.Fatal(err)
} else if info.Mode() != oldPerms {
t.Fatalf("Perms changed during atomic write: 0%o", info.Mode())
}
}