From a43eddbdf4d5dad5de987b3e32621ae2b9e84420 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 1 May 2020 19:39:07 +0200 Subject: [PATCH] user-record: split out code that generates automatic image path for records No change of behaviour, just some refactoring, so that we can use this new helper function elswhere, too. --- src/shared/user-record.c | 46 ++++++++++++++++++++++++++-------------- src/shared/user-record.h | 2 ++ 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/shared/user-record.c b/src/shared/user-record.c index f648311208..83d86f69e7 100644 --- a/src/shared/user-record.c +++ b/src/shared/user-record.c @@ -1059,7 +1059,34 @@ static int dispatch_status(const char *name, JsonVariant *variant, JsonDispatchF return json_dispatch(m, status_dispatch_table, NULL, flags, userdata); } +int user_record_build_image_path(UserStorage storage, const char *user_name_and_realm, char **ret) { + const char *suffix; + char *z; + + assert(storage >= 0); + assert(user_name_and_realm); + assert(ret); + + if (storage == USER_LUKS) + suffix = ".home"; + else if (IN_SET(storage, USER_DIRECTORY, USER_SUBVOLUME, USER_FSCRYPT)) + suffix = ".homedir"; + else { + *ret = NULL; + return 0; + } + + z = strjoin("/home/", user_name_and_realm, suffix); + if (!z) + return -ENOMEM; + + *ret = z; + return 1; +} + static int user_record_augment(UserRecord *h, JsonDispatchFlags json_flags) { + int r; + assert(h); if (!FLAGS_SET(h->mask, USER_RECORD_REGULAR)) @@ -1084,22 +1111,9 @@ static int user_record_augment(UserRecord *h, JsonDispatchFlags json_flags) { } if (!h->image_path && !h->image_path_auto) { - const char *suffix; - UserStorage storage; - - storage = user_record_storage(h); - if (storage == USER_LUKS) - suffix = ".home"; - else if (IN_SET(storage, USER_DIRECTORY, USER_SUBVOLUME, USER_FSCRYPT)) - suffix = ".homedir"; - else - suffix = NULL; - - if (suffix) { - h->image_path_auto = strjoin("/home/", user_record_user_name_and_realm(h), suffix); - if (!h->image_path_auto) - return json_log_oom(h->json, json_flags); - } + r = user_record_build_image_path(user_record_storage(h), user_record_user_name_and_realm(h), &h->image_path_auto); + if (r < 0) + return json_log(h->json, json_flags, r, "Failed to determine default image path: %m"); } return 0; diff --git a/src/shared/user-record.h b/src/shared/user-record.h index 83c5a71d4e..9fd10610d9 100644 --- a/src/shared/user-record.h +++ b/src/shared/user-record.h @@ -349,6 +349,8 @@ usec_t user_record_ratelimit_interval_usec(UserRecord *h); uint64_t user_record_ratelimit_burst(UserRecord *h); bool user_record_can_authenticate(UserRecord *h); +int user_record_build_image_path(UserStorage storage, const char *user_name_and_realm, char **ret); + bool user_record_equal(UserRecord *a, UserRecord *b); bool user_record_compatible(UserRecord *a, UserRecord *b); int user_record_compare_last_change(UserRecord *a, UserRecord *b);