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.
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) {
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);
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;
return 0;
}
-int context_make_offline(
+static int context_make_offline(
Context **ret,
const char *node,
const char *component,
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);
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;
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;
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;