1 /* Work around rename bugs in some systems.
3 Copyright (C) 2001-2003, 2005-2006, 2009-2018 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Volker Borchert, Eric Blake. */
26 #if defined _WIN32 && ! defined __CYGWIN__
27 /* The mingw rename has problems with trailing slashes; it also
28 requires use of native Windows calls to allow atomic renames over
34 # include <sys/stat.h>
37 # define WIN32_LEAN_AND_MEAN
42 /* Rename the file SRC to DST. This replacement is necessary on
43 Windows, on which the system rename function will not replace
46 rpl_rename (char const *src
, char const *dst
)
49 size_t src_len
= strlen (src
);
50 size_t dst_len
= strlen (dst
);
51 char *src_base
= last_component (src
);
52 char *dst_base
= last_component (dst
);
59 /* Filter out dot as last component. */
60 if (!src_len
|| !dst_len
)
67 size_t len
= base_len (src_base
);
68 if (len
== 1 || (len
== 2 && src_base
[1] == '.'))
76 size_t len
= base_len (dst_base
);
77 if (len
== 1 || (len
== 2 && dst_base
[1] == '.'))
84 /* Presence of a trailing slash requires directory semantics. If
85 the source does not exist, or if the destination cannot be turned
86 into a directory, give up now. Otherwise, strip trailing slashes
87 before calling rename. There are no symlinks on mingw, so stat
88 works instead of lstat. */
89 src_slash
= ISSLASH (src
[src_len
- 1]);
90 dst_slash
= ISSLASH (dst
[dst_len
- 1]);
91 if (stat (src
, &src_st
))
93 if (stat (dst
, &dst_st
))
95 if (errno
!= ENOENT
|| (!S_ISDIR (src_st
.st_mode
) && dst_slash
))
101 if (S_ISDIR (dst_st
.st_mode
) != S_ISDIR (src_st
.st_mode
))
103 errno
= S_ISDIR (dst_st
.st_mode
) ? EISDIR
: ENOTDIR
;
109 /* There are no symlinks, so if a file existed with a trailing
110 slash, it must be a directory, and we don't have to worry about
111 stripping strip trailing slash. However, mingw refuses to
112 replace an existing empty directory, so we have to help it out.
113 And canonicalize_file_name is not yet ported to mingw; however,
114 for directories, getcwd works as a viable alternative. Ensure
115 that we can get back to where we started before using it; later
116 attempts to return are fatal. Note that we can end up losing a
117 directory if rename then fails, but it was empty, so not much
119 if (dst_exists
&& S_ISDIR (dst_st
.st_mode
))
121 char *cwd
= getcwd (NULL
, 0);
124 if (!cwd
|| chdir (cwd
))
126 if (IS_ABSOLUTE_FILE_NAME (src
))
128 dst_temp
= chdir (dst
) ? NULL
: getcwd (NULL
, 0);
129 src_temp
= chdir (src
) ? NULL
: getcwd (NULL
, 0);
133 src_temp
= chdir (src
) ? NULL
: getcwd (NULL
, 0);
134 if (!IS_ABSOLUTE_FILE_NAME (dst
) && chdir (cwd
))
136 dst_temp
= chdir (dst
) ? NULL
: getcwd (NULL
, 0);
141 if (!src_temp
|| !dst_temp
)
148 src_len
= strlen (src_temp
);
149 if (strncmp (src_temp
, dst_temp
, src_len
) == 0
150 && (ISSLASH (dst_temp
[src_len
]) || dst_temp
[src_len
] == '\0'))
152 error
= dst_temp
[src_len
];
174 /* MoveFileEx works if SRC is a directory without any flags, but
175 fails with MOVEFILE_REPLACE_EXISTING, so try without flags first.
176 Thankfully, MoveFileEx handles hard links correctly, even though
177 rename() does not. */
178 if (MoveFileEx (src
, dst
, 0))
181 /* Retry with MOVEFILE_REPLACE_EXISTING if the move failed
182 due to the destination already existing. */
183 error
= GetLastError ();
184 if (error
== ERROR_FILE_EXISTS
|| error
== ERROR_ALREADY_EXISTS
)
186 if (MoveFileEx (src
, dst
, MOVEFILE_REPLACE_EXISTING
))
189 error
= GetLastError ();
194 case ERROR_FILE_NOT_FOUND
:
195 case ERROR_PATH_NOT_FOUND
:
196 case ERROR_BAD_PATHNAME
:
197 case ERROR_DIRECTORY
:
201 case ERROR_ACCESS_DENIED
:
202 case ERROR_SHARING_VIOLATION
:
206 case ERROR_OUTOFMEMORY
:
210 case ERROR_CURRENT_DIRECTORY
:
214 case ERROR_NOT_SAME_DEVICE
:
218 case ERROR_WRITE_PROTECT
:
222 case ERROR_WRITE_FAULT
:
223 case ERROR_READ_FAULT
:
224 case ERROR_GEN_FAILURE
:
228 case ERROR_HANDLE_DISK_FULL
:
229 case ERROR_DISK_FULL
:
230 case ERROR_DISK_TOO_FRAGMENTED
:
234 case ERROR_FILE_EXISTS
:
235 case ERROR_ALREADY_EXISTS
:
239 case ERROR_BUFFER_OVERFLOW
:
240 case ERROR_FILENAME_EXCED_RANGE
:
241 errno
= ENAMETOOLONG
;
244 case ERROR_INVALID_NAME
:
245 case ERROR_DELETE_PENDING
:
246 errno
= EPERM
; /* ? */
249 # ifndef ERROR_FILE_TOO_LARGE
250 /* This value is documented but not defined in all versions of windows.h. */
251 # define ERROR_FILE_TOO_LARGE 223
253 case ERROR_FILE_TOO_LARGE
:
265 #else /* ! W32 platform */
271 # include <sys/stat.h>
274 # include "dirname.h"
275 # include "same-inode.h"
277 /* Rename the file SRC to DST, fixing any trailing slash bugs. */
280 rpl_rename (char const *src
, char const *dst
)
282 size_t src_len
= strlen (src
);
283 size_t dst_len
= strlen (dst
);
284 char *src_temp
= (char *) src
;
285 char *dst_temp
= (char *) dst
;
288 bool dst_exists _GL_UNUSED
;
290 int rename_errno
= ENOTDIR
;
294 if (!src_len
|| !dst_len
)
295 return rename (src
, dst
); /* Let strace see the ENOENT failure. */
297 # if RENAME_DEST_EXISTS_BUG
299 char *src_base
= last_component (src
);
300 char *dst_base
= last_component (dst
);
301 if (*src_base
== '.')
303 size_t len
= base_len (src_base
);
304 if (len
== 1 || (len
== 2 && src_base
[1] == '.'))
310 if (*dst_base
== '.')
312 size_t len
= base_len (dst_base
);
313 if (len
== 1 || (len
== 2 && dst_base
[1] == '.'))
320 # endif /* RENAME_DEST_EXISTS_BUG */
322 src_slash
= src
[src_len
- 1] == '/';
323 dst_slash
= dst
[dst_len
- 1] == '/';
325 # if !RENAME_HARD_LINK_BUG && !RENAME_DEST_EXISTS_BUG
326 /* If there are no trailing slashes, then trust the native
327 implementation unless we also suspect issues with hard link
328 detection or file/directory conflicts. */
329 if (!src_slash
&& !dst_slash
)
330 return rename (src
, dst
);
331 # endif /* !RENAME_HARD_LINK_BUG && !RENAME_DEST_EXISTS_BUG */
333 /* Presence of a trailing slash requires directory semantics. If
334 the source does not exist, or if the destination cannot be turned
335 into a directory, give up now. Otherwise, strip trailing slashes
336 before calling rename. */
337 if (lstat (src
, &src_st
))
339 if (lstat (dst
, &dst_st
))
341 if (errno
!= ENOENT
|| (!S_ISDIR (src_st
.st_mode
) && dst_slash
))
347 if (S_ISDIR (dst_st
.st_mode
) != S_ISDIR (src_st
.st_mode
))
349 errno
= S_ISDIR (dst_st
.st_mode
) ? EISDIR
: ENOTDIR
;
352 # if RENAME_HARD_LINK_BUG
353 if (SAME_INODE (src_st
, dst_st
))
355 # endif /* RENAME_HARD_LINK_BUG */
359 # if (RENAME_TRAILING_SLASH_SOURCE_BUG || RENAME_DEST_EXISTS_BUG \
360 || RENAME_HARD_LINK_BUG)
361 /* If the only bug was that a trailing slash was allowed on a
362 non-existing file destination, as in Solaris 10, then we've
363 already covered that situation. But if there is any problem with
364 a trailing slash on an existing source or destination, as in
365 Solaris 9, or if a directory can overwrite a symlink, as on
366 Cygwin 1.5, or if directories cannot be created with trailing
367 slash, as on NetBSD 1.6, then we must strip the offending slash
368 and check that we have not encountered a symlink instead of a
371 Stripping a trailing slash interferes with POSIX semantics, where
372 rename behavior on a symlink with a trailing slash operates on
373 the corresponding target directory. We prefer the GNU semantics
374 of rejecting any use of a symlink with trailing slash, but do not
375 enforce them, since Solaris 10 is able to obey POSIX semantics
376 and there might be clients expecting it, as counter-intuitive as
379 Technically, we could also follow the POSIX behavior by chasing a
380 readlink trail, but that is harder to implement. */
383 src_temp
= strdup (src
);
386 /* Rather than rely on strdup-posix, we set errno ourselves. */
387 rename_errno
= ENOMEM
;
390 strip_trailing_slashes (src_temp
);
391 if (lstat (src_temp
, &src_st
))
393 rename_errno
= errno
;
396 if (S_ISLNK (src_st
.st_mode
))
401 dst_temp
= strdup (dst
);
404 rename_errno
= ENOMEM
;
407 strip_trailing_slashes (dst_temp
);
408 if (lstat (dst_temp
, &dst_st
))
412 rename_errno
= errno
;
416 else if (S_ISLNK (dst_st
.st_mode
))
419 # endif /* RENAME_TRAILING_SLASH_SOURCE_BUG || RENAME_DEST_EXISTS_BUG
420 || RENAME_HARD_LINK_BUG */
422 # if RENAME_DEST_EXISTS_BUG
423 /* Cygwin 1.5 sometimes behaves oddly when moving a non-empty
424 directory on top of an empty one (the old directory name can
425 reappear if the new directory tree is removed). Work around this
426 by removing the target first, but don't remove the target if it
427 is a subdirectory of the source. Note that we can end up losing
428 a directory if rename then fails, but it was empty, so not much
430 if (dst_exists
&& S_ISDIR (dst_st
.st_mode
))
432 if (src_st
.st_dev
!= dst_st
.st_dev
)
434 rename_errno
= EXDEV
;
439 src_temp
= canonicalize_file_name (src
);
442 dst_temp
= canonicalize_file_name (dst
);
443 if (!src_temp
|| !dst_temp
)
445 rename_errno
= ENOMEM
;
448 src_len
= strlen (src_temp
);
449 if (strncmp (src_temp
, dst_temp
, src_len
) == 0
450 && dst_temp
[src_len
] == '/')
452 rename_errno
= EINVAL
;
457 rename_errno
= errno
;
461 # endif /* RENAME_DEST_EXISTS_BUG */
463 ret_val
= rename (src_temp
, dst_temp
);
464 rename_errno
= errno
;
466 out
: _GL_UNUSED_LABEL
;
472 errno
= rename_errno
;
475 #endif /* ! W32 platform */