Skip to content

Commit 24a2fb6

Browse files
authored
Merge pull request #177 from rhatdan/relabel
If chcon fails, check if label is already correct
2 parents b625eea + b85f8fd commit 24a2fb6

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

go-selinux/rchcon.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,18 @@ import (
1212
)
1313

1414
func rchcon(fpath, label string) error {
15+
fastMode := false
16+
// If the current label matches the new label, assume
17+
// other labels are correct.
18+
if cLabel, err := lFileLabel(fpath); err == nil && cLabel == label {
19+
fastMode = true
20+
}
1521
return pwalkdir.Walk(fpath, func(p string, _ fs.DirEntry, _ error) error {
22+
if fastMode {
23+
if cLabel, err := lFileLabel(fpath); err == nil && cLabel == label {
24+
return nil
25+
}
26+
}
1627
e := lSetFileLabel(p, label)
1728
// Walk a file tree can race with removal, so ignore ENOENT.
1829
if errors.Is(e, os.ErrNotExist) {

go-selinux/selinux_linux.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,24 @@ func chcon(fpath string, label string, recurse bool) error {
11291129
}
11301130

11311131
if !recurse {
1132-
return setFileLabel(fpath, label)
1132+
err := lSetFileLabel(fpath, label)
1133+
if err != nil {
1134+
// Check if file doesn't exist, must have been removed
1135+
if errors.Is(err, os.ErrNotExist) {
1136+
return nil
1137+
}
1138+
// Check if current label is correct on disk
1139+
flabel, nerr := lFileLabel(fpath)
1140+
if nerr == nil && flabel == label {
1141+
return nil
1142+
}
1143+
// Check if file doesn't exist, must have been removed
1144+
if errors.Is(nerr, os.ErrNotExist) {
1145+
return nil
1146+
}
1147+
return err
1148+
}
1149+
return nil
11331150
}
11341151

11351152
return rchcon(fpath, label)

0 commit comments

Comments
 (0)