commands/cp: add verbose mode which displays progress bar
Signed-off-by: Hubert Feurstein <h.feurstein@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
parent
a59471d133
commit
7bb009c744
|
@ -32,6 +32,7 @@
|
||||||
#include <fs.h>
|
#include <fs.h>
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
|
#include <getopt.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param[in] cmdtp FIXME
|
* @param[in] cmdtp FIXME
|
||||||
|
@ -44,8 +45,21 @@ static int do_cp(struct command *cmdtp, int argc, char *argv[])
|
||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
int last_is_dir = 0;
|
int last_is_dir = 0;
|
||||||
int i;
|
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;
|
return COMMAND_ERROR_USAGE;
|
||||||
|
|
||||||
if (!stat(argv[argc - 1], &statbuf)) {
|
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;
|
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]);
|
printf("cp: target `%s' is not a directory\n", argv[argc - 1]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 1; i < argc - 1; i++) {
|
for (i = optind; i < argc - 1; i++) {
|
||||||
if (last_is_dir) {
|
if (last_is_dir) {
|
||||||
char *dst;
|
char *dst;
|
||||||
dst = concat_path_file(argv[argc - 1], basename(argv[i]));
|
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)
|
if (ret)
|
||||||
goto out;
|
goto out;
|
||||||
free(dst);
|
free(dst);
|
||||||
} else {
|
} else {
|
||||||
ret = copy_file(argv[i], argv[argc - 1]);
|
ret = copy_file(argv[i], argv[argc - 1], verbose);
|
||||||
if (ret)
|
if (ret)
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
@ -79,7 +93,7 @@ out:
|
||||||
}
|
}
|
||||||
|
|
||||||
BAREBOX_CMD_HELP_START(cp)
|
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_SHORT("copy file from <source> to <destination>.\n")
|
||||||
BAREBOX_CMD_HELP_END
|
BAREBOX_CMD_HELP_END
|
||||||
|
|
||||||
|
|
|
@ -259,7 +259,7 @@ static int handle_dnload(struct usb_function *f, const struct usb_ctrlrequest *c
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto err_out;
|
goto err_out;
|
||||||
}
|
}
|
||||||
ret = copy_file(DFU_TEMPFILE, dfu_devs[dfualt].dev);
|
ret = copy_file(DFU_TEMPFILE, dfu_devs[dfualt].dev, 0);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
printf("copy file failed\n");
|
printf("copy file failed\n");
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
|
|
|
@ -26,7 +26,7 @@ int recursive_action(const char *fileName, unsigned flags,
|
||||||
|
|
||||||
char * safe_strncpy(char *dst, const char *src, size_t size);
|
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);
|
int process_escape_sequence(const char *source, char *dest, int destlen);
|
||||||
|
|
||||||
|
|
|
@ -4,20 +4,23 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#include <libbb.h>
|
#include <libbb.h>
|
||||||
|
#include <progress.h>
|
||||||
|
|
||||||
#define RW_BUF_SIZE (ulong)4096
|
#define RW_BUF_SIZE (ulong)4096
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param[in] src FIXME
|
* @param[in] src FIXME
|
||||||
* @param[out] dst 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;
|
char *rw_buf = NULL;
|
||||||
int srcfd = 0, dstfd = 0;
|
int srcfd = 0, dstfd = 0;
|
||||||
int r, w;
|
int r, w;
|
||||||
int ret = 1;
|
int ret = 1;
|
||||||
void *buf;
|
void *buf;
|
||||||
|
int total = 0;
|
||||||
|
|
||||||
rw_buf = xmalloc(RW_BUF_SIZE);
|
rw_buf = xmalloc(RW_BUF_SIZE);
|
||||||
|
|
||||||
|
@ -33,6 +36,15 @@ int copy_file(const char *src, const char *dst)
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (verbose) {
|
||||||
|
struct stat statbuf;
|
||||||
|
|
||||||
|
if (stat(src, &statbuf) < 0)
|
||||||
|
statbuf.st_size = 0;
|
||||||
|
|
||||||
|
init_progression_bar(statbuf.st_size);
|
||||||
|
}
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
r = read(srcfd, rw_buf, RW_BUF_SIZE);
|
r = read(srcfd, rw_buf, RW_BUF_SIZE);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
|
@ -51,11 +63,18 @@ int copy_file(const char *src, const char *dst)
|
||||||
}
|
}
|
||||||
buf += w;
|
buf += w;
|
||||||
r -= w;
|
r -= w;
|
||||||
|
total += w;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (verbose)
|
||||||
|
show_progress(total);
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
out:
|
out:
|
||||||
|
if (verbose)
|
||||||
|
putchar('\n');
|
||||||
|
|
||||||
free(rw_buf);
|
free(rw_buf);
|
||||||
if (srcfd > 0)
|
if (srcfd > 0)
|
||||||
close(srcfd);
|
close(srcfd);
|
||||||
|
|
Loading…
Reference in New Issue