From 5cc4824fdb87ee65a90d83b9d83417f78f8b674e Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Tue, 3 Oct 2017 16:40:49 +0200 Subject: [PATCH] shell: mufs: implement mufs cp to copy files --- source/shell/mufs.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/source/shell/mufs.c b/source/shell/mufs.c index c66c9f1..8e2d380 100644 --- a/source/shell/mufs.c +++ b/source/shell/mufs.c @@ -15,6 +15,7 @@ #include #include +#include extern struct flash_dev *flash; struct mufs *mufs = NULL; @@ -274,6 +275,75 @@ int shell_mv(struct console *con, size_t argc, const char **argv) return 0; } +int shell_cp(struct console *con, size_t argc, const char **argv) +{ + struct mufs_stat stat; + struct mufs_file *in, *out; + char data[256]; + size_t ret; + size_t count = 0; + size_t total; + + if (!mufs) { + fprintf(con->fp, "error: no file system mounted.\n"); + return -1; + } + + if (argc < 2) { + fprintf(con->fp, "usage: mufs cp \n"); + return -1; + } + + if (mufs_stat(mufs, argv[0], &stat) < 0) { + fprintf(con->fp, "error: unable to stat the file\n"); + return -1; + } + + if (stat.type != MUFS_FILE) { + fprintf(con->fp, "error: only able to copy files\n"); + return -1; + } + + total = stat.file_size; + + if (!(in = mufs_open(mufs, argv[0], MUFS_READ))) { + fprintf(con->fp, "error: unable to open the file\n"); + return -1; + } + + if (!(out = mufs_open(mufs, argv[1], MUFS_WRITE))) { + fprintf(con->fp, "error: unable to open the file\n"); + goto err_close_in; + } + + while (count < total) { + draw_progress(con->fp, count, total); + + ret = mufs_read(in, data, sizeof data); + + if (ret == 0) + break; + + ret = mufs_write(out, data, ret); + + if (ret == 0) + break; + + count += ret; + } + + fputc('\n', con->fp); + + mufs_close(out); + mufs_close(in); + + return 0; + +err_close_in: + mufs_close(in); + return -1; +} + int shell_rm(struct console *con, size_t argc, const char **argv) { if (argc < 1) { @@ -334,6 +404,7 @@ static struct cmd mufs_cmds[] = { { "write", NULL, shell_write }, { "append", NULL, shell_append }, { "mv", NULL, shell_mv, }, + { "cp", NULL, shell_cp, }, { "rm", NULL, shell_rm, }, { NULL, NULL, NULL }, };