From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH 1/3] libfile: give copy_file a flags parameter
Date: Mon, 2 Jun 2025 12:05:03 +0200 [thread overview]
Message-ID: <20250602100505.830492-1-a.fatoum@barebox.org> (raw)
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
next 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 Ahmad Fatoum [this message]
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
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-1-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