shell: mufs: implement mufs cp to copy files
This commit is contained in:
parent
1638f9b0f1
commit
5cc4824fdb
1 changed files with 71 additions and 0 deletions
|
@ -15,6 +15,7 @@
|
|||
#include <shell.h>
|
||||
|
||||
#include <shell/mufs.h>
|
||||
#include <shell/progress.h>
|
||||
|
||||
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 <src> <dst>\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 },
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue