curl: add --remove-on-error

If a transfer returns an error, using this option makes curl remove the
leftover downloded (partial) local file before exiting.

Added test 376 to verify

Closes #8503
This commit is contained in:
Daniel Stenberg 2022-02-24 10:30:10 +01:00
parent 7b0fd39db2
commit 08a96c6e4e
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
9 changed files with 93 additions and 2 deletions

View file

@ -294,6 +294,8 @@ struct OperationConfig {
struct OperationConfig *prev;
struct OperationConfig *next; /* Always last in the struct */
struct State state; /* for create_transfer() */
bool rm_partial; /* on error, remove partially written output
files */
};
struct GlobalConfig {

View file

@ -284,6 +284,7 @@ static const struct LongShort aliases[]= {
{"fb", "styled-output", ARG_BOOL},
{"fc", "mail-rcpt-allowfails", ARG_BOOL},
{"fd", "fail-with-body", ARG_BOOL},
{"fe", "remove-on-error", ARG_BOOL},
{"F", "form", ARG_STRING},
{"Fs", "form-string", ARG_STRING},
{"g", "globoff", ARG_BOOL},
@ -1830,7 +1831,10 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
case 'd': /* --fail-with-body */
config->failwithbody = toggle;
break;
default: /* --fail (hard on errors) */
case 'e': /* --remove-on-error */
config->rm_partial = toggle;
break;
default: /* --fail (hard on errors) */
config->failonerror = toggle;
break;
}

View file

@ -574,6 +574,9 @@ const struct helptxt helptext[] = {
{"-R, --remote-time",
"Set the remote file's time on the local output",
CURLHELP_OUTPUT},
{" --remove-on-error",
"Remove output file on errors",
CURLHELP_CURL},
{"-X, --request <method>",
"Specify request method to use",
CURLHELP_CONNECTION},

View file

@ -595,6 +595,10 @@ static CURLcode post_per_transfer(struct GlobalConfig *global,
if(global->showerror)
fprintf(global->errors, "curl: (%d) Failed writing body\n", result);
}
if(result && config->rm_partial) {
notef(global, "Removing output file: %s", per->outfile);
unlink(per->outfile);
}
}
/* File time can only be set _after_ the file has been closed */