Coverage Report

Created: 2025-10-10 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PROJ/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
/* The last 2 #include files should be in this order */
36
#include "curl_memory.h"
37
#include "memdebug.h"
38
39
/* return 0 on success, 1 on error */
40
int Curl_rename(const char *oldpath, const char *newpath)
41
0
{
42
#if defined(_WIN32) && !defined(UNDER_CE)
43
  /* rename() on Windows does not overwrite, so we cannot use it here.
44
     MoveFileEx() will overwrite and is usually atomic, however it fails
45
     when there are open handles to the file. */
46
  const int max_wait_ms = 1000;
47
  struct curltime start = curlx_now();
48
  TCHAR *tchar_oldpath = curlx_convert_UTF8_to_tchar(oldpath);
49
  TCHAR *tchar_newpath = curlx_convert_UTF8_to_tchar(newpath);
50
  for(;;) {
51
    timediff_t diff;
52
    if(MoveFileEx(tchar_oldpath, tchar_newpath, MOVEFILE_REPLACE_EXISTING)) {
53
      curlx_unicodefree(tchar_oldpath);
54
      curlx_unicodefree(tchar_newpath);
55
      break;
56
    }
57
    diff = curlx_timediff(curlx_now(), start);
58
    if(diff < 0 || diff > max_wait_ms) {
59
      curlx_unicodefree(tchar_oldpath);
60
      curlx_unicodefree(tchar_newpath);
61
      return 1;
62
    }
63
    Sleep(1);
64
  }
65
#else
66
0
  if(rename(oldpath, newpath))
67
0
    return 1;
68
0
#endif
69
0
  return 0;
70
0
}
71
72
#endif