Skip to contentSkip to conversation

commands/cp: add verbose mode which displays progress bar

RFC PATCH Superseded Hubert Feurstein 3 participants 5 messages Nov 18, 2011 – Nov 22, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/2 commands/cp: add verbose mode which displays progress bar

---
 commands/cp.c            |   20 ++++++++++++++------
 drivers/usb/gadget/dfu.c |    2 +-
 include/libbb.h          |    2 +-
 lib/copy_file.c          |   20 +++++++++++++++++++-
 4 files changed, 35 insertions(+), 9 deletions(-)
commands/cp.c +14 -6
index 3428105..bbe350d 100644
@@ -44,8 +44,15 @@ static int do_cp(struct command *cmdtp, int argc, char *argv[])
 	struct stat statbuf;
 	int last_is_dir = 0;
 	int i;
+	int verbose = 0;
+	int argc_min = 3;
 
-	if (argc < 3)
+	if (argc >= argc_min && strcmp(argv[1], "-v") == 0) {
+		verbose = 1;
+		argc_min++;
+	}
+
+	if (argc < argc_min)
 		return COMMAND_ERROR_USAGE;
 
 	if (!stat(argv[argc - 1], &statbuf)) {
@@ -53,21 +60,22 @@ static int do_cp(struct command *cmdtp, int argc, char *argv[])
 			last_is_dir = 1;
 	}
 
