fs.go 966 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package procfs
  2. import (
  3. "fmt"
  4. "os"
  5. "path"
  6. )
  7. // FS represents the pseudo-filesystem proc, which provides an interface to
  8. // kernel data structures.
  9. type FS string
  10. // DefaultMountPoint is the common mount point of the proc filesystem.
  11. const DefaultMountPoint = "/proc"
  12. // NewFS returns a new FS mounted under the given mountPoint. It will error
  13. // if the mount point can't be read.
  14. func NewFS(mountPoint string) (FS, error) {
  15. info, err := os.Stat(mountPoint)
  16. if err != nil {
  17. return "", fmt.Errorf("could not read %s: %s", mountPoint, err)
  18. }
  19. if !info.IsDir() {
  20. return "", fmt.Errorf("mount point %s is not a directory", mountPoint)
  21. }
  22. return FS(mountPoint), nil
  23. }
  24. func (fs FS) stat(p string) (os.FileInfo, error) {
  25. return os.Stat(path.Join(string(fs), p))
  26. }
  27. func (fs FS) open(p string) (*os.File, error) {
  28. return os.Open(path.Join(string(fs), p))
  29. }
  30. func (fs FS) readlink(p string) (string, error) {
  31. return os.Readlink(path.Join(string(fs), p))
  32. }