path_linux.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Use and distribution licensed under the Apache license version 2.
  2. //
  3. // See the COPYING file in the root project directory for full text.
  4. //
  5. package linuxpath
  6. import (
  7. "fmt"
  8. "path/filepath"
  9. "github.com/jaypipes/ghw/pkg/context"
  10. )
  11. // PathRoots holds the roots of all the filesystem subtrees
  12. // ghw wants to access.
  13. type PathRoots struct {
  14. Etc string
  15. Proc string
  16. Run string
  17. Sys string
  18. Var string
  19. }
  20. // DefaultPathRoots return the canonical default value for PathRoots
  21. func DefaultPathRoots() PathRoots {
  22. return PathRoots{
  23. Etc: "/etc",
  24. Proc: "/proc",
  25. Run: "/run",
  26. Sys: "/sys",
  27. Var: "/var",
  28. }
  29. }
  30. // PathRootsFromContext initialize PathRoots from the given Context,
  31. // allowing overrides of the canonical default paths.
  32. func PathRootsFromContext(ctx *context.Context) PathRoots {
  33. roots := DefaultPathRoots()
  34. if pathEtc, ok := ctx.PathOverrides["/etc"]; ok {
  35. roots.Etc = pathEtc
  36. }
  37. if pathProc, ok := ctx.PathOverrides["/proc"]; ok {
  38. roots.Proc = pathProc
  39. }
  40. if pathRun, ok := ctx.PathOverrides["/run"]; ok {
  41. roots.Run = pathRun
  42. }
  43. if pathSys, ok := ctx.PathOverrides["/sys"]; ok {
  44. roots.Sys = pathSys
  45. }
  46. if pathVar, ok := ctx.PathOverrides["/var"]; ok {
  47. roots.Var = pathVar
  48. }
  49. return roots
  50. }
  51. type Paths struct {
  52. VarLog string
  53. ProcMeminfo string
  54. ProcCpuinfo string
  55. ProcMounts string
  56. SysKernelMMHugepages string
  57. SysBlock string
  58. SysDevicesSystemNode string
  59. SysDevicesSystemMemory string
  60. SysDevicesSystemCPU string
  61. SysBusPciDevices string
  62. SysClassDRM string
  63. SysClassDMI string
  64. SysClassNet string
  65. RunUdevData string
  66. }
  67. // New returns a new Paths struct containing filepath fields relative to the
  68. // supplied Context
  69. func New(ctx *context.Context) *Paths {
  70. roots := PathRootsFromContext(ctx)
  71. return &Paths{
  72. VarLog: filepath.Join(ctx.Chroot, roots.Var, "log"),
  73. ProcMeminfo: filepath.Join(ctx.Chroot, roots.Proc, "meminfo"),
  74. ProcCpuinfo: filepath.Join(ctx.Chroot, roots.Proc, "cpuinfo"),
  75. ProcMounts: filepath.Join(ctx.Chroot, roots.Proc, "self", "mounts"),
  76. SysKernelMMHugepages: filepath.Join(ctx.Chroot, roots.Sys, "kernel", "mm", "hugepages"),
  77. SysBlock: filepath.Join(ctx.Chroot, roots.Sys, "block"),
  78. SysDevicesSystemNode: filepath.Join(ctx.Chroot, roots.Sys, "devices", "system", "node"),
  79. SysDevicesSystemMemory: filepath.Join(ctx.Chroot, roots.Sys, "devices", "system", "memory"),
  80. SysDevicesSystemCPU: filepath.Join(ctx.Chroot, roots.Sys, "devices", "system", "cpu"),
  81. SysBusPciDevices: filepath.Join(ctx.Chroot, roots.Sys, "bus", "pci", "devices"),
  82. SysClassDRM: filepath.Join(ctx.Chroot, roots.Sys, "class", "drm"),
  83. SysClassDMI: filepath.Join(ctx.Chroot, roots.Sys, "class", "dmi"),
  84. SysClassNet: filepath.Join(ctx.Chroot, roots.Sys, "class", "net"),
  85. RunUdevData: filepath.Join(ctx.Chroot, roots.Run, "udev", "data"),
  86. }
  87. }
  88. func (p *Paths) NodeCPU(nodeID int, lpID int) string {
  89. return filepath.Join(
  90. p.SysDevicesSystemNode,
  91. fmt.Sprintf("node%d", nodeID),
  92. fmt.Sprintf("cpu%d", lpID),
  93. )
  94. }
  95. func (p *Paths) NodeCPUCache(nodeID int, lpID int) string {
  96. return filepath.Join(
  97. p.NodeCPU(nodeID, lpID),
  98. "cache",
  99. )
  100. }
  101. func (p *Paths) NodeCPUCacheIndex(nodeID int, lpID int, cacheIndex int) string {
  102. return filepath.Join(
  103. p.NodeCPUCache(nodeID, lpID),
  104. fmt.Sprintf("index%d", cacheIndex),
  105. )
  106. }