sysupdate: Factor context creation out of installdb_cleanup_component()

This makes it like all the other verbs and therefore easier to refactor.

At the same time, remove the separate `component` argument and instead
use the `component` set on the `Context`. This guards against bugs, as
various parts of the `Context` state depend on the component (for
example, `installdb_fd`) and overriding the component without also
overriding its dependent variables will lead to bugs.
This commit is contained in:
Philip Withnall
2026-06-23 16:23:54 +01:00
parent 23bfc4b9e8
commit 7f3c27128b
4 changed files with 34 additions and 23 deletions

View File

@@ -352,30 +352,23 @@ static int context_installdb_process_entry(
return context_installdb_process_directory(c, path, /* relpath= */ NULL, dir_fd, de, pattern);
}
int installdb_cleanup_component(const char *node, const char *component) {
int installdb_cleanup_component(Context *context) {
int r;
_cleanup_(context_freep) Context* context = NULL;
r = context_make_offline(
&context,
node,
component,
/* read_definitions_flags= */ 0);
if (r < 0)
return r;
assert(context);
r = context_installdb_acquire_fd(context, /* make= */ false);
if (r < 0)
return r;
if (r == 0) {
log_debug("Not cleaning up component '%s', install database is empty.", strna(component));
log_debug("Not cleaning up component '%s', install database is empty.", strna(context->component));
return 0;
}
_cleanup_free_ DirectoryEntries *de = NULL;
r = readdir_all(context->installdb_fd, RECURSE_DIR_ENSURE_TYPE|RECURSE_DIR_MUST_BE_SYMLINK, &de);
if (r < 0)
return log_error_errno(r, "Failed to enumerate install database for component '%s': %m", strna(component));
return log_error_errno(r, "Failed to enumerate install database for component '%s': %m", strna(context->component));
int ret = 0;
FOREACH_ARRAY(_d, de->entries, de->n_entries) {

View File

@@ -5,5 +5,5 @@
int context_installdb_record(Context *c, const char *path, char **patterns);
int installdb_cleanup_component(const char *node, const char *component);
int installdb_cleanup_component(Context *context);
int installdb_list_components(char ***ret);

View File

@@ -171,6 +171,11 @@ static int read_definitions(
return 0;
}
typedef enum ReadDefinitionsFlags {
READ_DEFINITIONS_REQUIRES_ENABLED_TRANSFERS = 1 << 0,
READ_DEFINITIONS_REQUIRES_ANY_TRANSFERS = 1 << 1,
} ReadDefinitionsFlags;
static int context_read_definitions(Context *c, const char* node, ReadDefinitionsFlags flags) {
_cleanup_strv_free_ char **dirs = NULL;
int r;
@@ -926,7 +931,7 @@ static int context_vacuum(
return 0;
}
int context_make_offline(
static int context_make_offline(
Context **ret,
const char *node,
const char *component,
@@ -1929,6 +1934,7 @@ static int verb_components(int argc, char *argv[], uintptr_t _data, void *userda
VERB_NOARG(verb_cleanup, "cleanup", "Clean up orphaned files");
static int verb_cleanup(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(context_freep) Context* context = NULL;
int r;
assert(argc <= 1);
@@ -1944,8 +1950,16 @@ static int verb_cleanup(int argc, char *argv[], uintptr_t _data, void *userdata)
const char *node = loop_device ? loop_device->node : NULL;
r = context_make_offline(
&context,
node,
arg_component,
/* read_definitions_flags= */ 0);
if (r < 0)
return r;
int ret = 0;
RET_GATHER(ret, installdb_cleanup_component(node, arg_component));
RET_GATHER(ret, installdb_cleanup_component(context));
if (arg_component_all) {
_cleanup_strv_free_ char **z = NULL;
@@ -1953,8 +1967,19 @@ static int verb_cleanup(int argc, char *argv[], uintptr_t _data, void *userdata)
if (r < 0)
return log_error_errno(r, "Failed to enumerate components: %m");
STRV_FOREACH(i, z)
RET_GATHER(ret, installdb_cleanup_component(node, *i));
STRV_FOREACH(i, z) {
_cleanup_(context_freep) Context* component_context = NULL;
r = context_make_offline(
&component_context,
node,
*i,
/* read_definitions_flags= */ 0);
if (r < 0)
return r;
RET_GATHER(ret, installdb_cleanup_component(component_context));
}
}
return ret;

View File

@@ -28,13 +28,6 @@ typedef struct Context {
Context* context_free(Context *c);
DEFINE_TRIVIAL_CLEANUP_FUNC(Context*, context_free);
typedef enum ReadDefinitionsFlags {
READ_DEFINITIONS_REQUIRES_ENABLED_TRANSFERS = 1 << 0,
READ_DEFINITIONS_REQUIRES_ANY_TRANSFERS = 1 << 1,
} ReadDefinitionsFlags;
int context_make_offline(Context **ret, const char *node, const char *component, ReadDefinitionsFlags read_definitions_flags);
extern bool arg_sync;
extern uint64_t arg_instances_max;
extern char *arg_root;