1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "bootspec-fundamental.h"
4
bootspec_pick_name_version_sort_key(const sd_char * os_pretty_name,const sd_char * os_image_id,const sd_char * os_name,const sd_char * os_id,const sd_char * os_image_version,const sd_char * os_version,const sd_char * os_version_id,const sd_char * os_build_id,const sd_char ** ret_name,const sd_char ** ret_version,const sd_char ** ret_sort_key)5 sd_bool bootspec_pick_name_version_sort_key(
6 const sd_char *os_pretty_name,
7 const sd_char *os_image_id,
8 const sd_char *os_name,
9 const sd_char *os_id,
10 const sd_char *os_image_version,
11 const sd_char *os_version,
12 const sd_char *os_version_id,
13 const sd_char *os_build_id,
14 const sd_char **ret_name,
15 const sd_char **ret_version,
16 const sd_char **ret_sort_key) {
17
18 const sd_char *good_name, *good_version, *good_sort_key;
19
20 /* Find the best human readable title, version string and sort key for a boot entry (using the
21 * os-release(5) fields). Precise is preferred over vague, and human readable over machine
22 * readable. Thus:
23 *
24 * 1. First priority gets the PRETTY_NAME field, which is the primary string intended for display,
25 * and should already contain both a nice description and a version indication (if that concept
26 * applies).
27 *
28 * 2. Otherwise we go for IMAGE_ID and IMAGE_VERSION (thus we show details about the image,
29 * i.e. specific combination of packages and configuration), if that concept applies.
30 *
31 * 3. Otherwise we go for NAME and VERSION (i.e. human readable OS name and version)
32 *
33 * 4. Otherwise we go for ID and VERSION_ID (i.e. machine readable OS name and version)
34 *
35 * 5. Finally, for the version we'll use BUILD_ID (i.e. a machine readable version that identifies
36 * the original OS build used during installation)
37 *
38 * Note that the display logic will show only the name by default, except if that isn't unique in
39 * which case the version is shown too.
40 *
41 * Note that name/version determined here are used only for display purposes. Boot entry preference
42 * sorting (i.e. algorithmic ordering of boot entries) is done based on the order of the sort key (if
43 * defined) or entry "id" string (i.e. entry file name) otherwise. */
44
45 good_name = os_pretty_name ?: (os_image_id ?: (os_name ?: os_id));
46 good_version = os_image_version ?: (os_version ?: (os_version_id ? : os_build_id));
47 good_sort_key = os_image_id ?: os_id;
48
49 if (!good_name || !good_version)
50 return sd_false;
51
52 if (ret_name)
53 *ret_name = good_name;
54
55 if (ret_version)
56 *ret_version = good_version;
57
58 if (ret_sort_key)
59 *ret_sort_key = good_sort_key;
60
61 return sd_true;
62 }
63