]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
fat cp: DEBUG; flags p,n,f, (v);leading '/'
authorLeo C <erbl259-lmu@yahoo.de>
Fri, 13 Apr 2018 06:59:57 +0000 (08:59 +0200)
committerLeo C <erbl259-lmu@yahoo.de>
Fri, 13 Apr 2018 06:59:57 +0000 (08:59 +0200)
avr/cmd_fat.c
include/avr/ffconf.h

index cad7cdcfc4097f717820bafdaa040a55b6576b41..07499fe3466ce37b462e0d71af55846552666cd3 100644 (file)
 #include "env.h"
 #include "getopt-min.h"
 
+
+#define DEBUG_CP               0       /* set to 1 to debug */
+#define DEBUG_LS               0       /* set to 1 to debug */
+
+#define debug_cp(fmt, args...)                                                        \
+       debug_cond(DEBUG_CP, fmt, ##args)
+#define debug_ls(fmt, args...)                                                        \
+       debug_cond(DEBUG_LS, fmt, ##args)
+
+
 /* TODO: use memory size test function (detect_ramsize() in cmd_loadihex.c) */
 /* TODO: detect_ramsize() should be moved to z80-if.c */
 #define MAX_MEMORY     CONFIG_SYS_RAMSIZE_MAX
 #define BUFFER_SIZE    512
-#define MAXBSIZE       512 /* TODO */
 
 
 /*
@@ -296,7 +305,7 @@ command_ret_t do_ls(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc,
                pattern = strdup("*");
        strip_trailing_slash_relpath(path);
 
-//printf_P(PSTR("*: |%s| |%s|\n"), path ? path : "<NULL>", pattern ? pattern : "<NULL>");
+debug_ls("==== path: '%s', pattern: '%s'\n", path ? path : "<NULL>", pattern ? pattern : "<NULL>");
 
        p1 = s1 = s2 = 0;
        res = f_findfirst(&Dir, &Finfo, path, pattern);  /* Start to search for files */
@@ -343,14 +352,14 @@ command_ret_t do_tst(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc
 
        char * buf = (char *) malloc(BUFFER_SIZE);
        if (buf == NULL) {
-               printf_P(PSTR("pwd: Out of Memory!\n"));
+               printf_P(PSTR("tst: Out of Memory!\n"));
                free(buf);
                return CMD_RET_FAILURE;
        }
        res = f_getcwd(buf, BUFFER_SIZE);  /* Get current directory path */
 
        if (!res) {
-               printf_P(PSTR("cwd: |%s|\n"), buf);
+               printf_P(PSTR("cwd: '%s'\n"), buf);
        }
        free(buf);
        if (res) {
@@ -363,7 +372,7 @@ command_ret_t do_tst(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc
        else
                path = "";
 
-       printf_P(PSTR("arg: |%s|\n"), path);
+       printf_P(PSTR("arg: '%s'\n"), path);
        printf_P(PSTR("==== f_stat:      "));
        res = f_stat(path, &Finfo);
        put_rc(res);
@@ -436,8 +445,16 @@ typedef struct {
 
 static void strip_trailing_slash(PATH_T *p)
 {
-       while (p->p_end > p->p_path && p->p_end[-1] == '/')
-               *--p->p_end = '\0';
+       char *beg = p->p_path;
+       char *end = p->p_end;
+       if ((beg + 2) < end && (*beg & 0x38) == '0' &&  *(beg+1) == ':')
+               beg += 2;
+       if (beg < end && *beg == '/')
+               ++beg;
+       while (end > beg && end[-1] == '/')
+               *--end = '\0';
+
+       p->p_end =end;
 }
 
 /*
@@ -538,28 +555,25 @@ static int blockbuf_size;
 #define V_FLAG (1<<5)  // explain what is being done
 
 static void
-setfile(FILINFO *fs, FIL *fd)
+setfile(FILINFO *fs)
 {
-       (void) fs;(void) fd;
-#if 0 /* TODO: */
-       static struct timeval tv[2];
+       FRESULT fr;
 
-       fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
+       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);
+       if (fr != FR_OK)
+               err(PSTR("f_chmod: %s: %S"), to->p_path, rctostr(fr));
 
-       tv[0].tv_sec = fs->st_atime;
-       tv[1].tv_sec = fs->st_mtime;
-       if (utimes(to->p_path, tv))
-               err(PSTR("utimes: %s: %s"), to->p_path, strerror(errno));
-#endif
 }
 
 void copy_file(FILINFO *fs, uint_fast8_t dne)
 {
        FIL from_fd, to_fd;
        UINT rcount, wcount;
-       //FILINFO to_stat;
-       //char *p;
        FRESULT fr;
+       BYTE open_mode;
 
        if (blockbuf == NULL) {
                blockbuf_size = get_freemem() / 512 * 512;
@@ -571,8 +585,8 @@ void copy_file(FILINFO *fs, uint_fast8_t dne)
                }
        }
 
-debug("==== copy_file(): dne: %u, blockbuf_size: %d\n", dne, blockbuf_size);
-debug("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
+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);
 
 
        if ((fr = f_open(&from_fd, from->p_path, FA_READ)) != FR_OK) {
@@ -582,23 +596,33 @@ debug("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
 
        /*
         * If the file exists and we're interactive, verify with the user.
-        * If the file DNE, set the mode to be the from file, minus setuid
-        * bits, modified by the umask; arguably wrong, but it makes copying
-        * executables work right and it's been that way forever.  (The
-        * other choice is 666 or'ed with the execute bits on the from file
-        * modified by the umask.)
         */
        if (!dne) {
-               if (flags & I_FLAG) {
+               if (flags & N_FLAG) {
+                       if (flags & V_FLAG)
+                               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);
                        if (!confirm_yes()) {
                                f_close(&from_fd);
                                return;
                        }
                }
-               fr = f_open(&to_fd, to->p_path, FA_WRITE|FA_CREATE_ALWAYS);
-       } else
-               fr = f_open(&to_fd, to->p_path, FA_WRITE|FA_CREATE_NEW);
+               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);
+                       open_mode = FA_WRITE|FA_CREATE_NEW;
+               } else {
+                       /* Overwrite existing destination file name. */
+                       open_mode = FA_WRITE|FA_CREATE_ALWAYS;
+               }
+       } else {
+               open_mode = FA_WRITE|FA_CREATE_NEW;
+       }
+       fr = f_open(&to_fd, to->p_path, open_mode);
 
        if (fr != FR_OK) {
                err(PSTR("%s: %S"), to->p_path, rctostr(fr));
@@ -617,20 +641,19 @@ debug("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
        if (fr != FR_OK)
                err(PSTR("%s: S"), from->p_path, rctostr(fr));
 
-       if (flags & P_FLAG)
-               setfile(fs, &to_fd);
-
        f_close(&from_fd);
        if ((fr = f_close(&to_fd)) != FR_OK)
                err(PSTR("%s: %S"), to->p_path, rctostr(fr));
 
+       if (flags & P_FLAG)
+               setfile(fs);
 }
 
 #if 1
 static void copy_dir(void)
 {
-debug("==== copy_dir()");
-debug("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
+debug_cp("==== copy_dir()");
+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);
@@ -644,8 +667,8 @@ static void copy_dir(void)
        int dir_cnt, i;
        char *old_from, *old_to;
 
-debug("==== copy_file(): dne: %u\n", dne);
-debug("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
+debug_cp("==== copy_file(): dne: %u\n", dne);
+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) {
@@ -726,8 +749,8 @@ static void copy()
        uint_fast8_t dne;
        FRESULT fr;
 
-debug("==== copy()\n");
-debug("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
+debug_cp("==== copy()\n");
+debug_cp("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
 
        fr = f_stat(from->p_path, &from_stat);
        if (fr != FR_OK) {
@@ -770,7 +793,7 @@ debug("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
                }
                copy_dir();
                if (flags & P_FLAG)
-                       setfile(&from_stat, 0);
+                       setfile(&from_stat);
 
                return;
        }
@@ -793,8 +816,7 @@ command_ret_t do_cp(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc,
        optind = 0;
 
        int opt;
-//     while ((opt = getopt(argc, argv, PSTR("fiprv"))) != -1) {
-       while ((opt = getopt(argc, argv, PSTR("fiprv"))) != -1) {
+       while ((opt = getopt(argc, argv, PSTR("finprv"))) != -1) {
                switch (opt) {
                        case 'f':
                                tflags &= ~(I_FLAG | N_FLAG);
@@ -841,11 +863,9 @@ command_ret_t do_cp(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc,
        from->p_end = from->p_path; *from->p_path = '\0';
        to->p_end = to->p_path; *to->p_path = '\0';
 
-       /* consume last argument first. */
-       if (!path_set(to, argv[--argc])) {
-               command_ret = CMD_RET_FAILURE;
+       /* last argument is destination */
+       if (!path_set(to, argv[--argc]))
                goto cleanup;
-       }
 
        /*
         * Cp has two distinct cases:
@@ -863,8 +883,8 @@ command_ret_t do_cp(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc,
         */
 
        fr = f_stat(to->p_path, &to_stat);
-debug("==== main, stat to: fr: %d, attr: %02x, flags:%02x\n", fr, to_stat.fattrib, flags);
-debug("     from:'%s'  to:'%s'\n", from->p_path, to->p_path);
+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);
 
        if (fr != FR_OK && fr != FR_NO_FILE && fr != FR_NO_PATH) {
                err(PSTR("Test1: %s: %S"), to->p_path, rctostr(fr));
index eee4d784cc89e73c3da981ae7bdad5b85cfa898a..a7a908d5a53fbf3aa48cf411e01cd39220c68a92 100644 (file)
@@ -51,7 +51,7 @@
 /* This option switches f_expand function. (0:Disable or 1:Enable) */
 
 
-#define _USE_CHMOD             0
+#define _USE_CHMOD             1
 /* This option switches attribute manipulation functions, f_chmod() and f_utime().
 /  (0:Disable or 1:Enable) Also _FS_READONLY needs to be 0 to enable this option. */