25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

56 lines
1.7 KiB

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