]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
userdbctl: split out parse_from_file()
authorZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Thu, 7 May 2026 20:55:00 +0000 (22:55 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Fri, 8 May 2026 10:17:53 +0000 (12:17 +0200)
parse_from_file doesn't set arg_from_file itself, but returns a
sd_json_variant ref to the caller. I think the change of arg_from_file
is more readable with this structure.

src/userdb/userdbctl.c

index 6b4371aa509b1de4a08acf9d2be220f42b197441..75b30215b1da314c81cdd981ebfc67313b4e5262 100644 (file)
@@ -19,6 +19,7 @@
 #include "format-util.h"
 #include "fs-util.h"
 #include "io-util.h"
+#include "json-util.h"
 #include "log.h"
 #include "main-func.h"
 #include "mkdir.h"
@@ -1590,6 +1591,30 @@ static int verb_help(int argc, char *argv[], uintptr_t _data, void *userdata) {
         return help();
 }
 
+static int parse_from_file(const char *arg, sd_json_variant **ret) {
+        sd_json_variant *v = NULL;
+        int r;
+
+        assert(ret);
+
+        if (!isempty(arg)) {
+                const char *fn = streq(arg, "-") ? NULL : arg;
+                unsigned line = 0;
+                r = sd_json_parse_file(
+                                fn ? NULL : stdin,
+                                fn ?: "<stdin>",
+                                SD_JSON_PARSE_MUST_BE_OBJECT | SD_JSON_PARSE_SENSITIVE,
+                                &v,
+                                &line,
+                                /* reterr_column= */ NULL);
+                if (r < 0)
+                        return log_syntax(/* unit= */ NULL, LOG_ERR, fn ?: "<stdin>", line, r, "JSON parse failure.");
+        }
+
+        *ret = v;
+        return 0;
+}
+
 static int parse_argv(int argc, char *argv[]) {
 
         enum {
@@ -1832,20 +1857,13 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case 'F': {
-                        if (isempty(optarg)) {
-                                arg_from_file = sd_json_variant_unref(arg_from_file);
-                                break;
-                        }
+                        sd_json_variant *v = NULL;  /* initialization to appease gcc-14 */
 
-                        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
-                        const char *fn = streq(optarg, "-") ? NULL : optarg;
-                        unsigned line = 0;
-                        r = sd_json_parse_file(fn ? NULL : stdin, fn ?: "<stdin>", SD_JSON_PARSE_MUST_BE_OBJECT|SD_JSON_PARSE_SENSITIVE, &v, &line, /* reterr_column= */ NULL);
+                        r = parse_from_file(optarg, &v);
                         if (r < 0)
-                                return log_syntax(/* unit= */ NULL, LOG_ERR, fn ?: "<stdin>", line, r, "JSON parse failure.");
+                                return r;
 
-                        sd_json_variant_unref(arg_from_file);
-                        arg_from_file = TAKE_PTR(v);
+                        json_variant_unref_and_replace(arg_from_file, v);
                         break;
                 }