1 /* Work around rename bugs in some systems.
3 Copyright (C) 2001, 2002, 2003, 2005, 2006, 2009 Free Software
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 Volker Borchert, Eric Blake. */
27 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
28 /* The mingw rename has problems with trailing slashes; it also
29 requires use of native Windows calls to allow atomic renames over
34 # define WIN32_LEAN_AND_MEAN
37 /* Rename the file SRC to DST. This replacement is necessary on
38 Windows, on which the system rename function will not replace
41 rpl_rename (char const *src, char const *dst)
45 /* MoveFileEx works if SRC is a directory without any flags,
46 but fails with MOVEFILE_REPLACE_EXISTING, so try without
48 if (MoveFileEx (src, dst, 0))
51 /* Retry with MOVEFILE_REPLACE_EXISTING if the move failed
52 * due to the destination already existing. */
53 error = GetLastError ();
54 if (error == ERROR_FILE_EXISTS || error == ERROR_ALREADY_EXISTS)
56 if (MoveFileEx (src, dst, MOVEFILE_REPLACE_EXISTING))
59 error = GetLastError ();
64 case ERROR_FILE_NOT_FOUND:
65 case ERROR_PATH_NOT_FOUND:
66 case ERROR_BAD_PATHNAME:
71 case ERROR_ACCESS_DENIED:
72 case ERROR_SHARING_VIOLATION:
76 case ERROR_OUTOFMEMORY:
80 case ERROR_CURRENT_DIRECTORY:
84 case ERROR_NOT_SAME_DEVICE:
88 case ERROR_WRITE_PROTECT:
92 case ERROR_WRITE_FAULT:
93 case ERROR_READ_FAULT:
94 case ERROR_GEN_FAILURE:
98 case ERROR_HANDLE_DISK_FULL:
100 case ERROR_DISK_TOO_FRAGMENTED:
104 case ERROR_FILE_EXISTS:
105 case ERROR_ALREADY_EXISTS:
109 case ERROR_BUFFER_OVERFLOW:
110 case ERROR_FILENAME_EXCED_RANGE:
111 errno = ENAMETOOLONG;
114 case ERROR_INVALID_NAME:
115 case ERROR_DELETE_PENDING:
116 errno = EPERM; /* ? */
119 # ifndef ERROR_FILE_TOO_LARGE
120 /* This value is documented but not defined in all versions of windows.h. */
121 # define ERROR_FILE_TOO_LARGE 223
123 case ERROR_FILE_TOO_LARGE:
135 #else /* ! W32 platform */
141 # include <sys/stat.h>
144 # include "dirname.h"
145 # include "same-inode.h"
147 /* Rename the file SRC to DST, fixing any trailing slash bugs. */
150 rpl_rename (char const *src, char const *dst)
152 size_t src_len = strlen (src);
153 size_t dst_len = strlen (dst);
154 char *src_temp = (char *) src;
155 char *dst_temp = (char *) dst;
160 int rename_errno = ENOTDIR;
164 if (!src_len || !dst_len)
165 return rename (src, dst); /* Let strace see the ENOENT failure. */
167 # if RENAME_DEST_EXISTS_BUG
169 char *src_base = last_component (src);
170 char *dst_base = last_component (dst);
171 if (*src_base == '.')
173 size_t len = base_len (src_base);
174 if (len == 1 || (len == 2 && src_base[1] == '.'))
180 if (*dst_base == '.')
182 size_t len = base_len (dst_base);
183 if (len == 1 || (len == 2 && dst_base[1] == '.'))
190 # endif /* RENAME_DEST_EXISTS_BUG */
192 src_slash = src[src_len - 1] == '/';
193 dst_slash = dst[dst_len - 1] == '/';
195 # if !RENAME_HARD_LINK_BUG && !RENAME_DEST_EXISTS_BUG
196 /* If there are no trailing slashes, then trust the native
197 implementation unless we also suspect issues with hard link
198 detection or file/directory conflicts. */
199 if (!src_slash && !dst_slash)
200 return rename (src, dst);
201 # endif /* !RENAME_HARD_LINK_BUG && !RENAME_DEST_EXISTS_BUG */
203 /* Presence of a trailing slash requires directory semantics. If
204 the source does not exist, or if the destination cannot be turned
205 into a directory, give up now. Otherwise, strip trailing slashes
206 before calling rename. */
207 if (lstat (src, &src_st))
209 if (lstat (dst, &dst_st))
211 if (errno != ENOENT || (!S_ISDIR (src_st.st_mode) && dst_slash))
217 if (S_ISDIR (dst_st.st_mode) != S_ISDIR (src_st.st_mode))
219 errno = S_ISDIR (dst_st.st_mode) ? EISDIR : ENOTDIR;
222 # if RENAME_HARD_LINK_BUG
223 if (SAME_INODE (src_st, dst_st))
225 # endif /* RENAME_HARD_LINK_BUG */
229 # if (RENAME_TRAILING_SLASH_SOURCE_BUG || RENAME_DEST_EXISTS_BUG \
230 || RENAME_HARD_LINK_BUG)
231 /* If the only bug was that a trailing slash was allowed on a
232 non-existing file destination, as in Solaris 10, then we've
233 already covered that situation. But if there is any problem with
234 a trailing slash on an existing source or destination, as in
235 Solaris 9, or if a directory can overwrite a symlink, as on
236 Cygwin 1.5, or if directories cannot be created with trailing
237 slash, as on NetBSD 1.6, then we must strip the offending slash
238 and check that we have not encountered a symlink instead of a
241 Stripping a trailing slash interferes with POSIX semantics, where
242 rename behavior on a symlink with a trailing slash operates on
243 the corresponding target directory. We prefer the GNU semantics
244 of rejecting any use of a symlink with trailing slash, but do not
245 enforce them, since Solaris 10 is able to obey POSIX semantics
246 and there might be clients expecting it, as counter-intuitive as
249 Technically, we could also follow the POSIX behavior by chasing a
250 readlink trail, but that is harder to implement. */
253 src_temp = strdup (src);
256 /* Rather than rely on strdup-posix, we set errno ourselves. */
257 rename_errno = ENOMEM;
260 strip_trailing_slashes (src_temp);
261 if (lstat (src_temp, &src_st))
263 rename_errno = errno;
266 if (S_ISLNK (src_st.st_mode))
271 dst_temp = strdup (dst);
274 rename_errno = ENOMEM;
277 strip_trailing_slashes (dst_temp);
278 if (lstat (dst_temp, &dst_st))
282 rename_errno = errno;
286 else if (S_ISLNK (dst_st.st_mode))
289 # endif /* RENAME_TRAILING_SLASH_SOURCE_BUG || RENAME_DEST_EXISTS_BUG
290 || RENAME_HARD_LINK_BUG */
292 # if RENAME_DEST_EXISTS_BUG
293 /* Cygwin 1.5 sometimes behaves oddly when moving a non-empty
294 directory on top of an empty one (the old directory name can
295 reappear if the new directory tree is removed). Work around this
296 by removing the target first, but don't remove the target if it
297 is a subdirectory of the source. */
298 if (dst_exists && S_ISDIR (dst_st.st_mode))
302 src_temp = canonicalize_file_name (src);
305 dst_temp = canonicalize_file_name (dst);
306 if (!src_temp || !dst_temp)
308 rename_errno = ENOMEM;
311 src_len = strlen (src_temp);
312 if (strncmp (src_temp, dst_temp, src_len) == 0
313 && dst_temp[src_len] == '/')
315 rename_errno = EINVAL;
320 rename_errno = errno;
324 # endif /* RENAME_DEST_EXISTS_BUG */
326 ret_val = rename (src_temp, dst_temp);
327 rename_errno = errno;
333 errno = rename_errno;
336 #endif /* ! W32 platform */