1 /* Create a hard link relative to open directories.
2 Copyright (C) 2009-2021 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 <https://www.gnu.org/licenses/>. */
17 /* written by Eric Blake */
30 #include "areadlink.h"
32 #include "eloop-threshold.h"
33 #include "filenamecat.h"
34 #include "openat-priv.h"
36 #if !HAVE_LINKAT || LINKAT_SYMLINK_NOTSUP
38 /* Create a link. If FILE1 is a symlink, either create a hardlink to
39 that symlink, or fake it by creating an identical symlink. */
40 # if LINK_FOLLOWS_SYMLINKS == 0
41 # define link_immediate link
44 link_immediate (char const *file1
, char const *file2
)
46 char *target
= areadlink (file1
);
49 /* A symlink cannot be modified in-place. Therefore, creating
50 an identical symlink behaves like a hard link to a symlink,
51 except for incorrect st_ino and st_nlink. However, we must
52 be careful of EXDEV. */
55 char *dir
= mdir_name (file2
);
62 if (lstat (file1
, &st1
) == 0 && stat (dir
, &st2
) == 0)
64 if (st1
.st_dev
== st2
.st_dev
)
66 int result
= symlink (target
, file2
);
67 int saved_errno
= errno
;
83 return link (file1
, file2
);
85 # endif /* LINK_FOLLOWS_SYMLINKS == 0 */
87 /* Create a link. If FILE1 is a symlink, create a hardlink to the
88 canonicalized file. */
89 # if 0 < LINK_FOLLOWS_SYMLINKS
90 # define link_follow link
93 link_follow (char const *file1
, char const *file2
)
95 char *name
= (char *) file1
;
98 int i
= __eloop_threshold ();
100 /* Using realpath or canonicalize_file_name is too heavy-handed: we
101 don't need an absolute name, and we don't need to resolve
102 intermediate symlinks, just the basename of each iteration. */
103 while (i
-- && (target
= areadlink (name
)))
105 if (IS_ABSOLUTE_FILE_NAME (target
))
113 char *dir
= mdir_name (name
);
122 name
= mfile_name_concat (dir
, target
, NULL
);
137 if (!target
&& errno
!= EINVAL
)
141 int saved_errno
= errno
;
147 result
= link (name
, file2
);
150 int saved_errno
= errno
;
156 # endif /* 0 < LINK_FOLLOWS_SYMLINKS */
158 /* On Solaris, link() doesn't follow symlinks by default, but does so as soon
159 as a library or executable takes part in the program that has been compiled
160 with "c99" or "cc -xc99=all" or "cc ... /usr/lib/values-xpg4.o ...". */
161 # if LINK_FOLLOWS_SYMLINKS == -1
163 /* Reduce the penalty of link_immediate and link_follow by incorporating the
164 knowledge that link()'s behaviour depends on the __xpg4 variable. */
168 solaris_optimized_link_immediate (char const *file1
, char const *file2
)
171 return link (file1
, file2
);
172 return link_immediate (file1
, file2
);
176 solaris_optimized_link_follow (char const *file1
, char const *file2
)
179 return link (file1
, file2
);
180 return link_follow (file1
, file2
);
183 # define link_immediate solaris_optimized_link_immediate
184 # define link_follow solaris_optimized_link_follow
188 #endif /* !HAVE_LINKAT || LINKAT_SYMLINK_NOTSUP */
192 /* Create a link to FILE1, in the directory open on descriptor FD1, to FILE2,
193 in the directory open on descriptor FD2. If FILE1 is a symlink, FLAG
194 controls whether to dereference FILE1 first. If possible, do it without
195 changing the working directory. Otherwise, resort to using
196 save_cwd/fchdir, then rename/restore_cwd. If either the save_cwd or
197 the restore_cwd fails, then give a diagnostic and exit nonzero. */
200 linkat (int fd1
, char const *file1
, int fd2
, char const *file2
, int flag
)
202 if (flag
& ~AT_SYMLINK_FOLLOW
)
207 return at_func2 (fd1
, file1
, fd2
, file2
,
208 flag
? link_follow
: link_immediate
);
211 #else /* HAVE_LINKAT */
215 /* Create a link. If FILE1 is a symlink, create a hardlink to the
216 canonicalized file. */
219 linkat_follow (int fd1
, char const *file1
, int fd2
, char const *file2
)
221 char *name
= (char *) file1
;
224 int i
= __eloop_threshold ();
226 /* There is no realpathat. */
227 while (i
-- && (target
= areadlinkat (fd1
, name
)))
229 if (IS_ABSOLUTE_FILE_NAME (target
))
237 char *dir
= mdir_name (name
);
246 name
= mfile_name_concat (dir
, target
, NULL
);
261 if (!target
&& errno
!= EINVAL
)
265 int saved_errno
= errno
;
271 result
= linkat (fd1
, name
, fd2
, file2
, 0);
274 int saved_errno
= errno
;
282 /* Like linkat, but guarantee that AT_SYMLINK_FOLLOW works even on
283 older Linux kernels. */
286 rpl_linkat (int fd1
, char const *file1
, int fd2
, char const *file2
, int flag
)
288 if (flag
& ~AT_SYMLINK_FOLLOW
)
294 # if LINKAT_TRAILING_SLASH_BUG
295 /* Reject trailing slashes on non-directories. */
297 size_t len1
= strlen (file1
);
298 size_t len2
= strlen (file2
);
299 if ((len1
&& file1
[len1
- 1] == '/')
300 || (len2
&& file2
[len2
- 1] == '/'))
302 /* Let linkat() decide whether hard-linking directories is legal.
303 If fstatat() fails, then linkat() should fail for the same reason;
304 if fstatat() succeeds, require a directory. */
306 if (fstatat (fd1
, file1
, &st
, flag
? 0 : AT_SYMLINK_NOFOLLOW
))
308 if (!S_ISDIR (st
.st_mode
))
319 int result
= linkat (fd1
, file1
, fd2
, file2
, flag
);
320 # if LINKAT_SYMLINK_NOTSUP
321 /* OS X 10.10 has linkat() but it doesn't support
322 hardlinks to symlinks. Fallback to our emulation
324 if (result
== -1 && (errno
== ENOTSUP
|| errno
== EOPNOTSUPP
))
325 return at_func2 (fd1
, file1
, fd2
, file2
, link_immediate
);
330 /* Cache the information on whether the system call really works. */
332 static int have_follow_really
; /* 0 = unknown, 1 = yes, -1 = no */
333 if (0 <= have_follow_really
)
335 int result
= linkat (fd1
, file1
, fd2
, file2
, flag
);
336 if (!(result
== -1 && errno
== EINVAL
))
338 have_follow_really
= 1;
341 have_follow_really
= -1;
344 return linkat_follow (fd1
, file1
, fd2
, file2
);
347 #endif /* HAVE_LINKAT */