malloc-h: New module.
[gnulib.git] / lib / link.c
blob3c0c341d54cf2e64838a2dc75daa91e9e8c0ca91
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)
8 any later version.
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/>. */
18 #include <config.h>
20 #include <unistd.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/stat.h>
27 #if !HAVE_LINK
28 # if defined _WIN32 && ! defined __CYGWIN__
30 # define WIN32_LEAN_AND_MEAN
31 # include <windows.h>
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;
52 static void
53 initialize (void)
55 HMODULE kernel32 = GetModuleHandle ("kernel32.dll");
56 if (kernel32 != NULL)
58 CreateHardLinkFunc =
59 (CreateHardLinkFuncType) GetProcAddress (kernel32, "CreateHardLinkA");
61 initialized = TRUE;
64 # else
66 # define CreateHardLinkFunc CreateHardLink
68 # endif
70 int
71 link (const char *file1, const char *file2)
73 char *dir;
74 size_t len1 = strlen (file1);
75 size_t len2 = strlen (file2);
77 # if !(_WIN32_WINNT >= _WIN32_WINNT_WINXP)
78 if (!initialized)
79 initialize ();
80 # endif
82 if (CreateHardLinkFunc == NULL)
84 /* System does not support hard links. */
85 errno = EPERM;
86 return -1;
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] == '\\')))
93 struct stat st;
94 if (stat (file1, &st) == 0 && S_ISDIR (st.st_mode))
95 errno = EPERM;
96 else
97 errno = ENOTDIR;
98 return -1;
100 /* CreateHardLink("b/.","a",NULL) creates file "b", so we must check
101 that dirname(file2) exists. */
102 dir = strdup (file2);
103 if (!dir)
104 return -1;
106 struct stat st;
107 char *p = strchr (dir, '\0');
108 while (dir < p && (*--p != '/' && *p != '\\'));
109 *p = '\0';
110 if (p != dir && stat (dir, &st) != 0 && errno != EOVERFLOW)
112 int saved_errno = errno;
113 free (dir);
114 errno = saved_errno;
115 return -1;
117 free (dir);
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
124 * system. */
125 DWORD err = GetLastError ();
126 switch (err)
128 case ERROR_ACCESS_DENIED:
129 errno = EACCES;
130 break;
132 case ERROR_INVALID_FUNCTION: /* fs does not support hard links */
133 errno = EPERM;
134 break;
136 case ERROR_NOT_SAME_DEVICE:
137 errno = EXDEV;
138 break;
140 case ERROR_PATH_NOT_FOUND:
141 case ERROR_FILE_NOT_FOUND:
142 errno = ENOENT;
143 break;
145 case ERROR_INVALID_PARAMETER:
146 errno = ENAMETOOLONG;
147 break;
149 case ERROR_TOO_MANY_LINKS:
150 errno = EMLINK;
151 break;
153 case ERROR_ALREADY_EXISTS:
154 errno = EEXIST;
155 break;
157 default:
158 errno = EIO;
160 return -1;
163 return 0;
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 */
173 # undef link
175 /* Create a hard link from FILE1 to FILE2, working around platform bugs. */
177 rpl_link (char const *file1, char const *file2)
179 size_t len1;
180 size_t len2;
181 struct stat st;
183 /* Don't allow IRIX to dereference dangling file2 symlink. */
184 if (lstat (file2, &st) == 0 || errno == EOVERFLOW)
186 errno = EEXIST;
187 return -1;
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))
201 return -1;
202 if (!S_ISDIR (st.st_mode))
204 errno = ENOTDIR;
205 return -1;
208 else
210 /* Fix Cygwin 1.5.x bug where link("a","b/.") creates file "b". */
211 char *dir = strdup (file2);
212 char *p;
213 if (!dir)
214 return -1;
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, '/');
218 if (p)
220 *p = '\0';
221 if (stat (dir, &st) != 0 && errno != EOVERFLOW)
223 int saved_errno = errno;
224 free (dir);
225 errno = saved_errno;
226 return -1;
229 free (dir);
231 return link (file1, file2);
233 #endif /* HAVE_LINK */