Skip to content

Commit 585f355

Browse files
authored
refactor(system): move locale env collection to system package for child process reuse (#419)
Export CollectAndClearLocaleEnvs and OriginalLocaleEnvs to allow child processes (e.g. apt commands) to inherit locale environment variables from the daemon process. 将语言环境变量收集函数迁移至 system 包并导出,使子进程(如 apt 命令) 能够从守护进程继承语言环境变量。 Log: 将语言环境变量收集函数迁移至 system 包 PMS: BUG-361139 Influence: 子进程执行 apt 命令时可正确继承语言环境变量,确保输出信息语言一致。
1 parent e924d7a commit 585f355

6 files changed

Lines changed: 74 additions & 21 deletions

File tree

src/internal/system/apt/proxy.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,14 @@ func (p *APTSystem) OsBackup(jobId string) error {
756756
environ := map[string]string{
757757
"IMMUTABLE_DISABLE_REMOUNT": "false",
758758
}
759+
for _, env := range system.OriginalLocaleEnvs {
760+
parts := strings.SplitN(env, "=", 2)
761+
if len(parts) != 2 {
762+
continue
763+
}
764+
environ[parts[0]] = parts[1]
765+
}
766+
759767
c.SetEnv(environ)
760768
c.Indicator(system.JobProgressInfo{
761769
JobId: jobId,

src/internal/system/common.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/linuxdeepin/go-lib/dbusutil"
2020
"github.com/linuxdeepin/go-lib/keyfile"
2121
"github.com/linuxdeepin/go-lib/log"
22+
"github.com/linuxdeepin/lastore-daemon/src/internal/utils"
2223
)
2324

2425
const (
@@ -39,13 +40,27 @@ type MirrorSource struct {
3940

4041
var RepoInfos []RepositoryInfo
4142
var logger = log.NewLogger("lastore/system")
43+
var OriginalLocaleEnvs []string
4244

4345
type RepositoryInfo struct {
4446
Name string `json:"name"`
4547
Url string `json:"url"`
4648
Mirror string `json:"mirror"`
4749
}
4850

51+
// CollectAndClearLocaleEnvs stores current locale envs for child processes that
52+
// still need them, then clears them from the daemon process environment.
53+
func CollectAndClearLocaleEnvs() {
54+
OriginalLocaleEnvs = nil
55+
for _, localeEnvName := range []string{"LC_ALL", "LANGUAGE", "LC_MESSAGES", "LANG"} {
56+
localeEnvValue, exists := os.LookupEnv(localeEnvName)
57+
if exists {
58+
OriginalLocaleEnvs = append(OriginalLocaleEnvs, localeEnvName+"="+localeEnvValue)
59+
}
60+
_ = utils.UnsetEnv(localeEnvName)
61+
}
62+
}
63+
4964
func DecodeJson(fpath string, d interface{}) error {
5065
// #nosec G304
5166
f, err := os.Open(fpath)

src/internal/system/common_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package system
66

77
import (
8+
"os"
89
"testing"
910

1011
"github.com/stretchr/testify/assert"
@@ -32,3 +33,51 @@ func Test_GetCategorySourceMap(t *testing.T) {
3233
sourceMap = GetCategorySourceMap()
3334
assert.Equal(t, SoftLinkSystemSourceDir, sourceMap[SystemUpdate])
3435
}
36+
37+
func TestCollectAndClearLocaleEnvs(t *testing.T) {
38+
oldOriginalLocaleEnvs := OriginalLocaleEnvs
39+
originalValues := make(map[string]*string)
40+
localeEnvNames := []string{"LC_ALL", "LANGUAGE", "LC_MESSAGES", "LANG"}
41+
42+
for _, name := range localeEnvNames {
43+
value, exists := os.LookupEnv(name)
44+
if exists {
45+
valueCopy := value
46+
originalValues[name] = &valueCopy
47+
} else {
48+
originalValues[name] = nil
49+
}
50+
}
51+
52+
t.Cleanup(func() {
53+
OriginalLocaleEnvs = oldOriginalLocaleEnvs
54+
for _, name := range localeEnvNames {
55+
value := originalValues[name]
56+
if value == nil {
57+
_ = os.Unsetenv(name)
58+
continue
59+
}
60+
_ = os.Setenv(name, *value)
61+
}
62+
})
63+
64+
OriginalLocaleEnvs = nil
65+
assert.NoError(t, os.Setenv("LC_ALL", "zh_CN.UTF-8"))
66+
assert.NoError(t, os.Setenv("LANGUAGE", "zh_CN:en_US"))
67+
assert.NoError(t, os.Setenv("LC_MESSAGES", "zh_CN.UTF-8"))
68+
assert.NoError(t, os.Setenv("LANG", "zh_CN.UTF-8"))
69+
70+
CollectAndClearLocaleEnvs()
71+
72+
assert.Equal(t, []string{
73+
"LC_ALL=zh_CN.UTF-8",
74+
"LANGUAGE=zh_CN:en_US",
75+
"LC_MESSAGES=zh_CN.UTF-8",
76+
"LANG=zh_CN.UTF-8",
77+
}, OriginalLocaleEnvs)
78+
79+
for _, name := range localeEnvNames {
80+
_, exists := os.LookupEnv(name)
81+
assert.False(t, exists, name)
82+
}
83+
}

src/lastore-daemon/common.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/linuxdeepin/lastore-daemon/src/internal/config"
2929
"github.com/linuxdeepin/lastore-daemon/src/internal/system"
3030
"github.com/linuxdeepin/lastore-daemon/src/internal/system/apt"
31-
"github.com/linuxdeepin/lastore-daemon/src/internal/utils"
3231

3332
"github.com/godbus/dbus/v5"
3433
"github.com/linuxdeepin/go-lib/dbusutil"
@@ -149,24 +148,6 @@ func getLang(envVars procfs.EnvVars) string {
149148
return ""
150149
}
151150

152-
var originalLocaleEnvs []string
153-
154-
// collectAndClearLocaleEnvs collects current locale environment variables,
155-
// clears them from the environment, and stores the original values for later use
156-
func collectAndClearLocaleEnvs() {
157-
originalLocaleEnvs = nil
158-
// Iterate over the list of environment variable names related to locale settings
159-
for _, localeEnvName := range []string{"LC_ALL", "LANGUAGE",
160-
"LC_MESSAGES", "LANG"} {
161-
localeEnvValue, exists := os.LookupEnv(localeEnvName)
162-
if exists {
163-
// Store the original locale environment variables for later use
164-
originalLocaleEnvs = append(originalLocaleEnvs, localeEnvName+"="+localeEnvValue)
165-
}
166-
_ = utils.UnsetEnv(localeEnvName)
167-
}
168-
}
169-
170151
func listPackageDesktopFiles(pkg string) []string {
171152
var result []string
172153
filenames := system.ListPackageFile(pkg)

src/lastore-daemon/immutable.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (i *immutableManager) osTreeCmd(args []string) (out string, err error) {
1515
if system.NormalFileExists(system.DeepinImmutableCtlPath) {
1616
cmd := exec.Command(system.DeepinImmutableCtlPath, args...) // #nosec G204
1717
cmd.Env = append(os.Environ(), "IMMUTABLE_DISABLE_REMOUNT=false")
18-
cmd.Env = append(cmd.Env, originalLocaleEnvs...)
18+
cmd.Env = append(cmd.Env, system.OriginalLocaleEnvs...)
1919
logger.Info("run command:", cmd.Args)
2020
var stdout, stderr bytes.Buffer
2121
cmd.Stdout = &stdout

src/lastore-daemon/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func main() {
6565
}
6666

6767
// Collect and clear locale environment variables
68-
collectAndClearLocaleEnvs()
68+
system.CollectAndClearLocaleEnvs()
6969

7070
gettext.InitI18n()
7171
gettext.Textdomain("lastore-daemon")

0 commit comments

Comments
 (0)