1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2020 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * SPDX-License-Identifier: curl
23 ***************************************************************************/
27 #include "curl_setup.h"
29 #if (!defined(CURL_DISABLE_HTTP) || !defined(CURL_DISABLE_COOKIES)) || \
30 !defined(CURL_DISABLE_ALTSVC)
32 #include "curl_multibyte.h"
35 /* The last 3 #include files should be in this order */
36 #include "curl_printf.h"
37 #include "curl_memory.h"
40 /* return 0 on success, 1 on error */
41 int Curl_rename(const char *oldpath, const char *newpath)
44 /* rename() on Windows doesn't overwrite, so we can't use it here.
45 MoveFileEx() will overwrite and is usually atomic, however it fails
46 when there are open handles to the file. */
47 const int max_wait_ms = 1000;
48 struct curltime start = Curl_now();
49 TCHAR *tchar_oldpath = curlx_convert_UTF8_to_tchar((char *)oldpath);
50 TCHAR *tchar_newpath = curlx_convert_UTF8_to_tchar((char *)newpath);
53 if(MoveFileEx(tchar_oldpath, tchar_newpath, MOVEFILE_REPLACE_EXISTING)) {
54 curlx_unicodefree(tchar_oldpath);
55 curlx_unicodefree(tchar_newpath);
58 diff = Curl_timediff(Curl_now(), start);
59 if(diff < 0 || diff > max_wait_ms) {
60 curlx_unicodefree(tchar_oldpath);
61 curlx_unicodefree(tchar_newpath);
67 if(rename(oldpath, newpath))