1 /* Emulate link on platforms that lack it, namely native Windows platforms.
3 Copyright (C) 2009-2021 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 <https://www.gnu.org/licenses/>. */
28 # if defined _WIN32 && ! defined __CYGWIN__
30 # define WIN32_LEAN_AND_MEAN
33 /* Don't assume that UNICODE is not defined. */
34 # undef GetModuleHandle
35 # define GetModuleHandle GetModuleHandleA
36 # undef CreateHardLink
37 # define CreateHardLink CreateHardLinkA
39 # if !(_WIN32_WINNT >= _WIN32_WINNT_WINXP)
41 /* Avoid warnings from gcc -Wcast-function-type. */
42 # define GetProcAddress \
43 (void *) GetProcAddress
45 /* CreateHardLink was introduced only in Windows 2000. */
46 typedef BOOL (WINAPI * CreateHardLinkFuncType) (LPCSTR lpFileName,
47 LPCSTR lpExistingFileName,
48 LPSECURITY_ATTRIBUTES lpSecurityAttributes);
49 static CreateHardLinkFuncType CreateHardLinkFunc = NULL;
50 static BOOL initialized = FALSE;
55 HMODULE kernel32 = GetModuleHandle ("kernel32.dll");
59 (CreateHardLinkFuncType) GetProcAddress (kernel32, "CreateHardLinkA");
66 # define CreateHardLinkFunc CreateHardLink
71 link (const char *file1, const char *file2)
74 size_t len1 = strlen (file1);
75 size_t len2 = strlen (file2);
77 # if !(_WIN32_WINNT >= _WIN32_WINNT_WINXP)
82 if (CreateHardLinkFunc == NULL)
84 /* System does not support hard links. */
88 /* Reject trailing slashes on non-directories; native Windows does not
89 support hard-linking directories. */
90 if ((len1 && (file1[len1 - 1] == '/' || file1[len1 - 1] == '\\'))
91 || (len2 && (file2[len2 - 1] == '/' || file2[len2 - 1] == '\\')))
93 /* If stat() fails, then link() should fail for the same reason. */
95 if (stat (file1, &st))
97 if (errno == EOVERFLOW)
98 /* It's surely a file, not a directory (see stat-w32.c). */
102 if (!S_ISDIR (st.st_mode))
108 /* CreateHardLink("b/.","a",NULL) creates file "b", so we must check
109 that dirname(file2) exists. */
110 dir = strdup (file2);
115 char *p = strchr (dir, '\0');
116 while (dir < p && (*--p != '/' && *p != '\\'));
118 if (p != dir && stat (dir, &st) != 0 && errno != EOVERFLOW)
125 /* Now create the link. */
126 if (CreateHardLinkFunc (file2, file1, NULL) == 0)
128 /* It is not documented which errors CreateHardLink() can produce.
129 * The following conversions are based on tests on a Windows XP SP2
131 DWORD err = GetLastError ();
134 case ERROR_ACCESS_DENIED:
138 case ERROR_INVALID_FUNCTION: /* fs does not support hard links */
142 case ERROR_NOT_SAME_DEVICE:
146 case ERROR_PATH_NOT_FOUND:
147 case ERROR_FILE_NOT_FOUND:
151 case ERROR_INVALID_PARAMETER:
152 errno = ENAMETOOLONG;
155 case ERROR_TOO_MANY_LINKS:
159 case ERROR_ALREADY_EXISTS:
172 # else /* !Windows */
174 # error "This platform lacks a link function, and Gnulib doesn't provide a replacement. This is a bug in Gnulib."
176 # endif /* !Windows */
177 #else /* HAVE_LINK */
181 /* Create a hard link from FILE1 to FILE2, working around platform bugs. */
183 rpl_link (char const *file1, char const *file2)
189 /* Don't allow IRIX to dereference dangling file2 symlink. */
190 if (lstat (file2, &st) == 0 || errno == EOVERFLOW)
196 /* Reject trailing slashes on non-directories. */
197 len1 = strlen (file1);
198 len2 = strlen (file2);
199 if ((len1 && file1[len1 - 1] == '/')
200 || (len2 && file2[len2 - 1] == '/'))
202 /* Let link() decide whether hard-linking directories is legal.
203 If stat() fails, then link() should fail for the same reason
204 (although on Solaris 9, link("file/","oops") mistakenly
205 succeeds); if stat() succeeds, require a directory. */
206 if (stat (file1, &st))
208 if (!S_ISDIR (st.st_mode))
216 /* Fix Cygwin 1.5.x bug where link("a","b/.") creates file "b". */
217 char *dir = strdup (file2);
221 /* We already know file2 does not end in slash. Strip off the
222 basename, then check that the dirname exists. */
223 p = strrchr (dir, '/');
227 if (stat (dir, &st) != 0 && errno != EOVERFLOW)
235 return link (file1, file2);
237 #endif /* HAVE_LINK */