Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions locale-helper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ import (

"github.com/linuxdeepin/go-lib/dbusutil"
"github.com/linuxdeepin/go-lib/log"
dutils "github.com/linuxdeepin/go-lib/utils"
)

//go:generate dbusutil-gen em -type Helper

const (
dbusServiceName = "org.deepin.dde.LocaleHelper1"
dbusPath = "/org/deepin/dde/LocaleHelper1"
dbusInterface = dbusServiceName
localeGenBin = "/usr/sbin/locale-gen"
dbusServiceName = "org.deepin.dde.LocaleHelper1"
dbusPath = "/org/deepin/dde/LocaleHelper1"
dbusInterface = dbusServiceName
localeGenBin = "/usr/sbin/locale-gen"
deepinImmutableCtlBin = "/usr/sbin/deepin-immutable-ctl"
)

type Helper struct {
Expand Down Expand Up @@ -90,10 +92,32 @@ func (h *Helper) canQuit() bool {
}

func (h *Helper) doGenLocale() error {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider extracting the duplicated logic in both methods into a single helper function to simplify the code.

You can collapse the nearly‐identical logic in both methods into a single helper. For example:

// new helper extracted from doGenLocale + doGenLocaleWithParam
func (h *Helper) execLocaleCmd(args ...string) error {
    if !dutils.IsFileExist(deepinImmutableCtlBin) {
        logger.Warning("deepin-immutable-ctl not found, use locale-gen directly")
        return exec.Command(localeGenBin, args...).Run()
    }

    // Use “--” to separate flags from the command itself
    ctlArgs := append([]string{"admin", "exec", "--", localeGenBin}, args...)
    output, err := exec.Command(deepinImmutableCtlBin, ctlArgs...).CombinedOutput()
    if err != nil {
        logger.Warning(
            "deepin-immutable-ctl exec locale-gen failed,",
            "err:", err,
            "output:", string(output),
        )
    }
    return err
}

Then simplify your two API methods to:

func (h *Helper) doGenLocale() error {
    return h.execLocaleCmd()
}

func (h *Helper) doGenLocaleWithParam(locale string) error {
    return h.execLocaleCmd(locale)
}

This removes the duplication, keeps the exact same behavior, and flattens the control flow.

return exec.Command(localeGenBin).Run()
if !dutils.IsFileExist(deepinImmutableCtlBin) {
logger.Warning("deepin-immutable-ctl not found, use locale-gen directly")
return exec.Command(localeGenBin).Run()
} else {
// TODO 在磐石适配 locale-gen 前使用 deepin-immutable-ctl 执行 locale-gen,否则有权限问题
output, err := exec.Command(deepinImmutableCtlBin, "admin", "exec", localeGenBin).CombinedOutput()
if err != nil {
logger.Warning("deepin-immutable-ctl exec locale-gen failed, err:", err, "output:", string(output))
return err
}
return nil
}
}

// locales version <= 2.13
func (h *Helper) doGenLocaleWithParam(locale string) error {
return exec.Command(localeGenBin, locale).Run()
if !dutils.IsFileExist(deepinImmutableCtlBin) {
logger.Warning("deepin-immutable-ctl not found, use locale-gen directly")
return exec.Command(localeGenBin, locale).Run()
} else {
// TODO 在磐石适配 locale-gen 前使用 deepin-immutable-ctl 执行 locale-gen,否则有权限问题
output, err := exec.Command(deepinImmutableCtlBin, "admin", "exec", "--", localeGenBin, locale).CombinedOutput()
if err != nil {
logger.Warning("deepin-immutable-ctl exec locale-gen failed, err:", err, "output:", string(output))
return err
}
return nil
}
}
14 changes: 10 additions & 4 deletions misc/systemd/system/deepin-locale-helper.service
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ ExecStart=/usr/lib/deepin-api/locale-helper

ReadWritePaths=/etc/default/locale
ReadWritePaths=/etc/locale.gen
ReadWritePaths=/usr/lib/locale/
ExecPaths=/usr/sbin/locale-gen

# Temporary workaround: ReadWritePaths conflicts with deepin-immutable-ctl
# TODO: Remove this comment when immutable system wraps locale-gen properly
# ReadWritePaths=/usr/lib/locale/

DevicePolicy=closed

ProtectSystem=full
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
PrivateDevices=yes
Expand All @@ -29,7 +31,11 @@ ProtectKernelModules=yes
ProtectKernelLogs=yes
ProtectControlGroups=yes
RestrictAddressFamilies=AF_UNIX
RestrictNamespaces=yes

# Need to call /usr/sbin/deepin-immutable-ctl command
# TODO: Remove this comment when immutable system wraps locale-gen properly
# RestrictNamespaces=yes

LockPersonality=yes
RestrictRealtime=yes
RestrictSUIDSGID=yes
Expand Down