tool_operate: don't truncate the etag save file by default

This fixes a regression of 75d79a4486. The
code in tool-operate truncated the etag save file, under the assumption
that the file would be written with a new etag value. However since
75d79a4486 that might not be the case
anymore and could result in the file being truncated when --etag-compare
and --etag-save was used and that the etag value matched with what the
server responded. Instead the truncation should not be done when a new
etag value should be written.

Test 3204 was added to verify that the file with the etag value doesn't
change the contents when used by --etag-compare and --etage-save and
that value matches with what the server returns on a non 2xx response.

Closes #13432
This commit is contained in:
Gusted 2024-04-20 18:44:42 +02:00 committed by Daniel Stenberg
parent f8011ffa1e
commit 00bef95946
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
4 changed files with 70 additions and 2 deletions

View file

@ -24,6 +24,9 @@
#include "tool_setup.h"
#include "strcase.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#define ENABLE_CURLX_PRINTF
/* use our own printf() functions */
@ -130,6 +133,19 @@ size_t tool_header_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
if(eot >= etag_h) {
size_t etag_length = eot - etag_h + 1;
/*
* Truncate the etag save stream, it can have an existing etag value.
*/
#ifdef HAVE_FTRUNCATE
if(ftruncate(fileno(etag_save->stream), 0)) {
return CURL_WRITEFUNC_ERROR;
}
#else
if(fseek(etag_save->stream, 0, SEEK_SET)) {
return CURL_WRITEFUNC_ERROR;
}
#endif
fwrite(etag_h, size, etag_length, etag_save->stream);
/* terminate with newline */
fputc('\n', etag_save->stream);

View file

@ -925,7 +925,7 @@ static CURLcode single_transfer(struct GlobalConfig *global,
if(config->etag_save_file) {
/* open file for output: */
if(strcmp(config->etag_save_file, "-")) {
FILE *newfile = fopen(config->etag_save_file, "wb");
FILE *newfile = fopen(config->etag_save_file, "ab");
if(!newfile) {
warnf(global, "Failed creating file for saving etags: \"%s\". "
"Skip this transfer", config->etag_save_file);