1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Emulate link on platforms that lack it, namely native Windows platforms.
5 Copyright (C) 2009-2011 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
31 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
33 # define WIN32_LEAN_AND_MEAN
36 /* CreateHardLink was introduced only in Windows 2000. */
37 typedef BOOL (WINAPI * CreateHardLinkFuncType) (LPCTSTR lpFileName,
38 LPCTSTR lpExistingFileName,
39 LPSECURITY_ATTRIBUTES lpSecurityAttributes);
40 static CreateHardLinkFuncType CreateHardLinkFunc = NULL;
41 static BOOL initialized = FALSE;
46 HMODULE kernel32 = GetModuleHandle ("kernel32.dll");
50 (CreateHardLinkFuncType) GetProcAddress (kernel32, "CreateHardLinkA");
56 link (const char *file1, const char *file2)
59 size_t len1 = strlen (file1);
60 size_t len2 = strlen (file2);
63 if (CreateHardLinkFunc == NULL)
65 /* System does not support hard links. */
69 /* Reject trailing slashes on non-directories; mingw does not
70 support hard-linking directories. */
71 if ((len1 && (file1[len1 - 1] == '/' || file1[len1 - 1] == '\\'))
72 || (len2 && (file2[len2 - 1] == '/' || file2[len2 - 1] == '\\')))
75 if (stat (file1, &st) == 0 && S_ISDIR (st.st_mode))
81 /* CreateHardLink("b/.","a",NULL) creates file "b", so we must check
82 that dirname(file2) exists. */
88 char *p = strchr (dir, '\0');
89 while (dir < p && (*--p != '/' && *p != '\\'));
91 if (p != dir && stat (dir, &st) == -1)
93 int saved_errno = errno;
100 /* Now create the link. */
101 if (CreateHardLinkFunc (file2, file1, NULL) == 0)
103 /* It is not documented which errors CreateHardLink() can produce.
104 * The following conversions are based on tests on a Windows XP SP2
106 DWORD err = GetLastError ();
109 case ERROR_ACCESS_DENIED:
113 case ERROR_INVALID_FUNCTION: /* fs does not support hard links */
117 case ERROR_NOT_SAME_DEVICE:
121 case ERROR_PATH_NOT_FOUND:
122 case ERROR_FILE_NOT_FOUND:
126 case ERROR_INVALID_PARAMETER:
127 errno = ENAMETOOLONG;
130 case ERROR_TOO_MANY_LINKS:
134 case ERROR_ALREADY_EXISTS:
147 # else /* !Windows */
149 # error "This platform lacks a link function, and Gnulib doesn't provide a replacement. This is a bug in Gnulib."
151 # endif /* !Windows */
152 #else /* HAVE_LINK */
156 /* Create a hard link from FILE1 to FILE2, working around platform bugs. */
158 rpl_link (char const *file1, char const *file2)
160 /* Reject trailing slashes on non-directories. */
161 size_t len1 = strlen (file1);
162 size_t len2 = strlen (file2);
163 if ((len1 && file1[len1 - 1] == '/')
164 || (len2 && file2[len2 - 1] == '/'))
166 /* Let link() decide whether hard-linking directories is legal.
167 If stat() fails, then link() should fail for the same reason
168 (although on Solaris 9, link("file/","oops") mistakenly
169 succeeds); if stat() succeeds, require a directory. */
171 if (stat (file1, &st))
173 if (!S_ISDIR (st.st_mode))
181 /* Fix Cygwin 1.5.x bug where link("a","b/.") creates file "b". */
182 char *dir = strdup (file2);
187 /* We already know file2 does not end in slash. Strip off the
188 basename, then check that the dirname exists. */
189 p = strrchr (dir, '/');
193 if (stat (dir, &st) == -1)
195 int saved_errno = errno;
203 return link (file1, file2);
205 #endif /* HAVE_LINK */