]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
repart: rework --empty= handling a bit
authorLennart Poettering <lennart@poettering.net>
Fri, 29 Sep 2023 14:09:50 +0000 (16:09 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 5 Oct 2023 17:04:40 +0000 (19:04 +0200)
Introduce a new enum value EMPTY_UNSET to which arg_empty now is set
initially. Only after we finished parsing the command line we'll now set
this to EMPTY_REFUSE as before.

This prepares ground for later changes, where we then can make different
decisions after havig all input from the command line.

As of now this doesn't change behaviour of systemd-repart, it just
rearranges things a bit.

src/partition/repart.c

index 9b56444404898f57a08cf5bc739ac7ac746d7b58..8ba6a9cb05bbabcf2ca2f314a0c0536b0b8ca02e 100644 (file)
  * waste 3K per partition, which is probably fine. */
 
 static enum {
+        EMPTY_UNSET,    /* no choice has been made yet */
         EMPTY_REFUSE,   /* refuse empty disks, never create a partition table */
         EMPTY_ALLOW,    /* allow empty disks, create partition table if necessary */
         EMPTY_REQUIRE,  /* require an empty disk, create a partition table */
         EMPTY_FORCE,    /* make disk empty, erase everything, create a partition table always */
         EMPTY_CREATE,   /* create disk as loopback file, create a partition table always */
-} arg_empty = EMPTY_REFUSE;
+} arg_empty = EMPTY_UNSET;
 
 typedef enum FilterPartitionType {
         FILTER_PARTITIONS_NONE,
@@ -2416,6 +2417,9 @@ static int context_load_partition_table(Context *context) {
                 /* Always reinitiaize the disk, don't consider what there was on the disk before */
                 from_scratch = true;
                 break;
+
+        case EMPTY_UNSET:
+                assert_not_reached();
         }
 
         if (from_scratch) {
@@ -6481,7 +6485,9 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_EMPTY:
-                        if (isempty(optarg) || streq(optarg, "refuse"))
+                        if (isempty(optarg))
+                                arg_empty = EMPTY_UNSET;
+                        else if (streq(optarg, "refuse"))
                                 arg_empty = EMPTY_REFUSE;
                         else if (streq(optarg, "allow"))
                                 arg_empty = EMPTY_ALLOW;
@@ -6795,6 +6801,9 @@ static int parse_argv(int argc, char *argv[]) {
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                        "Expected at most one argument, the path to the block device.");
 
+        if (arg_empty == EMPTY_UNSET) /* default to refuse mode, if not otherwise specified */
+                arg_empty = EMPTY_REFUSE;
+
         if (arg_factory_reset > 0 && IN_SET(arg_empty, EMPTY_FORCE, EMPTY_REQUIRE, EMPTY_CREATE))
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                        "Combination of --factory-reset=yes and --empty=force/--empty=require/--empty=create is invalid.");