1 /* Emulate link on platforms that lack it, namely native Windows platforms.
3 Copyright (C) 2009-2018 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 /* Avoid warnings from gcc -Wcast-function-type. */
34 # define GetProcAddress \
35 (void *) GetProcAddress
37 /* CreateHardLink was introduced only in Windows 2000. */
38 typedef BOOL (WINAPI
* CreateHardLinkFuncType
) (LPCTSTR lpFileName
,
39 LPCTSTR lpExistingFileName
,
40 LPSECURITY_ATTRIBUTES lpSecurityAttributes
);
41 static CreateHardLinkFuncType CreateHardLinkFunc
= NULL
;
42 static BOOL initialized
= FALSE
;
47 HMODULE kernel32
= GetModuleHandle ("kernel32.dll");
51 (CreateHardLinkFuncType
) GetProcAddress (kernel32
, "CreateHardLinkA");
57 link (const char *file1
, const char *file2
)
60 size_t len1
= strlen (file1
);
61 size_t len2
= strlen (file2
);
64 if (CreateHardLinkFunc
== NULL
)
66 /* System does not support hard links. */
70 /* Reject trailing slashes on non-directories; mingw does not
71 support hard-linking directories. */
72 if ((len1
&& (file1
[len1
- 1] == '/' || file1
[len1
- 1] == '\\'))
73 || (len2
&& (file2
[len2
- 1] == '/' || file2
[len2
- 1] == '\\')))
76 if (stat (file1
, &st
) == 0 && S_ISDIR (st
.st_mode
))
82 /* CreateHardLink("b/.","a",NULL) creates file "b", so we must check
83 that dirname(file2) exists. */
89 char *p
= strchr (dir
, '\0');
90 while (dir
< p
&& (*--p
!= '/' && *p
!= '\\'));
92 if (p
!= dir
&& stat (dir
, &st
) == -1)
94 int saved_errno
= errno
;
101 /* Now create the link. */
102 if (CreateHardLinkFunc (file2
, file1
, NULL
) == 0)
104 /* It is not documented which errors CreateHardLink() can produce.
105 * The following conversions are based on tests on a Windows XP SP2
107 DWORD err
= GetLastError ();
110 case ERROR_ACCESS_DENIED
:
114 case ERROR_INVALID_FUNCTION
: /* fs does not support hard links */
118 case ERROR_NOT_SAME_DEVICE
:
122 case ERROR_PATH_NOT_FOUND
:
123 case ERROR_FILE_NOT_FOUND
:
127 case ERROR_INVALID_PARAMETER
:
128 errno
= ENAMETOOLONG
;
131 case ERROR_TOO_MANY_LINKS
:
135 case ERROR_ALREADY_EXISTS
:
148 # else /* !Windows */
150 # error "This platform lacks a link function, and Gnulib doesn't provide a replacement. This is a bug in Gnulib."
152 # endif /* !Windows */
153 #else /* HAVE_LINK */
157 /* Create a hard link from FILE1 to FILE2, working around platform bugs. */
159 rpl_link (char const *file1
, char const *file2
)
165 /* Don't allow IRIX to dereference dangling file2 symlink. */
166 if (!lstat (file2
, &st
))
172 /* Reject trailing slashes on non-directories. */
173 len1
= strlen (file1
);
174 len2
= strlen (file2
);
175 if ((len1
&& file1
[len1
- 1] == '/')
176 || (len2
&& file2
[len2
- 1] == '/'))
178 /* Let link() decide whether hard-linking directories is legal.
179 If stat() fails, then link() should fail for the same reason
180 (although on Solaris 9, link("file/","oops") mistakenly
181 succeeds); if stat() succeeds, require a directory. */
182 if (stat (file1
, &st
))
184 if (!S_ISDIR (st
.st_mode
))
192 /* Fix Cygwin 1.5.x bug where link("a","b/.") creates file "b". */
193 char *dir
= strdup (file2
);
197 /* We already know file2 does not end in slash. Strip off the
198 basename, then check that the dirname exists. */
199 p
= strrchr (dir
, '/');
203 if (stat (dir
, &st
) == -1)
205 int saved_errno
= errno
;
213 return link (file1
, file2
);
215 #endif /* HAVE_LINK */