From df4f2e769ce72a2364e96f2a1a766e03a84d3bdc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Mon, 29 Jan 2024 19:48:02 -0500
Subject: [PATCH] fileutils: handle more cases in createFileLink

* handle case where linkFile == target
* hanlde case where linkFile already exists
* keep link if it already points to target

Change-Id: I01bd866332a29847d787cf55c6d1b8925c02b41a
---
 src/fileutils.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/fileutils.cpp b/src/fileutils.cpp
index cefa41f747..5a37dc307d 100644
--- a/src/fileutils.cpp
+++ b/src/fileutils.cpp
@@ -195,6 +195,14 @@ createHardlink(const std::filesystem::path& linkFile, const std::filesystem::pat
 bool
 createFileLink(const std::filesystem::path& linkFile, const std::filesystem::path& target, bool hard)
 {
+    if (linkFile == target)
+        return true;
+    std::error_code ec;
+    if (std::filesystem::exists(linkFile, ec)) {
+        if (std::filesystem::is_symlink(linkFile, ec) && std::filesystem::read_symlink(linkFile, ec) == target)
+            return true;
+        std::filesystem::remove(linkFile, ec);
+    }
     if (not hard or not createHardlink(linkFile, target))
         return createSymlink(linkFile, target);
     return true;
-- 
GitLab