1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Rename a file 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 */
31 # include <sys/stat.h>
38 /* renameat does not honor trailing / on Solaris 10. Solve it in a
39 similar manner to rename. No need to worry about bugs not present
40 on Solaris, since all other systems either lack renameat or honor
41 trailing slash correctly. */
44 rpl_renameat (int fd1, char const *src, int fd2, char const *dst)
46 size_t src_len = strlen (src);
47 size_t dst_len = strlen (dst);
48 char *src_temp = (char *) src;
49 char *dst_temp = (char *) dst;
53 int rename_errno = ENOTDIR;
57 /* Let strace see any ENOENT failure. */
58 if (!src_len || !dst_len)
59 return renameat (fd1, src, fd2, dst);
61 src_slash = src[src_len - 1] == '/';
62 dst_slash = dst[dst_len - 1] == '/';
63 if (!src_slash && !dst_slash)
64 return renameat (fd1, src, fd2, dst);
66 /* Presence of a trailing slash requires directory semantics. If
67 the source does not exist, or if the destination cannot be turned
68 into a directory, give up now. Otherwise, strip trailing slashes
69 before calling rename. */
70 if (lstatat (fd1, src, &src_st))
72 if (lstatat (fd2, dst, &dst_st))
74 if (errno != ENOENT || !S_ISDIR (src_st.st_mode))
77 else if (!S_ISDIR (dst_st.st_mode))
82 else if (!S_ISDIR (src_st.st_mode))
88 # if RENAME_TRAILING_SLASH_SOURCE_BUG
89 /* See the lengthy comment in rename.c why Solaris 9 is forced to
90 GNU behavior, while Solaris 10 is left with POSIX behavior,
91 regarding symlinks with trailing slash. */
94 src_temp = strdup (src);
97 /* Rather than rely on strdup-posix, we set errno ourselves. */
98 rename_errno = ENOMEM;
101 strip_trailing_slashes (src_temp);
102 if (lstatat (fd1, src_temp, &src_st))
104 rename_errno = errno;
107 if (S_ISLNK (src_st.st_mode))
112 dst_temp = strdup (dst);
115 rename_errno = ENOMEM;
118 strip_trailing_slashes (dst_temp);
119 if (lstatat (fd2, dst_temp, &dst_st))
123 rename_errno = errno;
127 else if (S_ISLNK (dst_st.st_mode))
130 # endif /* RENAME_TRAILING_SLASH_SOURCE_BUG */
132 ret_val = renameat (fd1, src_temp, fd2, dst_temp);
133 rename_errno = errno;
139 errno = rename_errno;
143 #else /* !HAVE_RENAMEAT */
145 # include "openat-priv.h"
147 /* Rename FILE1, in the directory open on descriptor FD1, to FILE2, in
148 the directory open on descriptor FD2. If possible, do it without
149 changing the working directory. Otherwise, resort to using
150 save_cwd/fchdir, then rename/restore_cwd. If either the save_cwd or
151 the restore_cwd fails, then give a diagnostic and exit nonzero. */
154 renameat (int fd1, char const *file1, int fd2, char const *file2)
156 return at_func2 (fd1, file1, fd2, file2, rename);
159 #endif /* !HAVE_RENAMEAT */