8d419f
From 9b655d805e78a314a24b8493c6c116a4d943beb9 Mon Sep 17 00:00:00 2001
8d419f
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
8d419f
Date: Fri, 11 Mar 2022 14:27:46 +0100
8d419f
Subject: [PATCH] install: when linking a file, create the link first or abort
8d419f
8d419f
We'd create aliases and other symlinks first, and only then try to create
8d419f
the main link. Since that can fail, let's do things in opposite order, and
8d419f
abort immediately if we can't link the file itself.
8d419f
8d419f
(cherry picked from commit 20d68b3aec62110351bdc695fd1a55adcf3a6ee5)
8d419f
8d419f
Related: #2082131
8d419f
---
8d419f
 src/shared/install.c          | 12 +++++++-----
8d419f
 test/test-systemctl-enable.sh |  9 +++------
8d419f
 2 files changed, 10 insertions(+), 11 deletions(-)
8d419f
8d419f
diff --git a/src/shared/install.c b/src/shared/install.c
8d419f
index 785ef55dbd..fadd2be248 100644
8d419f
--- a/src/shared/install.c
8d419f
+++ b/src/shared/install.c
8d419f
@@ -1985,6 +1985,13 @@ static int install_info_apply(
8d419f
 
8d419f
         bool force = file_flags & UNIT_FILE_FORCE;
8d419f
 
8d419f
+        r = install_info_symlink_link(info, lp, config_path, force, changes, n_changes);
8d419f
+        /* Do not count links to the unit file towards the "carries_install_info" count */
8d419f
+        if (r < 0)
8d419f
+                /* If linking of the file failed, do not try to create other symlinks,
8d419f
+                 * because they might would pointing to a non-existent or wrong unit. */
8d419f
+                return r;
8d419f
+
8d419f
         r = install_info_symlink_alias(scope, info, lp, config_path, force, changes, n_changes);
8d419f
 
8d419f
         q = install_info_symlink_wants(scope, file_flags, info, lp, config_path, info->wanted_by, ".wants/", changes, n_changes);
8d419f
@@ -1995,11 +2002,6 @@ static int install_info_apply(
8d419f
         if (r == 0)
8d419f
                 r = q;
8d419f
 
8d419f
-        q = install_info_symlink_link(info, lp, config_path, force, changes, n_changes);
8d419f
-        /* Do not count links to the unit file towards the "carries_install_info" count */
8d419f
-        if (r == 0 && q < 0)
8d419f
-                r = q;
8d419f
-
8d419f
         return r;
8d419f
 }
8d419f
 
8d419f
diff --git a/test/test-systemctl-enable.sh b/test/test-systemctl-enable.sh
8d419f
index 3aa61222a8..c1fb9626ab 100644
8d419f
--- a/test/test-systemctl-enable.sh
8d419f
+++ b/test/test-systemctl-enable.sh
8d419f
@@ -173,12 +173,9 @@ islink "$root/etc/systemd/system/link1.path" "/link1.path"
8d419f
 islink "$root/etc/systemd/system/paths.target.wants/link1.path" "/link1.path"
8d419f
 
8d419f
 : -------enable already linked different path-----------------
8d419f
-# FIXME
8d419f
-# "$systemctl" --root="$root" enable '/subdir/link1.path' && { echo "Expected failure" >&2; exit 1; }
8d419f
-# test -h "$root/etc/systemd/system/link1.path"
8d419f
-# readlink "$root/etc/systemd/system/link1.path"
8d419f
-# test -h "$root/etc/systemd/system/paths.target.wants/link1.path"
8d419f
-# readlink "$root/etc/systemd/system/paths.target.wants/link1.path"
8d419f
+"$systemctl" --root="$root" enable '/subdir/link1.path' && { echo "Expected failure" >&2; exit 1; }
8d419f
+islink "$root/etc/systemd/system/link1.path" "/link1.path"
8d419f
+islink "$root/etc/systemd/system/paths.target.wants/link1.path" "/link1.path"
8d419f
 
8d419f
 : -------enable bad suffix------------------------------------
8d419f
 cp "$root/link1.path" "$root/subdir/link1.suffix"