From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH 3/3] libfile: add support for not clobbering files in copy_file
Date: Mon, 2 Jun 2025 12:05:05 +0200 [thread overview]
Message-ID: <20250602100505.830492-3-a.fatoum@barebox.org> (raw)
In-Reply-To: <20250602100505.830492-1-a.fatoum@barebox.org>
copy_file will always 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@barebox.org>
---
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 7b84ef70e875..3ed2598ab65e 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -933,11 +933,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 4cb6e11110f5..26571945f678 100644
--- a/include/libfile.h
+++ b/include/libfile.h
@@ -29,6 +29,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
next prev parent reply other threads:[~2025-06-02 10:05 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-02 10:05 [PATCH 1/3] libfile: give copy_file a flags parameter Ahmad Fatoum
2025-06-02 10:05 ` [PATCH 2/3] libfile: pass copy_file flags through copy_recursive Ahmad Fatoum
2025-06-02 10:05 ` Ahmad Fatoum [this message]
2025-06-02 12:28 ` [PATCH 1/3] libfile: give copy_file a flags parameter Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250602100505.830492-3-a.fatoum@barebox.org \
--to=a.fatoum@barebox.org \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox