]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared: modernize drop_in_file() a bit
authorLennart Poettering <lennart@poettering.net>
Tue, 15 Oct 2024 13:34:35 +0000 (15:34 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 15 Oct 2024 16:20:57 +0000 (18:20 +0200)
Make the return parameters optional, since we don't actually need them
in all cases (see later commits).

src/shared/dropin.c

index c3050634bd32c3348d9c757bfead72ba6cf6f4ad..de93e120e09dfce9418075737523a604a19dfa83 100644 (file)
@@ -31,14 +31,11 @@ int drop_in_file(
                 char **ret_unit_dir,
                 char **ret_path) {
 
-        char prefix[DECIMAL_STR_MAX(unsigned) + 1] = {};
-        _cleanup_free_ char *n = NULL, *unit_dir = NULL, *path = NULL;
+        _cleanup_free_ char *n = NULL, *unit_dir = NULL;
 
         assert(dir);
         assert(unit);
         assert(name);
-        assert(ret_unit_dir);
-        assert(ret_path);
 
         n = xescape(name, "/.");
         if (!n)
@@ -46,16 +43,28 @@ int drop_in_file(
         if (!filename_is_valid(n))
                 return -EINVAL;
 
-        if (level != UINT_MAX)
-                xsprintf(prefix, "%u-", level);
+        if (ret_unit_dir || ret_path) {
+                unit_dir = path_join(dir, strjoina(unit, ".d"));
+                if (!unit_dir)
+                        return -ENOMEM;
+        }
 
-        unit_dir = path_join(dir, strjoina(unit, ".d"));
-        path = strjoin(unit_dir, "/", prefix, n, ".conf");
-        if (!unit_dir || !path)
-                return -ENOMEM;
+        if (ret_path) {
+                char prefix[DECIMAL_STR_MAX(unsigned) + 1] = {};
+
+                if (level != UINT_MAX)
+                        xsprintf(prefix, "%u-", level);
+
+                _cleanup_free_ char *path = strjoin(unit_dir, "/", prefix, n, ".conf");
+                if (!path)
+                        return -ENOMEM;
+
+                *ret_path = TAKE_PTR(path);
+        }
+
+        if (ret_unit_dir)
+                *ret_unit_dir = TAKE_PTR(unit_dir);
 
-        *ret_unit_dir = TAKE_PTR(unit_dir);
-        *ret_path = TAKE_PTR(path);
         return 0;
 }