From 401ad3c40f5876da0e05511196b27d2b77b61957 Mon Sep 17 00:00:00 2001
From: Ray Donnelly <mingw.android@gmail.com>
Date: Wed, 20 Jun 2018 05:40:46 -0700
Subject: [PATCH 1/2] Use CreateSymbolicLinkW on Windows for symlinks (WIP)

This doesn't handle SYMBOLIC_LINK_FLAG_DIRECTORY yet
---
 libarchive/archive_write_disk_windows.c | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/libarchive/archive_write_disk_windows.c b/libarchive/archive_write_disk_windows.c
index 78eda4ab..b393a1fe 100644
--- a/libarchive/archive_write_disk_windows.c
+++ b/libarchive/archive_write_disk_windows.c
@@ -1514,7 +1514,32 @@ create_filesystem_object(struct archive_write_disk *a)
 #if HAVE_SYMLINK
 		return symlink(linkname, a->name) ? errno : 0;
 #else
-		return (EPERM);
+		if (linkname != NULL) {
+			wchar_t *linkfull, *namefull;
+
+			// Not sure which is best here, this is an absolute link, always:
+			// linkfull = __la_win_permissive_name_w(linkname);
+			linkfull = linkname;
+			namefull = __la_win_permissive_name_w(a->name);
+			if (linkfull == NULL || namefull == NULL) {
+				errno = EINVAL;
+				r = -1;
+			}
+			else {
+#if !defined(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)
+#define SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE 0x2
+#endif
+				r = CreateSymbolicLinkW(namefull, linkfull, SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
+				if (r == 0) {
+					la_dosmaperr(GetLastError());
+					r = errno;
+				}
+				else
+					r = 0;
+			}
+
+			return r;
+		}
 #endif
 	}
 
-- 
2.16.2

