Debian Codegraph @

deps/curl/lib/rename.c

raw
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 2020, Daniel Stenberg, <[email protected]>, 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.haxx.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 ***************************************************************************/
22
23 #include "rename.h"
24
25 #include "curl_setup.h"
26
27 #if (!defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)) || \
28 defined(USE_ALTSVC)
29
30 #include "timeval.h"
31
32 /* The last 3 #include files should be in this order */
33 #include "curl_printf.h"
34 #include "curl_memory.h"
35 #include "memdebug.h"
36
37 /* return 0 on success, 1 on error */
38 int Curl_rename(const char *oldpath, const char *newpath)
39 {
40 #ifdef WIN32
41 /* rename() on Windows doesn't overwrite, so we can't use it here.
42 MoveFileExA() will overwrite and is usually atomic, however it fails
43 when there are open handles to the file. */
44 const int max_wait_ms = 1000;
45 struct curltime start = Curl_now();
46 for(;;) {
47 timediff_t diff;
48 if(MoveFileExA(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
49 break;
50 diff = Curl_timediff(Curl_now(), start);
51 if(diff < 0 || diff > max_wait_ms)
52 return 1;
53 Sleep(1);
54 }
55 #else
56 if(rename(oldpath, newpath))
57 return 1;
58 #endif
59 return 0;
60 }
61
62 #endif