HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/sysupdate/sysupdate.c
Git blob: 53c690b9bc5a42764e46b95cc8a991b63a15dadc
Lines 1–1000 of 3,595
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <unistd.h>
#include "sd-bus.h"
#include "sd-daemon.h"
#include "sd-json.h"
#include "sd-varlink.h"
#include "build.h"
#include "bus-polkit.h"
#include "condition.h"
#include "conf-files.h"
#include "conf-parser.h"
#include "constants.h"
#include "discover-image.h"
#include "dissect-image.h"
#include "dlopen-note.h"
#include "dropin.h"
#include "env-util.h"
#include "errno-util.h"
#include "fd-util.h"
#include "format-table.h"
#include "glyph-util.h"
#include "hashmap.h"
#include "hexdecoct.h"
#include "image-policy.h"
#include "json-util.h"
#include "loop-util.h"
#include "main-func.h"
#include "mount-util.h"
#include "os-util.h"
#include "pager.h"
#include "parse-argument.h"
#include "parse-util.h"
#include "path-util.h"
#include "pretty-print.h"
#include "runtime-scope.h"
#include "set.h"
#include "siphash24.h"
#include "sort-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "strv.h"
#include "sysupdate.h"
#include "sysupdate-cleanup.h"
#include "sysupdate-config.h"
#include "sysupdate-feature.h"
#include "sysupdate-instance.h"
#include "sysupdate-target.h"
#include "sysupdate-transfer.h"
#include "sysupdate-update-set.h"
#include "sysupdate-util.h"
#include "varlink-io.systemd.SysUpdate.h"
#include "varlink-util.h"
#include "verbs.h"
static char *arg_definitions = NULL;
static bool arg_sync = true;
static uint64_t arg_instances_max = UINT64_MAX;
static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
static PagerFlags arg_pager_flags = 0;
static bool arg_legend = true;
static char *arg_root = NULL;
static char *arg_image = NULL;
static bool arg_reboot = false;
static int arg_cleanup = -1;
static SelectMode arg_feature_select = SELECT_EXPLICIT;
static char *arg_component = NULL;
static SelectMode arg_component_select = SELECT_EXPLICIT;
static int arg_verify = -1;
static ImagePolicy *arg_image_policy = NULL;
static bool arg_offline = false;
static char *arg_transfer_source = NULL;
static bool arg_varlink = false;
STATIC_DESTRUCTOR_REGISTER(arg_definitions, freep);
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
STATIC_DESTRUCTOR_REGISTER(arg_component, freep);
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
STATIC_DESTRUCTOR_REGISTER(arg_transfer_source, freep);
COMMAND(
"systemd-sysupdate\0",
"Update OS images.",
.man_pages = "systemd-sysupdate(8)\0",
.pager_flags = &arg_pager_flags,
);
#define CONTEXT_NULL \
(Context) { \
.component_enabled = true, \
.sync = true, \
.instances_max = UINT64_MAX, \
.verify = -1, \
.offline = true, \
.cleanup = -1, \
.installdb_fd = -EBADF, \
.target_identifier.class = _TARGET_CLASS_INVALID, \
.component_suggest = -1, \
}
void context_done(Context *c) {
assert(c);
c->mounted_dir = umount_and_rmdir_and_free(c->mounted_dir);
c->loop_device = loop_device_unref(c->loop_device);
FOREACH_ARRAY(tr, c->transfers, c->n_transfers)
transfer_free(*tr);
c->transfers = mfree(c->transfers);
c->n_transfers = 0;
FOREACH_ARRAY(tr, c->disabled_transfers, c->n_disabled_transfers)
transfer_free(*tr);
c->disabled_transfers = mfree(c->disabled_transfers);
c->n_disabled_transfers = 0;
c->features = hashmap_free(c->features);
FOREACH_ARRAY(us, c->update_sets, c->n_update_sets)
update_set_free(*us);
c->update_sets = mfree(c->update_sets);
c->n_update_sets = 0;
c->web_cache = hashmap_free(c->web_cache);
c->installdb_fd = safe_close(c->installdb_fd);
c->definitions = mfree(c->definitions);
c->root = mfree(c->root);
c->image = mfree(c->image);
c->component = mfree(c->component);
c->component_description = mfree(c->component_description);
c->component_documentation = strv_free(c->component_documentation);
c->image_policy = image_policy_free(c->image_policy);
c->transfer_source = mfree(c->transfer_source);
target_identifier_done(&c->target_identifier);
condition_free_list(c->component_suggest_on);
}
static int context_from_cmdline(Context *ret) {
assert(ret);
_cleanup_(context_done) Context context = CONTEXT_NULL;
context.instances_max = arg_instances_max;
context.sync = arg_sync;
context.reboot = arg_reboot;
context.verify = arg_verify;
context.offline = arg_offline;
context.cleanup = arg_cleanup;
context.component_select = arg_component_select;
context.feature_select = arg_feature_select;
if (strdup_to(&context.definitions, arg_definitions) < 0)
return log_oom();
if (strdup_to(&context.root, arg_root) < 0)
return log_oom();
if (strdup_to(&context.image, arg_image) < 0)
return log_oom();
if (strdup_to(&context.component, arg_component) < 0)
return log_oom();
if (strdup_to(&context.transfer_source, arg_transfer_source) < 0)
return log_oom();
if (arg_image_policy) {
context.image_policy = image_policy_copy(arg_image_policy);
if (!context.image_policy)
return log_oom();
}
*ret = TAKE_GENERIC(context, Context, CONTEXT_NULL);
return 0;
}
static int context_from_base_with_component(const Context *base, const char *component, Context *ret) {
assert(base);
assert(component);
assert(ret);
/* Copies the specified context, but changes the component to the specified one */
_cleanup_(context_done) Context context = CONTEXT_NULL;
context.instances_max = base->instances_max;
context.sync = base->sync;
context.reboot = base->reboot;
context.verify = base->verify;
context.offline = base->offline;
context.cleanup = base->cleanup;
context.feature_select = base->feature_select;
if (strdup_to(&context.root, base->root) < 0)
return log_oom();
if (strdup_to(&context.image, base->image) < 0)
return log_oom();
if (strdup_to(&context.transfer_source, base->transfer_source) < 0)
return log_oom();
if (base->image_policy) {
context.image_policy = image_policy_copy(base->image_policy);
if (!context.image_policy)
return log_oom();
}
if (strdup_to(&context.component, component) < 0)
return log_oom();
/* NB: we do not copy .loop_device/.mounted_dir here, since that's for lifetime tracking only, and
* not needed for access (we only need to copy .root/.image) for that. Under the assumption that the
* contexts initialized via context_from_base_with_component() have a shorter lifetime than the
* contexts they are copied from we don't bother with lifetime tracking of the loopback device/mount
* point here. */
*ret = TAKE_GENERIC(context, Context, CONTEXT_NULL);
return 0;
}
/* Stores any long-running server state which needs to persist between varlink calls, such as state for
* pending polkit requests */
typedef struct Server {
sd_bus *system_bus;
Hashmap *polkit_registry;
} Server;
#define SERVER_NULL \
(Server) { \
/* all fields fine with being initialised to NULL */ \
}
static void server_done(Server *s) {
assert(s);
s->polkit_registry = hashmap_free(s->polkit_registry);
s->system_bus = sd_bus_flush_close_unref(s->system_bus);
}
static DEFINE_POINTER_ARRAY_FREE_FUNC(Transfer*, transfer_free);
static int read_features(
Context *c,
const char **dirs) {
int r;
assert(c);
ConfFile **files = NULL;
size_t n_files = 0;
CLEANUP_ARRAY(files, n_files, conf_file_free_array);
r = conf_files_list_strv_full(
".feature",
c->root,
CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED|CONF_FILES_WARN|CONF_FILES_DONT_PREFIX_ROOT,
dirs,
&files,
&n_files);
if (r < 0)
return log_error_errno(r, "Failed to enumerate sysupdate.d/*.feature definitions: %m");
FOREACH_ARRAY(i, files, n_files) {
ConfFile *e = *i;
_cleanup_(feature_unrefp) Feature *f = feature_new();
if (!f)
return log_oom();
r = feature_read_definition(f, c->root, e->result, dirs);
if (r < 0)
return r;
r = hashmap_ensure_put(&c->features, &feature_hash_ops, f->id, f);
if (r < 0)
return log_error_errno(r, "Failed to insert feature '%s' into map: %m", f->id);
TAKE_PTR(f);
}
return 0;
}
static int read_transfers(
Context *c,
const char **dirs,
const char *suffix,
const char *node) {
ConfFile **files = NULL;
Transfer **transfers = NULL, **disabled = NULL;
size_t n_files = 0, n_transfers = 0, n_disabled = 0;
int r;
CLEANUP_ARRAY(files, n_files, conf_file_free_array);
CLEANUP_ARRAY(transfers, n_transfers, transfer_free_array);
CLEANUP_ARRAY(disabled, n_disabled, transfer_free_array);
assert(c);
assert(dirs);
assert(suffix);
r = conf_files_list_strv_full(suffix, c->root,
CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED|CONF_FILES_WARN|CONF_FILES_DONT_PREFIX_ROOT,
dirs, &files, &n_files);
if (r < 0)
return log_error_errno(r, "Failed to enumerate sysupdate.d/*%s definitions: %m", suffix);
FOREACH_ARRAY(i, files, n_files) {
_cleanup_(transfer_freep) Transfer *t = NULL;
Transfer **appended;
ConfFile *e = *i;
t = transfer_new(c);
if (!t)
return log_oom();
r = transfer_read_definition(t, e->result, dirs, c->features);
if (r < 0)
return r;
r = transfer_resolve_paths(t, c->root, node);
if (r < 0)
return r;
if (t->enabled)
appended = GREEDY_REALLOC_APPEND(transfers, n_transfers, &t, 1);
else
appended = GREEDY_REALLOC_APPEND(disabled, n_disabled, &t, 1);
if (!appended)
return log_oom();
TAKE_PTR(t);
}
c->transfers = TAKE_PTR(transfers);
c->n_transfers = n_transfers;
c->disabled_transfers = TAKE_PTR(disabled);
c->n_disabled_transfers = n_disabled;
return 0;
}
static int read_component(Context *c) {
int r;
assert(c);
/* Read a component description file, but only if we actually operate on a component */
if (c->definitions || !c->component)
return 0;
_cleanup_free_ char *j = strjoin("sysupdate.", c->component, ".component");
if (!j)
return log_oom();
ConfigTableItem table[] = {
{ "Component", "Description", config_parse_string, 0, &c->component_description },
{ "Component", "Documentation", config_parse_url_specifiers_many, 0, &c->component_documentation },
{ "Component", "Enabled", config_parse_bool, 0, &c->component_enabled },
{ "Component", "Suggest", config_parse_tristate, 0, &c->component_suggest },
{ "Component", "SuggestOnArchitecture", config_parse_condition, CONDITION_ARCHITECTURE, &c->component_suggest_on },
{ "Component", "SuggestOnFirmware", config_parse_condition, CONDITION_FIRMWARE, &c->component_suggest_on },
{ "Component", "SuggestOnVirtualization", config_parse_condition, CONDITION_VIRTUALIZATION, &c->component_suggest_on },
{ "Component", "SuggestOnHost", config_parse_condition, CONDITION_HOST, &c->component_suggest_on },
{ "Component", "SuggestOnFraction", config_parse_condition, CONDITION_FRACTION, &c->component_suggest_on },
{ "Component", "SuggestOnKernelCommandLine", config_parse_condition, CONDITION_KERNEL_COMMAND_LINE, &c->component_suggest_on },
{ "Component", "SuggestOnVersion", config_parse_condition, CONDITION_VERSION, &c->component_suggest_on },
{ "Component", "SuggestOnCredential", config_parse_condition, CONDITION_CREDENTIAL, &c->component_suggest_on },
{ "Component", "SuggestOnSecurity", config_parse_condition, CONDITION_SECURITY, &c->component_suggest_on },
{ "Component", "SuggestOnOSRelease", config_parse_condition, CONDITION_OS_RELEASE, &c->component_suggest_on },
{ "Component", "SuggestOnMachineTag", config_parse_condition, CONDITION_MACHINE_TAG, &c->component_suggest_on },
{}
};
r = config_parse_standard_file_with_dropins_full(
c->root,
/* root_fd= */ -EBADF,
j,
"Component\0",
config_item_table_lookup, table,
CONFIG_PARSE_WARN,
/* userdata= */ (void *) c->root,
/* ret_stats_by_path= */ NULL,
/* ret_dropin_files= */ NULL);
if (r < 0)
return r;
return 0;
}
typedef enum ReadDefinitionsFlags {
READ_DEFINITIONS_REQUIRES_ENABLED_TRANSFERS = 1 << 0, /* fail unless there's at least one enabled transfer */
READ_DEFINITIONS_REQUIRES_ANY_TRANSFERS = 1 << 1, /* fail unless there's at least one transfer */
READ_DEFINITIONS_REQUIRES_ENABLED_COMPONENT = 1 << 2, /* fail if component is disabled */
} ReadDefinitionsFlags;
static int context_read_definitions(Context *c, const char* node, ReadDefinitionsFlags flags) {
_cleanup_strv_free_ char **dirs = NULL;
int r;
assert(c);
if (c->definitions)
dirs = strv_new(c->definitions);
else if (c->component) {
char **l = CONF_PATHS_STRV("");
size_t i = 0;
dirs = new0(char*, strv_length(l) + 1);
if (!dirs)
return log_oom();
STRV_FOREACH(dir, l) {
char *j;
j = strjoin(*dir, "sysupdate.", c->component, ".d");
if (!j)
return log_oom();
dirs[i++] = j;
}
} else
dirs = strv_new(CONF_PATHS("sysupdate.d"));
if (!dirs)
return log_oom();
r = read_component(c);
if (r < 0)
return r;
r = read_features(c, (const char**) dirs);
if (r < 0)
return r;
r = read_transfers(c, (const char**) dirs, ".transfer", node);
if (r < 0)
return r;
if (c->n_transfers + c->n_disabled_transfers == 0) {
/* Backwards-compat: If no .transfer defs are found, fall back to trying .conf! */
r = read_transfers(c, (const char**) dirs, ".conf", node);
if (r < 0)
return r;
if (c->n_transfers + c->n_disabled_transfers > 0)
log_warning("As of v257, transfer definitions should have the '.transfer' extension.");
}
if (FLAGS_SET(flags, READ_DEFINITIONS_REQUIRES_ANY_TRANSFERS) &&
c->n_transfers + (FLAGS_SET(flags, READ_DEFINITIONS_REQUIRES_ENABLED_TRANSFERS) ? 0 : c->n_disabled_transfers) == 0) {
if (c->component)
return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
"No transfer definitions for component '%s' found.",
c->component);
return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
"No transfer definitions found.");
}
if (FLAGS_SET(flags, READ_DEFINITIONS_REQUIRES_ENABLED_COMPONENT) && !c->component_enabled)
return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN), "Component is disabled.");
return 0;
}
static int context_load_installed_instances(Context *c) {
int r;
assert(c);
log_debug("Discovering installed instances%s", glyph(GLYPH_ELLIPSIS));
FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
Transfer *t = *tr;
r = resource_load_instances(
&t->target,
c->verify >= 0 ? c->verify : t->verify,
&c->web_cache);
if (r < 0)
return r;
}
FOREACH_ARRAY(tr, c->disabled_transfers, c->n_disabled_transfers) {
Transfer *t = *tr;
r = resource_load_instances(
&t->target,
c->verify >= 0 ? c->verify : t->verify,
&c->web_cache);
if (r < 0)
return r;
}
return 0;
}
static int context_load_available_instances(Context *c) {
int r;
assert(c);
log_info("Discovering available instances%s", glyph(GLYPH_ELLIPSIS));
FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
Transfer *t = *tr;
r = resource_load_instances(
&t->source,
c->verify >= 0 ? c->verify : t->verify,
&c->web_cache);
if (r < 0)
return r;
}
return 0;
}
static int context_discover_update_sets_by_flag(Context *c, UpdateSetFlags flags) {
_cleanup_free_ char *boundary = NULL;
bool newest_found = false;
int r;
assert(c);
assert(IN_SET(flags, UPDATE_AVAILABLE, UPDATE_INSTALLED));
for (;;) {
_cleanup_free_ Instance **cursor_instances = NULL;
bool skip = false;
UpdateSetFlags extra_flags = 0;
_cleanup_free_ char *cursor = NULL;
UpdateSet *us = NULL;
/* First, let's find the newest version that's older than the boundary. */
FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
Resource *rr;
assert(*tr);
if (flags == UPDATE_AVAILABLE)
rr = &(*tr)->source;
else {
assert(flags == UPDATE_INSTALLED);
rr = &(*tr)->target;
}
FOREACH_ARRAY(inst, rr->instances, rr->n_instances) {
Instance *i = *inst; /* Sorted newest-to-oldest */
assert(i);
if (boundary && strverscmp_improved(i->metadata.version, boundary) >= 0)
continue; /* Not older than the boundary */
if (cursor && strverscmp_improved(i->metadata.version, cursor) <= 0)
break; /* Not newer than the cursor. The same will be true for all
* subsequent instances (due to sorting) so let's skip to the
* next transfer. */
if (free_and_strdup(&cursor, i->metadata.version) < 0)
return log_oom();
break; /* All subsequent instances will be older than this one */
}
if (flags == UPDATE_AVAILABLE && !cursor)
break; /* This transfer didn't have a version older than the boundary,
* so any older-than-boundary version that might exist in a different
* transfer must always be incomplete. For reasons described below,
* we don't include incomplete versions for AVAILABLE updates. So we
* are completely done looking. */
}
if (!cursor) /* We didn't find anything older than the boundary, so we're done. */
break;
cursor_instances = new0(Instance*, c->n_transfers);
if (!cursor_instances)
return log_oom();
/* Now let's find all the instances that match the version of the cursor, if we have them */
for (size_t k = 0; k < c->n_transfers; k++) {
Transfer *t = c->transfers[k];
Instance *match = NULL;
assert(t);
if (flags == UPDATE_AVAILABLE) {
match = resource_find_instance(&t->source, cursor);
if (!match) {
/* When we're looking for updates to download, we don't offer
* incomplete versions at all. The server wants to send us an update
* with parts of the OS missing. For robustness sake, let's not do
* that. */
skip = true;
break;
}
} else {
assert(flags == UPDATE_INSTALLED);
match = resource_find_instance(&t->target, cursor);
if (!match && !(extra_flags & (UPDATE_PARTIAL|UPDATE_PENDING)))
/* When we're looking for installed versions, let's be robust and treat
* an incomplete installation as an installation. Otherwise, there are
* situations that can lead to sysupdate wiping the currently booted OS.
* See https://github.com/systemd/systemd/issues/33339 */
extra_flags |= UPDATE_INCOMPLETE;
}
cursor_instances[k] = match;
if (t->min_version && strverscmp_improved(t->min_version, cursor) > 0)
extra_flags |= UPDATE_OBSOLETE;
if (strv_contains(t->protected_versions, cursor))
extra_flags |= UPDATE_PROTECTED;
/* Partial or pending updates by definition are not incomplete, they’re
* partial/pending instead. While an individual Instance cannot be both partial and
* pending, an UpdateSet as a whole can contain both partial and pending instances. */
assert(!match || !(match->is_partial && match->is_pending));
if (match && match->is_partial)
extra_flags = (extra_flags | UPDATE_PARTIAL) & ~UPDATE_INCOMPLETE;
if (match && match->is_pending)
extra_flags = (extra_flags | UPDATE_PENDING) & ~UPDATE_INCOMPLETE;
}
r = free_and_strdup_warn(&boundary, cursor);
if (r < 0)
return r;
if (skip)
continue;
/* See if we already have this update set in our table */
FOREACH_ARRAY(update_set, c->update_sets, c->n_update_sets) {
UpdateSet *u = *update_set;
if (strverscmp_improved(u->version, cursor) != 0)
continue;
/* Merge in what we've learned and continue onto the next version */
if (FLAGS_SET(u->flags, UPDATE_INCOMPLETE) ||
FLAGS_SET(u->flags, UPDATE_PARTIAL) ||
FLAGS_SET(u->flags, UPDATE_PENDING)) {
assert(u->n_instances == c->n_transfers);
/* Incomplete updates will have picked NULL instances for the transfers that
* are missing. Now we have more information, so let's try to fill them in. */
for (size_t j = 0; j < u->n_instances; j++) {
if (!u->instances[j])
u->instances[j] = cursor_instances[j];
/* Make sure that the list is full if the update is AVAILABLE */
assert(flags != UPDATE_AVAILABLE || u->instances[j]);
}
}
u->flags |= flags | extra_flags;
/* If this is the newest installed version, that is incomplete and just became marked
* as available, and if there is no other candidate available, we promote this to be
* the candidate. Ignore partial or pending status on the update set. */
if (FLAGS_SET(u->flags, UPDATE_NEWEST|UPDATE_INSTALLED|UPDATE_INCOMPLETE|UPDATE_AVAILABLE) &&
!c->candidate && !FLAGS_SET(u->flags, UPDATE_OBSOLETE))
c->candidate = u;
skip = true;
newest_found = true;
break;
}
if (skip)
continue;
/* Doesn't exist yet, let's add it */
if (!GREEDY_REALLOC(c->update_sets, c->n_update_sets + 1))
return log_oom();
us = new(UpdateSet, 1);
if (!us)
return log_oom();
*us = (UpdateSet) {
.flags = flags | (newest_found ? 0 : UPDATE_NEWEST) | extra_flags,
.version = TAKE_PTR(cursor),
.instances = TAKE_PTR(cursor_instances),
.n_instances = c->n_transfers,
};
c->update_sets[c->n_update_sets++] = us;
newest_found = true;
/* Remember which one is the newest installed */
if ((us->flags & (UPDATE_NEWEST|UPDATE_INSTALLED)) == (UPDATE_NEWEST|UPDATE_INSTALLED))
c->newest_installed = us;
/* Remember which is the newest non-obsolete, available (and not installed) version, which we declare the "candidate".
* It may be partial or pending. */
if ((us->flags & (UPDATE_NEWEST|UPDATE_INSTALLED|UPDATE_AVAILABLE|UPDATE_OBSOLETE)) == (UPDATE_NEWEST|UPDATE_AVAILABLE))
c->candidate = us;
}
/* Newest installed is newer than or equal to candidate? Then suppress the candidate */
if (c->newest_installed && !FLAGS_SET(c->newest_installed->flags, UPDATE_INCOMPLETE) &&
c->candidate && strverscmp_improved(c->newest_installed->version, c->candidate->version) >= 0)
c->candidate = NULL;
/* Newest installed is still pending or partial and no candidate is set? Then it becomes the candidate. */
if (c->newest_installed &&
(c->newest_installed->flags & (UPDATE_PENDING|UPDATE_PARTIAL)) &&
!c->candidate)
c->candidate = c->newest_installed;
return 0;
}
static int context_discover_update_sets(Context *c) {
int r;
assert(c);
log_info("Determining installed update sets%s", glyph(GLYPH_ELLIPSIS));
r = context_discover_update_sets_by_flag(c, UPDATE_INSTALLED);
if (r < 0)
return r;
if (!c->offline) {
log_info("Determining available update sets%s", glyph(GLYPH_ELLIPSIS));
r = context_discover_update_sets_by_flag(c, UPDATE_AVAILABLE);
if (r < 0)
return r;
}
typesafe_qsort(c->update_sets, c->n_update_sets, update_set_cmp);
return 0;
}
static int context_show_table(Context *c) {
_cleanup_(table_unrefp) Table *t = NULL;
int r;
assert(c);
t = table_new("", "version", "installed", "available", "assessment");
if (!t)
return log_oom();
(void) table_set_align_percent(t, table_get_cell(t, 0, 0), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 2), 50);
(void) table_set_align_percent(t, table_get_cell(t, 0, 3), 50);
FOREACH_ARRAY(update_set, c->update_sets, c->n_update_sets) {
UpdateSet *us = *update_set;
const char *color;
color = update_set_flags_to_color(us->flags);
r = table_add_many(t,
TABLE_STRING, update_set_flags_to_glyph(us->flags),
TABLE_SET_COLOR, color,
TABLE_STRING, us->version,
TABLE_SET_COLOR, color,
TABLE_STRING, glyph_check_mark_space(FLAGS_SET(us->flags, UPDATE_INSTALLED)),
TABLE_SET_COLOR, color,
TABLE_STRING, glyph_check_mark_space(FLAGS_SET(us->flags, UPDATE_AVAILABLE)),
TABLE_SET_COLOR, color,
TABLE_STRING, update_set_flags_to_string(us->flags),
TABLE_SET_COLOR, color);
if (r < 0)
return table_log_add_error(r);
}
return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
}
static UpdateSet* context_update_set_by_version(Context *c, const char *version) {
assert(c);
assert(version);
FOREACH_ARRAY(update_set, c->update_sets, c->n_update_sets)
if (streq((*update_set)->version, version))
return *update_set;
return NULL;
}
static int context_show_version(Context *c, const char *version) {
bool show_fs_columns = false, show_partition_columns = false,
have_fs_attributes = false, have_partition_attributes = false,
have_size = false, have_tries = false, have_no_auto = false,
have_read_only = false, have_growfs = false, have_sha256 = false;
_cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
_cleanup_(table_unrefp) Table *t = NULL;
_cleanup_strv_free_ char **changelog_urls = NULL;
UpdateSet *us;
int r;
assert(c);
assert(version);
us = context_update_set_by_version(c, version);
if (!us)
return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Update '%s' not found.", version);
if (arg_json_format_flags & (SD_JSON_FORMAT_OFF|SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_PRETTY_AUTO))
pager_open(arg_pager_flags);
t = table_new("type", "path", "ptuuid", "ptflags", "mtime", "mode", "size", "tries-done", "tries-left", "noauto", "ro", "growfs", "sha256");
if (!t)
return log_oom();
(void) table_set_align_percent(t, table_get_cell(t, 0, 3), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 4), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 5), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 6), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 7), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 8), 100);
table_set_ersatz_string(t, TABLE_ERSATZ_DASH);
/* Starting in v257, these fields would be automatically formatted with underscores. This would have
* been a breaking change, so to avoid that let's hard-code their original names. */
(void) table_set_json_field_name(t, 7, "tries-done");
(void) table_set_json_field_name(t, 8, "tries-left");
/* Determine if the target will make use of partition/fs attributes for any of the transfers */
FOREACH_ARRAY(transfer, c->transfers, c->n_transfers) {
Transfer *tr = *transfer;
if (tr->target.type == RESOURCE_PARTITION)
show_partition_columns = true;
if (RESOURCE_IS_FILESYSTEM(tr->target.type))
show_fs_columns = true;
STRV_FOREACH(changelog, tr->changelog) {
assert(*changelog);
_cleanup_free_ char *changelog_url = strreplace(*changelog, "@v", version);
if (!changelog_url)
return log_oom();
/* Avoid duplicates */
if (strv_contains(changelog_urls, changelog_url))
continue;
/* changelog_urls takes ownership of expanded changelog_url */
r = strv_consume(&changelog_urls, TAKE_PTR(changelog_url));
if (r < 0)
return log_oom();
}
}
FOREACH_ARRAY(inst, us->instances, us->n_instances) {
Instance *i = *inst;
if (!i) {
assert(us->flags & (UPDATE_INCOMPLETE|UPDATE_PARTIAL|UPDATE_PENDING));
continue;
}
r = table_add_many(t,
TABLE_STRING, resource_type_to_string(i->resource->type),
TABLE_PATH, i->path);
if (r < 0)
return table_log_add_error(r);
if (i->metadata.partition_uuid_set) {
have_partition_attributes = true;
r = table_add_cell(t, NULL, TABLE_UUID, &i->metadata.partition_uuid);
} else
r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return table_log_add_error(r);
if (i->metadata.partition_flags_set) {
have_partition_attributes = true;
r = table_add_cell(t, NULL, TABLE_UINT64_HEX, &i->metadata.partition_flags);
} else
r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return table_log_add_error(r);
if (i->metadata.mtime != USEC_INFINITY) {
have_fs_attributes = true;
r = table_add_cell(t, NULL, TABLE_TIMESTAMP, &i->metadata.mtime);
} else
r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return table_log_add_error(r);
if (i->metadata.mode != MODE_INVALID) {
have_fs_attributes = true;
r = table_add_cell(t, NULL, TABLE_MODE, &i->metadata.mode);
} else
r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return table_log_add_error(r);
if (i->metadata.size != UINT64_MAX) {
have_size = true;
r = table_add_cell(t, NULL, TABLE_SIZE, &i->metadata.size);
} else
r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return table_log_add_error(r);
if (i->metadata.tries_done != UINT64_MAX) {
have_tries = true;
r = table_add_cell(t, NULL, TABLE_UINT64, &i->metadata.tries_done);
} else
r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return table_log_add_error(r);
if (i->metadata.tries_left != UINT64_MAX) {
have_tries = true;
r = table_add_cell(t, NULL, TABLE_UINT64, &i->metadata.tries_left);
} else
r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return table_log_add_error(r);
if (i->metadata.no_auto >= 0) {
bool b;
have_no_auto = true;
b = i->metadata.no_auto;
r = table_add_cell(t, NULL, TABLE_BOOLEAN, &b);
} else
r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return table_log_add_error(r);
if (i->metadata.read_only >= 0) {
bool b;
have_read_only = true;
b = i->metadata.read_only;
r = table_add_cell(t, NULL, TABLE_BOOLEAN, &b);
} else
r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return table_log_add_error(r);
if (i->metadata.growfs >= 0) {
bool b;
have_growfs = true;
b = i->metadata.growfs;
r = table_add_cell(t, NULL, TABLE_BOOLEAN, &b);
} else
r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return table_log_add_error(r);
if (i->metadata.sha256sum_set) {
_cleanup_free_ char *formatted = NULL;
have_sha256 = true;
formatted = hexmem(i->metadata.sha256sum, sizeof(i->metadata.sha256sum));
if (!formatted)
return log_oom();
r = table_add_cell(t, NULL, TABLE_STRING, formatted);
} else
r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return table_log_add_error(r);
}
/* Hide the fs/partition columns if we don't have any data to show there */
if (!have_fs_attributes)
show_fs_columns = false;
if (!have_partition_attributes)
show_partition_columns = false;
if (!show_partition_columns)
(void) table_hide_column_from_display(t, 2, 3);
if (!show_fs_columns)
(void) table_hide_column_from_display(t, 4, 5);
if (!have_size)
(void) table_hide_column_from_display(t, 6);
if (!have_tries)
(void) table_hide_column_from_display(t, 7, 8);
if (!have_no_auto)
(void) table_hide_column_from_display(t, 9);