Skip to content

Commit c0f94a3

Browse files
authored
fix: remove "bash -c" in command (#275)
Task: https://pms.uniontech.com/task-view-385159.html
1 parent 23b70ba commit c0f94a3

5 files changed

Lines changed: 42 additions & 43 deletions

File tree

src/internal/updateplatform/message_report.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,9 @@ func (m *UpdatePlatformManager) PrepareCheckScripts() {
15991599
for _, g := range checkGroups {
16001600
for _, c := range g.list {
16011601
filePath := filepath.Join(g.dir, c.Name)
1602-
content, err := base64.RawStdEncoding.DecodeString(c.Shell)
1602+
// Remove padding for StdEncoding, "IyEvYmluL2Jhc2gKCmVjaG8gImhlbGxvIGRlZXBpbiI="
1603+
shell := strings.TrimRight(c.Shell, "=")
1604+
content, err := base64.RawStdEncoding.DecodeString(shell)
16031605
if err != nil {
16041606
logger.Warningf("decode shell for %s failed: %v", c.Name, err)
16051607
continue

src/lastore-daemon/manager_update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func (m *Manager) updateSource(sender dbus.Sender) (*Job, error) {
213213
ErrDetail: "failed to get update policy by token" + err.Error(),
214214
}
215215
} else {
216-
logger.Warning("updatePlatform gen token failed", err)
216+
logger.Warningf("updatePlatform gen token failed: %v", err)
217217
return nil
218218
}
219219
}

src/lastore-update-tools/controller/check/check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func CheckDynHook(cfg *cache.CacheInfo, checkType int8) error {
119119
for _, hookFile := range hookFiles {
120120
hookPath := filepath.Join(hookDir, hookFile)
121121
logger.Infof("Executing hook: %s", hookPath)
122-
output, err := runcmd.RunnerOutput(60, "bash", hookPath)
122+
output, err := runcmd.RunnerOutput(60, hookPath)
123123
if err != nil {
124124
return fmt.Errorf("hook execution failed: %s\nOutput:\n%s\nError:%s", hookPath, output, err.Error())
125125
}

src/lastore-update-tools/sysinfo/diskinfo.go

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package sysinfo
66

77
import (
88
// "os/exec"
9+
"fmt"
910
"strconv"
1011
"strings"
1112

@@ -37,23 +38,22 @@ import (
3738
// return freeSpace, nil
3839
// }
3940

40-
// TODO:(DingHao) fix to udisksctl
41-
func GetRootDiskFreeSpace() (uint64, error) {
42-
freeSpace, err := runcmd.RunnerOutput(10, "bash", "-c", "df -l --output=avail / | tail -n 1")
43-
if err != nil {
44-
return 0, err
41+
func parseDfAvailOuput(output string) (uint64, error) {
42+
fields := strings.Fields(output)
43+
if len(fields) == 0 {
44+
return 0, fmt.Errorf("unexpected df output: %q", output)
4545
}
46+
return strconv.ParseUint(fields[len(fields)-1], 10, 64)
47+
}
4648

47-
// 将输出结果转换为字符串并去除空格
48-
freeSpaceStr := strings.TrimSpace(string(freeSpace))
49-
50-
// 将字符串转换为uint64类型的整数
51-
freeSpaceInt, err := strconv.ParseUint(freeSpaceStr, 10, 64)
49+
// TODO:(DingHao) fix to udisksctl
50+
func GetRootDiskFreeSpace() (uint64, error) {
51+
freeSpace, err := runcmd.RunnerOutput(10, "df", "-l", "--output=avail", "/")
5252
if err != nil {
5353
return 0, err
5454
}
5555

56-
return freeSpaceInt, nil
56+
return parseDfAvailOuput(freeSpace)
5757
}
5858

5959
// TODO:(DingHao) fix to udisksctl
@@ -66,19 +66,10 @@ func GetDataDiskFreeSpace() (uint64, error) {
6666
return sysSpace, nil
6767
}
6868

69-
freeSpace, err := runcmd.RunnerOutput(10, "bash", "-c", "df -l --output=avail /data | tail -n 1")
70-
if err != nil {
71-
return 0, err
72-
}
73-
74-
// 将输出结果转换为字符串并去除空格
75-
freeSpaceStr := strings.TrimSpace(string(freeSpace))
76-
77-
// 将字符串转换为uint64类型的整数
78-
freeSpaceInt, err := strconv.ParseUint(freeSpaceStr, 10, 64)
69+
freeSpace, err := runcmd.RunnerOutput(10, "df", "-l", "--output=avail", "/data")
7970
if err != nil {
8071
return 0, err
8172
}
8273

83-
return freeSpaceInt, nil
74+
return parseDfAvailOuput(freeSpace)
8475
}

src/lastore-update-tools/sysinfo/pkginfo.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ package sysinfo
66

77
import (
88
"fmt"
9-
"os/exec"
109
"strings"
1110

11+
"github.com/linuxdeepin/go-lib/log"
1212
"github.com/linuxdeepin/lastore-daemon/src/lastore-update-tools/config/cache"
1313
runcmd "github.com/linuxdeepin/lastore-daemon/src/lastore-update-tools/pkg/utils/cmd"
1414
"github.com/linuxdeepin/lastore-daemon/src/lastore-update-tools/pkg/utils/fs"
1515
)
1616

17+
var logger = log.NewLogger("lastore/update-tools/sysinfo")
18+
1719
// CheckAppIsExist
1820
func CheckAppIsExist(app string) (bool, error) {
1921
if err := fs.CheckFileExistState(app); err != nil {
@@ -29,28 +31,31 @@ func GetCurrInstPkgStat(pkgs map[string]*cache.AppTinyInfo) error {
2931
}
3032

3133
// bash -c "dpkg -l | tail -n +6 | awk '{print $1,$2,$3}'"
32-
outputStream, err := runcmd.RunnerOutput(10, "bash", "-c", "dpkg -l | tail -n +6 | awk '{print $1,$2,$3}'")
34+
outputStream, err := runcmd.RunnerOutput(
35+
10,
36+
"dpkg-query",
37+
"-W",
38+
"-f=${db:Status-Abbrev} ${Package} ${Version}\n",
39+
)
3340
if err != nil {
34-
return err
41+
return fmt.Errorf("failed to run dpkg-query: %w", err)
3542
}
36-
// logger.Debug(outputStream)
3743

44+
logger.Debugf("dpkg-query output: %s", outputStream)
3845
outputLines := strings.Split(outputStream, "\n")
3946

40-
// logger.Debugf("out:+%v", outputLines)
41-
4247
for _, line := range outputLines {
43-
spv := strings.Split(line, " ")
48+
spv := strings.Fields(line)
4449
if len(spv) != 3 {
45-
// logger.Debugf("skip line: %v", spv)
50+
logger.Debugf("skip line: %v", spv)
4651
continue
4752
}
4853
appInfo := cache.AppTinyInfo{
4954
Name: strings.Split(spv[1], ":")[0],
5055
Version: spv[2],
5156
State: cache.PkgState(spv[0]),
5257
}
53-
// logger.Debugf("pkg:%+v", appInfo)
58+
logger.Debugf("pkg:%+v", appInfo)
5459

5560
pkgs[appInfo.Name] = &appInfo
5661
pkgs[fmt.Sprintf("%s#%s", appInfo.Name, appInfo.Version)] = &appInfo
@@ -62,19 +67,20 @@ func GetCurrInstPkgStat(pkgs map[string]*cache.AppTinyInfo) error {
6267

6368
// ToDo:(DingHao)替换成袁老师的hash函数
6469
func GetSysPkgStateAndVersion(pkgname string) (string, string, error) {
65-
command := "bash"
66-
arg1 := "-c"
67-
arg2 := "dpkg -l | tail -n +6 | awk '{print $1,$2,$3}'|grep \"^.. " + pkgname + " \""
68-
cmd := exec.Command(command, arg1, arg2)
69-
output, err := cmd.Output()
70+
output, err := runcmd.RunnerOutput(
71+
10,
72+
"dpkg-query",
73+
"-W",
74+
"-f=${db:Status-Abbrev} ${Version}\n",
75+
pkgname,
76+
)
7077
if err != nil {
7178
return "", "", err
7279
}
7380

74-
pkgInfo := strings.Split(string(output), " ")
75-
if len(pkgInfo) != 3 {
76-
// log.Debugf("failed format: %s len: %d", pkgInfo, len(pkgInfo))
81+
pkgInfo := strings.Fields(output)
82+
if len(pkgInfo) < 2 {
7783
return "", "", fmt.Errorf("failed format: %s len: %d", pkgInfo, len(pkgInfo))
7884
}
79-
return pkgInfo[0], pkgInfo[2], nil
85+
return pkgInfo[0], pkgInfo[1], nil
8086
}

0 commit comments

Comments
 (0)