curl: add --no-clobber

Does not overwrite output files if they already exist

Closes #7708
Co-authored-by: Daniel Stenberg
This commit is contained in:
HexTheDragon 2021-09-11 20:36:21 -08:00 committed by Daniel Stenberg
parent eed2e8e257
commit 1831a6e7f1
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
16 changed files with 333 additions and 34 deletions

View file

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@ -48,50 +48,86 @@
#define OPENMODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
#endif
/* create a local file for writing, return TRUE on success */
/* create/open a local file for writing, return TRUE on success */
bool tool_create_output_file(struct OutStruct *outs,
struct OperationConfig *config)
{
struct GlobalConfig *global;
FILE *file = NULL;
char *fname = outs->filename;
char *aname = NULL;
DEBUGASSERT(outs);
DEBUGASSERT(config);
global = config->global;
if(!outs->filename || !*outs->filename) {
if(!fname || !*fname) {
warnf(global, "Remote filename has no length!\n");
return FALSE;
}
if(outs->is_cd_filename) {
/* don't overwrite existing files */
if(config->output_dir && outs->is_cd_filename) {
aname = aprintf("%s/%s", config->output_dir, fname);
if(!aname) {
errorf(global, "out of memory\n");
return FALSE;
}
fname = aname;
}
if(config->file_clobber_mode == CLOBBER_ALWAYS ||
(config->file_clobber_mode == CLOBBER_DEFAULT &&
!outs->is_cd_filename)) {
/* open file for writing */
file = fopen(fname, "wb");
}
else {
int fd;
char *name = outs->filename;
char *aname = NULL;
if(config->output_dir) {
aname = aprintf("%s/%s", config->output_dir, name);
if(!aname) {
do {
fd = open(fname, O_CREAT | O_WRONLY | O_EXCL | O_BINARY, OPENMODE);
/* Keep retrying in the hope that it isn't interrupted sometime */
} while(fd == -1 && errno == EINTR);
if(config->file_clobber_mode == CLOBBER_NEVER && fd == -1) {
int next_num = 1;
size_t len = strlen(fname);
char *newname = malloc(len + 13); /* nul + 1-11 digits + dot */
if(!newname) {
errorf(global, "out of memory\n");
return FALSE;
}
name = aname;
memcpy(newname, fname, len);
newname[len] = '.';
while(fd == -1 && /* haven't sucessfully opened a file */
(errno == EEXIST || errno == EISDIR) &&
/* because we keep having files that already exist */
next_num < 100 /* and we haven't reached the retry limit */ ) {
curlx_msnprintf(newname + len + 1, 12, "%d", next_num);
next_num++;
do {
fd = open(newname, O_CREAT | O_WRONLY | O_EXCL | O_BINARY, OPENMODE);
/* Keep retrying in the hope that it isn't interrupted sometime */
} while(fd == -1 && errno == EINTR);
}
outs->filename = newname; /* remember the new one */
outs->alloc_filename = TRUE;
}
fd = open(name, O_CREAT | O_WRONLY | O_EXCL | O_BINARY, OPENMODE);
/* An else statement to not overwrite existing files and not retry with
new numbered names (which would cover
config->file_clobber_mode == CLOBBER_DEFAULT && outs->is_cd_filename)
is not needed because we would have failed earlier, in the while loop
and `fd` would now be -1 */
if(fd != -1) {
file = fdopen(fd, "wb");
if(!file)
close(fd);
}
free(aname);
}
else
/* open file for writing */
file = fopen(outs->filename, "wb");
if(!file) {
warnf(global, "Failed to create the file %s: %s\n", outs->filename,
warnf(global, "Failed to open the file %s: %s\n", fname,
strerror(errno));
free(aname);
return FALSE;
}
free(aname);
outs->s_isreg = TRUE;
outs->fopened = TRUE;
outs->stream = file;

View file

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@ -45,6 +45,7 @@ void config_init(struct OperationConfig *config)
config->happy_eyeballs_timeout_ms = CURL_HET_DEFAULT;
config->http09_allowed = FALSE;
config->ftp_skip_ip = TRUE;
config->file_clobber_mode = CLOBBER_DEFAULT;
}
static void free_config_fields(struct OperationConfig *config)

View file

@ -290,6 +290,15 @@ struct OperationConfig {
bool haproxy_protocol; /* whether to send HAProxy protocol v1 */
bool disallow_username_in_url; /* disallow usernames in URLs */
char *aws_sigv4;
enum {
CLOBBER_DEFAULT, /* Provides compatability with previous versions of curl,
by using the default behavior for -o, -O, and -J.
If those options would have overwritten files, like
-o and -O would, then overwrite them. In the case of
-J, this will not overwrite any files. */
CLOBBER_NEVER, /* If the file exists, always fail */
CLOBBER_ALWAYS /* If the file exists, always overwrite it */
} file_clobber_mode;
struct GlobalConfig *global;
struct OperationConfig *prev;
struct OperationConfig *next; /* Always last in the struct */

View file

@ -314,6 +314,7 @@ static const struct LongShort aliases[]= {
{"O", "remote-name", ARG_NONE},
{"Oa", "remote-name-all", ARG_BOOL},
{"Ob", "output-dir", ARG_STRING},
{"Oc", "clobber", ARG_BOOL},
{"p", "proxytunnel", ARG_BOOL},
{"P", "ftp-port", ARG_STRING},
{"q", "disable", ARG_BOOL},
@ -1999,10 +2000,7 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
case 'N':
/* disable the output I/O buffering. note that the option is called
--buffer but is mostly used in the negative form: --no-buffer */
if(longopt)
config->nobuffer = (!toggle)?TRUE:FALSE;
else
config->nobuffer = toggle;
config->nobuffer = longopt ? !toggle : TRUE;
break;
case 'O': /* --remote-name */
if(subletter == 'a') { /* --remote-name-all */
@ -2013,6 +2011,10 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
GetStr(&config->output_dir, nextarg);
break;
}
else if(subletter == 'c') { /* --clobber / --no-clobber */
config->file_clobber_mode = toggle ? CLOBBER_ALWAYS : CLOBBER_NEVER;
break;
}
/* FALLTHROUGH */
case 'o': /* --output */
/* output file */

View file

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms

View file

@ -385,6 +385,9 @@ const struct helptxt helptext[] = {
{"-N, --no-buffer",
"Disable buffering of the output stream",
CURLHELP_CURL},
{" --no-clobber",
"Do not overwrite files that already exist",
CURLHELP_CURL | CURLHELP_OUTPUT},
{" --no-keepalive",
"Disable TCP keepalive on the connection",
CURLHELP_CONNECTION},