1 /* Rename a file 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 */
21 /* Solaris 10, which predates POSIX-2008, declares its renameat in
22 unistd.h. Include unistd.h before including stdio.h, so that
23 gnulib's stdio.h doesn't #define renameat to rpl_renameat before
24 Solaris 10's unistd.h declares the system renameat. */
35 # include <sys/stat.h>
42 /* renameat does not honor trailing / on Solaris 10. Solve it in a
43 similar manner to rename. No need to worry about bugs not present
44 on Solaris, since all other systems either lack renameat or honor
45 trailing slash correctly. */
48 rpl_renameat (int fd1, char const *src, int fd2, char const *dst)
50 size_t src_len = strlen (src);
51 size_t dst_len = strlen (dst);
52 char *src_temp = (char *) src;
53 char *dst_temp = (char *) dst;
57 int rename_errno = ENOTDIR;
61 /* Let strace see any ENOENT failure. */
62 if (!src_len || !dst_len)
63 return renameat (fd1, src, fd2, dst);
65 src_slash = src[src_len - 1] == '/';
66 dst_slash = dst[dst_len - 1] == '/';
67 if (!src_slash && !dst_slash)
68 return renameat (fd1, src, fd2, dst);
70 /* Presence of a trailing slash requires directory semantics. If
71 the source does not exist, or if the destination cannot be turned
72 into a directory, give up now. Otherwise, strip trailing slashes
73 before calling rename. */
74 if (lstatat (fd1, src, &src_st))
76 if (lstatat (fd2, dst, &dst_st))
78 if (errno != ENOENT || !S_ISDIR (src_st.st_mode))
81 else if (!S_ISDIR (dst_st.st_mode))
86 else if (!S_ISDIR (src_st.st_mode))
92 # if RENAME_TRAILING_SLASH_SOURCE_BUG
93 /* See the lengthy comment in rename.c why Solaris 9 is forced to
94 GNU behavior, while Solaris 10 is left with POSIX behavior,
95 regarding symlinks with trailing slash. */
98 src_temp = strdup (src);
101 /* Rather than rely on strdup-posix, we set errno ourselves. */
102 rename_errno = ENOMEM;
105 strip_trailing_slashes (src_temp);
106 if (lstatat (fd1, src_temp, &src_st))
108 rename_errno = errno;
111 if (S_ISLNK (src_st.st_mode))
116 dst_temp = strdup (dst);
119 rename_errno = ENOMEM;
122 strip_trailing_slashes (dst_temp);
123 if (lstatat (fd2, dst_temp, &dst_st))
127 rename_errno = errno;
131 else if (S_ISLNK (dst_st.st_mode))
134 # endif /* RENAME_TRAILING_SLASH_SOURCE_BUG */
136 ret_val = renameat (fd1, src_temp, fd2, dst_temp);
137 rename_errno = errno;
143 errno = rename_errno;
147 #else /* !HAVE_RENAMEAT */
149 # include "openat-priv.h"
151 /* Rename FILE1, in the directory open on descriptor FD1, to FILE2, in
152 the directory open on descriptor FD2. If possible, do it without
153 changing the working directory. Otherwise, resort to using
154 save_cwd/fchdir, then rename/restore_cwd. If either the save_cwd or
155 the restore_cwd fails, then give a diagnostic and exit nonzero. */
158 renameat (int fd1, char const *file1, int fd2, char const *file2)
160 return at_func2 (fd1, file1, fd2, file2, rename);
163 #endif /* !HAVE_RENAMEAT */