Coverage Report

Created: 2024-02-25 06:14

/src/PROJ/curl/lib/fopen.c
Line
Count
Source (jump to first uncovered line)
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 "curl_setup.h"
26
27
#if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_ALTSVC) ||  \
28
  !defined(CURL_DISABLE_HSTS)
29
30
#ifdef HAVE_FCNTL_H
31
#include <fcntl.h>
32
#endif
33
34
#include "urldata.h"
35
#include "rand.h"
36
#include "fopen.h"
37
/* The last 3 #include files should be in this order */
38
#include "curl_printf.h"
39
#include "curl_memory.h"
40
#include "memdebug.h"
41
42
/*
43
  The dirslash() function breaks a null-terminated pathname string into
44
  directory and filename components then returns the directory component up
45
  to, *AND INCLUDING*, a final '/'.  If there is no directory in the path,
46
  this instead returns a "" string.
47
48
  This function returns a pointer to malloc'ed memory.
49
50
  The input path to this function is expected to have a file name part.
51
*/
52
53
#ifdef _WIN32
54
#define PATHSEP "\\"
55
#define IS_SEP(x) (((x) == '/') || ((x) == '\\'))
56
#elif defined(MSDOS) || defined(__EMX__) || defined(OS2)
57
#define PATHSEP "\\"
58
#define IS_SEP(x) ((x) == '\\')
59
#else
60
0
#define PATHSEP "/"
61
0
#define IS_SEP(x) ((x) == '/')
62
#endif
63
64
static char *dirslash(const char *path)
65
0
{
66
0
  size_t n;
67
0
  struct dynbuf out;
68
0
  DEBUGASSERT(path);
69
0
  Curl_dyn_init(&out, CURL_MAX_INPUT_LENGTH);
70
0
  n = strlen(path);
71
0
  if(n) {
72
    /* find the rightmost path separator, if any */
73
0
    while(n && !IS_SEP(path[n-1]))
74
0
      --n;
75
    /* skip over all the path separators, if any */
76
0
    while(n && IS_SEP(path[n-1]))
77
0
      --n;
78
0
  }
79
0
  if(Curl_dyn_addn(&out, path, n))
80
0
    return NULL;
81
  /* if there was a directory, append a single trailing slash */
82
0
  if(n && Curl_dyn_addn(&out, PATHSEP, 1))
83
0
    return NULL;
84
0
  return Curl_dyn_ptr(&out);
85
0
}
86
87
/*
88
 * Curl_fopen() opens a file for writing with a temp name, to be renamed
89
 * to the final name when completed. If there is an existing file using this
90
 * name at the time of the open, this function will clone the mode from that
91
 * file.  if 'tempname' is non-NULL, it needs a rename after the file is
92
 * written.
93
 */
94
CURLcode Curl_fopen(struct Curl_easy *data, const char *filename,
95
                    FILE **fh, char **tempname)
96
0
{
97
0
  CURLcode result = CURLE_WRITE_ERROR;
98
0
  unsigned char randbuf[41];
99
0
  char *tempstore = NULL;
100
0
  struct_stat sb;
101
0
  int fd = -1;
102
0
  char *dir = NULL;
103
0
  *tempname = NULL;
104
105
0
  *fh = fopen(filename, FOPEN_WRITETEXT);
106
0
  if(!*fh)
107
0
    goto fail;
108
0
  if(fstat(fileno(*fh), &sb) == -1 || !S_ISREG(sb.st_mode)) {
109
0
    return CURLE_OK;
110
0
  }
111
0
  fclose(*fh);
112
0
  *fh = NULL;
113
114
0
  result = Curl_rand_alnum(data, randbuf, sizeof(randbuf));
115
0
  if(result)
116
0
    goto fail;
117
118
0
  dir = dirslash(filename);
119
0
  if(dir) {
120
    /* The temp file name should not end up too long for the target file
121
       system */
122
0
    tempstore = aprintf("%s%s.tmp", dir, randbuf);
123
0
    free(dir);
124
0
  }
125
126
0
  if(!tempstore) {
127
0
    result = CURLE_OUT_OF_MEMORY;
128
0
    goto fail;
129
0
  }
130
131
0
  result = CURLE_WRITE_ERROR;
132
0
  fd = open(tempstore, O_WRONLY | O_CREAT | O_EXCL, 0600|sb.st_mode);
133
0
  if(fd == -1)
134
0
    goto fail;
135
136
0
  *fh = fdopen(fd, FOPEN_WRITETEXT);
137
0
  if(!*fh)
138
0
    goto fail;
139
140
0
  *tempname = tempstore;
141
0
  return CURLE_OK;
142
143
0
fail:
144
0
  if(fd != -1) {
145
0
    close(fd);
146
0
    unlink(tempstore);
147
0
  }
148
149
0
  free(tempstore);
150
0
  return result;
151
0
}
152
153
#endif /* ! disabled */