Coverage Report

Created: 2026-09-14 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/curl_fopen.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
#include "curl_setup.h"
25
26
#if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_ALTSVC) || \
27
  !defined(CURL_DISABLE_HSTS)
28
29
#include "urldata.h"
30
#include "rand.h"
31
#include "curl_fopen.h"
32
33
/* The dirslash() function breaks a null-terminated pathname string into
34
   directory and filename components then returns the directory component up
35
   to, *AND INCLUDING*, a final '/'. If there is no directory in the path,
36
   this instead returns a "" string.
37
38
   This function returns a pointer to malloc'ed memory.
39
40
   The input path to this function is expected to have a filename part.
41
 */
42
43
#ifdef _WIN32
44
#define PATHSEP   "\\"
45
#define IS_SEP(x) (((x) == '/') || ((x) == '\\'))
46
#elif defined(MSDOS) || defined(OS2)
47
#define PATHSEP   "\\"
48
#define IS_SEP(x) ((x) == '\\')
49
#else
50
0
#define PATHSEP   "/"
51
0
#define IS_SEP(x) ((x) == '/')
52
#endif
53
54
static char *dirslash(const char *path)
55
0
{
56
0
  size_t n;
57
0
  struct dynbuf out;
58
0
  DEBUGASSERT(path);
59
0
  curlx_dyn_init(&out, CURL_MAX_INPUT_LENGTH);
60
0
  n = strlen(path);
61
0
  if(n) {
62
    /* find the rightmost path separator, if any */
63
0
    while(n && !IS_SEP(path[n - 1]))
64
0
      --n;
65
    /* skip over all the path separators, if any */
66
0
    while(n && IS_SEP(path[n - 1]))
67
0
      --n;
68
0
  }
69
0
  if(curlx_dyn_addn(&out, path, n))
70
0
    return NULL;
71
  /* if there was a directory, append a single trailing slash */
72
0
  if(n && curlx_dyn_addn(&out, PATHSEP, 1))
73
0
    return NULL;
74
0
  return curlx_dyn_ptr(&out);
75
0
}
76
77
/*
78
 * Curl_fopen() opens a file for writing with a temp name, to be renamed
79
 * to the final name when completed. If there is an existing file using this
80
 * name at the time of the open, this function will clone the mode from that
81
 * file. if 'tempname' is non-NULL, it needs a rename after the file is
82
 * written.
83
 */
84
CURLcode Curl_fopen(struct Curl_easy *data, const char *filename,
85
                    FILE **fh, char **tempname)
86
63.9k
{
87
63.9k
  CURLcode result = CURLE_WRITE_ERROR;
88
63.9k
  unsigned char randbuf[41];
89
63.9k
  char *tempstore = NULL;
90
63.9k
#ifndef _WIN32
91
63.9k
  curlx_struct_stat sb;
92
63.9k
#endif
93
63.9k
  int fd = -1;
94
63.9k
  char *dir = NULL;
95
63.9k
  *tempname = NULL;
96
97
63.9k
#ifndef _WIN32
98
63.9k
  *fh = curlx_fopen(filename, FOPEN_WRITETEXT);
99
63.9k
  if(!*fh)
100
0
    goto fail;
101
63.9k
  if(curlx_fstat(fileno(*fh), &sb) == -1 || !S_ISREG(sb.st_mode)) {
102
63.9k
    return CURLE_OK;
103
63.9k
  }
104
0
  curlx_fclose(*fh);
105
0
#endif /* !_WIN32 */
106
0
  *fh = NULL;
107
108
0
  result = Curl_rand_alnum(data, randbuf, sizeof(randbuf));
109
0
  if(result)
110
0
    goto fail;
111
112
0
  dir = dirslash(filename);
113
0
  if(dir) {
114
    /* The temp filename should not end up too long for the target file
115
       system */
116
0
    tempstore = curl_maprintf("%s%s.tmp", dir, randbuf);
117
0
    curlx_free(dir);
118
0
  }
119
120
0
  if(!tempstore) {
121
0
    result = CURLE_OUT_OF_MEMORY;
122
0
    goto fail;
123
0
  }
124
125
0
  result = CURLE_WRITE_ERROR;
126
#ifdef _WIN32
127
  fd = curlx_open(tempstore, _O_WRONLY | _O_CREAT | _O_EXCL,
128
                  _S_IREAD | _S_IWRITE);
129
#elif (defined(ANDROID) || defined(__ANDROID__)) && \
130
  (defined(__i386__) || defined(__arm__))
131
  fd = curlx_open(tempstore, O_WRONLY | O_CREAT | O_EXCL,
132
                  (mode_t)(S_IRUSR | S_IWUSR | 0600));
133
#else
134
0
  fd = curlx_open(tempstore, O_WRONLY | O_CREAT | O_EXCL,
135
0
                  S_IRUSR | S_IWUSR | 0600);
136
0
#endif
137
0
  if(fd == -1)
138
0
    goto fail;
139
140
0
#ifdef HAVE_FCHMOD
141
0
  {
142
0
    curlx_struct_stat nsb;
143
0
    if((curlx_fstat(fd, &nsb) != -1) &&
144
0
       (nsb.st_uid == sb.st_uid) && (nsb.st_gid == sb.st_gid)) {
145
      /* if the user and group are the same, clone the original mode */
146
0
      if(fchmod(fd, sb.st_mode) == -1)
147
0
        goto fail;
148
0
    }
149
0
  }
150
0
#endif
151
152
0
  *fh = curlx_fdopen(fd, FOPEN_WRITETEXT);
153
0
  if(!*fh)
154
0
    goto fail;
155
156
0
  *tempname = tempstore;
157
0
  return CURLE_OK;
158
159
0
fail:
160
0
  if(fd != -1) {
161
0
    curlx_close(fd);
162
0
    unlink(tempstore);
163
0
  }
164
165
0
  curlx_free(tempstore);
166
0
  return result;
167
0
}
168
169
#endif /* !disabled */