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 Foundation, Inc.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* written by Volker Borchert */
26 #if RENAME_DEST_EXISTS_BUG
27 /* This replacement must come first, otherwise when cross
28 * compiling to Windows we will guess that it has the trailing
29 * slash bug and entirely miss this one. */
32 #define WIN32_LEAN_AND_MEAN
35 /* Rename the file SRC to DST. This replacement is necessary on
36 Windows, on which the system rename function will not replace
39 rpl_rename (char const *src, char const *dst)
43 /* MoveFileEx works if SRC is a directory without any flags,
44 but fails with MOVEFILE_REPLACE_EXISTING, so try without
46 if (MoveFileEx (src, dst, 0))
49 /* Retry with MOVEFILE_REPLACE_EXISTING if the move failed
50 * due to the destination already existing. */
51 error = GetLastError ();
52 if (error == ERROR_FILE_EXISTS || error == ERROR_ALREADY_EXISTS)
54 if (MoveFileEx (src, dst, MOVEFILE_REPLACE_EXISTING))
57 error = GetLastError ();
62 case ERROR_FILE_NOT_FOUND:
63 case ERROR_PATH_NOT_FOUND:
64 case ERROR_BAD_PATHNAME:
69 case ERROR_ACCESS_DENIED:
70 case ERROR_SHARING_VIOLATION:
74 case ERROR_OUTOFMEMORY:
78 case ERROR_CURRENT_DIRECTORY:
82 case ERROR_NOT_SAME_DEVICE:
86 case ERROR_WRITE_PROTECT:
90 case ERROR_WRITE_FAULT:
91 case ERROR_READ_FAULT:
92 case ERROR_GEN_FAILURE:
96 case ERROR_HANDLE_DISK_FULL:
98 case ERROR_DISK_TOO_FRAGMENTED:
102 case ERROR_FILE_EXISTS:
103 case ERROR_ALREADY_EXISTS:
107 case ERROR_BUFFER_OVERFLOW:
108 case ERROR_FILENAME_EXCED_RANGE:
109 errno = ENAMETOOLONG;
112 case ERROR_INVALID_NAME:
113 case ERROR_DELETE_PENDING:
114 errno = EPERM; /* ? */
117 #ifndef ERROR_FILE_TOO_LARGE
118 /* This value is documented but not defined in all versions of windows.h. */
119 #define ERROR_FILE_TOO_LARGE 223
121 case ERROR_FILE_TOO_LARGE:
132 #elif RENAME_TRAILING_SLASH_BUG
140 /* Rename the file SRC to DST, removing any trailing
141 slashes from SRC. Needed for SunOS 4.1.1_U1. */
144 rpl_rename (char const *src, char const *dst)
148 size_t s_len = strlen (src);
150 if (s_len && src[s_len - 1] == '/')
152 src_temp = xstrdup (src);
153 strip_trailing_slashes (src_temp);
156 src_temp = (char *) src;
158 ret_val = rename (src_temp, dst);
165 #endif /* RENAME_TRAILING_SLASH_BUG */