From 6cf1cf171b0a74917c3c6ba60dcacbce8fad459e Mon Sep 17 00:00:00 2001 From: Leo C. Date: Thu, 1 Aug 2024 14:01:36 +0200 Subject: fat cp/mv: handle rootdir as dest correctly --- avr/cmd_fat.c | 48 +++++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 27 deletions(-) (limited to 'avr/cmd_fat.c') diff --git a/avr/cmd_fat.c b/avr/cmd_fat.c index 41821e1..7ab6536 100644 --- a/avr/cmd_fat.c +++ b/avr/cmd_fat.c @@ -427,8 +427,16 @@ void ff_iterate(fatfunc_t fatfunc, int count, char* const file[], char* dest) DIR dir; FILINFO finfo; char srcpath[PATH_MAX], destpath[PATH_MAX]; + uint8_t dest_is_dir = 0; + + if (dest != NULL) { + if (f_opendir(&dir, dest) == FR_OK) { + dest_is_dir = 1; + f_closedir(&dir); + } else if (f_stat(dest, &finfo) == FR_OK && finfo.fattrib & AM_DIR) + dest_is_dir = 1; + } - uint8_t dest_is_dir = dest != NULL && f_stat(dest, &finfo) == FR_OK && finfo.fattrib & AM_DIR; for (uint8_t i = 0; i < count; i++) { char* pattern = NULL; strcpy(srcpath, file[i]); @@ -457,7 +465,8 @@ void ff_iterate(fatfunc_t fatfunc, int count, char* const file[], char* dest) if (dest != NULL) { strcpy(destpath, dest); if (dest_is_dir) { - strcat_P(destpath, PSTR("/")); + if (destpath[strlen(destpath) - 1] != '/') + strcat_P(destpath, PSTR("/")); strcat(destpath, finfo.fname); } } @@ -543,48 +552,33 @@ command_ret_t do_tst(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc 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)); - char * buf = (char *) malloc(BUFFER_SIZE); - if (buf == NULL) { - cmd_error(CMD_RET_FAILURE, ENOMEM, NULL); - } res = f_getcwd(buf, BUFFER_SIZE); /* Get current directory path */ - if (!res) { - printf_P(PSTR("cwd: '%s'\n"), buf); - } - free(buf); - if (res) - cmd_error(CMD_RET_FAILURE, res, NULL); + 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); - printf_P(PSTR("==== f_stat: ")); + res = f_stat(path, &finfo); - cmd_error(0, res, NULL); - if (res == FR_OK) { - print_dirent(&finfo); - } + printf_P(PSTR("f_stat: --> res: %d, %S\n"), res, fat_rctostr(res)); + if (res == FR_OK) print_dirent(&finfo); - printf_P(PSTR("==== f_findfirst: ")); - res = f_findfirst(&Dir, &finfo, path, pattern); /* Start to search for files */ - cmd_error(CMD_RET_FAILURE, res, NULL); - 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); - printf_P(PSTR("==== f_opendir: ")); - res = f_opendir(&Dir, path); - cmd_error(CMD_RET_FAILURE, res, NULL); + 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; -- cgit v1.2.3