1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Create a hard link relative to open directories.
4 Copyright (C) 2009-2014 Free Software Foundation, Inc.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* written by Eric Blake */
32 #include "areadlink.h"
34 #include "filenamecat.h"
35 #include "openat-priv.h"
38 # include <sys/param.h>
42 # define MAXSYMLINKS SYMLOOP_MAX
44 # define MAXSYMLINKS 20
50 /* Create a link. If FILE1 is a symlink, either create a hardlink to
51 that symlink, or fake it by creating an identical symlink. */
52 # if LINK_FOLLOWS_SYMLINKS == 0
53 # define link_immediate link
56 link_immediate (char const *file1, char const *file2)
58 char *target = areadlink (file1);
61 /* A symlink cannot be modified in-place. Therefore, creating
62 an identical symlink behaves like a hard link to a symlink,
63 except for incorrect st_ino and st_nlink. However, we must
64 be careful of EXDEV. */
67 char *dir = mdir_name (file2);
74 if (lstat (file1, &st1) == 0 && stat (dir, &st2) == 0)
76 if (st1.st_dev == st2.st_dev)
78 int result = symlink (target, file2);
79 int saved_errno = errno;
95 return link (file1, file2);
97 # endif /* LINK_FOLLOWS_SYMLINKS == 0 */
99 /* Create a link. If FILE1 is a symlink, create a hardlink to the
100 canonicalized file. */
101 # if 0 < LINK_FOLLOWS_SYMLINKS
102 # define link_follow link
105 link_follow (char const *file1, char const *file2)
107 char *name = (char *) file1;
112 /* Using realpath or canonicalize_file_name is too heavy-handed: we
113 don't need an absolute name, and we don't need to resolve
114 intermediate symlinks, just the basename of each iteration. */
115 while (i-- && (target = areadlink (name)))
117 if (IS_ABSOLUTE_FILE_NAME (target))
125 char *dir = mdir_name (name);
134 name = mfile_name_concat (dir, target, NULL);
149 if (!target && errno != EINVAL)
153 int saved_errno = errno;
159 result = link (name, file2);
162 int saved_errno = errno;
168 # endif /* 0 < LINK_FOLLOWS_SYMLINKS */
170 /* On Solaris, link() doesn't follow symlinks by default, but does so as soon
171 as a library or executable takes part in the program that has been compiled
172 with "c99" or "cc -xc99=all" or "cc ... /usr/lib/values-xpg4.o ...". */
173 # if LINK_FOLLOWS_SYMLINKS == -1
175 /* Reduce the penalty of link_immediate and link_follow by incorporating the
176 knowledge that link()'s behaviour depends on the __xpg4 variable. */
180 solaris_optimized_link_immediate (char const *file1, char const *file2)
183 return link (file1, file2);
184 return link_immediate (file1, file2);
188 solaris_optimized_link_follow (char const *file1, char const *file2)
191 return link (file1, file2);
192 return link_follow (file1, file2);
195 # define link_immediate solaris_optimized_link_immediate
196 # define link_follow solaris_optimized_link_follow
200 /* Create a link to FILE1, in the directory open on descriptor FD1, to FILE2,
201 in the directory open on descriptor FD2. If FILE1 is a symlink, FLAG
202 controls whether to dereference FILE1 first. If possible, do it without
203 changing the working directory. Otherwise, resort to using
204 save_cwd/fchdir, then rename/restore_cwd. If either the save_cwd or
205 the restore_cwd fails, then give a diagnostic and exit nonzero. */
208 linkat (int fd1, char const *file1, int fd2, char const *file2, int flag)
210 if (flag & ~AT_SYMLINK_FOLLOW)
215 return at_func2 (fd1, file1, fd2, file2,
216 flag ? link_follow : link_immediate);
219 #else /* HAVE_LINKAT */
223 /* Create a link. If FILE1 is a symlink, create a hardlink to the
224 canonicalized file. */
227 linkat_follow (int fd1, char const *file1, int fd2, char const *file2)
229 char *name = (char *) file1;
234 /* There is no realpathat. */
235 while (i-- && (target = areadlinkat (fd1, name)))
237 if (IS_ABSOLUTE_FILE_NAME (target))
245 char *dir = mdir_name (name);
254 name = mfile_name_concat (dir, target, NULL);
269 if (!target && errno != EINVAL)
273 int saved_errno = errno;
279 result = linkat (fd1, name, fd2, file2, 0);
282 int saved_errno = errno;
290 /* Like linkat, but guarantee that AT_SYMLINK_FOLLOW works even on
291 older Linux kernels. */
294 rpl_linkat (int fd1, char const *file1, int fd2, char const *file2, int flag)
296 if (flag & ~AT_SYMLINK_FOLLOW)
302 # if LINKAT_TRAILING_SLASH_BUG
303 /* Reject trailing slashes on non-directories. */
305 size_t len1 = strlen (file1);
306 size_t len2 = strlen (file2);
307 if ((len1 && file1[len1 - 1] == '/')
308 || (len2 && file2[len2 - 1] == '/'))
310 /* Let linkat() decide whether hard-linking directories is legal.
311 If fstatat() fails, then linkat() should fail for the same reason;
312 if fstatat() succeeds, require a directory. */
314 if (fstatat (fd1, file1, &st, flag ? 0 : AT_SYMLINK_NOFOLLOW))
316 if (!S_ISDIR (st.st_mode))
326 return linkat (fd1, file1, fd2, file2, flag);
328 /* Cache the information on whether the system call really works. */
330 static int have_follow_really; /* 0 = unknown, 1 = yes, -1 = no */
331 if (0 <= have_follow_really)
333 int result = linkat (fd1, file1, fd2, file2, flag);
334 if (!(result == -1 && errno == EINVAL))
336 have_follow_really = 1;
339 have_follow_really = -1;
342 return linkat_follow (fd1, file1, fd2, file2);
345 #endif /* HAVE_LINKAT */