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