1 /* Emulate link on platforms that lack it, namely native Windows platforms.
3 Copyright (C) 2009-2020 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, 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; mingw does not
89 support hard-linking directories. */
90 if ((len1
&& (file1
[len1
- 1] == '/' || file1
[len1
- 1] == '\\'))
91 || (len2
&& (file2
[len2
- 1] == '/' || file2
[len2
- 1] == '\\')))
94 if (stat (file1
, &st
) == 0 && S_ISDIR (st
.st_mode
))
100 /* CreateHardLink("b/.","a",NULL) creates file "b", so we must check
101 that dirname(file2) exists. */
102 dir
= strdup (file2
);
107 char *p
= strchr (dir
, '\0');
108 while (dir
< p
&& (*--p
!= '/' && *p
!= '\\'));
110 if (p
!= dir
&& stat (dir
, &st
) == -1)
112 int saved_errno
= errno
;
119 /* Now create the link. */
120 if (CreateHardLinkFunc (file2
, file1
, NULL
) == 0)
122 /* It is not documented which errors CreateHardLink() can produce.
123 * The following conversions are based on tests on a Windows XP SP2
125 DWORD err
= GetLastError ();
128 case ERROR_ACCESS_DENIED
:
132 case ERROR_INVALID_FUNCTION
: /* fs does not support hard links */
136 case ERROR_NOT_SAME_DEVICE
:
140 case ERROR_PATH_NOT_FOUND
:
141 case ERROR_FILE_NOT_FOUND
:
145 case ERROR_INVALID_PARAMETER
:
146 errno
= ENAMETOOLONG
;
149 case ERROR_TOO_MANY_LINKS
:
153 case ERROR_ALREADY_EXISTS
:
166 # else /* !Windows */
168 # error "This platform lacks a link function, and Gnulib doesn't provide a replacement. This is a bug in Gnulib."
170 # endif /* !Windows */
171 #else /* HAVE_LINK */
175 /* Create a hard link from FILE1 to FILE2, working around platform bugs. */
177 rpl_link (char const *file1
, char const *file2
)
183 /* Don't allow IRIX to dereference dangling file2 symlink. */
184 if (!lstat (file2
, &st
))
190 /* Reject trailing slashes on non-directories. */
191 len1
= strlen (file1
);
192 len2
= strlen (file2
);
193 if ((len1
&& file1
[len1
- 1] == '/')
194 || (len2
&& file2
[len2
- 1] == '/'))
196 /* Let link() decide whether hard-linking directories is legal.
197 If stat() fails, then link() should fail for the same reason
198 (although on Solaris 9, link("file/","oops") mistakenly
199 succeeds); if stat() succeeds, require a directory. */
200 if (stat (file1
, &st
))
202 if (!S_ISDIR (st
.st_mode
))
210 /* Fix Cygwin 1.5.x bug where link("a","b/.") creates file "b". */
211 char *dir
= strdup (file2
);
215 /* We already know file2 does not end in slash. Strip off the
216 basename, then check that the dirname exists. */
217 p
= strrchr (dir
, '/');
221 if (stat (dir
, &st
) == -1)
223 int saved_errno
= errno
;
231 return link (file1
, file2
);
233 #endif /* HAVE_LINK */