]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
homectl: split out helper parse_rebalance_weight
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 12 Jan 2026 10:43:06 +0000 (11:43 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 13 Jan 2026 16:59:54 +0000 (17:59 +0100)
src/home/homectl.c

index aa6bbec1458fba335b74093c93ec129f7c88f8c2..9df62d6dd7b4ed751d349b9cb7fd3c35cff3bdfe 100644 (file)
@@ -3373,6 +3373,41 @@ static int parse_auto_resize_mode_field(sd_json_variant **identity, const char *
         return parse_string_field(identity, field, arg);
 }
 
+static int parse_rebalance_weight(sd_json_variant **identity, const char *field, const char *arg) {
+        int r;
+
+        assert(identity);
+        assert(field);
+
+        if (isempty(arg))
+                return drop_from_identity(field);
+
+        uint64_t u;
+        if (streq(arg, "off"))
+                u = REBALANCE_WEIGHT_OFF;
+        else {
+                r = safe_atou64(arg, &u);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to parse rebalance weight parameter: %s", arg);
+
+                if (u < REBALANCE_WEIGHT_MIN || u > REBALANCE_WEIGHT_MAX)
+                        return log_error_errno(SYNTHETIC_ERRNO(ERANGE),
+                                               "Rebalancing weight out of valid range %" PRIu64 "%s%" PRIu64 ": %s",
+                                               REBALANCE_WEIGHT_MIN, glyph(GLYPH_ELLIPSIS), REBALANCE_WEIGHT_MAX,
+                                               arg);
+        }
+
+        /* Drop from per machine stuff and everywhere */
+        r = drop_from_identity(field);
+        if (r < 0)
+                return r;
+
+        r = sd_json_variant_set_field_unsigned(identity, field, u);
+        if (r < 0)
+                return log_error_errno(r, "Failed to set %s field: %m", field);
+        return 0;
+}
+
 static int parse_rlimit_field(sd_json_variant **identity, const char *field, const char *arg) {
         int r;
 
@@ -4636,40 +4671,12 @@ static int parse_argv(int argc, char *argv[]) {
                                 return r;
                         break;
 
-                case ARG_REBALANCE_WEIGHT: {
-                        uint64_t u;
-
-                        if (isempty(optarg)) {
-                                r = drop_from_identity("rebalanceWeight");
-                                if (r < 0)
-                                        return r;
-                                break;
-                        }
-
-                        if (streq(optarg, "off"))
-                                u = REBALANCE_WEIGHT_OFF;
-                        else {
-                                r = safe_atou64(optarg, &u);
-                                if (r < 0)
-                                        return log_error_errno(r, "Failed to parse --rebalance-weight= argument: %s", optarg);
-
-                                if (u < REBALANCE_WEIGHT_MIN || u > REBALANCE_WEIGHT_MAX)
-                                        return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Rebalancing weight out of valid range %" PRIu64 "%s%" PRIu64 ": %s",
-                                                               REBALANCE_WEIGHT_MIN, glyph(GLYPH_ELLIPSIS), REBALANCE_WEIGHT_MAX, optarg);
-                        }
-
-                        /* Drop from per machine stuff and everywhere */
-                        r = drop_from_identity("rebalanceWeight");
+                case ARG_REBALANCE_WEIGHT:
+                        r = parse_rebalance_weight(match_identity ?: &arg_identity_extra,
+                                                   "rebalanceWeight", optarg);
                         if (r < 0)
                                 return r;
-
-                        /* Add to main identity */
-                        r = sd_json_variant_set_field_unsigned(match_identity ?: &arg_identity_extra, "rebalanceWeight", u);
-                        if (r < 0)
-                                return log_error_errno(r, "Failed to set rebalanceWeight field: %m");
-
                         break;
-                }
 
                 case 'j':
                         arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO;