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