mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] libfile: give copy_file a flags parameter
@ 2025-06-02 10:05 Ahmad Fatoum
  2025-06-02 10:05 ` [PATCH 2/3] libfile: pass copy_file flags through copy_recursive Ahmad Fatoum
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2025-06-02 10:05 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

In preparation for adding an additional flag input to copy_file, let's
turn the current copy_file verbose parameter in a bitmask.

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 commands/cp.c     |  8 ++++----
 commands/tftp.c   |  2 +-
 common/fastboot.c |  2 +-
 include/libfile.h |  5 ++++-
 lib/libfile.c     | 12 +++++++-----
 5 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/commands/cp.c b/commands/cp.c
index 71448f9aff6f..e2f91123ca92 100644
--- a/commands/cp.c
+++ b/commands/cp.c
@@ -25,13 +25,13 @@ static int do_cp(int argc, char *argv[])
 	int last_is_dir = 0;
 	int i;
 	int opt;
-	int verbose = 0, recursive = 0;
+	int flags = 0, recursive = 0;
 	int argc_min;
 
 	while ((opt = getopt(argc, argv, "vr")) > 0) {
 		switch (opt) {
 		case 'v':
-			verbose = 1;
+			flags |= COPY_FILE_VERBOSE;
 			break;
 		case 'r':
 			recursive = 1;
@@ -68,9 +68,9 @@ static int do_cp(int argc, char *argv[])
 		if (recursive)
 			ret = copy_recursive(argv[i], dst);
 		else if (last_is_dir)
-			ret = copy_file(argv[i], dst, verbose);
+			ret = copy_file(argv[i], dst, flags);
 		else
-			ret = copy_file(argv[i], argv[argc - 1], verbose);
+			ret = copy_file(argv[i], argv[argc - 1], flags);
 
 		free(dst);
 		if (ret)
diff --git a/commands/tftp.c b/commands/tftp.c
index 4356792ce51f..53a34c8c29bf 100644
--- a/commands/tftp.c
+++ b/commands/tftp.c
@@ -78,7 +78,7 @@ static int do_tftpb(int argc, char *argv[])
 
 	debug("%s: %s -> %s\n", __func__, source, dest);
 
-	ret = copy_file(source, dest, 1);
+	ret = copy_file(source, dest, COPY_FILE_VERBOSE);
 
 	umount(TFTP_MOUNT_PATH);
 
diff --git a/common/fastboot.c b/common/fastboot.c
index de50797f31d8..7227149dfb50 100644
--- a/common/fastboot.c
+++ b/common/fastboot.c
@@ -792,7 +792,7 @@ static void cb_flash(struct fastboot *fb, const char *cmd)
 	}
 
 copy:
-	ret = copy_file(fb->tempname, filename, 1);
+	ret = copy_file(fb->tempname, filename, COPY_FILE_VERBOSE);
 	if (ret)
 		fastboot_tx_print(fb, FASTBOOT_MSG_FAIL,
 				  "write partition: %pe", ERR_PTR(ret));
diff --git a/include/libfile.h b/include/libfile.h
index b795a0253039..b3f6f18b3e46 100644
--- a/include/libfile.h
+++ b/include/libfile.h
@@ -4,6 +4,7 @@
 
 #include <linux/types.h>
 #include <linux/compiler.h>
+#include <linux/bits.h>
 
 struct resource;
 
@@ -27,7 +28,9 @@ int read_file_2(const char *filename, size_t *size, void **outbuf,
 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);
 
-int copy_file(const char *src, const char *dst, int verbose);
+#define COPY_FILE_VERBOSE	BIT(0)
+
+int copy_file(const char *src, const char *dst, unsigned flags);
 
 int copy_recursive(const char *src, const char *dst);
 
diff --git a/lib/libfile.c b/lib/libfile.c
index 80d4591dd6e5..76a091878ebb 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -432,11 +432,13 @@ EXPORT_SYMBOL(write_file_flash);
  * copy_file - Copy a file
  * @src:	The source filename
  * @dst:	The destination filename
- * @verbose:	if true, show a progression bar
+ * @flags:	A bitmask of COPY_FILE_* flags. Possible values:
+ *
+ *                COPY_FILE_VERBOSE: show a progression bar
  *
  * Return: 0 for success or negative error code
  */
-int copy_file(const char *src, const char *dst, int verbose)
+int copy_file(const char *src, const char *dst, unsigned flags)
 {
 	char *rw_buf = NULL;
 	int srcfd = 0, dstfd = 0;
@@ -488,7 +490,7 @@ int copy_file(const char *src, const char *dst, int verbose)
 		}
 	}
 
-	if (verbose)
+	if (flags & COPY_FILE_VERBOSE)
 		init_progression_bar(srcstat.st_size);
 
 	while (1) {
@@ -509,7 +511,7 @@ int copy_file(const char *src, const char *dst, int verbose)
 
 		total += r;
 
-		if (verbose) {
+		if (flags & COPY_FILE_VERBOSE) {
 			if (srcstat.st_size && srcstat.st_size != FILESIZE_MAX)
 				show_progress(total);
 			else
@@ -519,7 +521,7 @@ int copy_file(const char *src, const char *dst, int verbose)
 
 	ret = 0;
 out:
-	if (verbose)
+	if (flags & COPY_FILE_VERBOSE)
 		putchar('\n');
 
 	free(rw_buf);
-- 
2.39.5




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] libfile: pass copy_file flags through copy_recursive
  2025-06-02 10:05 [PATCH 1/3] libfile: give copy_file a flags parameter Ahmad Fatoum
@ 2025-06-02 10:05 ` Ahmad Fatoum
  2025-06-02 10:05 ` [PATCH 3/3] libfile: add support for not clobbering files in copy_file Ahmad Fatoum
  2025-06-02 12:28 ` [PATCH 1/3] libfile: give copy_file a flags parameter Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2025-06-02 10:05 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

