fs.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2019 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package fs
  14. import (
  15. "fmt"
  16. "os"
  17. "path/filepath"
  18. )
  19. const (
  20. // DefaultProcMountPoint is the common mount point of the proc filesystem.
  21. DefaultProcMountPoint = "/proc"
  22. // DefaultSysMountPoint is the common mount point of the sys filesystem.
  23. DefaultSysMountPoint = "/sys"
  24. // DefaultConfigfsMountPoint is the common mount point of the configfs.
  25. DefaultConfigfsMountPoint = "/sys/kernel/config"
  26. // DefaultSelinuxMountPoint is the common mount point of the selinuxfs.
  27. DefaultSelinuxMountPoint = "/sys/fs/selinux"
  28. )
  29. // FS represents a pseudo-filesystem, normally /proc or /sys, which provides an
  30. // interface to kernel data structures.
  31. type FS string
  32. // NewFS returns a new FS mounted under the given mountPoint. It will error
  33. // if the mount point can't be read.
  34. func NewFS(mountPoint string) (FS, error) {
  35. info, err := os.Stat(mountPoint)
  36. if err != nil {
  37. return "", fmt.Errorf("could not read %q: %w", mountPoint, err)
  38. }
  39. if !info.IsDir() {
  40. return "", fmt.Errorf("mount point %q is not a directory", mountPoint)
  41. }
  42. return FS(mountPoint), nil
  43. }
  44. // Path appends the given path elements to the filesystem path, adding separators
  45. // as necessary.
  46. func (fs FS) Path(p ...string) string {
  47. return filepath.Join(append([]string{string(fs)}, p...)...)
  48. }