113498266Sopenharmony_ci/*************************************************************************** 213498266Sopenharmony_ci * _ _ ____ _ 313498266Sopenharmony_ci * Project ___| | | | _ \| | 413498266Sopenharmony_ci * / __| | | | |_) | | 513498266Sopenharmony_ci * | (__| |_| | _ <| |___ 613498266Sopenharmony_ci * \___|\___/|_| \_\_____| 713498266Sopenharmony_ci * 813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1313498266Sopenharmony_ci * 1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1713498266Sopenharmony_ci * 1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 1913498266Sopenharmony_ci * KIND, either express or implied. 2013498266Sopenharmony_ci * 2113498266Sopenharmony_ci * SPDX-License-Identifier: curl 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci ***************************************************************************/ 2413498266Sopenharmony_ci 2513498266Sopenharmony_ci#include "rename.h" 2613498266Sopenharmony_ci 2713498266Sopenharmony_ci#include "curl_setup.h" 2813498266Sopenharmony_ci 2913498266Sopenharmony_ci#if (!defined(CURL_DISABLE_HTTP) || !defined(CURL_DISABLE_COOKIES)) || \ 3013498266Sopenharmony_ci !defined(CURL_DISABLE_ALTSVC) 3113498266Sopenharmony_ci 3213498266Sopenharmony_ci#include "curl_multibyte.h" 3313498266Sopenharmony_ci#include "timeval.h" 3413498266Sopenharmony_ci 3513498266Sopenharmony_ci/* The last 3 #include files should be in this order */ 3613498266Sopenharmony_ci#include "curl_printf.h" 3713498266Sopenharmony_ci#include "curl_memory.h" 3813498266Sopenharmony_ci#include "memdebug.h" 3913498266Sopenharmony_ci 4013498266Sopenharmony_ci/* return 0 on success, 1 on error */ 4113498266Sopenharmony_ciint Curl_rename(const char *oldpath, const char *newpath) 4213498266Sopenharmony_ci{ 4313498266Sopenharmony_ci#ifdef _WIN32 4413498266Sopenharmony_ci /* rename() on Windows doesn't overwrite, so we can't use it here. 4513498266Sopenharmony_ci MoveFileEx() will overwrite and is usually atomic, however it fails 4613498266Sopenharmony_ci when there are open handles to the file. */ 4713498266Sopenharmony_ci const int max_wait_ms = 1000; 4813498266Sopenharmony_ci struct curltime start = Curl_now(); 4913498266Sopenharmony_ci TCHAR *tchar_oldpath = curlx_convert_UTF8_to_tchar((char *)oldpath); 5013498266Sopenharmony_ci TCHAR *tchar_newpath = curlx_convert_UTF8_to_tchar((char *)newpath); 5113498266Sopenharmony_ci for(;;) { 5213498266Sopenharmony_ci timediff_t diff; 5313498266Sopenharmony_ci if(MoveFileEx(tchar_oldpath, tchar_newpath, MOVEFILE_REPLACE_EXISTING)) { 5413498266Sopenharmony_ci curlx_unicodefree(tchar_oldpath); 5513498266Sopenharmony_ci curlx_unicodefree(tchar_newpath); 5613498266Sopenharmony_ci break; 5713498266Sopenharmony_ci } 5813498266Sopenharmony_ci diff = Curl_timediff(Curl_now(), start); 5913498266Sopenharmony_ci if(diff < 0 || diff > max_wait_ms) { 6013498266Sopenharmony_ci curlx_unicodefree(tchar_oldpath); 6113498266Sopenharmony_ci curlx_unicodefree(tchar_newpath); 6213498266Sopenharmony_ci return 1; 6313498266Sopenharmony_ci } 6413498266Sopenharmony_ci Sleep(1); 6513498266Sopenharmony_ci } 6613498266Sopenharmony_ci#else 6713498266Sopenharmony_ci if(rename(oldpath, newpath)) 6813498266Sopenharmony_ci return 1; 6913498266Sopenharmony_ci#endif 7013498266Sopenharmony_ci return 0; 7113498266Sopenharmony_ci} 7213498266Sopenharmony_ci 7313498266Sopenharmony_ci#endif 74