changeset 2674:6ee0280e8c15

Fix #544: copy symlinks as symlinks instead of dereferencing them https://github.com/BestImageViewer/geeqie/issues/544 Relative symbolic links and the Move operation are still not handled
author gqpwruser <>
date Sat, 25 Nov 2017 17:08:34 +0000
parents bef1f1a91df9
children 5580d9c6e929
files src/ui_fileops.c
diffstat 1 files changed, 54 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/ui_fileops.c	Fri Nov 24 10:27:12 2017 +0000
+++ b/src/ui_fileops.c	Sat Nov 25 17:08:34 2017 +0000
@@ -535,6 +535,60 @@
 		goto end;
 		}
 
+	/* Do not dereference absolute symlinks, but copy them "as is".
+	* For a relative symlink, we don't know how to properly change it when
+	* copied/moved to another dir to keep pointing it to same target as
+	* a relative symlink, so we turn it into absolute symlink using
+	* realpath() instead. */
+	struct stat st;
+	if (lstat_utf8(sl, &st) && S_ISLNK(st.st_mode))
+		{
+		gchar *link_target;
+
+		link_target = g_malloc(PATH_MAX + 1);
+		readlink(sl, link_target, st.st_size);
+		link_target[st.st_size] = '\0';
+
+		if (link_target[0] != G_DIR_SEPARATOR) // if it is a relative symlink
+			{
+			gchar *absolute;
+
+			absolute = g_malloc(PATH_MAX + 1);
+			char *lastslash = strrchr(sl, G_DIR_SEPARATOR);
+			int len = lastslash - sl + 1;
+
+			strncpy(absolute, sl, len);
+			strcpy(absolute + len, link_target);
+			strcpy(link_target, absolute);
+
+			char *realPath;
+			realPath = realpath(link_target, absolute);
+
+			if (realPath != NULL) // successfully resolved into an absolute path
+				{
+				g_free(link_target);
+				link_target = absolute;
+				}
+			else                 // could not get absolute path, got some error instead
+				{
+				g_free(absolute);
+				goto orig_copy;  // so try a "normal" copy
+				}
+			}
+
+		if (stat_utf8(tl, &st)) unlink(tl); // first try to remove directory entry in destination directory if such entry exists
+
+		gint success = (symlink(link_target, tl) == 0);
+		g_free(link_target);
+
+		if (success)
+			{
+			ret = TRUE;
+			goto end;
+			}
+		} // if symlink did not succeed, continue on to try a copy procedure
+	orig_copy:
+
 	fi = fopen(sl, "rb");
 	if (!fi) goto end;