Commit 50194dcd by Patrick Steinhardt

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.
parent 93d37a1d
...@@ -414,18 +414,37 @@ int p_readlink(const char *path, char *buf, size_t bufsiz) ...@@ -414,18 +414,37 @@ int p_readlink(const char *path, char *buf, size_t bufsiz)
return (int)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) int p_symlink(const char *target, const char *path)
{ {
git_win32_path target_w, path_w; git_win32_path target_w, path_w;
DWORD dwFlags; DWORD dwFlags;
if (git_win32_path_from_utf8(path_w, path) < 0 || 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; return -1;
dwFlags = SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; dwFlags = SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
if (target_is_dir(target, path))
if (GetFileAttributesW(target_w) & FILE_ATTRIBUTE_DIRECTORY)
dwFlags |= SYMBOLIC_LINK_FLAG_DIRECTORY; dwFlags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
if (!CreateSymbolicLinkW(path_w, target_w, dwFlags)) if (!CreateSymbolicLinkW(path_w, target_w, dwFlags))
......
...@@ -285,3 +285,27 @@ void test_core_posix__unlink_removes_symlink(void) ...@@ -285,3 +285,27 @@ void test_core_posix__unlink_removes_symlink(void)
cl_must_pass(p_unlink("file")); cl_must_pass(p_unlink("file"));
cl_must_pass(p_rmdir("dir")); 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);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment