1 /* Work around rename bugs in some systems. On SunOS 4.1.1_U1
2 and mips-dec-ultrix4.4, rename fails when the source file has
3 a trailing slash. On mingw, rename fails when the destination
6 Copyright (C) 2001, 2002, 2003, 2005, 2006, 2009 Free Software
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* written by Volker Borchert */
30 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
31 /* The mingw rename has problems with trailing slashes; it also
32 requires use of native Windows calls to allow atomic renames over
37 # define WIN32_LEAN_AND_MEAN
40 /* Rename the file SRC to DST. This replacement is necessary on
41 Windows, on which the system rename function will not replace
44 rpl_rename (char const *src, char const *dst)
48 /* MoveFileEx works if SRC is a directory without any flags,
49 but fails with MOVEFILE_REPLACE_EXISTING, so try without
51 if (MoveFileEx (src, dst, 0))
54 /* Retry with MOVEFILE_REPLACE_EXISTING if the move failed
55 * due to the destination already existing. */
56 error = GetLastError ();
57 if (error == ERROR_FILE_EXISTS || error == ERROR_ALREADY_EXISTS)
59 if (MoveFileEx (src, dst, MOVEFILE_REPLACE_EXISTING))
62 error = GetLastError ();
67 case ERROR_FILE_NOT_FOUND:
68 case ERROR_PATH_NOT_FOUND:
69 case ERROR_BAD_PATHNAME:
74 case ERROR_ACCESS_DENIED:
75 case ERROR_SHARING_VIOLATION:
79 case ERROR_OUTOFMEMORY:
83 case ERROR_CURRENT_DIRECTORY:
87 case ERROR_NOT_SAME_DEVICE:
91 case ERROR_WRITE_PROTECT:
95 case ERROR_WRITE_FAULT:
96 case ERROR_READ_FAULT:
97 case ERROR_GEN_FAILURE:
101 case ERROR_HANDLE_DISK_FULL:
102 case ERROR_DISK_FULL:
103 case ERROR_DISK_TOO_FRAGMENTED:
107 case ERROR_FILE_EXISTS:
108 case ERROR_ALREADY_EXISTS:
112 case ERROR_BUFFER_OVERFLOW:
113 case ERROR_FILENAME_EXCED_RANGE:
114 errno = ENAMETOOLONG;
117 case ERROR_INVALID_NAME:
118 case ERROR_DELETE_PENDING:
119 errno = EPERM; /* ? */
122 # ifndef ERROR_FILE_TOO_LARGE
123 /* This value is documented but not defined in all versions of windows.h. */
124 # define ERROR_FILE_TOO_LARGE 223
126 case ERROR_FILE_TOO_LARGE:
138 #else /* ! W32 platform */
140 # if RENAME_DEST_EXISTS_BUG
141 # error Please report your platform and this message to bug-gnulib@gnu.org.
142 # elif RENAME_TRAILING_SLASH_BUG
147 # include "dirname.h"
150 /* Rename the file SRC to DST, removing any trailing
151 slashes from SRC. Needed for SunOS 4.1.1_U1. */
154 rpl_rename (char const *src, char const *dst)
158 size_t s_len = strlen (src);
160 if (s_len && src[s_len - 1] == '/')
162 src_temp = xstrdup (src);
163 strip_trailing_slashes (src_temp);
166 src_temp = (char *) src;
168 ret_val = rename (src_temp, dst);
175 # endif /* RENAME_TRAILING_SLASH_BUG */
176 #endif /* ! W32 platform */