Coverage Report

Created: 2025-12-04 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/rename.c
Line
Count
Source
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 "curlx/multibyte.h"
33
#include "curlx/timeval.h"
34
35
/* return 0 on success, 1 on error */
36
int Curl_rename(const char *oldpath, const char *newpath)
37
0
{
38
#ifdef _WIN32
39
  /* rename() on Windows does not overwrite, so we cannot use it here.
40
     MoveFileEx() will overwrite and is usually atomic, however it fails
41
     when there are open handles to the file. */
42
  const int max_wait_ms = 1000;
43
  struct curltime start = curlx_now();
44
  TCHAR *tchar_oldpath = curlx_convert_UTF8_to_tchar(oldpath);
45
  TCHAR *tchar_newpath = curlx_convert_UTF8_to_tchar(newpath);
46
  for(;;) {
47
    timediff_t diff;
48
    if(MoveFileEx(tchar_oldpath, tchar_newpath, MOVEFILE_REPLACE_EXISTING)) {
49
      curlx_unicodefree(tchar_oldpath);
50
      curlx_unicodefree(tchar_newpath);
51
      break;
52
    }
53
    diff = curlx_timediff_ms(curlx_now(), start);
54
    if(diff < 0 || diff > max_wait_ms) {
55
      curlx_unicodefree(tchar_oldpath);
56
      curlx_unicodefree(tchar_newpath);
57
      return 1;
58
    }
59
    Sleep(1);
60
  }
61
#else
62
0
  if(rename(oldpath, newpath))
63
0
    return 1;
64
0
#endif
65
0
  return 0;
66
0
}
67
68
#endif