[PATCH 08/11] libfile: give copy_file a flags parameter
Ahmad Fatoum
a.fatoum at pengutronix.de
Thu Mar 13 03:17:25 PDT 2025
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 at pengutronix.de>
---
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 5e8d8d17761d..f17b589ee62d 100644
--- a/commands/tftp.c
+++ b/commands/tftp.c
@@ -81,7 +81,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 60bef0ec776b..9088b988cb6c 100644
--- a/common/fastboot.c
+++ b/common/fastboot.c
@@ -741,7 +741,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: %s", strerror(-ret));
diff --git a/include/libfile.h b/include/libfile.h
index bf775b022dc4..a9bef5065d08 100644
--- a/include/libfile.h
+++ b/include/libfile.h
@@ -3,6 +3,7 @@
#define __LIBFILE_H
#include <linux/types.h>
+#include <linux/bits.h>
struct resource;
@@ -26,7 +27,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
More information about the barebox
mailing list