Skip to content

Commit

Permalink
fix: chown/chmod should shell out on perm error
Browse files Browse the repository at this point in the history
  • Loading branch information
femnad committed Aug 4, 2024
1 parent 3503df8 commit 7354168
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions internal/filecontent.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ func checksum(f string) (string, error) {
}

func Chown(file, user, group string) error {
isHomePath := IsHomePath(file)
if user == "" && group == "" {
if isHomePath {
return nil
}
user = rootUser
group = rootUser
}

userId, err := getent(user, getentUserDatabase)
if err != nil {
return err
Expand All @@ -82,26 +73,32 @@ func Chown(file, user, group string) error {
return err
}

if isHomePath {
return os.Chown(file, userId, groupId)
err = os.Chown(file, userId, groupId)
if err == nil {
return nil
}

chownCmd := "chown "
if user != "" {
chownCmd += user
}
if group != "" {
chownCmd += ":" + user
if !os.IsPermission(err) {
return err
}
chownCmd += " " + file

chownCmd := fmt.Sprintf("chown %s:%s %s", user, group, file)
return MaybeRunWithSudo(chownCmd)
}

func Chmod(target string, mode int) error {
err := os.Chmod(target, os.FileMode(mode))
if err == nil {
return nil
}

if !os.IsPermission(err) {
return err
}

octal := strconv.FormatInt(int64(mode), 8)
chmodCmd := fmt.Sprintf("chmod %s %s", octal, target)
return MaybeRunWithSudoForPath(chmodCmd, target)
return MaybeRunWithSudo(chmodCmd)
}

func getent(key, database string) (int, error) {
Expand Down

0 comments on commit 7354168

Please sign in to comment.