1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-21 22:04:01 +03:00

locale-setup: split locale_context_load() into small pieces

No functional change, just refactoring and preparation for later
commits.
This commit is contained in:
Yu Watanabe 2022-12-13 17:59:35 +09:00
parent 7294912f18
commit 018befcff6

View File

@ -19,12 +19,14 @@ void locale_context_clear(LocaleContext *c) {
c->locale[i] = mfree(c->locale[i]);
}
int locale_context_load(LocaleContext *c, LocaleLoadFlag flag) {
static int locale_context_load_proc(LocaleContext *c, LocaleLoadFlag flag) {
int r;
assert(c);
if (FLAGS_SET(flag, LOCALE_LOAD_PROC_CMDLINE)) {
if (!FLAGS_SET(flag, LOCALE_LOAD_PROC_CMDLINE))
return 0;
locale_context_clear(c);
r = proc_cmdline_get_key_many(PROC_CMDLINE_STRIP_RD_PREFIX,
@ -42,21 +44,30 @@ int locale_context_load(LocaleContext *c, LocaleLoadFlag flag) {
"locale.LC_TELEPHONE", &c->locale[VARIABLE_LC_TELEPHONE],
"locale.LC_MEASUREMENT", &c->locale[VARIABLE_LC_MEASUREMENT],
"locale.LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION]);
if (r < 0 && r != -ENOENT)
log_debug_errno(r, "Failed to read /proc/cmdline, ignoring: %m");
if (r > 0)
goto finalize;
}
if (r == -ENOENT)
return 0;
if (r < 0)
return log_debug_errno(r, "Failed to read /proc/cmdline: %m");
return r;
}
if (FLAGS_SET(flag, LOCALE_LOAD_LOCALE_CONF)) {
static int locale_context_load_conf(LocaleContext *c, LocaleLoadFlag flag) {
struct stat st;
usec_t t;
int r;
assert(c);
if (!FLAGS_SET(flag, LOCALE_LOAD_LOCALE_CONF))
return 0;
r = stat("/etc/locale.conf", &st);
if (r < 0 && errno != ENOENT)
if (r < 0) {
if (errno == ENOENT)
return 0;
return log_debug_errno(errno, "Failed to stat /etc/locale.conf: %m");
}
if (r >= 0) {
/* If mtime is not changed, then we do not need to re-read the file. */
t = timespec_load(&st.st_mtim);
if (c->mtime != USEC_INFINITY && t == c->mtime)
@ -83,11 +94,17 @@ int locale_context_load(LocaleContext *c, LocaleLoadFlag flag) {
if (r < 0)
return log_debug_errno(r, "Failed to read /etc/locale.conf: %m");
goto finalize;
}
}
return 1; /* loaded */
}
static int locale_context_load_env(LocaleContext *c, LocaleLoadFlag flag) {
int r;
assert(c);
if (!FLAGS_SET(flag, LOCALE_LOAD_ENVIRONMENT))
return 0;
if (FLAGS_SET(flag, LOCALE_LOAD_ENVIRONMENT)) {
locale_context_clear(c);
/* Fill in what we got passed from systemd. */
@ -99,14 +116,34 @@ int locale_context_load(LocaleContext *c, LocaleLoadFlag flag) {
return log_oom_debug();
}
goto finalize;
}
return 1; /* loaded */
}
int locale_context_load(LocaleContext *c, LocaleLoadFlag flag) {
int r;
assert(c);
r = locale_context_load_proc(c, flag);
if (r > 0)
goto finalize;
r = locale_context_load_conf(c, flag);
if (r != 0)
goto finalize;
r = locale_context_load_env(c, flag);
finalize:
if (r < 0)
return r;
if (r == 0) {
/* Nothing loaded. */
locale_context_clear(c);
return 0;
}
finalize:
if (FLAGS_SET(flag, LOCALE_LOAD_SIMPLIFY))
locale_variables_simplify(c->locale);