15a207
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
15a207
From: Jonathan Lebon <jlebon@redhat.com>
15a207
Date: Mon, 14 Aug 2017 14:37:20 -0400
15a207
Subject: [PATCH] editenv: handle relative symlinks
15a207
15a207
Handle symlinks with targets relative to the containing dir. This
15a207
ensures that the rename operation does not depend on the cwd.
15a207
15a207
Resolves: rhbz#1479960
15a207
15a207
Signed-off-by: Jonathan Lebon <jlebon@redhat.com>
15a207
---
15a207
 util/editenv.c | 16 ++++++++++++++--
15a207
 1 file changed, 14 insertions(+), 2 deletions(-)
15a207
15a207
diff --git a/util/editenv.c b/util/editenv.c
e15314
index e61dc1283a4..1f7f6f3ae18 100644
15a207
--- a/util/editenv.c
15a207
+++ b/util/editenv.c
15a207
@@ -28,6 +28,7 @@
15a207
 
15a207
 #include <errno.h>
15a207
 #include <string.h>
15a207
+#include <libgen.h>
15a207
 
15a207
 #define DEFAULT_ENVBLK_SIZE	1024
15a207
 
e15314
@@ -88,9 +89,20 @@ grub_util_create_envblk_file (const char *name)
15a207
 	  continue;
15a207
 	}
15a207
 
15a207
-      free (rename_target);
15a207
       linkbuf[retsize] = '\0';
15a207
-      rename_target = linkbuf;
15a207
+      if (linkbuf[0] == '/')
15a207
+        {
15a207
+          free (rename_target);
15a207
+          rename_target = linkbuf;
15a207
+        }
15a207
+      else
15a207
+        {
15a207
+          char *dbuf = xstrdup (rename_target);
15a207
+          const char *dir = dirname (dbuf);
15a207
+          free (rename_target);
15a207
+          rename_target = xasprintf("%s/%s", dir, linkbuf);
15a207
+          free (dbuf);
15a207
+        }
15a207
     }
15a207
 
15a207
   int rc = grub_util_rename (namenew, rename_target);