WriteAtomic kept a file's mode but not its group: the temp file renamed over it takes its folder's group, so a config shared with a group (Aside's group-writable u/1/models.json, group wheel, on #1499) could end up in the user's own group and the other writers lose it. The temp file is now given the old file's group before the rename, best effort (a group the user isn't in is left as made). Windows has no Unix group; its keepGroup does nothing. Run: go test ./internal/edit (TestWriteAtomicKeepsGroupAndMode passes; with keepGroup commented out it fails with the folder's group); gofmt -l; GOOS=linux and GOOS=windows go vet -tags nogui ./internal/edit.
38 lines
950 B
Go
38 lines
950 B
Go
//go:build !windows
|
|
|
|
package edit
|
|
|
|
import (
|
|
"os"
|
|
"syscall"
|
|
)
|
|
|
|
// hardLinked reports whether the file at path has other names too. Where
|
|
// the system gives no link count it is taken to have none.
|
|
func hardLinked(path string) bool {
|
|
fi, err := os.Stat(path)
|
|
if err != nil || !fi.Mode().IsRegular() {
|
|
return false
|
|
}
|
|
st, ok := fi.Sys().(*syscall.Stat_t)
|
|
return ok && st.Nlink > 1
|
|
}
|
|
|
|
// keepGroup gives the temp file that replaces the file at st the group
|
|
// that file had: a new file takes its folder's group, so a config shared
|
|
// with a group (Aside's group-writable models.json, #1499) would lose it on
|
|
// the rename. A group the user can't give is left as it was made.
|
|
func keepGroup(tmp *os.File, st os.FileInfo) {
|
|
old, ok := st.Sys().(*syscall.Stat_t)
|
|
if !ok {
|
|
return
|
|
}
|
|
now, err := tmp.Stat()
|
|
if err != nil {
|
|
return
|
|
}
|
|
if cur, ok := now.Sys().(*syscall.Stat_t); ok && cur.Gid != old.Gid {
|
|
_ = tmp.Chown(-1, int(old.Gid))
|
|
}
|
|
}
|