]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
fat cp/mv: handle rootdir as dest correctly
authorLeo C. <erbl259-lmu@yahoo.de>
Thu, 1 Aug 2024 12:01:36 +0000 (14:01 +0200)
committerLeo C. <erbl259-lmu@yahoo.de>
Thu, 1 Aug 2024 12:01:36 +0000 (14:01 +0200)
avr/cmd_fat.c

index 41821e11584d0a7fcde879fd3689d21f73a6a2be..7ab65365710e4d5d2719020f7c5972ff79355bd0 100644 (file)
@@ -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;