1 /* Emulate link on platforms that lack it, namely native Windows platforms.
3 Copyright (C) 2009-2015 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, see <http://www.gnu.org/licenses/>. */
28 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
30 # define WIN32_LEAN_AND_MEAN
33 /* CreateHardLink was introduced only in Windows 2000. */
34 typedef BOOL (WINAPI * CreateHardLinkFuncType) (LPCTSTR lpFileName,
35 LPCTSTR lpExistingFileName,
36 LPSECURITY_ATTRIBUTES lpSecurityAttributes);
37 static CreateHardLinkFuncType CreateHardLinkFunc = NULL;
38 static BOOL initialized = FALSE;
43 HMODULE kernel32 = GetModuleHandle ("kernel32.dll");
47 (CreateHardLinkFuncType) GetProcAddress (kernel32, "CreateHardLinkA");
53 link (const char *file1, const char *file2)
56 size_t len1 = strlen (file1);
57 size_t len2 = strlen (file2);
60 if (CreateHardLinkFunc == NULL)
62 /* System does not support hard links. */
66 /* Reject trailing slashes on non-directories; mingw does not
67 support hard-linking directories. */
68 if ((len1 && (file1[len1 - 1] == '/' || file1[len1 - 1] == '\\'))
69 || (len2 && (file2[len2 - 1] == '/' || file2[len2 - 1] == '\\')))
72 if (stat (file1, &st) == 0 && S_ISDIR (st.st_mode))
78 /* CreateHardLink("b/.","a",NULL) creates file "b", so we must check
79 that dirname(file2) exists. */
85 char *p = strchr (dir, '\0');
86 while (dir < p && (*--p != '/' && *p != '\\'));
88 if (p != dir && stat (dir, &st) == -1)
90 int saved_errno = errno;
97 /* Now create the link. */
98 if (CreateHardLinkFunc (file2, file1, NULL) == 0)
100 /* It is not documented which errors CreateHardLink() can produce.
101 * The following conversions are based on tests on a Windows XP SP2
103 DWORD err = GetLastError ();
106 case ERROR_ACCESS_DENIED:
110 case ERROR_INVALID_FUNCTION: /* fs does not support hard links */
114 case ERROR_NOT_SAME_DEVICE:
118 case ERROR_PATH_NOT_FOUND:
119 case ERROR_FILE_NOT_FOUND:
123 case ERROR_INVALID_PARAMETER:
124 errno = ENAMETOOLONG;
127 case ERROR_TOO_MANY_LINKS:
131 case ERROR_ALREADY_EXISTS:
144 # else /* !Windows */
146 # error "This platform lacks a link function, and Gnulib doesn't provide a replacement. This is a bug in Gnulib."
148 # endif /* !Windows */
149 #else /* HAVE_LINK */
153 /* Create a hard link from FILE1 to FILE2, working around platform bugs. */
155 rpl_link (char const *file1, char const *file2)
161 /* Don't allow IRIX to dereference dangling file2 symlink. */
162 if (!lstat (file2, &st))
168 /* Reject trailing slashes on non-directories. */
169 len1 = strlen (file1);
170 len2 = strlen (file2);
171 if ((len1 && file1[len1 - 1] == '/')
172 || (len2 && file2[len2 - 1] == '/'))
174 /* Let link() decide whether hard-linking directories is legal.
175 If stat() fails, then link() should fail for the same reason
176 (although on Solaris 9, link("file/","oops") mistakenly
177 succeeds); if stat() succeeds, require a directory. */
178 if (stat (file1, &st))
180 if (!S_ISDIR (st.st_mode))
188 /* Fix Cygwin 1.5.x bug where link("a","b/.") creates file "b". */
189 char *dir = strdup (file2);
193 /* We already know file2 does not end in slash. Strip off the
194 basename, then check that the dirname exists. */
195 p = strrchr (dir, '/');
199 if (stat (dir, &st) == -1)
201 int saved_errno = errno;
209 return link (file1, file2);
211 #endif /* HAVE_LINK */