1 /* Create a hard link relative to open directories.
2 Copyright (C) 2009-2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* written by Eric Blake */
31 #include "areadlink.h"
33 #include "filenamecat.h"
34 #include "openat-priv.h"
37 # include <sys/param.h>
41 # define MAXSYMLINKS SYMLOOP_MAX
43 # define MAXSYMLINKS 20
49 /* Create a link. If FILE1 is a symlink, either create a hardlink to
50 that symlink, or fake it by creating an identical symlink. */
51 # if LINK_FOLLOWS_SYMLINKS == 0
52 # define link_immediate link
55 link_immediate (char const *file1, char const *file2)
57 char *target = areadlink (file1);
60 /* A symlink cannot be modified in-place. Therefore, creating
61 an identical symlink behaves like a hard link to a symlink,
62 except for incorrect st_ino and st_nlink. However, we must
63 be careful of EXDEV. */
66 char *dir = mdir_name (file2);
73 if (lstat (file1, &st1) == 0 && stat (dir, &st2) == 0)
75 if (st1.st_dev == st2.st_dev)
77 int result = symlink (target, file2);
78 int saved_errno = errno;
94 return link (file1, file2);
96 # endif /* LINK_FOLLOWS_SYMLINKS == 0 */
98 /* Create a link. If FILE1 is a symlink, create a hardlink to the
99 canonicalized file. */
100 # if 0 < LINK_FOLLOWS_SYMLINKS
101 # define link_follow link
104 link_follow (char const *file1, char const *file2)
106 char *name = (char *) file1;
111 /* Using realpath or canonicalize_file_name is too heavy-handed: we
112 don't need an absolute name, and we don't need to resolve
113 intermediate symlinks, just the basename of each iteration. */
114 while (i-- && (target = areadlink (name)))
116 if (IS_ABSOLUTE_FILE_NAME (target))
124 char *dir = mdir_name (name);
133 name = mfile_name_concat (dir, target, NULL);
148 if (!target && errno != EINVAL)
152 int saved_errno = errno;
158 result = link (name, file2);
161 int saved_errno = errno;
167 # endif /* 0 < LINK_FOLLOWS_SYMLINKS */
169 /* On Solaris, link() doesn't follow symlinks by default, but does so as soon
170 as a library or executable takes part in the program that has been compiled
171 with "c99" or "cc -xc99=all" or "cc ... /usr/lib/values-xpg4.o ...". */
172 # if LINK_FOLLOWS_SYMLINKS == -1
174 /* Reduce the penalty of link_immediate and link_follow by incorporating the
175 knowledge that link()'s behaviour depends on the __xpg4 variable. */
179 solaris_optimized_link_immediate (char const *file1, char const *file2)
182 return link (file1, file2);
183 return link_immediate (file1, file2);
187 solaris_optimized_link_follow (char const *file1, char const *file2)
190 return link (file1, file2);
191 return link_follow (file1, file2);
194 # define link_immediate solaris_optimized_link_immediate
195 # define link_follow solaris_optimized_link_follow
199 /* Create a link to FILE1, in the directory open on descriptor FD1, to FILE2,
200 in the directory open on descriptor FD2. If FILE1 is a symlink, FLAG
201 controls whether to dereference FILE1 first. If possible, do it without
202 changing the working directory. Otherwise, resort to using
203 save_cwd/fchdir, then rename/restore_cwd. If either the save_cwd or
204 the restore_cwd fails, then give a diagnostic and exit nonzero. */
207 linkat (int fd1, char const *file1, int fd2, char const *file2, int flag)
209 if (flag & ~AT_SYMLINK_FOLLOW)
214 return at_func2 (fd1, file1, fd2, file2,
215 flag ? link_follow : link_immediate);
218 #else /* HAVE_LINKAT */
222 /* Create a link. If FILE1 is a symlink, create a hardlink to the
223 canonicalized file. */
226 linkat_follow (int fd1, char const *file1, int fd2, char const *file2)
228 char *name = (char *) file1;
233 /* There is no realpathat. */
234 while (i-- && (target = areadlinkat (fd1, name)))
236 if (IS_ABSOLUTE_FILE_NAME (target))
244 char *dir = mdir_name (name);
253 name = mfile_name_concat (dir, target, NULL);
268 if (!target && errno != EINVAL)
272 int saved_errno = errno;
278 result = linkat (fd1, name, fd2, file2, 0);
281 int saved_errno = errno;
289 /* Like linkat, but guarantee that AT_SYMLINK_FOLLOW works even on
290 older Linux kernels. */
293 rpl_linkat (int fd1, char const *file1, int fd2, char const *file2, int flag)
295 if (flag & ~AT_SYMLINK_FOLLOW)
301 # if LINKAT_TRAILING_SLASH_BUG
302 /* Reject trailing slashes on non-directories. */
304 size_t len1 = strlen (file1);
305 size_t len2 = strlen (file2);
306 if ((len1 && file1[len1 - 1] == '/')
307 || (len2 && file2[len2 - 1] == '/'))
309 /* Let linkat() decide whether hard-linking directories is legal.
310 If fstatat() fails, then linkat() should fail for the same reason;
311 if fstatat() succeeds, require a directory. */
313 if (fstatat (fd1, file1, &st, flag ? 0 : AT_SYMLINK_NOFOLLOW))
315 if (!S_ISDIR (st.st_mode))
325 return linkat (fd1, file1, fd2, file2, flag);
327 /* Cache the information on whether the system call really works. */
329 static int have_follow_really; /* 0 = unknown, 1 = yes, -1 = no */
330 if (0 <= have_follow_really)
332 int result = linkat (fd1, file1, fd2, file2, flag);
333 if (!(result == -1 && errno == EINVAL))
335 have_follow_really = 1;
338 have_follow_really = -1;
341 return linkat_follow (fd1, file1, fd2, file2);
344 #endif /* HAVE_LINKAT */