copy_recursive calls copy_file internally, so it makes sense that it
should take the same flags that copy_file already takes.

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 commands/cp.c         |  2 +-
 commands/defaultenv.c |  2 +-
 include/libfile.h     |  2 +-
 lib/libfile.c         | 14 ++++++++++++--
 4 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/commands/cp.c b/commands/cp.c
index e2f91123ca92..5b8d976c3cfe 100644
--- a/commands/cp.c
+++ b/commands/cp.c
@@ -66,7 +66,7 @@ static int do_cp(int argc, char *argv[])
 		dst = concat_path_file(argv[argc - 1], posix_basename(argv[i]));
 
 		if (recursive)
-			ret = copy_recursive(argv[i], dst);
+			ret = copy_recursive(argv[i], dst, flags);
 		else if (last_is_dir)
 			ret = copy_file(argv[i], dst, flags);
 		else
diff --git a/commands/defaultenv.c b/commands/defaultenv.c
index c13d9aaac7c0..3d118f20a650 100644
--- a/commands/defaultenv.c
+++ b/commands/defaultenv.c
@@ -54,7 +54,7 @@ static int do_defaultenv(int argc, char *argv[])
 	if (scrub)
 		unlink_recursive(to, NULL);
 
-	ret = copy_recursive(from, to);
+	ret = copy_recursive(from, to, 0);
 	free(from);
 	free(to);
 
diff --git a/include/libfile.h b/include/libfile.h
index b3f6f18b3e46..4cb6e11110f5 100644
--- a/include/libfile.h
+++ b/include/libfile.h
@@ -32,7 +32,7 @@ int write_file_flash(const char *filename, const void *buf, size_t size);
 
 int copy_file(const char *src, const char *dst, unsigned flags);
 
-int copy_recursive(const char *src, const char *dst);
+int copy_recursive(const char *src, const char *dst, unsigned flags);
 
 int compare_file(const char *f1, const char *f2);
 
diff --git a/lib/libfile.c b/lib/libfile.c
index 76a091878ebb..aaade34a4c34 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -534,7 +534,17 @@ int copy_file(const char *src, const char *dst, unsigned flags)
 }
 EXPORT_SYMBOL(copy_file);
 
-int copy_recursive(const char *src, const char *dst)
+/**
+ * copy_recursive - Copy files recursively
+ * @src:	The source filename or directory
+ * @dst:	The destination filename or directory
+ * @flags:	A bitmask of COPY_FILE_* flags. Possible values:
+ *
+ *                COPY_FILE_VERBOSE: show a progression bar
+ *
+ * Return: 0 for success or negative error code
+ */
+int copy_recursive(const char *src, const char *dst, unsigned flags)
 {
 	struct stat s;
 	DIR *dir;
@@ -563,7 +573,7 @@ int copy_recursive(const char *src, const char *dst)
 
 		from = basprintf("%s/%s", src, d->d_name);
 		to = basprintf("%s/%s", dst, d->d_name);
-		ret = copy_recursive(from, to);
+		ret = copy_recursive(from, to, flags);
 		if (ret)
 			break;
 		free(from);
-- 
2.39.5




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] libfile: add support for not clobbering files in copy_file
  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
  2025-06-02 12:28 ` [PATCH 1/3] libfile: give copy_file a flags parameter Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2025-06-02 10:05 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

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




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/3] libfile: give copy_file a flags parameter
  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 ` [PATCH 3/3] libfile: add support for not clobbering files in copy_file Ahmad Fatoum
@ 2025-06-02 12:28 ` Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2025-06-02 12:28 UTC (permalink / raw)
  To: barebox, Ahmad Fatoum


On Mon, 02 Jun 2025 12:05:03 +0200, Ahmad Fatoum wrote:
> In preparation for adding an additional flag input to copy_file, let's
> turn the current copy_file verbose parameter in a bitmask.
> 
> 

Applied, thanks!

[1/3] libfile: give copy_file a flags parameter
      https://git.pengutronix.de/cgit/barebox/commit/?id=00a155fc5b52 (link may not be stable)
[2/3] libfile: pass copy_file flags through copy_recursive
      https://git.pengutronix.de/cgit/barebox/commit/?id=42a166ab0df4 (link may not be stable)
[3/3] libfile: add support for not clobbering files in copy_file
      https://git.pengutronix.de/cgit/barebox/commit/?id=45eaed3b1e30 (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-06-02 12:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH 3/3] libfile: add support for not clobbering files in copy_file Ahmad Fatoum
2025-06-02 12:28 ` [PATCH 1/3] libfile: give copy_file a flags parameter Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox