rename: modernize replacement
[gnulib.git] / lib / rename.c
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
4    exists.
5
6    Copyright (C) 2001, 2002, 2003, 2005, 2006, 2009 Free Software
7    Foundation, Inc.
8
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.
13
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.
18
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/>.  */
21
22 /* written by Volker Borchert */
23
24 #include <config.h>
25
26 #include <stdio.h>
27
28 #undef rename
29
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
33    existing files.  */
34
35 # include <errno.h>
36
37 # define WIN32_LEAN_AND_MEAN
38 # include <windows.h>
39
40 /* Rename the file SRC to DST.  This replacement is necessary on
41    Windows, on which the system rename function will not replace
42    an existing DST.  */
43 int
44 rpl_rename (char const *src, char const *dst)
45 {
46   int error;
47
48   /* MoveFileEx works if SRC is a directory without any flags,
49      but fails with MOVEFILE_REPLACE_EXISTING, so try without
50      flags first. */
51   if (MoveFileEx (src, dst, 0))
52     return 0;
53
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)
58     {
59       if (MoveFileEx (src, dst, MOVEFILE_REPLACE_EXISTING))
60         return 0;
61
62       error = GetLastError ();
63     }
64
65   switch (error)
66     {
67     case ERROR_FILE_NOT_FOUND:
68     case ERROR_PATH_NOT_FOUND:
69     case ERROR_BAD_PATHNAME:
70     case ERROR_DIRECTORY:
71       errno = ENOENT;
72       break;
73
74     case ERROR_ACCESS_DENIED:
75     case ERROR_SHARING_VIOLATION:
76       errno = EACCES;
77       break;
78
79     case ERROR_OUTOFMEMORY:
80       errno = ENOMEM;
81       break;
82
83     case ERROR_CURRENT_DIRECTORY:
84       errno = EBUSY;
85       break;
86
87     case ERROR_NOT_SAME_DEVICE:
88       errno = EXDEV;
89       break;
90
91     case ERROR_WRITE_PROTECT:
92       errno = EROFS;
93       break;
94
95     case ERROR_WRITE_FAULT:
96     case ERROR_READ_FAULT:
97     case ERROR_GEN_FAILURE:
98       errno = EIO;
99       break;
100
101     case ERROR_HANDLE_DISK_FULL:
102     case ERROR_DISK_FULL:
103     case ERROR_DISK_TOO_FRAGMENTED:
104       errno = ENOSPC;
105       break;
106
107     case ERROR_FILE_EXISTS:
108     case ERROR_ALREADY_EXISTS:
109       errno = EEXIST;
110       break;
111
112     case ERROR_BUFFER_OVERFLOW:
113     case ERROR_FILENAME_EXCED_RANGE:
114       errno = ENAMETOOLONG;
115       break;
116
117     case ERROR_INVALID_NAME:
118     case ERROR_DELETE_PENDING:
119       errno = EPERM;        /* ? */
120       break;
121
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
125 # endif
126     case ERROR_FILE_TOO_LARGE:
127       errno = EFBIG;
128       break;
129
130     default:
131       errno = EINVAL;
132       break;
133     }
134
135   return -1;
136 }
137
138 #else /* ! W32 platform */
139
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
143 #  include <stdio.h>
144 #  include <stdlib.h>
145 #  include <string.h>
146
147 #  include "dirname.h"
148 #  include "xalloc.h"
149
150 /* Rename the file SRC to DST, removing any trailing
151    slashes from SRC.  Needed for SunOS 4.1.1_U1.  */
152
153 int
154 rpl_rename (char const *src, char const *dst)
155 {
156   char *src_temp;
157   int ret_val;
158   size_t s_len = strlen (src);
159
160   if (s_len && src[s_len - 1] == '/')
161     {
162       src_temp = xstrdup (src);
163       strip_trailing_slashes (src_temp);
164     }
165   else
166     src_temp = (char *) src;
167
168   ret_val = rename (src_temp, dst);
169
170   if (src_temp != src)
171     free (src_temp);
172
173   return ret_val;
174 }
175 # endif /* RENAME_TRAILING_SLASH_BUG */
176 #endif /* ! W32 platform */