summaryrefslogtreecommitdiff
path: root/avr/cmd_fat.c
diff options
context:
space:
mode:
Diffstat (limited to 'avr/cmd_fat.c')
-rw-r--r--avr/cmd_fat.c430
1 files changed, 224 insertions, 206 deletions
diff --git a/avr/cmd_fat.c b/avr/cmd_fat.c
index 7ab6536..18784e0 100644
--- a/avr/cmd_fat.c
+++ b/avr/cmd_fat.c
@@ -23,18 +23,15 @@ uint32_t fat_time(const struct tm * timeptr);
#include "env.h"
#include "getopt-min.h"
-#define DEBUG_FA 0 /* set to 1 to debug */
#define DEBUG_LS 0 /* set to 1 to debug */
#define DEBUG_RM 0 /* set to 1 to debug */
#define DEBUG_CP 0 /* set to 1 to debug */
-#define debug_fa(fmt, args...) \
- debug_cond(DEBUG_FA, fmt, ##args)
-#define debug_ls(fmt, args...) \
+#define debug_ls(fmt, args...) \
debug_cond(DEBUG_LS, fmt, ##args)
-#define debug_rm(fmt, args...) \
+#define debug_rm(fmt, args...) \
debug_cond(DEBUG_RM, fmt, ##args)
-#define debug_cp(fmt, args...) \
+#define debug_cp(fmt, args...) \
debug_cond(DEBUG_CP, fmt, ##args)
@@ -126,56 +123,6 @@ void swirl(void)
}
}
-/*
- * pwd - Print current directory of the current drive.
- *
- */
-command_ret_t do_pwd(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
-{
- FRESULT res;
- TCHAR buf[PATH_MAX];
-
- res = f_getcwd(buf, PATH_MAX); /* Get current directory path */
-
- if (res != FR_OK)
- cmd_error(CMD_RET_FAILURE, res, NULL);
-
- puts(buf);
-
- return CMD_RET_SUCCESS;
-}
-
-
-/*
- * cd - Change the current/working directory.
- *
- */
-command_ret_t do_cd(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
-{
- TCHAR *path;
- FRESULT res = FR_OK;
-
- if (argc < 2) {
- path = getenv_str(PSTR(ENV_HOME));
- if (path == NULL) {
- cmd_error(CMD_RET_FAILURE, 0, PSTR("\"%S\" not set"), PSTR(ENV_HOME));
- }
- } else
- path = argv[1];
-
- if (strlen(path) > 1 && path[1] == ':')
- res = f_chdrive(path);
-
- if (res == FR_OK)
- res = f_chdir(path);
-
- if (res != FR_OK)
- cmd_error(CMD_RET_FAILURE, res, NULL);
-
- return CMD_RET_SUCCESS;
-}
-
-
static
void print_dirent(FILINFO *f)
{
@@ -190,6 +137,7 @@ void print_dirent(FILINFO *f)
f->fsize, f->fname);
}
+static
char *path_split(char *p)
{
if (isdigit(p[0]) && (p[1] == ':'))
@@ -207,96 +155,6 @@ char *path_split(char *p)
return ps;
}
-command_ret_t do_mkdir(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
-{
- int ret = CMD_RET_SUCCESS;
- FRESULT res;
-
- if (argc < 1)
- return CMD_RET_USAGE;
-
- for (uint8_t i = 1; i < argc; i++) {
- if ((res = f_mkdir(argv[i])) != FR_OK) {
- ret = CMD_RET_FAILURE;
- cmd_error(0, res, PSTR("cannot create directory '%s'"), argv[i]);
- }
- }
- return ret;
-}
-
-/*
- * ls path - Directory listing
- *
- */
-command_ret_t do_ls(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
-{
- FATFS *fs;
- DIR dir; /* Directory object */
- FILINFO finfo;
- unsigned long p1;
- unsigned int s1, s2;
- FRESULT res = FR_OK;
- char buf[PATH_MAX];
- char *path = buf;
- char *pattern;
-
-
- memset(buf, 0, PATH_MAX);
- if (argc < 2) {
- buf[0] = '.';
- } else {
- strncpy(path, argv[1], PATH_MAX-1);
- }
- pattern = path_split(path);
- debug_ls("### path, pattern: '%s', '%s'\n", path[0] ? path : "<NULL>", pattern ? pattern : "<NULL>");
-
- if (pattern == NULL) {
- res = f_opendir(&dir, path);
- if (res == FR_NO_PATH || res == FR_INVALID_NAME) {
- pattern = path;
- path = ".";
- } else if (res != FR_OK) {
- cmd_error(CMD_RET_FAILURE, res, PSTR("'%s'"), path);
- }
- }
- debug_ls("### path, pattern: '%s', '%s'\n", path, pattern ? pattern : "<NULL>");
-
- if (pattern == NULL || *pattern == '\0')
- pattern = "*";
-
- debug_ls("### path, pattern: '%s', '%s'\n", path, pattern ? pattern : "<NULL>");
-
- res = f_findfirst(&dir, &finfo, path, pattern);
-
- p1 = s1 = s2 = 0;
- for(;;) {
- if (res != FR_OK)
- cmd_error(CMD_RET_FAILURE, res, NULL);
- if (!finfo.fname[0])
- break;
- if (finfo.fattrib & AM_DIR) {
- s2++;
- } else {
- s1++; p1 += finfo.fsize;
- }
- print_dirent(&finfo);
- if (check_abort())
- break;
- res = f_findnext(&dir, &finfo);
- }
-
- if (res == FR_OK) {
- printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1, p1, s2);
- if (f_getfree(path, (DWORD*)&p1, &fs) == FR_OK)
- printf_P(PSTR(", %10luK bytes free\n"), p1 * fs->csize / 2);
- }
-
- if (res)
- cmd_error(CMD_RET_FAILURE, res, NULL);
-
- return CMD_RET_SUCCESS;
-}
-
typedef void (*fatfunc_t)(const TCHAR* path_old, const TCHAR* path_new);
@@ -308,23 +166,11 @@ uint8_t cmd_flags;
#define P_FLAG (1<<4) // preserve attributes and timestamps
#define V_FLAG (1<<5) // explain what is being done
-char* splitpath(const char* path)
-{
- char* fs = strrchr(path, '/');
- char* bs = strrchr(path, '\\');
- if (fs > bs) {
- *fs = '\0';
- return fs + 1;
- } else if (bs != NULL) {
- *bs = '\0';
- return bs + 1;
- }
- return NULL;
-}
/*
* Copy File
*/
+static
FRESULT copy_file(const TCHAR* src, const TCHAR* dst)
{
FIL fsrc, fdst;
@@ -359,7 +205,6 @@ FRESULT copy_file(const TCHAR* src, const TCHAR* dst)
}
}
-debug_cp("==== copy() res: %d, br: %d, bw: %d\n", res, br, bw);
f_close(&fdst);
if (res != FR_OK)
f_unlink(dst);
@@ -372,6 +217,7 @@ debug_cp("==== copy() res: %d, br: %d, bw: %d\n", res, br, bw);
return res;
}
+static
void ff_remove(const TCHAR *file, const TCHAR *dest UNUSED)
{
FRESULT res = FR_OK;
@@ -393,6 +239,7 @@ void ff_remove(const TCHAR *file, const TCHAR *dest UNUSED)
return;
}
+static
void ff_copy(const TCHAR* path_old, const TCHAR* path_new)
{
FRESULT res;
@@ -408,6 +255,7 @@ void ff_copy(const TCHAR* path_old, const TCHAR* path_new)
return;
}
+static
void ff_move(const TCHAR* path_old, const TCHAR* path_new)
{
FRESULT res;
@@ -421,6 +269,7 @@ void ff_move(const TCHAR* path_old, const TCHAR* path_new)
return;
}
+static
void ff_iterate(fatfunc_t fatfunc, int count, char* const file[], char* dest)
{
FRESULT res = 0;
@@ -481,6 +330,146 @@ void ff_iterate(fatfunc_t fatfunc, int count, char* const file[], char* dest)
}
}
+/*
+ * pwd - Print current directory of the current drive.
+ *
+ */
+command_ret_t do_pwd(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
+{
+ FRESULT res;
+ TCHAR buf[PATH_MAX];
+
+ res = f_getcwd(buf, PATH_MAX); /* Get current directory path */
+
+ if (res != FR_OK)
+ cmd_error(CMD_RET_FAILURE, res, NULL);
+
+ puts(buf);
+
+ return CMD_RET_SUCCESS;
+}
+
+
+/*
+ * cd - Change the current/working directory.
+ *
+ */
+command_ret_t do_cd(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
+{
+ TCHAR *path;
+ FRESULT res = FR_OK;
+
+ if (argc < 2) {
+ path = getenv_str(PSTR(ENV_HOME));
+ if (path == NULL) {
+ cmd_error(CMD_RET_FAILURE, 0, PSTR("\"%S\" not set"), PSTR(ENV_HOME));
+ }
+ } else
+ path = argv[1];
+
+ if (strlen(path) > 1 && path[1] == ':')
+ res = f_chdrive(path);
+
+ if (res == FR_OK)
+ res = f_chdir(path);
+
+ if (res != FR_OK)
+ cmd_error(CMD_RET_FAILURE, res, NULL);
+
+ return CMD_RET_SUCCESS;
+}
+
+
+command_ret_t do_mkdir(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
+{
+ int ret = CMD_RET_SUCCESS;
+ FRESULT res;
+
+ if (argc < 1)
+ return CMD_RET_USAGE;
+
+ for (uint8_t i = 1; i < argc; i++) {
+ if ((res = f_mkdir(argv[i])) != FR_OK) {
+ ret = CMD_RET_FAILURE;
+ cmd_error(0, res, PSTR("cannot create directory '%s'"), argv[i]);
+ }
+ }
+ return ret;
+}
+
+/*
+ * ls path - Directory listing
+ *
+ */
+command_ret_t do_ls(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
+{
+ FATFS *fs;
+ DIR dir; /* Directory object */
+ FILINFO finfo;
+ unsigned long p1;
+ unsigned int s1, s2;
+ FRESULT res = FR_OK;
+ char buf[PATH_MAX];
+ char *path = buf;
+ char *pattern;
+
+
+ memset(buf, 0, PATH_MAX);
+ if (argc < 2) {
+ buf[0] = '.';
+ } else {
+ strncpy(path, argv[1], PATH_MAX-1);
+ }
+ pattern = path_split(path);
+ debug_ls("### path, pattern: '%s', '%s'\n", path[0] ? path : "<NULL>", pattern ? pattern : "<NULL>");
+
+ if (pattern == NULL) {
+ res = f_opendir(&dir, path);
+ if (res == FR_NO_PATH || res == FR_INVALID_NAME) {
+ pattern = path;
+ path = ".";
+ } else if (res != FR_OK) {
+ cmd_error(CMD_RET_FAILURE, res, PSTR("'%s'"), path);
+ }
+ }
+ debug_ls("### path, pattern: '%s', '%s'\n", path, pattern ? pattern : "<NULL>");
+
+ if (pattern == NULL || *pattern == '\0')
+ pattern = "*";
+
+ debug_ls("### path, pattern: '%s', '%s'\n", path, pattern ? pattern : "<NULL>");
+
+ res = f_findfirst(&dir, &finfo, path, pattern);
+
+ p1 = s1 = s2 = 0;
+ for(;;) {
+ if (res != FR_OK)
+ cmd_error(CMD_RET_FAILURE, res, NULL);
+ if (!finfo.fname[0])
+ break;
+ if (finfo.fattrib & AM_DIR) {
+ s2++;
+ } else {
+ s1++; p1 += finfo.fsize;
+ }
+ print_dirent(&finfo);
+ if (check_abort())
+ break;
+ res = f_findnext(&dir, &finfo);
+ }
+
+ if (res == FR_OK) {
+ printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1, p1, s2);
+ if (f_getfree(path, (DWORD*)&p1, &fs) == FR_OK)
+ printf_P(PSTR(", %10luK bytes free\n"), p1 * fs->csize / 2);
+ }
+
+ if (res)
+ cmd_error(CMD_RET_FAILURE, res, NULL);
+
+ return CMD_RET_SUCCESS;
+}
+
command_ret_t do_rm(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
{
exit_val = CMD_RET_SUCCESS;
@@ -543,47 +532,6 @@ command_ret_t do_cp_or_mv(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int
return exit_val;
}
-/*
- * tst path - for debugging: test access with different functions
- *
- */
-command_ret_t do_tst(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
-{
- DIR Dir; /* Directory object */
- FILINFO finfo;
- FRESULT res = FR_OK;
- char buf[BUFFER_SIZE];
- char *path = "";
- char *pattern = "*";
-
- printf_P(PSTR("sizeof DIR: %u, sizeof FIL: %u\n"), sizeof (DIR), sizeof (FILINFO));
-
- res = f_getcwd(buf, BUFFER_SIZE); /* Get current directory path */
-
- printf_P(PSTR("cwd: '%s', --> res: %d, %S\n"), buf, res, fat_rctostr(res));
-
- if (argc > 1)
- path = argv[1];
- if (argc > 2)
- pattern = argv[2];
- printf_P(PSTR("arg: '%s' '%s'\n"), path, pattern);
-
- res = f_stat(path, &finfo);
- printf_P(PSTR("f_stat: --> res: %d, %S\n"), res, fat_rctostr(res));
- if (res == FR_OK) print_dirent(&finfo);
-
- res = f_opendir(&Dir, path);
- printf_P(PSTR("f_opendir: --> res: %d, %S\n"), res, fat_rctostr(res));
- f_closedir(&Dir);
-
- res = f_findfirst(&Dir, &finfo, path, pattern);
- printf_P(PSTR("f_findfirst: --> res: %d, %S\n"), res, fat_rctostr(res));
- if (res == FR_OK) print_dirent(&finfo);
- f_closedir(&Dir);
-
- return CMD_RET_SUCCESS;
-}
-
/* Work register for fs command */
struct stat_dat_s {
DWORD AccSize;
@@ -705,6 +653,47 @@ command_ret_t do_stat(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int arg
}
/*
+ * tst path - for debugging: test access with different functions
+ *
+ */
+command_ret_t do_tst(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
+{
+ DIR Dir; /* Directory object */
+ FILINFO finfo;
+ FRESULT res = FR_OK;
+ char buf[BUFFER_SIZE];
+ char *path = "";
+ char *pattern = "*";
+
+ printf_P(PSTR("sizeof DIR: %u, sizeof FIL: %u\n"), sizeof (DIR), sizeof (FILINFO));
+
+ res = f_getcwd(buf, BUFFER_SIZE); /* Get current directory path */
+
+ printf_P(PSTR("cwd: '%s', --> res: %d, %S\n"), buf, res, fat_rctostr(res));
+
+ if (argc > 1)
+ path = argv[1];
+ if (argc > 2)
+ pattern = argv[2];
+ printf_P(PSTR("arg: '%s' '%s'\n"), path, pattern);
+
+ res = f_stat(path, &finfo);
+ printf_P(PSTR("f_stat: --> res: %d, %S\n"), res, fat_rctostr(res));
+ if (res == FR_OK) print_dirent(&finfo);
+
+ res = f_opendir(&Dir, path);
+ printf_P(PSTR("f_opendir: --> res: %d, %S\n"), res, fat_rctostr(res));
+ f_closedir(&Dir);
+
+ res = f_findfirst(&Dir, &finfo, path, pattern);
+ printf_P(PSTR("f_findfirst: --> res: %d, %S\n"), res, fat_rctostr(res));
+ if (res == FR_OK) print_dirent(&finfo);
+ f_closedir(&Dir);
+
+ return CMD_RET_SUCCESS;
+}
+
+/*
* fatread/write - load binary file to/from a dos filesystem
* read <d:/path/filename> <addr> [bytes [pos]]
* write <d:/path/filename> <addr> <bytes>
@@ -841,17 +830,25 @@ CMD_TBL_ITEM(
"DIRECTORY..."
),
CMD_TBL_ITEM(
+ mkdir, CONFIG_SYS_MAXARGS, 0|CTBL_SUBCMDAUTO, do_mkdir,
+ "Create the DIRECTORY(ies), if they do not already exist.",
+ "DIRECTORY..."
+),
+CMD_TBL_ITEM(
ls, 2, CTBL_RPT|CTBL_SUBCMDAUTO, do_ls,
"Directory listing",
"path"
),
CMD_TBL_ITEM(
- tst, 3, CTBL_DBG|CTBL_RPT, do_tst,
- "FatFS test function",
- "[path [pattern]]"
+ rm, CONFIG_SYS_MAXARGS, 0, do_rm,
+ "Remove FILE(s) or empty DIRECTORY(ies)",
+ "[OPTION]... [FILE]...\n"
+ //" -i prompt before removal\n"
+ " -n do not remove\n"
+ " -v explain what is being done"
),
CMD_TBL_ITEM(
- rm, CONFIG_SYS_MAXARGS, 0, do_rm,
+ rm, CONFIG_SYS_MAXARGS, 0|CTBL_SUBCMDAUTO, do_rm,
"Remove FILE(s) or empty DIRECTORY(ies)",
"[OPTION]... [FILE]...\n"
//" -i prompt before removal\n"
@@ -870,12 +867,33 @@ CMD_TBL_ITEM(
" -v explain what is being done"
),
CMD_TBL_ITEM(
+ cp, CONFIG_SYS_MAXARGS, 0|CTBL_SUBCMDAUTO, do_cp_or_mv,
+ "Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.",
+ "[OPTION]... SOURCE... DEST\n"
+// " -f overwrite existing file ignoring write protection\n"
+// " this option is ignored when the -n option is also used\n"
+// " -i prompt before overwrite (overrides a previous -n option)\n"
+// " -n do not overwrite an existing file (overrides a previous -i option)\n"
+// " -p preserve attributes and timestamps\n"
+ " -v explain what is being done"
+),
+CMD_TBL_ITEM(
mv, CONFIG_SYS_MAXARGS, 0, do_cp_or_mv,
"Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.",
"[OPTION]... SOURCE DEST\n"
" -v explain what is being done"
),
-
+CMD_TBL_ITEM(
+ mv, CONFIG_SYS_MAXARGS, 0|CTBL_SUBCMDAUTO, do_cp_or_mv,
+ "Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.",
+ "[OPTION]... SOURCE DEST\n"
+ " -v explain what is being done"
+),
+CMD_TBL_ITEM(
+ tst, 3, CTBL_DBG|CTBL_RPT, do_tst,
+ "FatFS test function",
+ "[path [pattern]]"
+),
CMD_TBL_ITEM(
load, 5, 0, do_rw,
"load binary file from a dos filesystem",