]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sysupdate: Factor context creation out of installdb_cleanup_component()
authorPhilip Withnall <pwithnall@gnome.org>
Tue, 23 Jun 2026 15:23:54 +0000 (16:23 +0100)
committerPhilip Withnall <pwithnall@gnome.org>
Fri, 26 Jun 2026 09:43:51 +0000 (10:43 +0100)
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.

src/sysupdate/sysupdate-cleanup.c
src/sysupdate/sysupdate-cleanup.h
src/sysupdate/sysupdate.c
src/sysupdate/sysupdate.h

index 74e2a38119e792ecc59f7d61f97b83a484a8937b..5c517e401ac27dd3d9abda79c7c0d108a7a245b0 100644 (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) {
index 98fbb0a83b7aacf7ee75fa03c54daf39e1699969..cb3c7b64445201df13ebfee6319c2f4100b8184c 100644 (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);
index 9f78ee78bf3cb31efa395b611a6cec8b44b1cefc..151aa24d57cbd58eed65f9a14a3f11236a84b502 100644 (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;
index b925d4f5a214be550bc266c5e5998e281b3458d0..68b8a79687686cd77651267d889d7bc40e1947a0 100644 (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;