]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
tool_operate: fix headerfile writing
authorStefan Eissing <stefan@eissing.org>
Wed, 4 Jan 2023 13:37:52 +0000 (14:37 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Wed, 4 Jan 2023 22:11:54 +0000 (23:11 +0100)
Do not rely on the first transfer started to be the first to get a
response (remember -Z). All transfers now write the headefile (-D) in
append mode, making sure that the order of transfer responses does not
lead to overwrites of previous data.

Closes #10224

src/tool_operate.c

index 97ed5dbf07818efb9d544c988b21f1501e6f5fc6..6b139a3ccee68422ae7b42a4ccfaacb60d4bb510 100644 (file)
@@ -971,18 +971,19 @@ static CURLcode single_transfer(struct GlobalConfig *global,
             FILE *newfile;
 
             /*
-             * this checks if the previous transfer had the same
-             * OperationConfig, which would mean, that an output file has
-             * already been created and data can be appended to it, instead
-             * of overwriting it.
+             * Since every transfer has its own file handle for dumping
+             * the headers, we need to open it in append mode, since transfers
+             * might finish in any order.
+             * The first transfer just clears the file.
              * 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
+            if(!per->prev || per->prev->config != config) {
               newfile = fopen(config->headerfile, "wb+");
+              fclose(newfile);
+            }
+            newfile = fopen(config->headerfile, "ab+");
 
             if(!newfile) {
               warnf(global, "Failed to open %s\n", config->headerfile);