-	if (argc > 3 && !last_is_dir) {
+	if (argc > argc_min && !last_is_dir) {
 		printf("cp: target `%s' is not a directory\n", argv[argc - 1]);
 		return 1;
 	}
 
-	for (i = 1; i < argc - 1; i++) {
+	i = (!verbose) ? 1 : 2;
+	for (; i < argc - 1; i++) {
 		if (last_is_dir) {
 			char *dst;
 			dst = concat_path_file(argv[argc - 1], basename(argv[i]));
-			ret = copy_file(argv[i], dst);
+			ret = copy_file(argv[i], dst, verbose);
 			if (ret)
 				goto out;
 			free(dst);
 		} else {
-			ret = copy_file(argv[i], argv[argc - 1]);
+			ret = copy_file(argv[i], argv[argc - 1], verbose);
 			if (ret)
 				goto out;
 		}
@@ -79,7 +87,7 @@ out:
 }
 
 BAREBOX_CMD_HELP_START(cp)
-BAREBOX_CMD_HELP_USAGE("cp <source> <destination>\n")
+BAREBOX_CMD_HELP_USAGE("cp [-v] <source> <destination>\n")
 BAREBOX_CMD_HELP_SHORT("copy file from <source> to <destination>.\n")
 BAREBOX_CMD_HELP_END
 
drivers/usb/gadget/dfu.c +1 -1
index 1387c6d..0a0d244 100644
@@ -259,7 +259,7 @@ static int handle_dnload(struct usb_function *f, const struct usb_ctrlrequest *c
 				ret = -EINVAL;
 				goto err_out;
 			}
-			ret = copy_file(DFU_TEMPFILE, dfu_devs[dfualt].dev);
+			ret = copy_file(DFU_TEMPFILE, dfu_devs[dfualt].dev, 0);
 			if (ret) {
 				printf("copy file failed\n");
 				ret = -EINVAL;
include/libbb.h +1 -1
index 0962969..2d17c3f 100644
@@ -26,7 +26,7 @@ int recursive_action(const char *fileName, unsigned flags,
 
 char * safe_strncpy(char *dst, const char *src, size_t size);
 
-int copy_file(const char *src, const char *dst);
+int copy_file(const char *src, const char *dst, int verbose);
 
 int process_escape_sequence(const char *source, char *dest, int destlen);
 
lib/copy_file.c +19 -1
index 809befe..e4bb07c 100644
@@ -4,6 +4,7 @@
 #include <errno.h>
 #include <malloc.h>
 #include <libbb.h>
+#include <progress.h>
 
 #define RW_BUF_SIZE	(ulong)4096
 
@@ -11,13 +12,14 @@
  * @param[in] src FIXME
  * @param[out] dst FIXME
  */
-int copy_file(const char *src, const char *dst)
+int copy_file(const char *src, const char *dst, int verbose)
 {
 	char *rw_buf = NULL;
 	int srcfd = 0, dstfd = 0;
 	int r, w;
 	int ret = 1;
 	void *buf;
+	int total = 0;
 
 	rw_buf = xmalloc(RW_BUF_SIZE);
 
@@ -33,6 +35,15 @@ int copy_file(const char *src, const char *dst)
 		goto out;
 	}
 
+	if (verbose) {
+		struct stat statbuf;
+
+		if (stat(src, &statbuf) < 0)
+			statbuf.st_size = 0;
+
+		init_progression_bar(statbuf.st_size);
+	}
+
 	while(1) {
 		r = read(srcfd, rw_buf, RW_BUF_SIZE);
 		if (r < 0) {
@@ -51,11 +62,18 @@ int copy_file(const char *src, const char *dst)
 			}
 			buf += w;
 			r -= w;
+			total += w;
 		}
+
+		if (verbose)
+			show_progress(total);
 	}
 
 	ret = 0;
 out:
+	if (verbose)
+		putchar('\n');
+
 	free(rw_buf);
 	if (srcfd > 0)
 		close(srcfd);

--
1.7.4.1

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#1 lore raw
Quoted 7 lines · Hubert Feurstein
---
 commands/cp.c            |   20 ++++++++++++++------
 drivers/usb/gadget/dfu.c |    2 +-
 include/libbb.h          |    2 +-
 lib/copy_file.c          |   20 +++++++++++++++++++-
 4 files changed, 35 insertions(+), 9 deletions(-)

please use getopt

Best Regards,
J.

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#2 lore raw
In reply to Hubert Feurstein

Hi Hubert,

Quoted 14 lines · Hubert Feurstein
---
 commands/cp.c            |   20 ++++++++++++++------
 drivers/usb/gadget/dfu.c |    2 +-
 include/libbb.h          |    2 +-
 lib/copy_file.c          |   20 +++++++++++++++++++-
 4 files changed, 35 insertions(+), 9 deletions(-)
commands/cp.c +0 -0
index 3428105..bbe350d 100644
@@ -44,8 +44,15 @@ static int do_cp(struct command *cmdtp, int argc, char *argv[])
 	struct stat statbuf;

commands/cp.c static int do_cp(struct command *cmdtp, int argc, char *argv[]) · line 53

Quoted 10 lines
commands/cp.c +6 -1
 	int last_is_dir = 0;
 	int i;
+	int verbose = 0;
+	int argc_min = 3;
 
-	if (argc < 3)
+	if (argc >= argc_min && strcmp(argv[1], "-v") == 0) {
+		verbose = 1;
+		argc_min++;
+	}

Please use getopt here.

Quoted 139 lines
commands/cp.c +8 -5
+
+	if (argc < argc_min)
 		return COMMAND_ERROR_USAGE;
 
 	if (!stat(argv[argc - 1], &statbuf)) {
@@ -53,21 +60,22 @@ static int do_cp(struct command *cmdtp, int argc, char *argv[])
 			last_is_dir = 1;
 	}
 
-	if (argc > 3 && !last_is_dir) {
+	if (argc > argc_min && !last_is_dir) {
 		printf("cp: target `%s' is not a directory\n", argv[argc - 1]);
 		return 1;
 	}
 
-	for (i = 1; i < argc - 1; i++) {
+	i = (!verbose) ? 1 : 2;
+	for (; i < argc - 1; i++) {
 		if (last_is_dir) {
 			char *dst;
 			dst = concat_path_file(argv[argc - 1], basename(argv[i]));
-			ret = copy_file(argv[i], dst);
+			ret = copy_file(argv[i], dst, verbose);
 			if (ret)
 				goto out;
 			free(dst);
 		} else {
-			ret = copy_file(argv[i], argv[argc - 1]);
+			ret = copy_file(argv[i], argv[argc - 1], verbose);
 			if (ret)
 				goto out;
 		}
@@ -79,7 +87,7 @@ out:
 }
 
 BAREBOX_CMD_HELP_START(cp)
-BAREBOX_CMD_HELP_USAGE("cp <source> <destination>\n")
+BAREBOX_CMD_HELP_USAGE("cp [-v] <source> <destination>\n")
 BAREBOX_CMD_HELP_SHORT("copy file from <source> to <destination>.\n")
 BAREBOX_CMD_HELP_END
 
drivers/usb/gadget/dfu.c +1 -1
index 1387c6d..0a0d244 100644
@@ -259,7 +259,7 @@ static int handle_dnload(struct usb_function *f, const struct usb_ctrlrequest *c
 				ret = -EINVAL;
 				goto err_out;
 			}
-			ret = copy_file(DFU_TEMPFILE, dfu_devs[dfualt].dev);
+			ret = copy_file(DFU_TEMPFILE, dfu_devs[dfualt].dev, 0);
 			if (ret) {
 				printf("copy file failed\n");
 				ret = -EINVAL;
include/libbb.h +1 -1
index 0962969..2d17c3f 100644
@@ -26,7 +26,7 @@ int recursive_action(const char *fileName, unsigned flags,
 
 char * safe_strncpy(char *dst, const char *src, size_t size);
 
-int copy_file(const char *src, const char *dst);
+int copy_file(const char *src, const char *dst, int verbose);
 
 int process_escape_sequence(const char *source, char *dest, int destlen);
 
lib/copy_file.c +19 -2
index 809befe..e4bb07c 100644
@@ -4,6 +4,7 @@
 #include <errno.h>
 #include <malloc.h>
 #include <libbb.h>
+#include <progress.h>
 
 #define RW_BUF_SIZE	(ulong)4096
 
@@ -11,13 +12,14 @@
  * @param[in] src FIXME
  * @param[out] dst FIXME
  */
-int copy_file(const char *src, const char *dst)
+int copy_file(const char *src, const char *dst, int verbose)
 {
 	char *rw_buf = NULL;
 	int srcfd = 0, dstfd = 0;
 	int r, w;
 	int ret = 1;
 	void *buf;
+	int total = 0;
 
 	rw_buf = xmalloc(RW_BUF_SIZE);
 
@@ -33,6 +35,15 @@ int copy_file(const char *src, const char *dst)
 		goto out;
 	}
 
+	if (verbose) {
+		struct stat statbuf;
+
+		if (stat(src, &statbuf) < 0)
+			statbuf.st_size = 0;
+
+		init_progression_bar(statbuf.st_size);
+	}
+
 	while(1) {
 		r = read(srcfd, rw_buf, RW_BUF_SIZE);
 		if (r < 0) {
@@ -51,11 +62,18 @@ int copy_file(const char *src, const char *dst)
 			}
 			buf += w;
 			r -= w;
+			total += w;
 		}
+
+		if (verbose)
+			show_progress(total);
 	}
 
 	ret = 0;
 out:
+	if (verbose)
+		putchar('\n');
+
 	free(rw_buf);
 	if (srcfd > 0)
 		close(srcfd);
--

1.7.4.1

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw

PATCH v2 1/1 commands/cp: add verbose mode which displays progress bar

---
v2: use getopt

 commands/cp.c            |   26 ++++++++++++++++++++------
 drivers/usb/gadget/dfu.c |    2 +-
 include/libbb.h          |    2 +-
 lib/copy_file.c          |   21 ++++++++++++++++++++-
 4 files changed, 42 insertions(+), 9 deletions(-)
commands/cp.c +20 -6
index 3428105..8b2fa48 100644
@@ -32,6 +32,7 @@
 #include <fs.h>
 #include <malloc.h>
 #include <libgen.h>
+#include <getopt.h>
 
 /**
  * @param[in] cmdtp FIXME
@@ -44,8 +45,20 @@ static int do_cp(struct command *cmdtp, int argc, char *argv[])
 	struct stat statbuf;
 	int last_is_dir = 0;
 	int i;
+	int opt;
+	int verbose = 0;
+	int argc_min = 3;
 
-	if (argc < 3)
+	while ((opt = getopt(argc, argv, "v")) > 0) {
+		switch (opt) {
+		case 'v':
+			verbose = 1;
+			break;
+		}
+		argc_min++;
+	}
+
+	if (argc < argc_min)
 		return COMMAND_ERROR_USAGE;
 
 	if (!stat(argv[argc - 1], &statbuf)) {
@@ -53,21 +66,22 @@ static int do_cp(struct command *cmdtp, int argc, char *argv[])
 			last_is_dir = 1;
 	}
 
-	if (argc > 3 && !last_is_dir) {
+	if (argc > argc_min && !last_is_dir) {
 		printf("cp: target `%s' is not a directory\n", argv[argc - 1]);
 		return 1;
 	}
 
-	for (i = 1; i < argc - 1; i++) {
+	i = (!verbose) ? 1 : 2;
+	for (; i < argc - 1; i++) {
 		if (last_is_dir) {
 			char *dst;
 			dst = concat_path_file(argv[argc - 1], basename(argv[i]));
-			ret = copy_file(argv[i], dst);
+			ret = copy_file(argv[i], dst, verbose);
 			if (ret)
 				goto out;
 			free(dst);
 		} else {
-			ret = copy_file(argv[i], argv[argc - 1]);
+			ret = copy_file(argv[i], argv[argc - 1], verbose);
 			if (ret)
 				goto out;
 		}
@@ -79,7 +93,7 @@ out:
 }
 
 BAREBOX_CMD_HELP_START(cp)
-BAREBOX_CMD_HELP_USAGE("cp <source> <destination>\n")
+BAREBOX_CMD_HELP_USAGE("cp [-v] <source> <destination>\n")
 BAREBOX_CMD_HELP_SHORT("copy file from <source> to <destination>.\n")
 BAREBOX_CMD_HELP_END
 
drivers/usb/gadget/dfu.c +1 -1
index 1387c6d..0a0d244 100644
@@ -259,7 +259,7 @@ static int handle_dnload(struct usb_function *f, const struct usb_ctrlrequest *c
 				ret = -EINVAL;
 				goto err_out;
 			}
-			ret = copy_file(DFU_TEMPFILE, dfu_devs[dfualt].dev);
+			ret = copy_file(DFU_TEMPFILE, dfu_devs[dfualt].dev, 0);
 			if (ret) {
 				printf("copy file failed\n");
 				ret = -EINVAL;
include/libbb.h +1 -1
index 0962969..2d17c3f 100644
@@ -26,7 +26,7 @@ int recursive_action(const char *fileName, unsigned flags,
 
 char * safe_strncpy(char *dst, const char *src, size_t size);
 
-int copy_file(const char *src, const char *dst);
+int copy_file(const char *src, const char *dst, int verbose);
 
 int process_escape_sequence(const char *source, char *dest, int destlen);
 
lib/copy_file.c +20 -1
index 809befe..967806c 100644
@@ -4,20 +4,23 @@
 #include <errno.h>
 #include <malloc.h>
 #include <libbb.h>
+#include <progress.h>
 
 #define RW_BUF_SIZE	(ulong)4096
 
 /**
  * @param[in] src FIXME
  * @param[out] dst FIXME
+ * @param[in] verbose FIXME
  */
-int copy_file(const char *src, const char *dst)
+int copy_file(const char *src, const char *dst, int verbose)
 {
 	char *rw_buf = NULL;
 	int srcfd = 0, dstfd = 0;
 	int r, w;
 	int ret = 1;
 	void *buf;
+	int total = 0;
 
 	rw_buf = xmalloc(RW_BUF_SIZE);
 
@@ -33,6 +36,15 @@ int copy_file(const char *src, const char *dst)
 		goto out;
 	}
 
+	if (verbose) {
+		struct stat statbuf;
+
+		if (stat(src, &statbuf) < 0)
+			statbuf.st_size = 0;
+
+		init_progression_bar(statbuf.st_size);
+	}
+
 	while(1) {
 		r = read(srcfd, rw_buf, RW_BUF_SIZE);
 		if (r < 0) {
@@ -51,11 +63,18 @@ int copy_file(const char *src, const char *dst)
 			}
 			buf += w;
 			r -= w;
+			total += w;
 		}
+
+		if (verbose)
+			show_progress(total);
 	}
 
 	ret = 0;
 out:
+	if (verbose)
+		putchar('\n');
+
 	free(rw_buf);
 	if (srcfd > 0)
 		close(srcfd);

--
1.7.4.1

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#4 lore raw

PATCH v3 1/1 commands/cp: add verbose mode which displays progress bar

---
v2: use getopt
v3: use optind to determine the beginning of the file arguments

 commands/cp.c            |   26 ++++++++++++++++++++------
 drivers/usb/gadget/dfu.c |    2 +-
 include/libbb.h          |    2 +-
 lib/copy_file.c          |   21 ++++++++++++++++++++-
 4 files changed, 42 insertions(+), 9 deletions(-)
commands/cp.c +20 -6
index 3428105..f78e3de 100644
@@ -32,6 +32,7 @@
 #include <fs.h>
 #include <malloc.h>
 #include <libgen.h>
+#include <getopt.h>
 
 /**
  * @param[in] cmdtp FIXME
@@ -44,8 +45,21 @@ static int do_cp(struct command *cmdtp, int argc, char *argv[])
 	struct stat statbuf;
 	int last_is_dir = 0;
 	int i;
+	int opt;
+	int verbose = 0;
+	int argc_min;
 
-	if (argc < 3)
+	while ((opt = getopt(argc, argv, "v")) > 0) {
+		switch (opt) {
+		case 'v':
+			verbose = 1;
+			break;
+		}
+	}
+
+	argc_min = optind + 2;
+
+	if (argc < argc_min)
 		return COMMAND_ERROR_USAGE;
 
 	if (!stat(argv[argc - 1], &statbuf)) {
@@ -53,21 +67,21 @@ static int do_cp(struct command *cmdtp, int argc, char *argv[])
 			last_is_dir = 1;
 	}
 
-	if (argc > 3 && !last_is_dir) {
+	if (argc > argc_min && !last_is_dir) {
 		printf("cp: target `%s' is not a directory\n", argv[argc - 1]);
 		return 1;
 	}
 
-	for (i = 1; i < argc - 1; i++) {
+	for (i = optind; i < argc - 1; i++) {
 		if (last_is_dir) {
 			char *dst;
 			dst = concat_path_file(argv[argc - 1], basename(argv[i]));
-			ret = copy_file(argv[i], dst);
+			ret = copy_file(argv[i], dst, verbose);
 			if (ret)
 				goto out;
 			free(dst);
 		} else {
-			ret = copy_file(argv[i], argv[argc - 1]);
+			ret = copy_file(argv[i], argv[argc - 1], verbose);
 			if (ret)
 				goto out;
 		}
@@ -79,7 +93,7 @@ out:
 }
 
 BAREBOX_CMD_HELP_START(cp)
-BAREBOX_CMD_HELP_USAGE("cp <source> <destination>\n")
+BAREBOX_CMD_HELP_USAGE("cp [-v] <source> <destination>\n")
 BAREBOX_CMD_HELP_SHORT("copy file from <source> to <destination>.\n")
 BAREBOX_CMD_HELP_END
 
drivers/usb/gadget/dfu.c +1 -1
index 1387c6d..0a0d244 100644
@@ -259,7 +259,7 @@ static int handle_dnload(struct usb_function *f, const struct usb_ctrlrequest *c
 				ret = -EINVAL;
 				goto err_out;
 			}
-			ret = copy_file(DFU_TEMPFILE, dfu_devs[dfualt].dev);
+			ret = copy_file(DFU_TEMPFILE, dfu_devs[dfualt].dev, 0);
 			if (ret) {
 				printf("copy file failed\n");
 				ret = -EINVAL;
include/libbb.h +1 -1
index 0962969..2d17c3f 100644
@@ -26,7 +26,7 @@ int recursive_action(const char *fileName, unsigned flags,
 
 char * safe_strncpy(char *dst, const char *src, size_t size);
 
-int copy_file(const char *src, const char *dst);
+int copy_file(const char *src, const char *dst, int verbose);
 
 int process_escape_sequence(const char *source, char *dest, int destlen);
 
lib/copy_file.c +20 -1
index 809befe..967806c 100644
@@ -4,20 +4,23 @@
 #include <errno.h>
 #include <malloc.h>
 #include <libbb.h>
+#include <progress.h>
 
 #define RW_BUF_SIZE	(ulong)4096
 
 /**
  * @param[in] src FIXME
  * @param[out] dst FIXME
+ * @param[in] verbose FIXME
  */
-int copy_file(const char *src, const char *dst)
+int copy_file(const char *src, const char *dst, int verbose)
 {
 	char *rw_buf = NULL;
 	int srcfd = 0, dstfd = 0;
 	int r, w;
 	int ret = 1;
 	void *buf;
+	int total = 0;
 
 	rw_buf = xmalloc(RW_BUF_SIZE);
 
@@ -33,6 +36,15 @@ int copy_file(const char *src, const char *dst)
 		goto out;
 	}
 
+	if (verbose) {
+		struct stat statbuf;
+
+		if (stat(src, &statbuf) < 0)
+			statbuf.st_size = 0;
+
+		init_progression_bar(statbuf.st_size);
+	}
+
 	while(1) {
 		r = read(srcfd, rw_buf, RW_BUF_SIZE);
 		if (r < 0) {
@@ -51,11 +63,18 @@ int copy_file(const char *src, const char *dst)
 			}
 			buf += w;
 			r -= w;
+			total += w;
 		}
+
+		if (verbose)
+			show_progress(total);
 	}
 
 	ret = 0;
 out:
+	if (verbose)
+		putchar('\n');
+
 	free(rw_buf);
 	if (srcfd > 0)
 		close(srcfd);

--
1.7.4.1

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox