Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: preserve ownership and permissions on configfile #2228

Merged
merged 1 commit into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cli/config/configfile/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ func (configFile *ConfigFile) Save() error {
os.Remove(temp.Name())
return err
}
// Try copying the current config file (if any) ownership and permissions
copyFilePermissions(configFile.Filename, temp.Name())

return os.Rename(temp.Name(), configFile.Filename)
}

Expand Down
35 changes: 35 additions & 0 deletions cli/config/configfile/file_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// +build !windows

package configfile

import (
"os"
"syscall"
)

// copyFilePermissions copies file ownership and permissions from "src" to "dst",
// ignoring any error during the process.
func copyFilePermissions(src, dst string) {
var (
mode os.FileMode = 0600
uid, gid int
)

fi, err := os.Stat(src)
if err != nil {
return
}
if fi.Mode().IsRegular() {
mode = fi.Mode()
}
if err := os.Chmod(dst, mode); err != nil {
return
}

uid = int(fi.Sys().(*syscall.Stat_t).Uid)
gid = int(fi.Sys().(*syscall.Stat_t).Gid)

if uid > 0 && gid > 0 {
_ = os.Chown(dst, uid, gid)
}
}
5 changes: 5 additions & 0 deletions cli/config/configfile/file_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package configfile

func copyFilePermissions(src, dst string) {
// TODO implement for Windows
}