1 /* Emulate link on platforms that lack it, namely native Windows platforms.
3 Copyright (C) 2009-2011 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 3, 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. */
29 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
31 # define WIN32_LEAN_AND_MEAN
34 /* CreateHardLink was introduced only in Windows 2000. */
35 typedef BOOL (WINAPI * CreateHardLinkFuncType) (LPCTSTR lpFileName,
36 LPCTSTR lpExistingFileName,
37 LPSECURITY_ATTRIBUTES lpSecurityAttributes);
38 static CreateHardLinkFuncType CreateHardLinkFunc = NULL;
39 static BOOL initialized = FALSE;
44 HMODULE kernel32 = GetModuleHandle ("kernel32.dll");
48 (CreateHardLinkFuncType) GetProcAddress (kernel32, "CreateHardLinkA");
54 link (const char *file1, const char *file2)
57 size_t len1 = strlen (file1);
58 size_t len2 = strlen (file2);
61 if (CreateHardLinkFunc == NULL)
63 /* System does not support hard links. */
67 /* Reject trailing slashes on non-directories; mingw does not
68 support hard-linking directories. */
69 if ((len1 && (file1[len1 - 1] == '/' || file1[len1 - 1] == '\\'))
70 || (len2 && (file2[len2 - 1] == '/' || file2[len2 - 1] == '\\')))
73 if (stat (file1, &st) == 0 && S_ISDIR (st.st_mode))
79 /* CreateHardLink("b/.","a",NULL) creates file "b", so we must check
80 that dirname(file2) exists. */
86 char *p = strchr (dir, '\0');
87 while (dir < p && (*--p != '/' && *p != '\\'));
89 if (p != dir && stat (dir, &st) == -1)
91 int saved_errno = errno;
98 /* Now create the link. */
99 if (CreateHardLinkFunc (file2, file1, NULL) == 0)
101 /* It is not documented which errors CreateHardLink() can produce.
102 * The following conversions are based on tests on a Windows XP SP2
104 DWORD err = GetLastError ();
107 case ERROR_ACCESS_DENIED:
111 case ERROR_INVALID_FUNCTION: /* fs does not support hard links */
115 case ERROR_NOT_SAME_DEVICE:
119 case ERROR_PATH_NOT_FOUND:
120 case ERROR_FILE_NOT_FOUND:
124 case ERROR_INVALID_PARAMETER:
125 errno = ENAMETOOLONG;
128 case ERROR_TOO_MANY_LINKS:
132 case ERROR_ALREADY_EXISTS:
145 # else /* !Windows */
147 # error "This platform lacks a link function, and Gnulib doesn't provide a replacement. This is a bug in Gnulib."
149 # endif /* !Windows */
150 #else /* HAVE_LINK */
154 /* Create a hard link from FILE1 to FILE2, working around platform bugs. */
156 rpl_link (char const *file1, char const *file2)
158 /* Reject trailing slashes on non-directories. */
159 size_t len1 = strlen (file1);
160 size_t len2 = strlen (file2);
161 if ((len1 && file1[len1 - 1] == '/')
162 || (len2 && file2[len2 - 1] == '/'))
164 /* Let link() decide whether hard-linking directories is legal.
165 If stat() fails, then link() should fail for the same reason
166 (although on Solaris 9, link("file/","oops") mistakenly
167 succeeds); if stat() succeeds, require a directory. */
169 if (stat (file1, &st))
171 if (!S_ISDIR (st.st_mode))
179 /* Fix Cygwin 1.5.x bug where link("a","b/.") creates file "b". */
180 char *dir = strdup (file2);
185 /* We already know file2 does not end in slash. Strip off the
186 basename, then check that the dirname exists. */
187 p = strrchr (dir, '/');
191 if (stat (dir, &st) == -1)
193 int saved_errno = errno;
201 return link (file1, file2);
203 #endif /* HAVE_LINK */