fs.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. )
  25. // FS represents a pseudo-filesystem, normally /proc or /sys, which provides an
  26. // interface to kernel data structures.
  27. type FS string
  28. // NewFS returns a new FS mounted under the given mountPoint. It will error
  29. // if the mount point can't be read.
  30. func NewFS(mountPoint string) (FS, error) {
  31. info, err := os.Stat(mountPoint)
  32. if err != nil {
  33. return "", fmt.Errorf("could not read %s: %s", mountPoint, err)
  34. }
  35. if !info.IsDir() {
  36. return "", fmt.Errorf("mount point %s is not a directory", mountPoint)
  37. }
  38. return FS(mountPoint), nil
  39. }
  40. // Path appends the given path elements to the filesystem path, adding separators
  41. // as necessary.
  42. func (fs FS) Path(p ...string) string {
  43. return filepath.Join(append([]string{string(fs)}, p...)...)
  44. }