From 385093b702d5950e7f7203405fc152206b8d77e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 24 Mar 2020 12:17:43 +0100 Subject: [PATCH] Split out generator directory setup to a src/core/generator-setup.c Those functions have only one non-test user, so we can move them to src/core/. --- src/core/generator-setup.c | 58 ++++++++++++++++++++++++++++++++++++++ src/core/generator-setup.h | 8 ++++++ src/core/manager.c | 1 + src/core/meson.build | 2 ++ src/shared/path-lookup.c | 54 ----------------------------------- src/shared/path-lookup.h | 5 ---- 6 files changed, 69 insertions(+), 59 deletions(-) create mode 100644 src/core/generator-setup.c create mode 100644 src/core/generator-setup.h diff --git a/src/core/generator-setup.c b/src/core/generator-setup.c new file mode 100644 index 00000000000..78ff5909020 --- /dev/null +++ b/src/core/generator-setup.c @@ -0,0 +1,58 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ + +#include + +#include "generator-setup.h" +#include "macro.h" +#include "mkdir.h" +#include "rm-rf.h" + +int lookup_paths_mkdir_generator(LookupPaths *p) { + int r, q; + + assert(p); + + if (!p->generator || !p->generator_early || !p->generator_late) + return -EINVAL; + + r = mkdir_p_label(p->generator, 0755); + + q = mkdir_p_label(p->generator_early, 0755); + if (q < 0 && r >= 0) + r = q; + + q = mkdir_p_label(p->generator_late, 0755); + if (q < 0 && r >= 0) + r = q; + + return r; +} + +void lookup_paths_trim_generator(LookupPaths *p) { + assert(p); + + /* Trim empty dirs */ + + if (p->generator) + (void) rmdir(p->generator); + if (p->generator_early) + (void) rmdir(p->generator_early); + if (p->generator_late) + (void) rmdir(p->generator_late); +} + +void lookup_paths_flush_generator(LookupPaths *p) { + assert(p); + + /* Flush the generated unit files in full */ + + if (p->generator) + (void) rm_rf(p->generator, REMOVE_ROOT|REMOVE_PHYSICAL); + if (p->generator_early) + (void) rm_rf(p->generator_early, REMOVE_ROOT|REMOVE_PHYSICAL); + if (p->generator_late) + (void) rm_rf(p->generator_late, REMOVE_ROOT|REMOVE_PHYSICAL); + + if (p->temporary_dir) + (void) rm_rf(p->temporary_dir, REMOVE_ROOT|REMOVE_PHYSICAL); +} diff --git a/src/core/generator-setup.h b/src/core/generator-setup.h new file mode 100644 index 00000000000..9688601a76e --- /dev/null +++ b/src/core/generator-setup.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ +#pragma once + +#include "path-lookup.h" + +int lookup_paths_mkdir_generator(LookupPaths *p); +void lookup_paths_trim_generator(LookupPaths *p); +void lookup_paths_flush_generator(LookupPaths *p); diff --git a/src/core/manager.c b/src/core/manager.c index 4c59addfc8e..148df8d523b 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -44,6 +44,7 @@ #include "fd-util.h" #include "fileio.h" #include "fs-util.h" +#include "generator-setup.h" #include "hashmap.h" #include "install.h" #include "io-util.h" diff --git a/src/core/meson.build b/src/core/meson.build index 3586838f59b..0b31bb79ed0 100644 --- a/src/core/meson.build +++ b/src/core/meson.build @@ -72,6 +72,8 @@ libcore_sources = ''' emergency-action.h execute.c execute.h + generator-setup.c + generator-setup.h hostname-setup.c hostname-setup.h ima-setup.c diff --git a/src/shared/path-lookup.c b/src/shared/path-lookup.c index 2707dce7a83..5e85b74a052 100644 --- a/src/shared/path-lookup.c +++ b/src/shared/path-lookup.c @@ -6,19 +6,15 @@ #include "alloc-util.h" #include "fs-util.h" -#include "install.h" #include "log.h" #include "macro.h" -#include "mkdir.h" #include "path-lookup.h" #include "path-util.h" -#include "rm-rf.h" #include "stat-util.h" #include "string-util.h" #include "strv.h" #include "tmpfile-util.h" #include "user-util.h" -#include "util.h" int xdg_user_runtime_dir(char **ret, const char *suffix) { const char *e; @@ -783,56 +779,6 @@ void lookup_paths_log(LookupPaths *p) { } } -int lookup_paths_mkdir_generator(LookupPaths *p) { - int r, q; - - assert(p); - - if (!p->generator || !p->generator_early || !p->generator_late) - return -EINVAL; - - r = mkdir_p_label(p->generator, 0755); - - q = mkdir_p_label(p->generator_early, 0755); - if (q < 0 && r >= 0) - r = q; - - q = mkdir_p_label(p->generator_late, 0755); - if (q < 0 && r >= 0) - r = q; - - return r; -} - -void lookup_paths_trim_generator(LookupPaths *p) { - assert(p); - - /* Trim empty dirs */ - - if (p->generator) - (void) rmdir(p->generator); - if (p->generator_early) - (void) rmdir(p->generator_early); - if (p->generator_late) - (void) rmdir(p->generator_late); -} - -void lookup_paths_flush_generator(LookupPaths *p) { - assert(p); - - /* Flush the generated unit files in full */ - - if (p->generator) - (void) rm_rf(p->generator, REMOVE_ROOT|REMOVE_PHYSICAL); - if (p->generator_early) - (void) rm_rf(p->generator_early, REMOVE_ROOT|REMOVE_PHYSICAL); - if (p->generator_late) - (void) rm_rf(p->generator_late, REMOVE_ROOT|REMOVE_PHYSICAL); - - if (p->temporary_dir) - (void) rm_rf(p->temporary_dir, REMOVE_ROOT|REMOVE_PHYSICAL); -} - char **generator_binary_paths(UnitFileScope scope) { bool append = false; /* Add items from SYSTEMD_GENERATOR_PATH before normal directories */ _cleanup_strv_free_ char **paths = NULL; diff --git a/src/shared/path-lookup.h b/src/shared/path-lookup.h index b99e918144b..52f7a570a29 100644 --- a/src/shared/path-lookup.h +++ b/src/shared/path-lookup.h @@ -64,11 +64,6 @@ bool path_is_user_data_dir(const char *path); bool path_is_user_config_dir(const char *path); void lookup_paths_log(LookupPaths *p); - -int lookup_paths_mkdir_generator(LookupPaths *p); -void lookup_paths_trim_generator(LookupPaths *p); -void lookup_paths_flush_generator(LookupPaths *p); - void lookup_paths_free(LookupPaths *p); char **generator_binary_paths(UnitFileScope scope);