mirror of
https://github.com/systemd/systemd.git
synced 2025-08-31 09:49:54 +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:
@ -19,94 +19,131 @@ void locale_context_clear(LocaleContext *c) {
|
|||||||
c->locale[i] = mfree(c->locale[i]);
|
c->locale[i] = mfree(c->locale[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int locale_context_load_proc(LocaleContext *c, LocaleLoadFlag flag) {
|
||||||
|
int r;
|
||||||
|
|
||||||
|
assert(c);
|
||||||
|
|
||||||
|
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,
|
||||||
|
"locale.LANG", &c->locale[VARIABLE_LANG],
|
||||||
|
"locale.LANGUAGE", &c->locale[VARIABLE_LANGUAGE],
|
||||||
|
"locale.LC_CTYPE", &c->locale[VARIABLE_LC_CTYPE],
|
||||||
|
"locale.LC_NUMERIC", &c->locale[VARIABLE_LC_NUMERIC],
|
||||||
|
"locale.LC_TIME", &c->locale[VARIABLE_LC_TIME],
|
||||||
|
"locale.LC_COLLATE", &c->locale[VARIABLE_LC_COLLATE],
|
||||||
|
"locale.LC_MONETARY", &c->locale[VARIABLE_LC_MONETARY],
|
||||||
|
"locale.LC_MESSAGES", &c->locale[VARIABLE_LC_MESSAGES],
|
||||||
|
"locale.LC_PAPER", &c->locale[VARIABLE_LC_PAPER],
|
||||||
|
"locale.LC_NAME", &c->locale[VARIABLE_LC_NAME],
|
||||||
|
"locale.LC_ADDRESS", &c->locale[VARIABLE_LC_ADDRESS],
|
||||||
|
"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 == -ENOENT)
|
||||||
|
return 0;
|
||||||
|
if (r < 0)
|
||||||
|
return log_debug_errno(r, "Failed to read /proc/cmdline: %m");
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
if (errno == ENOENT)
|
||||||
|
return 0;
|
||||||
|
return log_debug_errno(errno, "Failed to stat /etc/locale.conf: %m");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
locale_context_clear(c);
|
||||||
|
c->mtime = t;
|
||||||
|
|
||||||
|
r = parse_env_file(NULL, "/etc/locale.conf",
|
||||||
|
"LANG", &c->locale[VARIABLE_LANG],
|
||||||
|
"LANGUAGE", &c->locale[VARIABLE_LANGUAGE],
|
||||||
|
"LC_CTYPE", &c->locale[VARIABLE_LC_CTYPE],
|
||||||
|
"LC_NUMERIC", &c->locale[VARIABLE_LC_NUMERIC],
|
||||||
|
"LC_TIME", &c->locale[VARIABLE_LC_TIME],
|
||||||
|
"LC_COLLATE", &c->locale[VARIABLE_LC_COLLATE],
|
||||||
|
"LC_MONETARY", &c->locale[VARIABLE_LC_MONETARY],
|
||||||
|
"LC_MESSAGES", &c->locale[VARIABLE_LC_MESSAGES],
|
||||||
|
"LC_PAPER", &c->locale[VARIABLE_LC_PAPER],
|
||||||
|
"LC_NAME", &c->locale[VARIABLE_LC_NAME],
|
||||||
|
"LC_ADDRESS", &c->locale[VARIABLE_LC_ADDRESS],
|
||||||
|
"LC_TELEPHONE", &c->locale[VARIABLE_LC_TELEPHONE],
|
||||||
|
"LC_MEASUREMENT", &c->locale[VARIABLE_LC_MEASUREMENT],
|
||||||
|
"LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION]);
|
||||||
|
if (r < 0)
|
||||||
|
return log_debug_errno(r, "Failed to read /etc/locale.conf: %m");
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
locale_context_clear(c);
|
||||||
|
|
||||||
|
/* Fill in what we got passed from systemd. */
|
||||||
|
for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) {
|
||||||
|
const char *name = ASSERT_PTR(locale_variable_to_string(p));
|
||||||
|
|
||||||
|
r = free_and_strdup(&c->locale[p], empty_to_null(getenv(name)));
|
||||||
|
if (r < 0)
|
||||||
|
return log_oom_debug();
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1; /* loaded */
|
||||||
|
}
|
||||||
|
|
||||||
int locale_context_load(LocaleContext *c, LocaleLoadFlag flag) {
|
int locale_context_load(LocaleContext *c, LocaleLoadFlag flag) {
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
assert(c);
|
assert(c);
|
||||||
|
|
||||||
if (FLAGS_SET(flag, LOCALE_LOAD_PROC_CMDLINE)) {
|
r = locale_context_load_proc(c, flag);
|
||||||
locale_context_clear(c);
|
if (r > 0)
|
||||||
|
|
||||||
r = proc_cmdline_get_key_many(PROC_CMDLINE_STRIP_RD_PREFIX,
|
|
||||||
"locale.LANG", &c->locale[VARIABLE_LANG],
|
|
||||||
"locale.LANGUAGE", &c->locale[VARIABLE_LANGUAGE],
|
|
||||||
"locale.LC_CTYPE", &c->locale[VARIABLE_LC_CTYPE],
|
|
||||||
"locale.LC_NUMERIC", &c->locale[VARIABLE_LC_NUMERIC],
|
|
||||||
"locale.LC_TIME", &c->locale[VARIABLE_LC_TIME],
|
|
||||||
"locale.LC_COLLATE", &c->locale[VARIABLE_LC_COLLATE],
|
|
||||||
"locale.LC_MONETARY", &c->locale[VARIABLE_LC_MONETARY],
|
|
||||||
"locale.LC_MESSAGES", &c->locale[VARIABLE_LC_MESSAGES],
|
|
||||||
"locale.LC_PAPER", &c->locale[VARIABLE_LC_PAPER],
|
|
||||||
"locale.LC_NAME", &c->locale[VARIABLE_LC_NAME],
|
|
||||||
"locale.LC_ADDRESS", &c->locale[VARIABLE_LC_ADDRESS],
|
|
||||||
"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 (FLAGS_SET(flag, LOCALE_LOAD_LOCALE_CONF)) {
|
|
||||||
struct stat st;
|
|
||||||
usec_t t;
|
|
||||||
|
|
||||||
r = stat("/etc/locale.conf", &st);
|
|
||||||
if (r < 0 && errno != ENOENT)
|
|
||||||
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)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
locale_context_clear(c);
|
|
||||||
c->mtime = t;
|
|
||||||
|
|
||||||
r = parse_env_file(NULL, "/etc/locale.conf",
|
|
||||||
"LANG", &c->locale[VARIABLE_LANG],
|
|
||||||
"LANGUAGE", &c->locale[VARIABLE_LANGUAGE],
|
|
||||||
"LC_CTYPE", &c->locale[VARIABLE_LC_CTYPE],
|
|
||||||
"LC_NUMERIC", &c->locale[VARIABLE_LC_NUMERIC],
|
|
||||||
"LC_TIME", &c->locale[VARIABLE_LC_TIME],
|
|
||||||
"LC_COLLATE", &c->locale[VARIABLE_LC_COLLATE],
|
|
||||||
"LC_MONETARY", &c->locale[VARIABLE_LC_MONETARY],
|
|
||||||
"LC_MESSAGES", &c->locale[VARIABLE_LC_MESSAGES],
|
|
||||||
"LC_PAPER", &c->locale[VARIABLE_LC_PAPER],
|
|
||||||
"LC_NAME", &c->locale[VARIABLE_LC_NAME],
|
|
||||||
"LC_ADDRESS", &c->locale[VARIABLE_LC_ADDRESS],
|
|
||||||
"LC_TELEPHONE", &c->locale[VARIABLE_LC_TELEPHONE],
|
|
||||||
"LC_MEASUREMENT", &c->locale[VARIABLE_LC_MEASUREMENT],
|
|
||||||
"LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION]);
|
|
||||||
if (r < 0)
|
|
||||||
return log_debug_errno(r, "Failed to read /etc/locale.conf: %m");
|
|
||||||
|
|
||||||
goto finalize;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (FLAGS_SET(flag, LOCALE_LOAD_ENVIRONMENT)) {
|
|
||||||
locale_context_clear(c);
|
|
||||||
|
|
||||||
/* Fill in what we got passed from systemd. */
|
|
||||||
for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) {
|
|
||||||
const char *name = ASSERT_PTR(locale_variable_to_string(p));
|
|
||||||
|
|
||||||
r = free_and_strdup(&c->locale[p], empty_to_null(getenv(name)));
|
|
||||||
if (r < 0)
|
|
||||||
return log_oom_debug();
|
|
||||||
}
|
|
||||||
|
|
||||||
goto finalize;
|
goto finalize;
|
||||||
}
|
|
||||||
|
|
||||||
/* Nothing loaded. */
|
r = locale_context_load_conf(c, flag);
|
||||||
locale_context_clear(c);
|
if (r != 0)
|
||||||
return 0;
|
goto finalize;
|
||||||
|
|
||||||
|
r = locale_context_load_env(c, flag);
|
||||||
|
|
||||||
finalize:
|
finalize:
|
||||||
|
if (r < 0)
|
||||||
|
return r;
|
||||||
|
|
||||||
|
if (r == 0) {
|
||||||
|
/* Nothing loaded. */
|
||||||
|
locale_context_clear(c);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (FLAGS_SET(flag, LOCALE_LOAD_SIMPLIFY))
|
if (FLAGS_SET(flag, LOCALE_LOAD_SIMPLIFY))
|
||||||
locale_variables_simplify(c->locale);
|
locale_variables_simplify(c->locale);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user