From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Adrian Negreanu <adrian.negreanu@nxp.com>,
Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 10/11] libfile: add support for not clobbering files in copy_file
Date: Thu, 13 Mar 2025 11:17:27 +0100 [thread overview]
Message-ID: <20250313101728.3546902-11-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250313101728.3546902-1-a.fatoum@pengutronix.de>
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@pengutronix.de>
---
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
next prev parent reply other threads:[~2025-03-13 10:37 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-13 10:17 [PATCH 00/11] firmware: qemu_fw_cfg: implement file system Ahmad Fatoum
2025-03-13 10:17 ` [PATCH 01/11] video: ramfb: fix frame buffer screen size Ahmad Fatoum
2025-03-13 10:17 ` [PATCH 02/11] firmware: qemu_fw_cfg: drop duplicate definitions Ahmad Fatoum
2025-03-13 10:17 ` [PATCH 03/11] firmware: qemu_fw_cfg: add support for seeking Ahmad Fatoum
2025-03-13 10:17 ` [PATCH 04/11] firmware: qemu_fw_cfg: rename from /dev/fw_cfg0 to /dev/fw_cfg Ahmad Fatoum
2025-03-13 10:17 ` [PATCH 05/11] fs: add qemu_fw_cfg file system Ahmad Fatoum
2025-03-13 10:17 ` [PATCH 06/11] firmware: qemu_fw_cfg: register at device initcall level Ahmad Fatoum
2025-03-13 10:17 ` [PATCH 07/11] video: ramfb: use new qemu fw_cfg FS Ahmad Fatoum
2025-03-13 10:17 ` [PATCH 08/11] libfile: give copy_file a flags parameter Ahmad Fatoum
2025-03-13 10:17 ` [PATCH 09/11] libfile: pass copy_file flags through copy_recursive Ahmad Fatoum
2025-03-13 10:17 ` Ahmad Fatoum [this message]
2025-03-13 10:17 ` [PATCH 11/11] fs: qemu_fw_cfg: support populating environment via QEMU fw_cfg Ahmad Fatoum
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=20250313101728.3546902-11-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=adrian.negreanu@nxp.com \
--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