Commit 50194dcda14a515bc3bbddee68c4092ca778d4dd

Patrick Steinhardt 2019-07-11T15:14:42

win32: fix symlinks to relative file targets When creating a symlink in Windows, one needs to tell Windows whether the symlink should be a file or directory symlink. To determine which flag to pass, we call `GetFileAttributesW` on the target file to see whether it is a directory and then pass the flag accordingly. The problem though is if create a symlink with a relative target path, then we will check that relative path while not necessarily being inside of the working directory where the symlink is to be created. Thus, getting its attributes will either fail or return attributes of the wrong target. Fix this by resolving the target path relative to the directory in which the symlink is to be created.

diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index 4de3217..078b509 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -414,18 +414,37 @@ int p_readlink(const char *path, char *buf, size_t bufsiz)
 	return (int)bufsiz;
 }
 
+static bool target_is_dir(const char *target, const char *path)
+{
+	git_buf resolved = GIT_BUF_INIT;
+	git_win32_path resolved_w;
+	bool isdir = true;
+
+	if (git_path_is_absolute(target))
+		git_win32_path_from_utf8(resolved_w, target);
+	else if (git_path_dirname_r(&resolved, path) < 0 ||
+		 git_path_apply_relative(&resolved, target) < 0 ||
+		 git_win32_path_from_utf8(resolved_w, resolved.ptr) < 0)
+		goto out;
+
+	isdir = GetFileAttributesW(resolved_w) & FILE_ATTRIBUTE_DIRECTORY;
+
+out:
+	git_buf_dispose(&resolved);
+	return isdir;
+}
+
 int p_symlink(const char *target, const char *path)
 {
 	git_win32_path target_w, path_w;
 	DWORD dwFlags;
 
 	if (git_win32_path_from_utf8(path_w, path) < 0 ||
-		git__utf8_to_16(target_w, MAX_PATH, target) < 0)
+	    git__utf8_to_16(target_w, MAX_PATH, target) < 0)
 		return -1;
 
 	dwFlags = SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
-
-	if (GetFileAttributesW(target_w) & FILE_ATTRIBUTE_DIRECTORY)
+	if (target_is_dir(target, path))
 		dwFlags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
 
 	if (!CreateSymbolicLinkW(path_w, target_w, dwFlags))
diff --git a/tests/core/posix.c b/tests/core/posix.c
index 10d689a..dcc619f 100644
--- a/tests/core/posix.c
+++ b/tests/core/posix.c
@@ -285,3 +285,27 @@ void test_core_posix__unlink_removes_symlink(void)
 	cl_must_pass(p_unlink("file"));
 	cl_must_pass(p_rmdir("dir"));
 }
+
+void test_core_posix__symlink_resolves_to_correct_type(void)
+{
+	git_buf contents = GIT_BUF_INIT;
+
+	if (!git_path_supports_symlinks(clar_sandbox_path()))
+		clar__skip();
+
+	cl_must_pass(git_futils_mkdir("dir", 0777, 0));
+	cl_must_pass(git_futils_mkdir("file", 0777, 0));
+	cl_git_mkfile("dir/file", "symlink target");
+
+	cl_git_pass(p_symlink("file", "dir/link"));
+
+	cl_git_pass(git_futils_readbuffer(&contents, "dir/file"));
+	cl_assert_equal_s(contents.ptr, "symlink target");
+
+	cl_must_pass(p_unlink("dir/link"));
+	cl_must_pass(p_unlink("dir/file"));
+	cl_must_pass(p_rmdir("dir"));
+	cl_must_pass(p_rmdir("file"));
+
+	git_buf_dispose(&contents);
+}