]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
import: introduce ImportFlags flags field
authorLennart Poettering <lennart@poettering.net>
Fri, 22 Jan 2021 16:10:50 +0000 (17:10 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 22 Jan 2021 19:54:51 +0000 (20:54 +0100)
This merges the two flags that are passed to the ImportTar/ImportRaw
objects into a single flags parameter, which we then can extend more
easily later on.

No change in behaviour.

This is inspired by 133b34f69a72dc90d4e336837d699245390c9f50 which does
the same for PullTar/PullRaw.

src/import/import-common.h
src/import/import-raw.c
src/import/import-raw.h
src/import/import-tar.c
src/import/import-tar.h
src/import/import.c

index b27a980839040bf68ca86770366f6873b67bbc3e..d7e8fc485f31e87f0776afa50b3698d5c76044d1 100644 (file)
@@ -3,6 +3,13 @@
 
 #include <sys/types.h>
 
+typedef enum ImportFlags {
+        IMPORT_FORCE     = 1 << 0, /* replace existing image */
+        IMPORT_READ_ONLY = 1 << 1, /* make generated image read-only */
+
+        IMPORT_FLAGS_MASK = IMPORT_FORCE|IMPORT_READ_ONLY,
+} ImportFlags;
+
 int import_make_read_only_fd(int fd);
 int import_make_read_only(const char *path);
 
index c0869ffcf96cf2b19c86523838f2f342fd195a69..f6ee86d62570d3f562c5b4b97acd27bac72d7fb5 100644 (file)
@@ -34,8 +34,7 @@ struct RawImport {
         void *userdata;
 
         char *local;
-        bool force_local;
-        bool read_only;
+        ImportFlags flags;
 
         char *temp_path;
         char *final_path;
@@ -213,13 +212,13 @@ static int raw_import_finish(RawImport *i) {
                 (void) copy_xattr(i->input_fd, i->output_fd);
         }
 
-        if (i->read_only) {
+        if (i->flags & IMPORT_READ_ONLY) {
                 r = import_make_read_only_fd(i->output_fd);
                 if (r < 0)
                         return r;
         }
 
-        if (i->force_local)
+        if (i->flags & IMPORT_FORCE)
                 (void) rm_rf(i->final_path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
 
         r = rename_noreplace(AT_FDCWD, i->temp_path, AT_FDCWD, i->final_path);
@@ -386,12 +385,13 @@ static int raw_import_on_defer(sd_event_source *s, void *userdata) {
         return raw_import_process(i);
 }
 
-int raw_import_start(RawImport *i, int fd, const char *local, bool force_local, bool read_only) {
+int raw_import_start(RawImport *i, int fd, const char *local, ImportFlags flags) {
         int r;
 
         assert(i);
         assert(fd >= 0);
         assert(local);
+        assert(!(flags & ~IMPORT_FLAGS_MASK));
 
         if (!hostname_is_valid(local, 0))
                 return -EINVAL;
@@ -406,8 +406,8 @@ int raw_import_start(RawImport *i, int fd, const char *local, bool force_local,
         r = free_and_strdup(&i->local, local);
         if (r < 0)
                 return r;
-        i->force_local = force_local;
-        i->read_only = read_only;
+
+        i->flags = flags;
 
         if (fstat(fd, &i->st) < 0)
                 return -errno;
index 4612a9ffef162e7cde69690a9417a00cae1e2e7a..e99703c15597c1d0dd27da288e7fbb0e1eaa206f 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "sd-event.h"
 
+#include "import-common.h"
 #include "import-util.h"
 #include "macro.h"
 
@@ -15,4 +16,4 @@ RawImport* raw_import_unref(RawImport *import);
 
 DEFINE_TRIVIAL_CLEANUP_FUNC(RawImport*, raw_import_unref);
 
-int raw_import_start(RawImport *i, int fd, const char *local, bool force_local, bool read_only);
+int raw_import_start(RawImport *i, int fd, const char *local, ImportFlags flags);
index d68ce916130d382e22efdd3ad5c32d0cb468e04e..cc86bebb0e95dbde5f24f653ec4e33e9b4f322e2 100644 (file)
@@ -36,8 +36,7 @@ struct TarImport {
         void *userdata;
 
         char *local;
-        bool force_local;
-        bool read_only;
+        ImportFlags flags;
 
         char *temp_path;
         char *final_path;
@@ -183,13 +182,13 @@ static int tar_import_finish(TarImport *i) {
         if (r < 0)
                 return r;
 
-        if (i->read_only) {
+        if (i->flags & IMPORT_READ_ONLY) {
                 r = import_make_read_only(i->temp_path);
                 if (r < 0)
                         return r;
         }
 
-        if (i->force_local)
+        if (i->flags & IMPORT_FORCE)
                 (void) rm_rf(i->final_path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
 
         r = rename_noreplace(AT_FDCWD, i->temp_path, AT_FDCWD, i->final_path);
@@ -322,12 +321,13 @@ static int tar_import_on_defer(sd_event_source *s, void *userdata) {
         return tar_import_process(i);
 }
 
-int tar_import_start(TarImport *i, int fd, const char *local, bool force_local, bool read_only) {
+int tar_import_start(TarImport *i, int fd, const char *local, ImportFlags flags) {
         int r;
 
         assert(i);
         assert(fd >= 0);
         assert(local);
+        assert(!(flags & ~IMPORT_FLAGS_MASK));
 
         if (!hostname_is_valid(local, 0))
                 return -EINVAL;
@@ -342,8 +342,8 @@ int tar_import_start(TarImport *i, int fd, const char *local, bool force_local,
         r = free_and_strdup(&i->local, local);
         if (r < 0)
                 return r;
-        i->force_local = force_local;
-        i->read_only = read_only;
+
+        i->flags = flags;
 
         if (fstat(fd, &i->st) < 0)
                 return -errno;
index afbe98ad055d1f41d3d561439211db827c7f80c5..63b0bd4da6cca7aef13b4dfe79e16da9ae1ef01b 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "sd-event.h"
 
+#include "import-common.h"
 #include "import-util.h"
 #include "macro.h"
 
@@ -15,4 +16,4 @@ TarImport* tar_import_unref(TarImport *import);
 
 DEFINE_TRIVIAL_CLEANUP_FUNC(TarImport*, tar_import_unref);
 
-int tar_import_start(TarImport *import, int fd, const char *local, bool force_local, bool read_only);
+int tar_import_start(TarImport *import, int fd, const char *local, ImportFlags flags);
index d358eec49c426df1b57f13d935861f089afda20e..842e1142f824b445e3eb87eb967bb7e57d2f0319 100644 (file)
@@ -19,9 +19,8 @@
 #include "string-util.h"
 #include "verbs.h"
 
-static bool arg_force = false;
-static bool arg_read_only = false;
 static const char *arg_image_root = "/var/lib/machines";
+static ImportFlags arg_import_flags = 0;
 
 static int interrupt_signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
         log_notice("Transfer aborted.");
@@ -67,7 +66,7 @@ static int import_tar(int argc, char *argv[], void *userdata) {
                                                "Local image name '%s' is not valid.",
                                                local);
 
-                if (!arg_force) {
+                if (!FLAGS_SET(arg_import_flags, IMPORT_FORCE)) {
                         r = image_find(IMAGE_MACHINE, local, NULL, NULL);
                         if (r < 0) {
                                 if (r != -ENOENT)
@@ -110,7 +109,7 @@ static int import_tar(int argc, char *argv[], void *userdata) {
         if (r < 0)
                 return log_error_errno(r, "Failed to allocate importer: %m");
 
-        r = tar_import_start(import, fd, local, arg_force, arg_read_only);
+        r = tar_import_start(import, fd, local, arg_import_flags);
         if (r < 0)
                 return log_error_errno(r, "Failed to import image: %m");
 
@@ -160,7 +159,7 @@ static int import_raw(int argc, char *argv[], void *userdata) {
                                                "Local image name '%s' is not valid.",
                                                local);
 
-                if (!arg_force) {
+                if (!FLAGS_SET(arg_import_flags, IMPORT_FORCE)) {
                         r = image_find(IMAGE_MACHINE, local, NULL, NULL);
                         if (r < 0) {
                                 if (r != -ENOENT)
@@ -203,7 +202,7 @@ static int import_raw(int argc, char *argv[], void *userdata) {
         if (r < 0)
                 return log_error_errno(r, "Failed to allocate importer: %m");
 
-        r = raw_import_start(import, fd, local, arg_force, arg_read_only);
+        r = raw_import_start(import, fd, local, arg_import_flags);
         if (r < 0)
                 return log_error_errno(r, "Failed to import image: %m");
 
@@ -266,7 +265,7 @@ static int parse_argv(int argc, char *argv[]) {
                         return version();
 
                 case ARG_FORCE:
-                        arg_force = true;
+                        arg_import_flags |= IMPORT_FORCE;
                         break;
 
                 case ARG_IMAGE_ROOT:
@@ -274,7 +273,7 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_READ_ONLY:
-                        arg_read_only = true;
+                        arg_import_flags |= IMPORT_READ_ONLY;
                         break;
 
                 case '?':