Hash :
beb30248
Author :
Date :
2009-01-21T00:58:26
Make the 'link' module link on Windows NT 4.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
/* Emulate link on platforms that lack it, namely native Windows platforms.
Copyright (C) 2009 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#include <config.h>
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
#define WIN32_LEAN_AND_MEAN
#include <unistd.h>
#include <windows.h>
#include <errno.h>
/* 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 (!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
* system. */
DWORD err = GetLastError ();
switch (err)
{
case ERROR_ACCESS_DENIED:
errno = EACCES;
break;
case ERROR_INVALID_FUNCTION: /* fs does not support hard links */
errno = EPERM;
break;
case ERROR_NOT_SAME_DEVICE:
errno = EXDEV;
break;
case ERROR_PATH_NOT_FOUND:
case ERROR_FILE_NOT_FOUND:
errno = ENOENT;
break;
case ERROR_INVALID_PARAMETER:
errno = ENAMETOOLONG;
break;
case ERROR_TOO_MANY_LINKS:
errno = EMLINK;
break;
case ERROR_ALREADY_EXISTS:
errno = EEXIST;
break;
default:
errno = EIO;
}
return -1;
}
return 0;
}
#else /* !Windows */
#error "This platform lacks a link function, and Gnulib doesn't provide a replacement. This is a bug in Gnulib."
#endif /* !Windows */