]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
fat cp: copy_dir (untested)
authorLeo C <leo@lenti.loc>
Mon, 16 Apr 2018 19:28:50 +0000 (21:28 +0200)
committerLeo C <leo@lenti.loc>
Mon, 16 Apr 2018 19:28:50 +0000 (21:28 +0200)
avr/cmd_fat.c

index 07499fe3466ce37b462e0d71af55846552666cd3..8acbf8af173052df64a58fd84ebbd08b920b19e6 100644 (file)
@@ -540,10 +540,10 @@ char *path_basename(PATH_T *p)
 
 
 static uint8_t flags;
-PATH_T from_static;
-PATH_T to_static;
-PATH_T *from = &from_static;
-PATH_T *to = &to_static;
+PATH_T from;
+PATH_T to;
+//PATH_T *from = &from_static;
+//PATH_T *to = &to_static;
 static uint8_t *blockbuf;
 static int blockbuf_size;
 
@@ -559,12 +559,12 @@ setfile(FILINFO *fs)
 {
        FRESULT fr;
 
-       fr = f_utime(to->p_path, fs);
+       fr = f_utime(to.p_path, fs);
        if (fr != FR_OK)
-               err(PSTR("f_utime: %s: %S"), to->p_path, rctostr(fr));
-       fr = f_chmod(to->p_path, fs->fattrib, AM_RDO|AM_ARC|AM_SYS|AM_HID);
+               err(PSTR("f_utime: %s: %S"), to.p_path, rctostr(fr));
+       fr = f_chmod(to.p_path, fs->fattrib, AM_RDO|AM_ARC|AM_SYS|AM_HID);
        if (fr != FR_OK)
-               err(PSTR("f_chmod: %s: %S"), to->p_path, rctostr(fr));
+               err(PSTR("f_chmod: %s: %S"), to.p_path, rctostr(fr));
 
 }
 
@@ -586,11 +586,11 @@ void copy_file(FILINFO *fs, uint_fast8_t dne)
        }
 
 debug_cp("==== copy_file(): dne: %u, blockbuf_size: %d\n", dne, blockbuf_size);
