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
35 # include <sys/stat.h>
38 # define WIN32_LEAN_AND_MEAN
43 /* Rename the file SRC to DST. This replacement is necessary on
44 Windows, on which the system rename function will not replace
47 rpl_rename (char const *src, char const *dst)
50 size_t src_len = strlen (src);
51 size_t dst_len = strlen (dst);
52 char *src_base = last_component (src);
53 char *dst_base = last_component (dst);
60 /* Filter out dot as last component. */
61 if (!src_len || !dst_len)
68 size_t len = base_len (src_base);
69 if (len == 1 || (len == 2 && src_base[1] == '.'))
77 size_t len = base_len (dst_base);
78 if (len == 1 || (len == 2 && dst_base[1] == '.'))
85 /* Presence of a trailing slash requires directory semantics. If
86 the source does not exist, or if the destination cannot be turned
87 into a directory, give up now. Otherwise, strip trailing slashes
88 before calling rename. There are no symlinks on mingw, so stat
89 works instead of lstat. */
90 src_slash = ISSLASH (src[src_len - 1]);
91 dst_slash = ISSLASH (dst[dst_len - 1]);
92 if (stat (src, &src_st))
94 if (stat (dst, &dst_st))
96 if (errno != ENOENT || (!S_ISDIR (src_st.st_mode) && dst_slash))
102 if (S_ISDIR (dst_st.st_mode) != S_ISDIR (src_st.st_mode))
104 errno = S_ISDIR (dst_st.st_mode) ? EISDIR : ENOTDIR;
110 /* There are no symlinks, so if a file existed with a trailing
111 slash, it must be a directory, and we don't have to worry about
112 stripping strip trailing slash. However, mingw refuses to
113 replace an existing empty directory, so we have to help it out.
114 And canonicalize_file_name is not yet ported to mingw; however,
115 for directories, getcwd works as a viable alternative. Ensure
116 that we can get back to where we started before using it. */
117 if (dst_exists && S_ISDIR (dst_st.st_mode))
119 char *cwd = getcwd (NULL, 0);
124 if (IS_ABSOLUTE_FILE_NAME (src))
126 dst_temp = chdir (dst) ? NULL : getcwd (NULL, 0);
127 src_temp = chdir (src) ? NULL : getcwd (NULL, 0);
131 src_temp = chdir (src) ? NULL : getcwd (NULL, 0);
132 if (!IS_ABSOLUTE_FILE_NAME (dst))
134 dst_temp = chdir (dst) ? NULL : getcwd (NULL, 0);
138 if (!src_temp || !dst_temp)
145 src_len = strlen (src_temp);
146 if (strncmp (src_temp, dst_temp, src_len) == 0
147 && (ISSLASH (dst_temp[src_len]) || dst_temp[src_len] == '\0'))
149 error = dst_temp[src_len];
171 /* MoveFileEx works if SRC is a directory without any flags, but
172 fails with MOVEFILE_REPLACE_EXISTING, so try without flags first.
173 Thankfully, MoveFileEx handles hard links correctly, even though
174 rename() does not. */
175 if (MoveFileEx (src, dst, 0))
178 /* Retry with MOVEFILE_REPLACE_EXISTING if the move failed
179 due to the destination already existing. */
180 error = GetLastError ();
181 if (error == ERROR_FILE_EXISTS || error == ERROR_ALREADY_EXISTS)
183 if (MoveFileEx (src, dst, MOVEFILE_REPLACE_EXISTING))
186 error = GetLastError ();
191 case ERROR_FILE_NOT_FOUND:
192 case ERROR_PATH_NOT_FOUND:
193 case ERROR_BAD_PATHNAME:
194 case ERROR_DIRECTORY:
198 case ERROR_ACCESS_DENIED:
199 case ERROR_SHARING_VIOLATION:
203 case ERROR_OUTOFMEMORY:
207 case ERROR_CURRENT_DIRECTORY:
211 case ERROR_NOT_SAME_DEVICE:
215 case ERROR_WRITE_PROTECT:
219 case ERROR_WRITE_FAULT:
220 case ERROR_READ_FAULT:
221 case ERROR_GEN_FAILURE:
225 case ERROR_HANDLE_DISK_FULL:
226 case ERROR_DISK_FULL:
227 case ERROR_DISK_TOO_FRAGMENTED:
231 case ERROR_FILE_EXISTS:
232 case ERROR_ALREADY_EXISTS:
236 case ERROR_BUFFER_OVERFLOW:
237 case ERROR_FILENAME_EXCED_RANGE:
238 errno = ENAMETOOLONG;
241 case ERROR_INVALID_NAME:
242 case ERROR_DELETE_PENDING:
243 errno = EPERM; /* ? */
246 # ifndef ERROR_FILE_TOO_LARGE
247 /* This value is documented but not defined in all versions of windows.h. */
248 # define ERROR_FILE_TOO_LARGE 223
250 case ERROR_FILE_TOO_LARGE:
262 #else /* ! W32 platform */
268 # include <sys/stat.h>
271 # include "dirname.h"
272 # include "same-inode.h"
274 /* Rename the file SRC to DST, fixing any trailing slash bugs. */
277 rpl_rename (char const *src, char const *dst)
279 size_t src_len = strlen (src);
280 size_t dst_len = strlen (dst);
281 char *src_temp = (char *) src;
282 char *dst_temp = (char *) dst;
287 int rename_errno = ENOTDIR;
291 if (!src_len || !dst_len)
292 return rename (src, dst); /* Let strace see the ENOENT failure. */
294 # if RENAME_DEST_EXISTS_BUG
296 char *src_base = last_component (src);
297 char *dst_base = last_component (dst);
298 if (*src_base == '.')
300 size_t len = base_len (src_base);
301 if (len == 1 || (len == 2 && src_base[1] == '.'))
307 if (*dst_base == '.')
309 size_t len = base_len (dst_base);
310 if (len == 1 || (len == 2 && dst_base[1] == '.'))
317 # endif /* RENAME_DEST_EXISTS_BUG */
319 src_slash = src[src_len - 1] == '/';
320 dst_slash = dst[dst_len - 1] == '/';
322 # if !RENAME_HARD_LINK_BUG && !RENAME_DEST_EXISTS_BUG
323 /* If there are no trailing slashes, then trust the native
324 implementation unless we also suspect issues with hard link
325 detection or file/directory conflicts. */
326 if (!src_slash && !dst_slash)
327 return rename (src, dst);
328 # endif /* !RENAME_HARD_LINK_BUG && !RENAME_DEST_EXISTS_BUG */
330 /* Presence of a trailing slash requires directory semantics. If
331 the source does not exist, or if the destination cannot be turned
332 into a directory, give up now. Otherwise, strip trailing slashes
333 before calling rename. */
334 if (lstat (src, &src_st))
336 if (lstat (dst, &dst_st))
338 if (errno != ENOENT || (!S_ISDIR (src_st.st_mode) && dst_slash))
344 if (S_ISDIR (dst_st.st_mode) != S_ISDIR (src_st.st_mode))
346 errno = S_ISDIR (dst_st.st_mode) ? EISDIR : ENOTDIR;
349 # if RENAME_HARD_LINK_BUG
350 if (SAME_INODE (src_st, dst_st))
352 # endif /* RENAME_HARD_LINK_BUG */
356 # if (RENAME_TRAILING_SLASH_SOURCE_BUG || RENAME_DEST_EXISTS_BUG \
357 || RENAME_HARD_LINK_BUG)
358 /* If the only bug was that a trailing slash was allowed on a
359 non-existing file destination, as in Solaris 10, then we've
360 already covered that situation. But if there is any problem with
361 a trailing slash on an existing source or destination, as in
362 Solaris 9, or if a directory can overwrite a symlink, as on
363 Cygwin 1.5, or if directories cannot be created with trailing
364 slash, as on NetBSD 1.6, then we must strip the offending slash
365 and check that we have not encountered a symlink instead of a
368 Stripping a trailing slash interferes with POSIX semantics, where
369 rename behavior on a symlink with a trailing slash operates on
370 the corresponding target directory. We prefer the GNU semantics
371 of rejecting any use of a symlink with trailing slash, but do not
372 enforce them, since Solaris 10 is able to obey POSIX semantics
373 and there might be clients expecting it, as counter-intuitive as
376 Technically, we could also follow the POSIX behavior by chasing a
377 readlink trail, but that is harder to implement. */
380 src_temp = strdup (src);
383 /* Rather than rely on strdup-posix, we set errno ourselves. */
384 rename_errno = ENOMEM;
387 strip_trailing_slashes (src_temp);
388 if (lstat (src_temp, &src_st))
390 rename_errno = errno;
393 if (S_ISLNK (src_st.st_mode))
398 dst_temp = strdup (dst);
401 rename_errno = ENOMEM;
404 strip_trailing_slashes (dst_temp);
405 if (lstat (dst_temp, &dst_st))
409 rename_errno = errno;
413 else if (S_ISLNK (dst_st.st_mode))
416 # endif /* RENAME_TRAILING_SLASH_SOURCE_BUG || RENAME_DEST_EXISTS_BUG
417 || RENAME_HARD_LINK_BUG */
419 # if RENAME_DEST_EXISTS_BUG
420 /* Cygwin 1.5 sometimes behaves oddly when moving a non-empty
421 directory on top of an empty one (the old directory name can
422 reappear if the new directory tree is removed). Work around this
423 by removing the target first, but don't remove the target if it
424 is a subdirectory of the source. */
425 if (dst_exists && S_ISDIR (dst_st.st_mode))
429 src_temp = canonicalize_file_name (src);
432 dst_temp = canonicalize_file_name (dst);
433 if (!src_temp || !dst_temp)
435 rename_errno = ENOMEM;
438 src_len = strlen (src_temp);
439 if (strncmp (src_temp, dst_temp, src_len) == 0
440 && dst_temp[src_len] == '/')
442 rename_errno = EINVAL;
447 rename_errno = errno;
451 # endif /* RENAME_DEST_EXISTS_BUG */
453 ret_val = rename (src_temp, dst_temp);
454 rename_errno = errno;
460 errno = rename_errno;
463 #endif /* ! W32 platform */