]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
windows: Simplify la_CreateSymbolicLinkW
authorTobias Stoeckmann <tobias@stoeckmann.org>
Tue, 2 Jun 2026 19:02:59 +0000 (21:02 +0200)
committerTobias Stoeckmann <tobias@stoeckmann.org>
Wed, 3 Jun 2026 18:50:51 +0000 (20:50 +0200)
The CreateSymbolicLinkW function is available since 0x0600 and is also
part of the Nano Server APIs. On earlier systems, don't even try.
Otherwise use it directly to simplify code.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
libarchive/archive_write_disk_windows.c

index bd6cff58aa934c45992dae190fcb6863c59c2280..6fda86f9c85e6159ffdaf390c3e34b5833305410 100644 (file)
@@ -580,24 +580,6 @@ la_mktemp(struct archive_write_disk *a)
        return (fd);
 }
 
-#if _WIN32_WINNT < _WIN32_WINNT_VISTA
-static void *
-la_GetFunctionKernel32(const char *name)
-{
-       static HINSTANCE lib;
-       static int set;
-       if (!set) {
-               set = 1;
-               lib = LoadLibrary(TEXT("kernel32.dll"));
-       }
-       if (lib == NULL) {
-               fprintf(stderr, "Can't load kernel32.dll?!\n");
-               exit(1);
-       }
-       return (void *)GetProcAddress(lib, name);
-}
-#endif
-
 static int
 la_CreateHardLinkW(wchar_t *linkname, wchar_t *target)
 {
@@ -635,30 +617,18 @@ la_CreateHardLinkW(wchar_t *linkname, wchar_t *target)
 static int
 la_CreateSymbolicLinkW(const wchar_t *linkname, const wchar_t *target,
     int linktype) {
-       static BOOLEAN (WINAPI *f)(LPCWSTR, LPCWSTR, DWORD);
+       BOOL ret = 0;
+#if _WIN32_WINNT < _WIN32_WINNT_VISTA ||\
+    !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
+       (void)linkname; /* UNUSED */
+       (void)target; /* UNUSED */
+       (void)linktype; /* UNUSED */
+#else
        wchar_t *ttarget, *p;
        size_t len;
        DWORD attrs = 0;
        DWORD flags = 0;
        DWORD newflags = 0;
-       BOOL ret = 0;
-
-#if _WIN32_WINNT < _WIN32_WINNT_VISTA
-/* CreateSymbolicLinkW is available since Vista and always loaded */
-       static int set;
-       if (!set) {
-               set = 1;
-               f = la_GetFunctionKernel32("CreateSymbolicLinkW");
-       }
-#else
-# if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
-       f = CreateSymbolicLinkW;
-# else
-       f = NULL;
-# endif
-#endif
-       if (!f)
-               return (0);
 
        len = wcslen(target);
        if (len == 0) {
@@ -720,15 +690,16 @@ la_CreateSymbolicLinkW(const wchar_t *linkname, const wchar_t *target,
                        disk_unlink(linkname);
        }
 
-       ret = (*f)(linkname, ttarget, newflags);
+       ret = CreateSymbolicLinkW(linkname, ttarget, newflags);
        /*
         * Prior to Windows 10 calling CreateSymbolicLinkW() will fail
         * if SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE is set
         */
        if (!ret) {
-               ret = (*f)(linkname, ttarget, flags);
+               ret = CreateSymbolicLinkW(linkname, ttarget, flags);
        }
        free(ttarget);
+#endif
        return (ret);
 }