-debug_cp("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
+debug_cp("     from:'%s'  to:'%s'\n", from.p_path, to.p_path);
 
 
-       if ((fr = f_open(&from_fd, from->p_path, FA_READ)) != FR_OK) {
-               err(PSTR("%s: %S"), from->p_path, rctostr(fr));
+       if ((fr = f_open(&from_fd, from.p_path, FA_READ)) != FR_OK) {
+               err(PSTR("%s: %S"), from.p_path, rctostr(fr));
                return;
        }
 
@@ -600,11 +600,11 @@ debug_cp("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
        if (!dne) {
                if (flags & N_FLAG) {
                        if (flags & V_FLAG)
-                               printf_P(PSTR("%s not overwritten\n"), to->p_path);
+                               printf_P(PSTR("%s not overwritten\n"), to.p_path);
                        f_close(&from_fd);
                        return;
                } if (flags & I_FLAG) {
-                       printf_P(PSTR("overwrite '%s'? "), to->p_path);
+                       printf_P(PSTR("overwrite '%s'? "), to.p_path);
                        if (!confirm_yes()) {
                                f_close(&from_fd);
                                return;
@@ -612,8 +612,8 @@ debug_cp("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
                }
                if (flags & F_FLAG) {
                        /* Remove existing destination file name create a new file. */
-                       f_chmod(to->p_path,0, AM_RDO);
-                       f_unlink(to->p_path);
+                       f_chmod(to.p_path,0, AM_RDO);
+                       f_unlink(to.p_path);
                        open_mode = FA_WRITE|FA_CREATE_NEW;
                } else {
                        /* Overwrite existing destination file name. */
@@ -622,10 +622,10 @@ debug_cp("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
        } else {
                open_mode = FA_WRITE|FA_CREATE_NEW;
        }
-       fr = f_open(&to_fd, to->p_path, open_mode);
+       fr = f_open(&to_fd, to.p_path, open_mode);
 
        if (fr != FR_OK) {
-               err(PSTR("%s: %S"), to->p_path, rctostr(fr));
+               err(PSTR("%s: %S"), to.p_path, rctostr(fr));
                f_close(&from_fd);
                return;
        }
@@ -634,26 +634,28 @@ debug_cp("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
                                                                                        rcount > 0) {
                fr = f_write(&to_fd, blockbuf, rcount, &wcount);
                if (fr || wcount < rcount) {
-                       err(PSTR("%s: %S"), to->p_path, rctostr(fr));
+                       err(PSTR("%s: %S"), to.p_path, rctostr(fr));
                        break;
                }
        }
        if (fr != FR_OK)
-               err(PSTR("%s: S"), from->p_path, rctostr(fr));
+               err(PSTR("%s: S"), from.p_path, rctostr(fr));
 
        f_close(&from_fd);
        if ((fr = f_close(&to_fd)) != FR_OK)
-               err(PSTR("%s: %S"), to->p_path, rctostr(fr));
+               err(PSTR("%s: %S"), to.p_path, rctostr(fr));
 
        if (flags & P_FLAG)
                setfile(fs);
 }
 
-#if 1
+static void copy();
+
+#if 0
 static void copy_dir(void)
 {
 debug_cp("==== copy_dir()");
-debug_cp("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
+debug_cp("     from:'%s'  to:'%s'\n", from->p_path, to.p_path);
 
        printf_P(PSTR("directory copy not supported, ommitting dir '%s'\n"),
                from->p_path);
@@ -661,6 +663,48 @@ debug_cp("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
 }
 #else
 static void copy_dir(void)
+{
+       DIR Dir;                                        /* Directory object */
+       FILINFO Finfo;
+       char *old_from, *old_to;
+       FRESULT res;
+       char *pattern = {"*"};
+
+debug_cp("==== copy_dir()");
+debug_cp("     from:'%s'  to:'%s'\n", from.p_path, to.p_path);
+
+
+       for (res = f_findfirst(&Dir, &Finfo, from.p_path, pattern);
+                res == FR_OK && Finfo.fname[0];
+                res = f_findfirst(&Dir, &Finfo, from.p_path, pattern)) {
+
+               if (!(Finfo.fattrib & AM_DIR) &&
+                               (old_from = path_append(&from, Finfo.fname, 0))) {
+                       if ((old_to = path_append(&to, Finfo.fname, 0))) {
+                               copy();
+                               path_restore(&to, old_to);
+                       }
+                       path_restore(&from, old_from);
+               }
+       }
+
+       for (res = f_findfirst(&Dir, &Finfo, from.p_path, pattern);
+                res == FR_OK && Finfo.fname[0];
+                res = f_findnext(&Dir, &Finfo)) {
+
+               if ((Finfo.fattrib & AM_DIR) &&
+                               (old_from = path_append(&from, Finfo.fname, 0))) {
+                       if ((old_to = path_append(&to, Finfo.fname, 0))) {
+                               copy();
+                               path_restore(&to, old_to);
+                       }
+                       path_restore(&from, old_from);
+               }
+       }
+}
+#endif
+#if 0
+static void copy_dir(void)
 {
        FILINFO from_stat;
        struct dirent *dp, **dir_list;
@@ -668,7 +712,7 @@ static void copy_dir(void)
        char *old_from, *old_to;
 
 debug_cp("==== copy_file(): dne: %u\n", dne);
-debug_cp("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
+debug_cp("     from:'%s'  to:'%s'\n", from->p_path, to.p_path);
 
        dir_cnt = scandir(from->p_path, &dir_list, NULL, NULL);
        if (dir_cnt == -1) {
@@ -750,21 +794,21 @@ static void copy()
        FRESULT fr;
 
 debug_cp("==== copy()\n");
-debug_cp("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
+debug_cp("     from:'%s'  to:'%s'\n", from.p_path, to.p_path);
 
-       fr = f_stat(from->p_path, &from_stat);
+       fr = f_stat(from.p_path, &from_stat);
        if (fr != FR_OK) {
-               err(PSTR("%s: %S"), from->p_path, rctostr(fr));
+               err(PSTR("%s: %S"), from.p_path, rctostr(fr));
                return;
        }
 
        /* not an error, but need to remember it happened */
-       if (f_stat(to->p_path, &to_stat) != FR_OK)
+       if (f_stat(to.p_path, &to_stat) != FR_OK)
                dne = 1;
        else {
-               if (strcmp(to->p_path, from->p_path) == 0) {
+               if (strcmp(to.p_path, from.p_path) == 0) {
                        (void)printf_P(PSTR("%s and %s are identical (not copied).\n"),
-                                       to->p_path, from->p_path);
+                                       to.p_path, from.p_path);
                        command_ret = CMD_RET_FAILURE;
                        return;
                }
@@ -774,7 +818,7 @@ debug_cp("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
        if(from_stat.fattrib & AM_DIR) {
                if (!(flags & R_FLAG)) {
                        (void)printf_P(PSTR("-r not specified; ommitting dir '%s'\n"),
-                           from->p_path);
+                           from.p_path);
                        command_ret = CMD_RET_FAILURE;
                        return;
                }
@@ -782,13 +826,13 @@ debug_cp("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
                        /*
                         * If the directory doesn't exist, create the new one.
                         */
-                       if ((fr = f_mkdir(to->p_path)) != FR_OK) {
-                               err(PSTR("%s: %S"), to->p_path, rctostr(fr));
+                       if ((fr = f_mkdir(to.p_path)) != FR_OK) {
+                               err(PSTR("%s: %S"), to.p_path, rctostr(fr));
                                return;
                        }
                }
                else if (!(to_stat.fattrib & AM_DIR)) {
-                       (void)printf_P(PSTR("%s: not a directory.\n"), to->p_path);
+                       (void)printf_P(PSTR("%s: not a directory.\n"), to.p_path);
                        return;
                }
                copy_dir();
@@ -860,11 +904,11 @@ command_ret_t do_cp(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc,
                goto cleanup;
        }
 #endif
-       from->p_end = from->p_path; *from->p_path = '\0';
-       to->p_end = to->p_path; *to->p_path = '\0';
+       from.p_end = from.p_path; from.p_path[0] = '\0';
+       to.p_end = to.p_path; to.p_path[0] = '\0';
 
        /* last argument is destination */
-       if (!path_set(to, argv[--argc]))
+       if (!path_set(&to, argv[--argc]))
                goto cleanup;
 
        /*
@@ -882,12 +926,12 @@ command_ret_t do_cp(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc,
         * In (2), the real target is not directory, but "directory/source".
         */
 
-       fr = f_stat(to->p_path, &to_stat);
+       fr = f_stat(to.p_path, &to_stat);
 debug_cp("==== main, stat to: fr: %d, attr: %02x, flags:%02x\n", fr, to_stat.fattrib, flags);
-debug_cp("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
+debug_cp("     from:'%s'  to:'%s'\n", from.p_path, to.p_path);
 
        if (fr != FR_OK && fr != FR_NO_FILE && fr != FR_NO_PATH) {
-               err(PSTR("Test1: %s: %S"), to->p_path, rctostr(fr));
+               err(PSTR("Test1: %s: %S"), to.p_path, rctostr(fr));
                command_ret = CMD_RET_FAILURE;
                goto cleanup;
        }
@@ -896,11 +940,11 @@ debug_cp("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
                 * Case (1).  Target is not a directory.
                 */
                if (argc > 1) {
-                       err(PSTR("target '%s' is not a directory"), to->p_path);
+                       err(PSTR("target '%s' is not a directory"), to.p_path);
                        //command_ret = CMD_RET_USAGE;
                        goto cleanup;
                }
-               if (!path_set(from, *argv)) {
+               if (!path_set(&from, *argv)) {
                        command_ret = CMD_RET_FAILURE;
                        goto cleanup;
                }
@@ -911,14 +955,14 @@ debug_cp("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
                 * Case (2).  Target is a directory.
                 */
                for (;; ++argv) {
-                       if (!path_set(from, *argv))
+                       if (!path_set(&from, *argv))
                                continue;
-                       if (!(old_to = path_append(to, path_basename(from), -1)))
+                       if (!(old_to = path_append(&to, path_basename(&from), -1)))
                                continue;
                        copy();
                        if (!--argc)
                                break;
-                       path_restore(to, old_to);
+                       path_restore(&to, old_to);
                }
        }
 
@@ -932,7 +976,7 @@ cleanup:
 
 #if 0
                if (flags & V_FLAG)
-                       printf_P((PSTR("%s  %s -> %s\n", badcp ? : "ERR:" : "    ", curr->fts_path, to->p_path)));
+                       printf_P((PSTR("%s  %s -> %s\n", badcp ? : "ERR:" : "    ", curr->fts_path, to.p_path)));
 
 #endif