Skip to content

Commit

Permalink
safeio: Preserve file ownership
Browse files Browse the repository at this point in the history
This patch makes safeio preserve file ownership. This is specially
useful when using command-line utilities as root, but the files they
change are owned by a different user.
  • Loading branch information
albertito committed Oct 21, 2016
1 parent ac7f32c commit cda11e0
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion internal/safeio/safeio.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path"
"syscall"
)

// WriteFile writes data to a file named by filename, atomically.
Expand All @@ -24,12 +25,20 @@ func WriteFile(filename string, data []byte, perm os.FileMode) error {
return err
}

if err = os.Chmod(tmpf.Name(), perm); err != nil {
if err = tmpf.Chmod(perm); err != nil {
tmpf.Close()
os.Remove(tmpf.Name())
return err
}

if uid, gid := getOwner(filename); uid >= 0 {
if err = tmpf.Chown(uid, gid); err != nil {
tmpf.Close()
os.Remove(tmpf.Name())
return err
}
}

if _, err = tmpf.Write(data); err != nil {
tmpf.Close()
os.Remove(tmpf.Name())
Expand All @@ -43,3 +52,17 @@ func WriteFile(filename string, data []byte, perm os.FileMode) error {

return os.Rename(tmpf.Name(), filename)
}

func getOwner(fname string) (uid, gid int) {
uid = -1
gid = -1
stat, err := os.Stat(fname)
if err == nil {
if sysstat, ok := stat.Sys().(*syscall.Stat_t); ok {
uid = int(sysstat.Uid)
gid = int(sysstat.Gid)
}
}

return
}

0 comments on commit cda11e0

Please sign in to comment.