Skip to content

Commit

Permalink
Merge pull request #177 from rhatdan/relabel
Browse files Browse the repository at this point in the history
If chcon fails, check if label is already correct
  • Loading branch information
rhatdan authored Sep 28, 2022
2 parents b625eea + b85f8fd commit 24a2fb6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
11 changes: 11 additions & 0 deletions go-selinux/rchcon.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@ import (
)

func rchcon(fpath, label string) error {
fastMode := false
// If the current label matches the new label, assume
// other labels are correct.
if cLabel, err := lFileLabel(fpath); err == nil && cLabel == label {
fastMode = true
}
return pwalkdir.Walk(fpath, func(p string, _ fs.DirEntry, _ error) error {
if fastMode {
if cLabel, err := lFileLabel(fpath); err == nil && cLabel == label {
return nil
}
}
e := lSetFileLabel(p, label)
// Walk a file tree can race with removal, so ignore ENOENT.
if errors.Is(e, os.ErrNotExist) {
Expand Down
19 changes: 18 additions & 1 deletion go-selinux/selinux_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,24 @@ func chcon(fpath string, label string, recurse bool) error {
}

if !recurse {
return setFileLabel(fpath, label)
err := lSetFileLabel(fpath, label)
if err != nil {
// Check if file doesn't exist, must have been removed
if errors.Is(err, os.ErrNotExist) {
return nil
}
// Check if current label is correct on disk
flabel, nerr := lFileLabel(fpath)
if nerr == nil && flabel == label {
return nil
}
// Check if file doesn't exist, must have been removed
if errors.Is(nerr, os.ErrNotExist) {
return nil
}
return err
}
return nil
}

return rchcon(fpath, label)
Expand Down

0 comments on commit 24a2fb6

Please sign in to comment.