1 /* Create a hard link relative to open directories.
2 Copyright (C) 2009-2010 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 /* Create a link to FILE1, in the directory open on descriptor FD1, to FILE2,
170 in the directory open on descriptor FD2. If FILE1 is a symlink, FLAG
171 controls whether to dereference FILE1 first. If possible, do it without
172 changing the working directory. Otherwise, resort to using
173 save_cwd/fchdir, then rename/restore_cwd. If either the save_cwd or
174 the restore_cwd fails, then give a diagnostic and exit nonzero. */
177 linkat (int fd1, char const *file1, int fd2, char const *file2, int flag)
179 if (flag & ~AT_SYMLINK_FOLLOW)
184 return at_func2 (fd1, file1, fd2, file2,
185 flag ? link_follow : link_immediate);
188 #else /* HAVE_LINKAT */
192 /* Create a link. If FILE1 is a symlink, create a hardlink to the
193 canonicalized file. */
196 linkat_follow (int fd1, char const *file1, int fd2, char const *file2)
198 char *name = (char *) file1;
203 /* There is no realpathat. */
204 while (i-- && (target = areadlinkat (fd1, name)))
206 if (IS_ABSOLUTE_FILE_NAME (target))
214 char *dir = mdir_name (name);
223 name = mfile_name_concat (dir, target, NULL);
238 if (!target && errno != EINVAL)
242 int saved_errno = errno;
248 result = linkat (fd1, name, fd2, file2, 0);
251 int saved_errno = errno;
259 /* Like linkat, but guarantee that AT_SYMLINK_FOLLOW works even on
260 older Linux kernels. */
263 rpl_linkat (int fd1, char const *file1, int fd2, char const *file2, int flag)
266 return linkat (fd1, file1, fd2, file2, flag);
267 if (flag & ~AT_SYMLINK_FOLLOW)
273 /* Cache the information on whether the system call really works. */
275 static int have_follow_really; /* 0 = unknown, 1 = yes, -1 = no */
276 if (0 <= have_follow_really)
278 int result = linkat (fd1, file1, fd2, file2, flag);
279 if (!(result == -1 && errno == EINVAL))
281 have_follow_really = 1;
284 have_follow_really = -1;
287 return linkat_follow (fd1, file1, fd2, file2);
290 #endif /* HAVE_LINKAT */