rchcon.go 745 B

12345678910111213141516171819202122232425262728293031323334
  1. //go:build linux && go1.16
  2. // +build linux,go1.16
  3. package selinux
  4. import (
  5. "errors"
  6. "io/fs"
  7. "os"
  8. "github.com/opencontainers/selinux/pkg/pwalkdir"
  9. )
  10. func rchcon(fpath, label string) error {
  11. fastMode := false
  12. // If the current label matches the new label, assume
  13. // other labels are correct.
  14. if cLabel, err := lFileLabel(fpath); err == nil && cLabel == label {
  15. fastMode = true
  16. }
  17. return pwalkdir.Walk(fpath, func(p string, _ fs.DirEntry, _ error) error {
  18. if fastMode {
  19. if cLabel, err := lFileLabel(fpath); err == nil && cLabel == label {
  20. return nil
  21. }
  22. }
  23. e := lSetFileLabel(p, label)
  24. // Walk a file tree can race with removal, so ignore ENOENT.
  25. if errors.Is(e, os.ErrNotExist) {
  26. return nil
  27. }
  28. return e
  29. })
  30. }