Files

39 lines
723 B
Go

//go:build !windows
package main
import (
"os"
"path/filepath"
"syscall"
)
func preserveAccessOwnership(temp, target string) error {
source := target
info, e := os.Stat(source)
if os.IsNotExist(e) {
source = filepath.Dir(target)
info, e = os.Stat(source)
}
if e != nil {
return e
}
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
current, e := os.Stat(temp)
if e != nil {
return e
}
existing := current.Sys().(*syscall.Stat_t)
if existing.Uid != stat.Uid || existing.Gid != stat.Gid {
if e = os.Chown(temp, int(stat.Uid), int(stat.Gid)); e != nil {
return e
}
}
}
mode := os.FileMode(0600)
if source == target {
mode = info.Mode().Perm()
}
return os.Chmod(temp, mode)
}