1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * SPDX-License-Identifier: curl
23 ***************************************************************************/
25 #include "curl_setup.h"
27 #if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_ALTSVC) || \
28 !defined(CURL_DISABLE_HSTS)
37 /* The last 3 #include files should be in this order */
38 #include "curl_printf.h"
39 #include "curl_memory.h"
43 * Curl_fopen() opens a file for writing with a temp name, to be renamed
44 * to the final name when completed. If there is an existing file using this
45 * name at the time of the open, this function will clone the mode from that
46 * file. if 'tempname' is non-NULL, it needs a rename after the file is
49 CURLcode Curl_fopen(struct Curl_easy *data, const char *filename,
50 FILE **fh, char **tempname)
52 CURLcode result = CURLE_WRITE_ERROR;
53 unsigned char randsuffix[9];
54 char *tempstore = NULL;
59 if(stat(filename, &sb) == -1 || !S_ISREG(sb.st_mode)) {
60 /* a non-regular file, fallback to direct fopen() */
61 *fh = fopen(filename, FOPEN_WRITETEXT);
67 result = Curl_rand_hex(data, randsuffix, sizeof(randsuffix));
71 tempstore = aprintf("%s.%s.tmp", filename, randsuffix);
73 result = CURLE_OUT_OF_MEMORY;
77 result = CURLE_WRITE_ERROR;
78 fd = open(tempstore, O_WRONLY | O_CREAT | O_EXCL, 0600);
85 if((fstat(fd, &nsb) != -1) &&
86 (nsb.st_uid == sb.st_uid) && (nsb.st_gid == sb.st_gid)) {
87 /* if the user and group are the same, clone the original mode */
88 if(fchmod(fd, sb.st_mode) == -1)
94 *fh = fdopen(fd, FOPEN_WRITETEXT);
98 *tempname = tempstore;
113 #endif /* ! disabled */