1 /* Rename a file relative to open directories.
2 Copyright (C) 2009-2016 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 */
29 # include <sys/stat.h>
36 /* renameat does not honor trailing / on Solaris 10. Solve it in a
37 similar manner to rename. No need to worry about bugs not present
38 on Solaris, since all other systems either lack renameat or honor
39 trailing slash correctly. */
42 rpl_renameat (int fd1, char const *src, int fd2, char const *dst)
44 size_t src_len = strlen (src);
45 size_t dst_len = strlen (dst);
46 char *src_temp = (char *) src;
47 char *dst_temp = (char *) dst;
51 int rename_errno = ENOTDIR;
55 /* Let strace see any ENOENT failure. */
56 if (!src_len || !dst_len)
57 return renameat (fd1, src, fd2, dst);
59 src_slash = src[src_len - 1] == '/';
60 dst_slash = dst[dst_len - 1] == '/';
61 if (!src_slash && !dst_slash)
62 return renameat (fd1, src, fd2, dst);
64 /* Presence of a trailing slash requires directory semantics. If
65 the source does not exist, or if the destination cannot be turned
66 into a directory, give up now. Otherwise, strip trailing slashes
67 before calling rename. */
68 if (lstatat (fd1, src, &src_st))
70 if (lstatat (fd2, dst, &dst_st))
72 if (errno != ENOENT || !S_ISDIR (src_st.st_mode))
75 else if (!S_ISDIR (dst_st.st_mode))
80 else if (!S_ISDIR (src_st.st_mode))
86 # if RENAME_TRAILING_SLASH_SOURCE_BUG
87 /* See the lengthy comment in rename.c why Solaris 9 is forced to
88 GNU behavior, while Solaris 10 is left with POSIX behavior,
89 regarding symlinks with trailing slash. */
92 src_temp = strdup (src);
95 /* Rather than rely on strdup-posix, we set errno ourselves. */
96 rename_errno = ENOMEM;
99 strip_trailing_slashes (src_temp);
100 if (lstatat (fd1, src_temp, &src_st))
102 rename_errno = errno;
105 if (S_ISLNK (src_st.st_mode))
110 dst_temp = strdup (dst);
113 rename_errno = ENOMEM;
116 strip_trailing_slashes (dst_temp);
117 if (lstatat (fd2, dst_temp, &dst_st))
121 rename_errno = errno;
125 else if (S_ISLNK (dst_st.st_mode))
128 # endif /* RENAME_TRAILING_SLASH_SOURCE_BUG */
130 ret_val = renameat (fd1, src_temp, fd2, dst_temp);
131 rename_errno = errno;
137 errno = rename_errno;
141 #else /* !HAVE_RENAMEAT */
143 # include "openat-priv.h"
145 /* Rename FILE1, in the directory open on descriptor FD1, to FILE2, in
146 the directory open on descriptor FD2. If possible, do it without
147 changing the working directory. Otherwise, resort to using
148 save_cwd/fchdir, then rename/restore_cwd. If either the save_cwd or
149 the restore_cwd fails, then give a diagnostic and exit nonzero. */
152 renameat (int fd1, char const *file1, int fd2, char const *file2)
154 return at_func2 (fd1, file1, fd2, file2, rename);
157 #endif /* !HAVE_RENAMEAT */