From e89a60f0451f1c63f53d3ecb609556785ce44930 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Fri, 26 Apr 2019 17:19:01 +0900
Subject: [PATCH 2/2] win32 tentative implement

This is a patch for discussion.

Win32 implement doesn't have relative symbolic links. AFAICS relative
symbolic links on Win32 requires special setting to use
CreateSymbolicLink with non-administrator accounts, and there's no way
to use DeviceIoControl for IO_REPARSE_TAG_SYMLINK with non-admistrator
accounts.

So, this patch creates "fake" symbolic links, which is, it's really a
mount point but looks as if a symbolic links. The difference is
revealed (that is, the "relative" link will be broken) when moving the
data directory and tablespace directory to another place.
---
 src/port/dirmod.c | 75 ++++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 58 insertions(+), 17 deletions(-)

diff --git a/src/port/dirmod.c b/src/port/dirmod.c
index d7932401ef..f8aaae2c64 100644
--- a/src/port/dirmod.c
+++ b/src/port/dirmod.c
@@ -159,10 +159,13 @@ int
 pgsymlink(const char *oldpath, const char *newpath)
 {
 	HANDLE		dirhandle;
-	DWORD		len;
-	char		buffer[MAX_PATH * sizeof(WCHAR) + offsetof(REPARSE_JUNCTION_DATA_BUFFER, PathBuffer)];
+	DWORD		len, plen;
+	char		buffer[MAX_PATH * sizeof(WCHAR) * 2 + offsetof(REPARSE_JUNCTION_DATA_BUFFER, PathBuffer)];
 	char		nativeTarget[MAX_PATH];
+	char		nativePrint[MAX_PATH];
+	char		tmp[MAX_PATH];
 	char	   *p = nativeTarget;
+	char	   *poldpath = oldpath;
 	REPARSE_JUNCTION_DATA_BUFFER *reparseBuf = (REPARSE_JUNCTION_DATA_BUFFER *) buffer;
 
 	CreateDirectory(newpath, 0);
@@ -173,25 +176,50 @@ pgsymlink(const char *oldpath, const char *newpath)
 	if (dirhandle == INVALID_HANDLE_VALUE)
 		return -1;
 
-	/* make sure we have an unparsed native win32 path */
-	if (memcmp("\\??\\", oldpath, 4) != 0)
-		snprintf(nativeTarget, sizeof(nativeTarget), "\\??\\%s", oldpath);
+	/* make absolute path if oldpath is not */
+	if (!is_absolute_path(poldpath))
+	{
+	  char *absnew = make_absolute_path(newpath);
+	  /* back up to pg_tblspc */
+	  get_parent_directory(absnew);
+	  join_path_components(tmp, absnew, oldpath);
+	  free(absnew);
+
+ canonicalize_path(tmp);
+
+	  /* Copy oldpath as print name */
+	  strlcpy(nativePrint, oldpath, sizeof(nativePrint));
+
+	  poldpath = tmp;
+	}
 	else
-		strlcpy(nativeTarget, oldpath, sizeof(nativeTarget));
+	  nativePrint[0] = 0;
+
+	/* make sure we have an unparsed native win32 path */
+	if (is_absolute_path(poldpath))
+	{
+		if (memcmp("\\??\\", poldpath, 4) != 0)
+		  snprintf(nativeTarget, sizeof(nativeTarget), "\\??\\%s", poldpath);
+		else
+		  strlcpy(nativeTarget, poldpath, sizeof(nativeTarget));
+	}
 
 	while ((p = strchr(p, '/')) != NULL)
 		*p++ = '\\';
 
 	len = strlen(nativeTarget) * sizeof(WCHAR);
+	plen = strlen(nativePrint) * sizeof(WCHAR);
 	reparseBuf->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
-	reparseBuf->ReparseDataLength = len + 12;
+	reparseBuf->ReparseDataLength = len + plen + 12;
 	reparseBuf->Reserved = 0;
 	reparseBuf->SubstituteNameOffset = 0;
 	reparseBuf->SubstituteNameLength = len;
 	reparseBuf->PrintNameOffset = len + sizeof(WCHAR);
-	reparseBuf->PrintNameLength = 0;
+	reparseBuf->PrintNameLength = plen;
 	MultiByteToWideChar(CP_ACP, 0, nativeTarget, -1,
 						reparseBuf->PathBuffer, MAX_PATH);
+	MultiByteToWideChar(CP_ACP, 0, nativePrint, -1,
+						&reparseBuf->PathBuffer[reparseBuf->PrintNameOffset/2], MAX_PATH);
 
 	/*
 	 * FSCTL_SET_REPARSE_POINT is coded differently depending on SDK version;
@@ -308,15 +336,27 @@ pgreadlink(const char *path, char *buf, size_t size)
 	/* Got it, let's get some results from this */
 	if (reparseBuf->ReparseTag != IO_REPARSE_TAG_MOUNT_POINT)
 	{
-		errno = EINVAL;
-		return -1;
+	  errno = EINVAL;
+	  return -1;
 	}
 
-	r = WideCharToMultiByte(CP_ACP, 0,
-							reparseBuf->PathBuffer, -1,
-							buf,
-							size,
-							NULL, NULL);
+	/*
+	 * if PrintName is registered, this link is emulated relative
+	 * symbolic link. Return the PrintName rather than SubstituteName.
+	 */
+	if (reparseBuf->PrintNameLength > 0)
+	  r = WideCharToMultiByte(CP_ACP, 0,
+							  &reparseBuf->PathBuffer[reparseBuf->PrintNameOffset / 2],
+							  -1,
+							  buf,
+							  size,
+							  NULL, NULL);
+	else
+	  r = WideCharToMultiByte(CP_ACP, 0,
+							  reparseBuf->PathBuffer, -1,
+							  buf,
+							  size,
+							  NULL, NULL);
 
 	if (r <= 0)
 	{
@@ -328,8 +368,9 @@ pgreadlink(const char *path, char *buf, size_t size)
 	 * If the path starts with "\??\", which it will do in most (all?) cases,
 	 * strip those out.
 	 */
-	if (r > 4 && strncmp(buf, "\\??\\", 4) == 0)
-	{
+	if (reparseBuf->PrintNameLength == 0 &&
+		r > 4 && strncmp(buf, "\\??\\", 4) == 0)
+    {
 		memmove(buf, buf + 4, strlen(buf + 4) + 1);
 		r -= 4;
 	}
-- 
2.16.3

