timevar: import from Bison.
[gnulib.git] / lib / rename.c
blob0b457aa3135228de149ade732cea2fa36578908b
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. */
20 #include <config.h>
22 #include <stdio.h>
24 #undef rename
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
29 existing files. */
31 # include <errno.h>
32 # include <stdbool.h>
33 # include <stdlib.h>
34 # include <sys/stat.h>
35 # include <unistd.h>
37 # define WIN32_LEAN_AND_MEAN
38 # include <windows.h>
40 # include "dirname.h"
42 /* Rename the file SRC to DST. This replacement is necessary on
43 Windows, on which the system rename function will not replace
44 an existing DST. */
45 int
46 rpl_rename (char const *src, char const *dst)
48 int error;
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);
53 bool src_slash;
54 bool dst_slash;
55 bool dst_exists;
56 struct stat src_st;
57 struct stat dst_st;
59 /* Filter out dot as last component. */
60 if (!src_len || !dst_len)
62 errno = ENOENT;
63 return -1;
65 if (*src_base == '.')
67 size_t len = base_len (src_base);
68 if (len == 1 || (len == 2 && src_base[1] == '.'))
70 errno = EINVAL;
71 return -1;
74 if (*dst_base == '.')
76 size_t len = base_len (dst_base);
77 if (len == 1 || (len == 2 && dst_base[1] == '.'))
79 errno = EINVAL;
80 return -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))
92 return -1;
93 if (stat (dst, &dst_st))
95 if (errno != ENOENT || (!S_ISDIR (src_st.st_mode) && dst_slash))
96 return -1;
97 dst_exists = false;
99 else
101 if (S_ISDIR (dst_st.st_mode) != S_ISDIR (src_st.st_mode))
103 errno = S_ISDIR (dst_st.st_mode) ? EISDIR : ENOTDIR;
104 return -1;
106 dst_exists = true;
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
118 damage was done. */
119 if (dst_exists && S_ISDIR (dst_st.st_mode))
121 char *cwd = getcwd (NULL, 0);
122 char *src_temp;
123 char *dst_temp;
124 if (!cwd || chdir (cwd))
125 return -1;
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);
131 else
133 src_temp = chdir (src) ? NULL : getcwd (NULL, 0);
134 if (!IS_ABSOLUTE_FILE_NAME (dst) && chdir (cwd))
135 abort ();
136 dst_temp = chdir (dst) ? NULL : getcwd (NULL, 0);
138 if (chdir (cwd))
139 abort ();
140 free (cwd);
141 if (!src_temp || !dst_temp)
143 free (src_temp);
144 free (dst_temp);
145 errno = ENOMEM;
146 return -1;
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];
153 free (src_temp);
154 free (dst_temp);
155 if (error)
157 errno = EINVAL;
158 return -1;
160 return 0;
162 if (rmdir (dst))
164 error = errno;
165 free (src_temp);
166 free (dst_temp);
167 errno = error;
168 return -1;
170 free (src_temp);
171 free (dst_temp);
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))
179 return 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))
187 return 0;
189 error = GetLastError ();
192 switch (error)
194 case ERROR_FILE_NOT_FOUND:
195 case ERROR_PATH_NOT_FOUND:
196 case ERROR_BAD_PATHNAME:
197 case ERROR_DIRECTORY:
198 errno = ENOENT;
199 break;
201 case ERROR_ACCESS_DENIED:
202 case ERROR_SHARING_VIOLATION:
203 errno = EACCES;
204 break;
206 case ERROR_OUTOFMEMORY:
207 errno = ENOMEM;
208 break;
210 case ERROR_CURRENT_DIRECTORY:
211 errno = EBUSY;
212 break;
214 case ERROR_NOT_SAME_DEVICE:
215 errno = EXDEV;
216 break;
218 case ERROR_WRITE_PROTECT:
219 errno = EROFS;
220 break;
222 case ERROR_WRITE_FAULT:
223 case ERROR_READ_FAULT:
224 case ERROR_GEN_FAILURE:
225 errno = EIO;
226 break;
228 case ERROR_HANDLE_DISK_FULL:
229 case ERROR_DISK_FULL:
230 case ERROR_DISK_TOO_FRAGMENTED:
231 errno = ENOSPC;
232 break;
234 case ERROR_FILE_EXISTS:
235 case ERROR_ALREADY_EXISTS:
236 errno = EEXIST;
237 break;
239 case ERROR_BUFFER_OVERFLOW:
240 case ERROR_FILENAME_EXCED_RANGE:
241 errno = ENAMETOOLONG;
242 break;
244 case ERROR_INVALID_NAME:
245 case ERROR_DELETE_PENDING:
246 errno = EPERM; /* ? */
247 break;
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
252 # endif
253 case ERROR_FILE_TOO_LARGE:
254 errno = EFBIG;
255 break;
257 default:
258 errno = EINVAL;
259 break;
262 return -1;
265 #else /* ! W32 platform */
267 # include <errno.h>
268 # include <stdio.h>
269 # include <stdlib.h>
270 # include <string.h>
271 # include <sys/stat.h>
272 # include <unistd.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;
286 bool src_slash;
287 bool dst_slash;
288 bool dst_exists _GL_UNUSED;
289 int ret_val = -1;
290 int rename_errno = ENOTDIR;
291 struct stat src_st;
292 struct stat dst_st;
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] == '.'))
306 errno = EINVAL;
307 return -1;
310 if (*dst_base == '.')
312 size_t len = base_len (dst_base);
313 if (len == 1 || (len == 2 && dst_base[1] == '.'))
315 errno = EINVAL;
316 return -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))
338 return -1;
339 if (lstat (dst, &dst_st))
341 if (errno != ENOENT || (!S_ISDIR (src_st.st_mode) && dst_slash))
342 return -1;
343 dst_exists = false;
345 else
347 if (S_ISDIR (dst_st.st_mode) != S_ISDIR (src_st.st_mode))
349 errno = S_ISDIR (dst_st.st_mode) ? EISDIR : ENOTDIR;
350 return -1;
352 # if RENAME_HARD_LINK_BUG
353 if (SAME_INODE (src_st, dst_st))
354 return 0;
355 # endif /* RENAME_HARD_LINK_BUG */
356 dst_exists = true;
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
369 directory.
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
377 those semantics are.
379 Technically, we could also follow the POSIX behavior by chasing a
380 readlink trail, but that is harder to implement. */
381 if (src_slash)
383 src_temp = strdup (src);
384 if (!src_temp)
386 /* Rather than rely on strdup-posix, we set errno ourselves. */
387 rename_errno = ENOMEM;
388 goto out;
390 strip_trailing_slashes (src_temp);
391 if (lstat (src_temp, &src_st))
393 rename_errno = errno;
394 goto out;
396 if (S_ISLNK (src_st.st_mode))
397 goto out;
399 if (dst_slash)
401 dst_temp = strdup (dst);
402 if (!dst_temp)
404 rename_errno = ENOMEM;
405 goto out;
407 strip_trailing_slashes (dst_temp);
408 if (lstat (dst_temp, &dst_st))
410 if (errno != ENOENT)
412 rename_errno = errno;
413 goto out;
416 else if (S_ISLNK (dst_st.st_mode))
417 goto out;
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
429 damage was done. */
430 if (dst_exists && S_ISDIR (dst_st.st_mode))
432 if (src_st.st_dev != dst_st.st_dev)
434 rename_errno = EXDEV;
435 goto out;
437 if (src_temp != src)
438 free (src_temp);
439 src_temp = canonicalize_file_name (src);
440 if (dst_temp != dst)
441 free (dst_temp);
442 dst_temp = canonicalize_file_name (dst);
443 if (!src_temp || !dst_temp)
445 rename_errno = ENOMEM;
446 goto out;
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;
453 goto out;
455 if (rmdir (dst))
457 rename_errno = errno;
458 goto out;
461 # endif /* RENAME_DEST_EXISTS_BUG */
463 ret_val = rename (src_temp, dst_temp);
464 rename_errno = errno;
466 out: _GL_UNUSED_LABEL;
468 if (src_temp != src)
469 free (src_temp);
470 if (dst_temp != dst)
471 free (dst_temp);
472 errno = rename_errno;
473 return ret_val;
475 #endif /* ! W32 platform */