|
|
|
@ -14,61 +14,41 @@ |
|
|
|
|
|
|
|
|
|
#include <fs/mufs.h> |
|
|
|
|
|
|
|
|
|
static void do_mufs_mount(const char *s); |
|
|
|
|
static void do_mufs_unmount(const char *s); |
|
|
|
|
static void do_mufs_format(const char *s); |
|
|
|
|
static void do_mufs_mkdir(const char *s); |
|
|
|
|
static void do_mufs_rmdir(const char *s); |
|
|
|
|
static void do_mufs_stat(const char *stat); |
|
|
|
|
static void do_mufs_cat(const char *s); |
|
|
|
|
static void do_mufs_append(const char *s); |
|
|
|
|
static void do_mufs_mv(const char *s); |
|
|
|
|
static void do_mufs_rm(const char *s); |
|
|
|
|
static void do_mufs_ls(const char *s); |
|
|
|
|
|
|
|
|
|
static struct cmd mufs_cmds[] = { |
|
|
|
|
{ "mount", do_mufs_mount }, |
|
|
|
|
{ "unmount", do_mufs_unmount }, |
|
|
|
|
{ "format", do_mufs_format }, |
|
|
|
|
{ "mkdir", do_mufs_mkdir }, |
|
|
|
|
{ "rmdir", do_mufs_rmdir }, |
|
|
|
|
{ "ls", do_mufs_ls }, |
|
|
|
|
{ "stat", do_mufs_stat }, |
|
|
|
|
{ "cat", do_mufs_cat }, |
|
|
|
|
{ "append", do_mufs_append }, |
|
|
|
|
{ "mv", do_mufs_mv, }, |
|
|
|
|
{ "rm", do_mufs_rm, }, |
|
|
|
|
{ NULL, NULL }, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
extern struct flash_dev *flash; |
|
|
|
|
struct mufs *mufs = NULL; |
|
|
|
|
|
|
|
|
|
static void do_mufs_mount(const char *s) |
|
|
|
|
static void do_mufs_mount(FILE *out, const char **argv, size_t argc) |
|
|
|
|
{ |
|
|
|
|
(void)s; |
|
|
|
|
(void)argc; |
|
|
|
|
(void)argv; |
|
|
|
|
|
|
|
|
|
if (!flash) { |
|
|
|
|
fprintf(stderr, "error: no flash device probed.\n"); |
|
|
|
|
fprintf(out, "error: no flash device probed.\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
do_mufs_unmount(NULL); |
|
|
|
|
if (mufs) { |
|
|
|
|
mufs_unmount(mufs); |
|
|
|
|
mufs = NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!(mufs = mufs_mount(flash))) { |
|
|
|
|
fprintf(stderr, "error: unable to mount the filesystem.\n"); |
|
|
|
|
fprintf(out, "error: unable to mount the filesystem.\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
flash = NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void do_mufs_unmount(const char *s) |
|
|
|
|
static void do_mufs_unmount(FILE *out, const char **argv, size_t argc) |
|
|
|
|
{ |
|
|
|
|
(void)s; |
|
|
|
|
(void)argc; |
|
|
|
|
(void)argv; |
|
|
|
|
|
|
|
|
|
if (!mufs) |
|
|
|
|
if (!mufs) { |
|
|
|
|
fprintf(out, "no mufs filesystem currently active\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (flash) |
|
|
|
|
flash_release(flash); |
|
|
|
@ -80,58 +60,74 @@ static void do_mufs_unmount(const char *s) |
|
|
|
|
mufs = NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void do_mufs_format(const char *s) |
|
|
|
|
static void do_mufs_format(FILE *out, const char **argv, size_t argc) |
|
|
|
|
{ |
|
|
|
|
(void)s; |
|
|
|
|
(void)argc; |
|
|
|
|
(void)argv; |
|
|
|
|
|
|
|
|
|
if (!flash) { |
|
|
|
|
fprintf(stderr, "error: no flash device probed.\n"); |
|
|
|
|
fprintf(out, "error: no flash device probed.\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (mufs_format(flash) < 0) { |
|
|
|
|
fprintf(stderr, "error: unable to format the flash device.\n"); |
|
|
|
|
fprintf(out, "error: unable to format the flash device.\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void do_mufs_mkdir(const char *s) |
|
|
|
|
static void do_mufs_mkdir(FILE *out, const char **argv, size_t argc) |
|
|
|
|
{ |
|
|
|
|
if (argc < 1) { |
|
|
|
|
fprintf(out, "usage: mufs mkdir <path>\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!mufs) { |
|
|
|
|
fprintf(stderr, "error: no file system mounted.\n"); |
|
|
|
|
fprintf(out, "error: no file system mounted.\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (mufs_mkdir(mufs, s) < 0) { |
|
|
|
|
fprintf(stderr, "error: unable to create the directory\n"); |
|
|
|
|
if (mufs_mkdir(mufs, argv[0]) < 0) { |
|
|
|
|
fprintf(out, "error: unable to create the directory\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void do_mufs_rmdir(const char *s) |
|
|
|
|
static void do_mufs_rmdir(FILE *out, const char **argv, size_t argc) |
|
|
|
|
{ |
|
|
|
|
if (argc < 1) { |
|
|
|
|
fprintf(out, "usage: mufs rmdir <path>\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!mufs) { |
|
|
|
|
fprintf(stderr, "error: no file system mounted.\n"); |
|
|
|
|
fprintf(out, "error: no file system mounted.\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (mufs_rmdir(mufs, s) < 0) { |
|
|
|
|
fprintf(stderr, "error: unable to remove the directory\n"); |
|
|
|
|
if (mufs_rmdir(mufs, argv[0]) < 0) { |
|
|
|
|
fprintf(out, "error: unable to remove the directory\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void do_mufs_stat(const char *s) |
|
|
|
|
static void do_mufs_stat(FILE *out, const char **argv, size_t argc) |
|
|
|
|
{ |
|
|
|
|
struct mufs_stat stat; |
|
|
|
|
|
|
|
|
|
if (argc < 1) { |
|
|
|
|
fprintf(out, "usage: mufs stat <path>\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!mufs) { |
|
|
|
|
fprintf(stderr, "error: no file system mounted.\n"); |
|
|
|
|
fprintf(out, "error: no file system mounted.\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (mufs_stat(mufs, s, &stat) < 0) { |
|
|
|
|
fprintf(stderr, "error: unable to stat the file\n"); |
|
|
|
|
if (mufs_stat(mufs, argv[0], &stat) < 0) { |
|
|
|
|
fprintf(out, "error: unable to stat the file\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -144,18 +140,23 @@ static void do_mufs_stat(const char *s) |
|
|
|
|
printf(" file size: %" PRIu32 " bytes\n", stat.file_size); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void do_mufs_cat(const char *s) |
|
|
|
|
static void do_mufs_cat(FILE *out, const char **argv, size_t argc) |
|
|
|
|
{ |
|
|
|
|
char data[256]; |
|
|
|
|
struct mufs_file *file; |
|
|
|
|
|
|
|
|
|
if (argc < 1) { |
|
|
|
|
fprintf(out, "usage: mufs cat <path>\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!mufs) { |
|
|
|
|
fprintf(stderr, "error: no file system mounted.\n"); |
|
|
|
|
fprintf(out, "error: no file system mounted.\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!(file = mufs_open(mufs, s, MUFS_READ))) { |
|
|
|
|
fprintf(stderr, "error: unable to open the file\n"); |
|
|
|
|
if (!(file = mufs_open(mufs, argv[0], MUFS_READ))) { |
|
|
|
|
fprintf(out, "error: unable to open the file\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -166,105 +167,116 @@ static void do_mufs_cat(const char *s) |
|
|
|
|
mufs_close(file); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void do_mufs_append(const char *s) |
|
|
|
|
static void do_mufs_append(FILE *out, const char **argv, size_t argc) |
|
|
|
|
{ |
|
|
|
|
struct mufs_file *file; |
|
|
|
|
char *path, *line; |
|
|
|
|
char data[256]; |
|
|
|
|
size_t n; |
|
|
|
|
|
|
|
|
|
if (!mufs) { |
|
|
|
|
fprintf(stderr, "error: no file system mounted.\n"); |
|
|
|
|
if (argc < 2) { |
|
|
|
|
fprintf(out, "usage: mufs append <path> <line>\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!(path = malloc(strlen(s) + 2))) |
|
|
|
|
if (!mufs) { |
|
|
|
|
fprintf(out, "error: no file system mounted.\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
strcpy(path, s); |
|
|
|
|
|
|
|
|
|
if (!(line = strchr(path, ' '))) { |
|
|
|
|
fprintf(stderr, "usage: append <path> <line>\n"); |
|
|
|
|
goto err_free_path; |
|
|
|
|
if (!(file = mufs_open(mufs, argv[0], MUFS_WRITE | MUFS_APPEND))) { |
|
|
|
|
fprintf(out, "error: unable to open the file\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
*line++ = '\0'; |
|
|
|
|
n = strlen(argv[1]); |
|
|
|
|
|
|
|
|
|
if (!(file = mufs_open(mufs, path, MUFS_WRITE | MUFS_APPEND))) { |
|
|
|
|
fprintf(stderr, "error: unable to open the file\n"); |
|
|
|
|
goto err_free_path; |
|
|
|
|
} |
|
|
|
|
memcpy(data, argv[1], n); |
|
|
|
|
data[n] = '\n'; |
|
|
|
|
data[n + 1] = '\0'; |
|
|
|
|
|
|
|
|
|
strcpy(line + strlen(line), "\n"); |
|
|
|
|
mufs_write(file, line, strlen(line)); |
|
|
|
|
mufs_write(file, data, n + 1); |
|
|
|
|
mufs_close(file); |
|
|
|
|
|
|
|
|
|
err_free_path: |
|
|
|
|
free(path); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void do_mufs_mv(const char *s) |
|
|
|
|
static void do_mufs_mv(FILE *out, const char **argv, size_t argc) |
|
|
|
|
{ |
|
|
|
|
char *old, *new; |
|
|
|
|
|
|
|
|
|
if (!mufs) { |
|
|
|
|
fprintf(stderr, "error: no file system mounted.\n"); |
|
|
|
|
if (argc < 2) { |
|
|
|
|
fprintf(out, "usage: mufs mv <old> <new>\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!(old = strdup(s))) |
|
|
|
|
if (!mufs) { |
|
|
|
|
fprintf(out, "error: no file system mounted.\n"); |
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
if (!(new = strchr(old, ' '))) { |
|
|
|
|
fprintf(stderr, "usage: mv <old> <new>\n"); |
|
|
|
|
goto err_free_old; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
*new++ = '\0'; |
|
|
|
|
|
|
|
|
|
if (mufs_rename(mufs, old, new) < 0) { |
|
|
|
|
fprintf(stderr, "error: unable to move the file\n"); |
|
|
|
|
goto err_free_old; |
|
|
|
|
if (mufs_rename(mufs, argv[0], argv[1]) < 0) { |
|
|
|
|
fprintf(out, "error: unable to move the file\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err_free_old: |
|
|
|
|
free(old); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void do_mufs_rm(const char *s) |
|
|
|
|
static void do_mufs_rm(FILE *out, const char **argv, size_t argc) |
|
|
|
|
{ |
|
|
|
|
if (argc < 1) { |
|
|
|
|
fprintf(out, "usage: mufs rm <path>\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!mufs) { |
|
|
|
|
fprintf(stderr, "error: no file system mounted.\n"); |
|
|
|
|
fprintf(out, "error: no file system mounted.\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (mufs_unlink(mufs, s) < 0) { |
|
|
|
|
fprintf(stderr, "error: unable to remove the file\n"); |
|
|
|
|
if (mufs_unlink(mufs, argv[0]) < 0) { |
|
|
|
|
fprintf(out, "error: unable to remove the file\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void do_mufs_ls(const char *s) |
|
|
|
|
static void do_mufs_ls(FILE *out, const char **argv, size_t argc) |
|
|
|
|
{ |
|
|
|
|
struct mufs_dirent ent; |
|
|
|
|
struct mufs_dir *dir; |
|
|
|
|
const char *path = ""; |
|
|
|
|
|
|
|
|
|
if (argc >= 1) { |
|
|
|
|
path = argv[0]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!mufs) { |
|
|
|
|
fprintf(stderr, "error: no file system mounted.\n"); |
|
|
|
|
fprintf(out, "error: no file system mounted.\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!(dir = mufs_opendir(mufs, s))) { |
|
|
|
|
fprintf(stderr, "error: unable to open the directory\n"); |
|
|
|
|
if (!(dir = mufs_opendir(mufs, path))) { |
|
|
|
|
fprintf(out, "error: unable to open the directory\n"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
while (mufs_readdir(dir, &ent) == 0) { |
|
|
|
|
printf("%s\n", ent.path); |
|
|
|
|
fprintf(out, "%s\n", ent.path); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mufs_closedir(dir); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void do_mufs_cmd(const char *line) |
|
|
|
|
static struct cmd mufs_cmds[] = { |
|
|
|
|
{ "mount", NULL, do_mufs_mount }, |
|
|
|
|
{ "umount", NULL, do_mufs_unmount }, |
|
|
|
|
{ "format", NULL, do_mufs_format }, |
|
|
|
|
{ "mkdir", NULL, do_mufs_mkdir }, |
|
|
|
|
{ "rmdir", NULL, do_mufs_rmdir }, |
|
|
|
|
{ "ls", NULL, do_mufs_ls }, |
|
|
|
|
{ "stat", NULL, do_mufs_stat }, |
|
|
|
|
{ "cat", NULL, do_mufs_cat }, |
|
|
|
|
{ "append", NULL, do_mufs_append }, |
|
|
|
|
{ "mv", NULL, do_mufs_mv, }, |
|
|
|
|
{ "rm", NULL, do_mufs_rm, }, |
|
|
|
|
{ NULL, NULL, NULL }, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
void do_mufs_cmd(FILE *out, const char **argv, size_t argc) |
|
|
|
|
{ |
|
|
|
|
cmd_exec(mufs_cmds, line); |
|
|
|
|
cmd_exec(mufs_cmds, out, argv, argc); |
|
|
|
|
} |
|
|
|
|