From 941b56d790dc8f056e1ae31c9a7d17e7b2af066a Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 2 Jan 2024 13:32:18 +0100 Subject: [PATCH] tool: prepend output_dir in header callback When Content-Disposition parsing is used and an output dir is prepended, make sure to store that new file name correctly so that it can be used for setting the file timestamp when --remote-time is used. Extended test 3012 to verify. Co-Authored-by: Jay Satiro Reported-by: hgdagon on github Fixes #12614 Closes #12617 --- src/tool_cb_hdr.c | 10 +++++++++- src/tool_cb_wrt.c | 14 -------------- tests/data/test3012 | 5 +++-- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/tool_cb_hdr.c b/src/tool_cb_hdr.c index 198a8d050c..30ee3b09c9 100644 --- a/src/tool_cb_hdr.c +++ b/src/tool_cb_hdr.c @@ -178,10 +178,18 @@ size_t tool_header_cb(char *ptr, size_t size, size_t nmemb, void *userdata) return CURL_WRITEFUNC_ERROR; } + if(per->config->output_dir) { + outs->filename = aprintf("%s/%s", per->config->output_dir, filename); + free(filename); + if(!outs->filename) + return CURL_WRITEFUNC_ERROR; + } + else + outs->filename = filename; + outs->is_cd_filename = TRUE; outs->s_isreg = TRUE; outs->fopened = FALSE; - outs->filename = filename; outs->alloc_filename = TRUE; hdrcbdata->honor_cd_filename = FALSE; /* done now! */ if(!tool_create_output_file(outs, per->config)) diff --git a/src/tool_cb_wrt.c b/src/tool_cb_wrt.c index 98063c39c2..143cba255b 100644 --- a/src/tool_cb_wrt.c +++ b/src/tool_cb_wrt.c @@ -57,7 +57,6 @@ bool tool_create_output_file(struct OutStruct *outs, struct GlobalConfig *global; FILE *file = NULL; char *fname = outs->filename; - char *aname = NULL; DEBUGASSERT(outs); DEBUGASSERT(config); global = config->global; @@ -66,15 +65,6 @@ bool tool_create_output_file(struct OutStruct *outs, return FALSE; } - if(config->output_dir && outs->is_cd_filename) { - aname = aprintf("%s/%s", config->output_dir, fname); - if(!aname) { - errorf(global, "out of memory"); - return FALSE; - } - fname = aname; - } - if(config->file_clobber_mode == CLOBBER_ALWAYS || (config->file_clobber_mode == CLOBBER_DEFAULT && !outs->is_cd_filename)) { @@ -94,14 +84,12 @@ bool tool_create_output_file(struct OutStruct *outs, char *newname; /* Guard against wraparound in new filename */ if(newlen < len) { - free(aname); errorf(global, "overflow in filename generation"); return FALSE; } newname = malloc(newlen); if(!newname) { errorf(global, "out of memory"); - free(aname); return FALSE; } memcpy(newname, fname, len); @@ -135,10 +123,8 @@ bool tool_create_output_file(struct OutStruct *outs, if(!file) { warnf(global, "Failed to open the file %s: %s", fname, strerror(errno)); - free(aname); return FALSE; } - free(aname); outs->s_isreg = TRUE; outs->fopened = TRUE; outs->stream = file; diff --git a/tests/data/test3012 b/tests/data/test3012 index 2bd329455c..a9c9505874 100644 --- a/tests/data/test3012 +++ b/tests/data/test3012 @@ -4,6 +4,7 @@ -O -J --output-dir +--remote-time # @@ -36,10 +37,10 @@ http http ---output-dir with -J +--output-dir with -J and -R -http://%HOSTIP:%HTTPPORT/this/is/the/%TESTNUMBER -OJ --output-dir %PWD/%LOGDIR +http://%HOSTIP:%HTTPPORT/this/is/the/%TESTNUMBER -OJR --output-dir %PWD/%LOGDIR