locale: split out checking existence of keymap

No functional change, preparation for later commits.
This commit is contained in:
Yu Watanabe
2023-02-02 01:50:37 +09:00
parent 3017b9b1a6
commit 384f22e39f
3 changed files with 47 additions and 9 deletions

View File

@@ -224,6 +224,46 @@ int vc_context_copy(VCContext *dest, const VCContext *src) {
return modified;
}
static int verify_keymap(const char *keymap, int log_level, sd_bus_error *error) {
int r;
assert(keymap);
r = keymap_exists(keymap); /* This also verifies that the keymap name is kosher. */
if (r < 0) {
if (error)
sd_bus_error_set_errnof(error, r, "Failed to check keymap %s: %m", keymap);
return log_full_errno(log_level, r, "Failed to check keymap %s: %m", keymap);
}
if (r == 0) {
if (error)
sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Keymap %s is not installed.", keymap);
return log_full_errno(log_level, SYNTHETIC_ERRNO(ENOENT), "Keymap %s is not installed.", keymap);
}
return 0;
}
int vc_context_verify_and_warn(const VCContext *vc, int log_level, sd_bus_error *error) {
int r;
assert(vc);
if (vc->keymap) {
r = verify_keymap(vc->keymap, log_level, error);
if (r < 0)
return r;
}
if (vc->toggle) {
r = verify_keymap(vc->toggle, log_level, error);
if (r < 0)
return r;
}
return 0;
}
void context_clear(Context *c) {
assert(c);

View File

@@ -52,6 +52,10 @@ bool vc_context_isempty(const VCContext *vc);
void vc_context_empty_to_null(VCContext *vc);
bool vc_context_equal(const VCContext *a, const VCContext *b);
int vc_context_copy(VCContext *dest, const VCContext *src);
int vc_context_verify_and_warn(const VCContext *vc, int log_level, sd_bus_error *error);
static inline int vc_context_verify(const VCContext *vc) {
return vc_context_verify_and_warn(vc, LOG_DEBUG, NULL);
}
int find_converted_keymap(const X11Context *xc, char **ret);
int find_legacy_keymap(const X11Context *xc, char **ret);

View File

@@ -378,15 +378,9 @@ static int method_set_vc_keyboard(sd_bus_message *m, void *userdata, sd_bus_erro
vc_context_empty_to_null(&in);
FOREACH_STRING(name, in.keymap ?: in.toggle, in.keymap ? in.toggle : NULL) {
r = keymap_exists(name); /* This also verifies that the keymap name is kosher. */
if (r < 0) {
log_error_errno(r, "Failed to check keymap %s: %m", name);
return sd_bus_error_set_errnof(error, r, "Failed to check keymap %s: %m", name);
}
if (r == 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Keymap %s is not installed.", name);
}
r = vc_context_verify_and_warn(&in, LOG_ERR, error);
if (r < 0)
return r;
r = vconsole_read_data(c, m);
if (r < 0) {