Skip to content

Commit

Permalink
Add utility function for chown
Browse files Browse the repository at this point in the history
  • Loading branch information
zeenix authored and cfergeau committed Dec 20, 2019
1 parent 2059a3a commit 0863cb2
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/os/util_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"os/exec"
"os/user"
"strings"

"github.com/code-ready/crc/pkg/crc/logging"
Expand Down Expand Up @@ -53,3 +54,14 @@ func GetFirstExistentPath(paths []string) (string, error) {

return readablePath, nil
}

func ChownAsRoot(user *user.User, filepath string) error {
logging.Infof("Will use root access to change owner & group of file %s to %s", filepath, user.Username)
cmd := exec.Command("sudo", "chown", fmt.Sprintf("%s.%s", user.Uid, user.Gid), filepath) // #nosec G204
buf := new(bytes.Buffer)
cmd.Stderr = buf
if err := cmd.Run(); err != nil {
return fmt.Errorf("Failed to change owner & group of %s to %s: %s: %s: %v", filepath, user.Username, buf.String(), err)
}
return nil
}

0 comments on commit 0863cb2

Please sign in to comment.