From beb302486c90af698101118d7f55162e71358d5a Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Wed, 21 Jan 2009 00:58:26 +0100 Subject: [PATCH] Make the 'link' module link on Windows NT 4. --- ChangeLog | 9 +++++++++ lib/link.c | 30 ++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 521eb24d6..5b4d64cff 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,14 @@ 2009-01-20 Bruno Haible + Make the 'link' module link on Windows NT 4. + * lib/link.c (_WIN32_WINNT): Don't define. + (CreateHardLinkFuncType): New type. + (CreateHardLinkFunc, initialized): New variables. + (initialize): New function. + (link): Invoke CreateHardLink indirectly through the function pointer. + +2009-01-20 Bruno Haible + Fix compilation failure on mingw. * tests/test-link.c (main): Don't assume that EOPNOTSUPP exists. diff --git a/lib/link.c b/lib/link.c index 6737d37e9..2b5a97f6f 100644 --- a/lib/link.c +++ b/lib/link.c @@ -21,16 +21,42 @@ #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ #define WIN32_LEAN_AND_MEAN -#define _WIN32_WINNT 0x0500 #include #include #include +/* CreateHardLink was introduced only in Windows 2000. */ +typedef BOOL (WINAPI * CreateHardLinkFuncType) (LPCTSTR lpFileName, + LPCTSTR lpExistingFileName, + LPSECURITY_ATTRIBUTES lpSecurityAttributes); +static CreateHardLinkFuncType CreateHardLinkFunc = NULL; +static BOOL initialized = FALSE; + +static void +initialize (void) +{ + HMODULE kernel32 = LoadLibrary ("kernel32.dll"); + if (kernel32 != NULL) + { + CreateHardLinkFunc = + (CreateHardLinkFuncType) GetProcAddress (kernel32, "CreateHardLinkA"); + } + initialized = TRUE; +} + int link (const char *path1, const char *path2) { - if (CreateHardLink (path2, path1, NULL) == 0) + if (!initialized) + initialize (); + if (CreateHardLinkFunc == NULL) + { + /* System does not support hard links. */ + errno = EPERM; + return -1; + } + if (CreateHardLinkFunc (path2, path1, NULL) == 0) { /* It is not documented which errors CreateHardLink() can produce. * The following conversions are based on tests on a Windows XP SP2 -- 2.11.0