copy_file will always overwrite overwrite an existing file and so will copy_recursive. This is not always the intended behavior, so add a flag to control this. Signed-off-by: Ahmad Fatoum <a.fatoum@xxxxxxxxxxxxxx> --- commands/Kconfig | 4 +++- commands/cp.c | 8 ++++++-- include/libfile.h | 1 + lib/libfile.c | 17 ++++++++++++----- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/commands/Kconfig b/commands/Kconfig index fe459fa862f5..0d60566ba9c6 100644 --- a/commands/Kconfig +++ b/commands/Kconfig @@ -921,11 +921,13 @@ config CMD_CP help Copy files - Usage: cp [-v] SRC DEST + Usage: cp [-rnv] SRC DEST Copy file from SRC to DEST. Options: + -r recursive + -n do not overwrite an existing file -v verbose config CMD_CMP diff --git a/commands/cp.c b/commands/cp.c index 5b8d976c3cfe..dc38cbab889d 100644 --- a/commands/cp.c +++ b/commands/cp.c @@ -28,11 +28,14 @@ static int do_cp(int argc, char *argv[]) int flags = 0, recursive = 0; int argc_min; - while ((opt = getopt(argc, argv, "vr")) > 0) { + while ((opt = getopt(argc, argv, "vnr")) > 0) { switch (opt) { case 'v': flags |= COPY_FILE_VERBOSE; break; + case 'n': + flags |= COPY_FILE_NO_OVERWRITE; + break; case 'r': recursive = 1; break; @@ -87,13 +90,14 @@ BAREBOX_CMD_HELP_TEXT("Copy file from SRC to DEST.") BAREBOX_CMD_HELP_TEXT("") BAREBOX_CMD_HELP_TEXT("Options:") BAREBOX_CMD_HELP_OPT ("-r", "recursive") +BAREBOX_CMD_HELP_OPT ("-n", "do not overwrite an existing file") BAREBOX_CMD_HELP_OPT ("-v", "verbose") BAREBOX_CMD_HELP_END BAREBOX_CMD_START(cp) .cmd = do_cp, BAREBOX_CMD_DESC("copy files") - BAREBOX_CMD_OPTS("[-rv] SRC DEST") + BAREBOX_CMD_OPTS("[-rnv] SRC DEST") BAREBOX_CMD_GROUP(CMD_GRP_FILE) BAREBOX_CMD_HELP(cmd_cp_help) BAREBOX_CMD_END diff --git a/include/libfile.h b/include/libfile.h index 00a7f86a1477..74333a2648f8 100644 --- a/include/libfile.h +++ b/include/libfile.h @@ -28,6 +28,7 @@ int write_file(const char *filename, const void *buf, size_t size); int write_file_flash(const char *filename, const void *buf, size_t size); #define COPY_FILE_VERBOSE BIT(0) +#define COPY_FILE_NO_OVERWRITE BIT(1) int copy_file(const char *src, const char *dst, unsigned flags); diff --git a/lib/libfile.c b/lib/libfile.c index aaade34a4c34..995531248ea4 100644 --- a/lib/libfile.c +++ b/lib/libfile.c @@ -435,6 +435,7 @@ EXPORT_SYMBOL(write_file_flash); * @flags: A bitmask of COPY_FILE_* flags. Possible values: * * COPY_FILE_VERBOSE: show a progression bar + * COPY_FILE_NO_OVERWRITE: don't clobber existing files * * Return: 0 for success or negative error code */ @@ -467,8 +468,13 @@ int copy_file(const char *src, const char *dst, unsigned flags) } /* Set O_TRUNC only if file exists and is a regular file */ - if (!s && S_ISREG(dststat.st_mode)) + if (!s && S_ISREG(dststat.st_mode)) { + if (flags & COPY_FILE_NO_OVERWRITE) { + ret = 0; + goto out; + } mode |= O_TRUNC; + } dstfd = open(dst, mode); if (dstfd < 0) { @@ -498,7 +504,7 @@ int copy_file(const char *src, const char *dst, unsigned flags) if (r < 0) { perror("read"); ret = r; - goto out; + goto out_newline; } if (!r) break; @@ -506,7 +512,7 @@ int copy_file(const char *src, const char *dst, unsigned flags) ret = write_full(dstfd, rw_buf, r); if (ret < 0) { perror("write"); - goto out; + goto out_newline; } total += r; @@ -520,10 +526,10 @@ int copy_file(const char *src, const char *dst, unsigned flags) } ret = 0; -out: +out_newline: if (flags & COPY_FILE_VERBOSE) putchar('\n'); - +out: free(rw_buf); if (srcfd > 0) close(srcfd); @@ -541,6 +547,7 @@ EXPORT_SYMBOL(copy_file); * @flags: A bitmask of COPY_FILE_* flags. Possible values: * * COPY_FILE_VERBOSE: show a progression bar + * COPY_FILE_NO_OVERWRITE: don't clobber existing files * * Return: 0 for success or negative error code */ -- 2.39.5