aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/opencontainers/selinux/go-selinux/rchcon.go
blob: 8bff29355798c9d24e0979b5f75bbe564a9869d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//go:build linux && go1.16
// +build linux,go1.16

package selinux

import (
	"errors"
	"io/fs"
	"os"

	"github.com/opencontainers/selinux/pkg/pwalkdir"
)

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) {
			return nil
		}
		return e
	})
}