1 /* Emulate link on platforms that lack it, namely native Windows platforms.
3 Copyright (C) 2009 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
23 #define WIN32_LEAN_AND_MEAN
29 /* CreateHardLink was introduced only in Windows 2000. */
30 typedef BOOL (WINAPI * CreateHardLinkFuncType) (LPCTSTR lpFileName,
31 LPCTSTR lpExistingFileName,
32 LPSECURITY_ATTRIBUTES lpSecurityAttributes);
33 static CreateHardLinkFuncType CreateHardLinkFunc = NULL;
34 static BOOL initialized = FALSE;
39 HMODULE kernel32 = LoadLibrary ("kernel32.dll");
43 (CreateHardLinkFuncType) GetProcAddress (kernel32, "CreateHardLinkA");
49 link (const char *path1, const char *path2)
53 if (CreateHardLinkFunc == NULL)
55 /* System does not support hard links. */
59 if (CreateHardLinkFunc (path2, path1, NULL) == 0)
61 /* It is not documented which errors CreateHardLink() can produce.
62 * The following conversions are based on tests on a Windows XP SP2
64 DWORD err = GetLastError ();
67 case ERROR_ACCESS_DENIED:
71 case ERROR_INVALID_FUNCTION: /* fs does not support hard links */
75 case ERROR_NOT_SAME_DEVICE:
79 case ERROR_PATH_NOT_FOUND:
80 case ERROR_FILE_NOT_FOUND:
84 case ERROR_INVALID_PARAMETER:
88 case ERROR_TOO_MANY_LINKS:
92 case ERROR_ALREADY_EXISTS:
107 #error "This platform lacks a link function, and Gnulib doesn't provide a replacement. This is a bug in Gnulib."
109 #endif /* !Windows */