From 3da9017c17c7d2432c4b76345c4d2efbeedd2935 Mon Sep 17 00:00:00 2001 From: Joey Jiaojg Date: Wed, 26 May 2021 19:38:04 +0800 Subject: pkg/compiler: add glob type * all: add new typename dirname The current way to check files under sysfs or proc is: - define a string to represent each file - open the file - pass the fd to write / read / close The issues above are: - Need to know what file present on target device - Need to write openat for each file With dirname added, which will open one file in the directory randomly and then pass the fd to write/read/close. * all: use typename glob to match filename Fixes #481 --- pkg/host/machine_info.go | 8 ++++++++ pkg/host/machine_info_linux.go | 14 ++++++++++++++ 2 files changed, 22 insertions(+) (limited to 'pkg/host') diff --git a/pkg/host/machine_info.go b/pkg/host/machine_info.go index a804ca32a..167b7ec03 100644 --- a/pkg/host/machine_info.go +++ b/pkg/host/machine_info.go @@ -38,8 +38,16 @@ func CollectModulesInfo() ([]KernelModule, error) { return machineModulesInfo() } +func CollectGlobsInfo(globs []string) (map[string][]string, error) { + if machineModulesInfo == nil { + return nil, nil + } + return machineGlobsInfo(globs) +} + var machineInfoFuncs []machineInfoFunc var machineModulesInfo func() ([]KernelModule, error) +var machineGlobsInfo func([]string) (map[string][]string, error) type machineInfoFunc struct { name string diff --git a/pkg/host/machine_info_linux.go b/pkg/host/machine_info_linux.go index 317d34cd9..3b0971495 100644 --- a/pkg/host/machine_info_linux.go +++ b/pkg/host/machine_info_linux.go @@ -21,6 +21,7 @@ func init() { {"KVM", readKVMInfo}, } machineModulesInfo = getModulesInfo + machineGlobsInfo = getGlobsInfo } func readCPUInfo(buffer *bytes.Buffer) error { @@ -140,3 +141,16 @@ func getModulesInfo() ([]KernelModule, error) { } return modules, nil } + +func getGlobsInfo(globs []string) (map[string][]string, error) { + files := make(map[string][]string, len(globs)) + for _, glob := range globs { + matches, err := filepath.Glob(glob) + if err != nil { + return nil, err + } + files[glob] = matches + } + + return files, nil +} -- cgit mrf-deployment