tool: determine the correct fopen option for -D

This commit fixes a bug in the dump-header feature regarding the
determination of the second fopen(3) option.

Reported-by: u20221022 on github

See #4753
See #4762
Fixes #10074
Closes #10079
This commit is contained in:
Emil Engler 2022-12-11 18:08:17 +01:00 committed by Daniel Stenberg
parent 1a88b6b653
commit 8b1e5df73d
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
4 changed files with 98 additions and 2 deletions

View file

@ -969,7 +969,21 @@ static CURLcode single_transfer(struct GlobalConfig *global,
/* open file for output: */
if(strcmp(config->headerfile, "-")) {
FILE *newfile;
newfile = fopen(config->headerfile, per->prev == NULL?"wb":"ab");
/*
* this checks if the previous transfer had the same
* OperationConfig, which would mean, that the an output file has
* already been created and data can be appened to it, instead
* of overwriting it.
* TODO: Consider placing the file handle inside the
* OperationConfig, so that it does not need to be opened/closed
* for every transfer.
*/
if(per->prev && per->prev->config == config)
newfile = fopen(config->headerfile, "ab+");
else
newfile = fopen(config->headerfile, "wb+");
if(!newfile) {
warnf(global, "Failed to open %s\n", config->headerfile);
result = CURLE_WRITE_ERROR;