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-2010 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 */
33 #include "areadlink.h"
35 #include "filenamecat.h"
36 #include "openat-priv.h"
39 # include <sys/param.h>
43 # define MAXSYMLINKS SYMLOOP_MAX
45 # define MAXSYMLINKS 20
51 /* Create a link. If FILE1 is a symlink, either create a hardlink to
52 that symlink, or fake it by creating an identical symlink. */
53 # if LINK_FOLLOWS_SYMLINKS == 0
54 # define link_immediate link
57 link_immediate (char const *file1, char const *file2)
59 char *target = areadlink (file1);
62 /* A symlink cannot be modified in-place. Therefore, creating
63 an identical symlink behaves like a hard link to a symlink,
64 except for incorrect st_ino and st_nlink. However, we must
65 be careful of EXDEV. */
68 char *dir = mdir_name (file2);
75 if (lstat (file1, &st1) == 0 && stat (dir, &st2) == 0)
77 if (st1.st_dev == st2.st_dev)
79 int result = symlink (target, file2);
80 int saved_errno = errno;
96 return link (file1, file2);
98 # endif /* LINK_FOLLOWS_SYMLINKS == 0 */
100 /* Create a link. If FILE1 is a symlink, create a hardlink to the
101 canonicalized file. */
102 # if 0 < LINK_FOLLOWS_SYMLINKS
103 # define link_follow link
106 link_follow (char const *file1, char const *file2)
108 char *name = (char *) file1;
113 /* Using realpath or canonicalize_file_name is too heavy-handed: we
114 don't need an absolute name, and we don't need to resolve
115 intermediate symlinks, just the basename of each iteration. */
116 while (i-- && (target = areadlink (name)))
118 if (IS_ABSOLUTE_FILE_NAME (target))
126 char *dir = mdir_name (name);
135 name = mfile_name_concat (dir, target, NULL);
150 if (!target && errno != EINVAL)
154 int saved_errno = errno;
160 result = link (name, file2);
163 int saved_errno = errno;
169 # endif /* 0 < LINK_FOLLOWS_SYMLINKS */
171 /* Create a link to FILE1, in the directory open on descriptor FD1, to FILE2,
172 in the directory open on descriptor FD2. If FILE1 is a symlink, FLAG
173 controls whether to dereference FILE1 first. If possible, do it without
174 changing the working directory. Otherwise, resort to using
175 save_cwd/fchdir, then rename/restore_cwd. If either the save_cwd or
176 the restore_cwd fails, then give a diagnostic and exit nonzero. */
179 linkat (int fd1, char const *file1, int fd2, char const *file2, int flag)
181 if (flag & ~AT_SYMLINK_FOLLOW)
186 return at_func2 (fd1, file1, fd2, file2,
187 flag ? link_follow : link_immediate);
190 #else /* HAVE_LINKAT */
194 /* Create a link. If FILE1 is a symlink, create a hardlink to the
195 canonicalized file. */
198 linkat_follow (int fd1, char const *file1, int fd2, char const *file2)
200 char *name = (char *) file1;
205 /* There is no realpathat. */
206 while (i-- && (target = areadlinkat (fd1, name)))
208 if (IS_ABSOLUTE_FILE_NAME (target))
216 char *dir = mdir_name (name);
225 name = mfile_name_concat (dir, target, NULL);
240 if (!target && errno != EINVAL)
244 int saved_errno = errno;
250 result = linkat (fd1, name, fd2, file2, 0);
253 int saved_errno = errno;
261 /* Like linkat, but guarantee that AT_SYMLINK_FOLLOW works even on
262 older Linux kernels. */
265 rpl_linkat (int fd1, char const *file1, int fd2, char const *file2, int flag)
267 if (flag & ~AT_SYMLINK_FOLLOW)
273 #if LINKAT_TRAILING_SLASH_BUG
274 /* Reject trailing slashes on non-directories. */
276 size_t len1 = strlen (file1);
277 size_t len2 = strlen (file2);
278 if ((len1 && file1[len1 - 1] == '/')
279 || (len2 && file2[len2 - 1] == '/'))
281 /* Let linkat() decide whether hard-linking directories is legal.
282 If fstatat() fails, then linkat() should fail for the same reason;
283 if fstatat() succeeds, require a directory. */
285 if (fstatat (fd1, file1, &st, flag ? 0 : AT_SYMLINK_NOFOLLOW))
287 if (!S_ISDIR (st.st_mode))
297 return linkat (fd1, file1, fd2, file2, flag);
299 /* Cache the information on whether the system call really works. */
301 static int have_follow_really; /* 0 = unknown, 1 = yes, -1 = no */
302 if (0 <= have_follow_really)
304 int result = linkat (fd1, file1, fd2, file2, flag);
305 if (!(result == -1 && errno == EINVAL))
307 have_follow_really = 1;
310 have_follow_really = -1;
313 return linkat_follow (fd1, file1, fd2, file2);
316 #endif /* HAVE_LINKAT */