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