- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsysctl.go
More file actions
Latest commit
100 lines (90 loc) · 2.6 KB
/
Copy pathsysctl.go
File metadata and controls
100 lines (90 loc) · 2.6 KB
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
package sysctl
import (
log "github.com/Sirupsen/logrus"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
const (
SYSTEM_PROC_DIR="/proc/sys/"
)
// export api
funcAll() map[string]string {
returnwalkAndCatFiles(SYSTEM_PROC_DIR)
}
funcFind(names...string) map[string]string {
settings:=make(map[string]string)
for_, name:=rangenames {
settings_file_path:=convert_settings_to_path(name)
if_, err:=os.Stat(settings_file_path); os.IsNotExist(err) {
log.Warnf("Failed to query settings %v, Because of %v\n", name, err.Error())
continue
}
bytes, err:=ioutil.ReadFile(settings_file_path)
iferr!=nil {
log.Warnf("Failed to read settings %v from proc, Because of %v", name, err.Error())
continue
}
settings[name] =string(bytes)
}
returnsettings
}
funcApply(namestring, valuestring) error {
settings_file_path:=convert_settings_to_path(name)
if_, err:=os.Stat(settings_file_path); os.IsNotExist(err) {
log.Warnf("Failed to apply settings change, Because of %v", err.Error())
returnerr
}
err:=ioutil.WriteFile(settings_file_path, []byte(value), 0644)
iferr!=nil {
log.Warnf("Failed to apply settings change, Because of %v", err.Error())
}
returnerr
}
// internal api
funcwalkAndCatFiles(rootPathstring) map[string]string {
settings:=make(map[string]string)
if!strings.Contains(rootPath, SYSTEM_PROC_DIR) {
log.Warnf("The specific path is invalid %v", rootPath)
returnnil
}
fi, err:=os.Stat(rootPath)
iferr!=nil {
log.Warnf("Failed to open files, Because of %v \n", rootPath)
returnnil
}
iffi.IsDir() ==false {
bytes, err:=ioutil.ReadFile(rootPath)
iferr!=nil {
log.Warnf("Failed to read files, Because of %v", err.Error())
returnnil
}
settings_name:=convert_path_to_settings(rootPath)
settings[settings_name] =strings.Replace(strings.Trim(string(bytes), "\n"), "\t", " ", -1)
returnsettings
} else {
files, err:=ioutil.ReadDir(rootPath)
iferr!=nil {
log.Warnf("Failed to list dir, Because of %v", err.Error())
returnnil
}
for_, file:=rangefiles {
base_path:=filepath.Join(rootPath, file.Name())
subfolder_settings:=walkAndCatFiles(base_path)
forkey, value:=rangesubfolder_settings {
settings[key] =value
}
}
}
returnsettings
}
funcconvert_path_to_settings(pathstring) string {
name:=path[len(SYSTEM_PROC_DIR):]
settings_arr:=strings.Split(name, "/")
returnstrings.Join(settings_arr, ".")
}
funcconvert_settings_to_path(namestring) string {
settings_arr:=strings.Split(name, ".")
returnfilepath.Join(SYSTEM_PROC_DIR, strings.Join(settings_arr, "/"))
}