FATFS Copy Files 您所在的位置:网站首页 wchat文件夹 FATFS Copy Files

FATFS Copy Files

2024-07-14 00:20| 来源: 网络整理| 查看: 265

1 // 文件复制 2 // 将psrc文件,copy到pdst. 3 // psrc,pdst:源文件和目标文件 4 // fwmode:文件写入模式 5 // 0:不覆盖原有的文件 6 // 1:覆盖原有的文件 7 u8 mf_copy ( u8 * psrc, u8 * pdst, u8 fwmode ) 8 { 9 u8 res; 10 u16 br = 0; 11 u16 bw = 0; 12 FIL * fsrc = 0; 13 FIL * fdst = 0; 14 u8 * fbuf = 0; 15 fsrc = ( FIL* )mymalloc ( SRAMIN, sizeof( FIL ) ); // 申请内存 16 fdst = ( FIL* )mymalloc ( SRAMIN, sizeof( FIL ) ); 17 fbuf = ( u8* )mymalloc ( SRAMIN, 512 ); 18 if ( fsrc == NULL || fdst == NULL || fbuf == NULL ) 19 { 20 res = 100; 21 } // 前面的值留给fatfs 22 else 23 { 24 if ( fwmode == 0 ) 25 { 26 fwmode = FA_CREATE_NEW; 27 } // 不覆盖 28 else 29 { 30 fwmode = FA_CREATE_ALWAYS; 31 } // 覆盖存在的文件 32 33 res = f_open ( fsrc, ( const TCHAR * )psrc, FA_READ | FA_OPEN_EXISTING ); 34 // 打开只读文件 35 if ( res == 0 ) 36 { 37 res = f_open ( fdst, ( const TCHAR * )pdst, FA_WRITE | fwmode ); 38 } // 第一个打开成功,才开始打开第二个 39 if ( res == 0 ) // 两个都打开成功了 40 { 41 while ( res == 0 ) // 开始复制 42 { 43 res = f_read ( fsrc, fbuf, 512, ( UINT* )&br ); // 源头读出512字节 44 if ( res || br == 0 ) 45 { 46 break; 47 } 48 res = f_write ( fdst, fbuf, ( UINT )br, ( UINT* )&bw ); // 写入目的文件 49 if ( res || bw lfsize = _MAX_LFN * 2 + 1; 119 finfo->lfname = mymalloc ( SRAMIN, finfo->lfsize ); // 申请内存 120 dstpathname = mymalloc ( SRAMIN, MAX_PATHNAME_DEPTH ); 121 srcpathname = mymalloc ( SRAMIN, MAX_PATHNAME_DEPTH ); 122 if ( finfo->lfname == NULL || dstpathname == NULL || srcpathname == NULL ) 123 { 124 res = 101; 125 } 126 if ( res == 0 ) 127 { 128 dstpathname[ 0 ] = 0; 129 srcpathname[ 0 ] = 0; 130 strcat ( ( char * )srcpathname, ( const char * )psrc ); // 复制原始源文件路径 131 strcat ( ( char * )dstpathname, ( const char * )pdst ); // 复制原始目标文件路径 132 res = f_opendir ( srcdir, ( const TCHAR * )psrc ); // 打开源目录 133 if ( res == 0 ) // 打开目录成功 134 { 135 strcat ( ( char * )dstpathname, ( const char * )"/" ); // 加入斜杠 136 fn = get_src_dname ( psrc ); 137 if ( fn == 0 ) // 卷标拷贝 138 { 139 dstpathlen = strlen ( ( const char * )dstpathname ); 140 dstpathname[ dstpathlen ] = psrc[ 0 ]; // 记录卷标 141 dstpathname[ dstpathlen + 1 ] = 0; // 结束符 142 } 143 else 144 { 145 strcat ( ( char * )dstpathname, ( const char * )fn ); 146 } // 加文件名 147 res = f_mkdir ( ( const TCHAR * )dstpathname ); 148 // 如果文件夹已经存在,就不创建.如果不存在就创建新的文件夹. 149 if ( res == FR_EXIST ) 150 { 151 res = 0; 152 } 153 while ( res == 0 ) // 开始复制文件夹里面的东东 154 { 155 res = f_readdir ( srcdir, finfo ); // 读取目录下的一个文件 156 if ( res != FR_OK || finfo->fname[ 0 ] == 0 ) 157 { 158 break; 159 } // 错误了/到末尾了,退出 160 if ( finfo->fname[ 0 ] == '.' ) 161 { 162 continue; 163 } // 忽略上级目录 164 fn = ( u8* )( *finfo->lfname ? finfo->lfname : finfo->fname ); 165 // 得到文件名 166 dstpathlen = strlen ( ( const char * )dstpathname ); // 得到当前目标路径的长度 167 srcpathlen = strlen ( ( const char * )srcpathname ); // 得到源路径长度 168 169 strcat ( ( char * )srcpathname, ( const char * )"/" ); // 源路径加斜杠 170 if ( finfo->fattrib & 0X10 ) // 是子目录 文件属性,0X20,归档文件;0X10,子目录; 171 { 172 strcat ( ( char * )srcpathname, ( const char * )fn ); // 源路径加上子目录名字 173 printf ( "\r\ncopy folder %s to %s\r\n", srcpathname, 174 dstpathname ); // 拷贝文件 175 res = mf_dcopy ( srcpathname, dstpathname, fwmode ); // 拷贝文件夹 176 } 177 else // 非目录 178 { 179 strcat ( ( char * )dstpathname, ( const char * )"/" ); // 目标路径加斜杠 180 strcat ( ( char * )dstpathname, ( const char * )fn ); // 目标路径加文件名 181 strcat ( ( char * )srcpathname, ( const char * )fn ); // 源路径加文件名 182 printf ( "\r\ncopy file %s to %s\r\n", srcpathname, 183 dstpathname ); // 拷贝文件 184 mf_copy ( srcpathname, dstpathname, fwmode ); // 复制文件 185 } 186 srcpathname[ srcpathlen ] = 0; // 加入结束符 187 dstpathname[ dstpathlen ] = 0; // 加入结束符 188 } 189 } 190 myfree ( SRAMIN, dstpathname ); 191 myfree ( SRAMIN, srcpathname ); 192 myfree ( SRAMIN, finfo->lfname ); 193 } 194 } 195 myfree ( SRAMIN, srcdir ); 196 myfree ( SRAMIN, dstdir ); 197 myfree ( SRAMIN, finfo ); 198 return res; 199 }

http://www.openedv.com/posts/list/6567.htm



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有