1 | /*************************************************************************** |
2 | * _ _ ____ _ |
3 | * Project ___| | | | _ \| | |
4 | * / __| | | | |_) | | |
5 | * | (__| |_| | _ <| |___ |
6 | * \___|\___/|_| \_\_____| |
7 | * |
8 | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | * |
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. |
13 | * |
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. |
17 | * |
18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | * KIND, either express or implied. |
20 | * |
21 | * SPDX-License-Identifier: curl |
22 | * |
23 | ***************************************************************************/ |
24 | |
25 | #include "rename.h" |
26 | |
27 | #include "curl_setup.h" |
28 | |
29 | #if (!defined(CURL_DISABLE_HTTP) || !defined(CURL_DISABLE_COOKIES)) || \ |
30 | !defined(CURL_DISABLE_ALTSVC) |
31 | |
32 | #include "curl_multibyte.h" |
33 | #include "timeval.h" |
34 | |
35 | /* The last 3 #include files should be in this order */ |
36 | #include "curl_printf.h" |
37 | #include "curl_memory.h" |
38 | #include "memdebug.h" |
39 | |
40 | /* return 0 on success, 1 on error */ |
41 | int Curl_rename(const char *oldpath, const char *newpath) |
42 | { |
43 | #ifdef WIN32 |
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); |
51 | for(;;) { |
52 | timediff_t diff; |
53 | if(MoveFileEx(tchar_oldpath, tchar_newpath, MOVEFILE_REPLACE_EXISTING)) { |
54 | curlx_unicodefree(tchar_oldpath); |
55 | curlx_unicodefree(tchar_newpath); |
56 | break; |
57 | } |
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); |
62 | return 1; |
63 | } |
64 | Sleep(1); |
65 | } |
66 | #else |
67 | if(rename(old: oldpath, new: newpath)) |
68 | return 1; |
69 | #endif |
70 | return 0; |
71 | } |
72 | |
73 | #endif |
74 | |