Skip to content

Commit

Permalink
fixing changing of folder ownership on macOS. Using chown instead to …
Browse files Browse the repository at this point in the history
…be more reliable
  • Loading branch information
reubenmiller committed Apr 12, 2021
1 parent c06bb33 commit 4893f9b
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions pkg/fileutilities/fileutilities.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package fileutilities

import (
"fmt"
"os"
"os/user"
"os/exec"
"runtime"
"strconv"
)

// CreateDirs create directory. All non-existing nested paths will be created.
Expand All @@ -16,24 +16,22 @@ func CreateDirs(p string) error {

// Change file ownership
if runtime.GOOS != "windows" {
var uid, gid int
owner := ""
sudoUser := os.Getenv("SUDO_USER")
if os.Geteuid() == 0 && sudoUser != "" {
currentUser, err := user.Lookup(sudoUser)

if err != nil {
return err
}

uid, _ = strconv.Atoi(currentUser.Uid)
gid, _ = strconv.Atoi(currentUser.Gid)

if os.Geteuid() == 0 && sudoUser != "" {
owner = sudoUser
} else {
uid = os.Getuid()
gid = os.Getgid()
owner = fmt.Sprintf("%d", os.Getuid())
}
if err := os.Chown(p, uid, gid); err != nil {
return err

// Note: os.Chown can't be used as os/user.Lookup is not reliable on macOS
// golang bug: https://github.com/golang/go/issues/24383
cmd := exec.Command("chown", "-R", "-L", owner, p)

b, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error change owner of dir %s to %s: %w %s", p, owner, err, b)
}
}
return nil
Expand Down

0 comments on commit 4893f9b

Please sign in to comment.