FATFS文件系统复制文件

原帖地址:
刚刚完成了fatfs下面的文件夹复制,貌似没有问题,Traids之前说的文件夹复制有问题,可以参考一下我这个代码

验证了一下SYSTEM文件夹的COPY,没有问题.
以下代码需要用到malloc,在论坛我已经发帖过,可以搜索一下,或者使用系统的malloc.

//文件复制
//将psrc文件,copy到pdst.
//psrc,pdst:源文件和目标文件
//fwmode:文件写入模式
//0:不覆盖原有的文件
//1:覆盖原有的文件
u8 mf_copy(u8 *psrc,u8 *pdst,u8 fwmode)
{
 u8 res;
    u16 br=0;
 u16 bw=0;
 FIL *fsrc=0;
 FIL *fdst=0;
 u8 *fbuf=0;
  fsrc=(FIL*)mymalloc(SRAMIN,sizeof(FIL));//申请内存
  fdst=(FIL*)mymalloc(SRAMIN,sizeof(FIL));
 fbuf=(u8*)mymalloc(SRAMIN,512);
   if(fsrc==NULL||fdst==NULL||fbuf==NULL)res=100;//前面的值留给fatfs
 else
  
  if(fwmode==0)fwmode=FA_CREATE_NEW;//不覆盖
  else fwmode=FA_CREATE_ALWAYS;   //覆盖存在的文件
  
   res=f_open(fsrc,(const TCHAR*)psrc,FA_READ|FA_OPEN_EXISTING); //打开只读文件
   if(res==0)res=f_open(fdst,(const TCHAR*)pdst,FA_WRITE|fwmode);  //第一个打开成功,才开始打开第二个
  if(res==0)//两个都打开成功了
  {
    while(res==0)//开始复制
   {
    res=f_read(fsrc,fbuf,512,(UINT*)&br); //源头读出512字节
    if(res||br==0)break;
    res=f_write(fdst,fbuf,(UINT)br,(UINT*)&bw); //写入目的文件
    if(res||bw

   }
      f_close(fsrc);
      f_close(fdst);
  }
 }
 myfree(SRAMIN,fsrc);//释放内存
 myfree(SRAMIN,fdst);
 myfree(SRAMIN,fbuf);
 return res;
}

//得到路径下的文件夹
//返回值:0,路径就是个卷标号.
//    其他,文件夹名字首地址
u8* get_src_dname(u8* dpfn)
{
 u16 temp=0;
  while(*dpfn!=0)
 {
  dpfn++;
  temp++; 
 }
 if(temp<4)return 0;
 while((*dpfn!=0x5c)&&(*dpfn!=0x2f))dpfn--; //追述到倒数第一个"\"或者"/"处
 return ++dpfn;
}
//文件夹复制
//将psrc文件夹,copy到pdst文件夹.
//pdst:必须形如"X:"/"X:XX"/"X:XX/XX"之类的.而且要实现确认上一级文件夹存在
//psrc,pdst:源文件夹和目标文件夹
//fwmode:文件写入模式
//0:不覆盖原有的文件
//1:覆盖原有的文件
u8 mf_dcopy(u8 *psrc,u8 *pdst,u8 fwmode)
{
#define MAX_PATHNAME_DEPTH 512+1 //最大目标文件路径+文件名深度
 u8 res=0;  
    DIR *srcdir=0;  //源目录
 DIR *dstdir=0;  //源目录
 FILINFO *finfo=0; //文件信息
 u8 *fn=0;     //长文件名

 u8 * dstpathname=0; //目标文件夹路径+文件名
 u8 * srcpathname=0; //源文件夹路径+文件名
 
  u16 dstpathlen=0; //目标路径长度
  u16 srcpathlen=0; //源路径长度

 
 srcdir=(DIR*)mymalloc(SRAMIN,sizeof(DIR));//申请内存
  dstdir=(DIR*)mymalloc(SRAMIN,sizeof(DIR));
 finfo=(FILINFO*)mymalloc(SRAMIN,sizeof(FILINFO));

    if(srcdir==NULL||dstdir==NULL||finfo==NULL)res=100;
 if(res==0)
 {
     finfo->lfsize=_MAX_LFN*2+1;
  finfo->lfname=mymalloc(SRAMIN,finfo->lfsize);//申请内存
   dstpathname=mymalloc(SRAMIN,MAX_PATHNAME_DEPTH);
  srcpathname=mymalloc(SRAMIN,MAX_PATHNAME_DEPTH);
   if(finfo->lfname==NULL||dstpathname==NULL||srcpathname==NULL)res=101;   
   if(res==0)
  {
   dstpathname[0]=0;
   srcpathname[0]=0;
   strcat((char*)srcpathname,(const char*)psrc);  //复制原始源文件路径 
   strcat((char*)dstpathname,(const char*)pdst);  //复制原始目标文件路径 
      res=f_opendir(srcdir,(const TCHAR*)psrc);   //打开源目录
      if(res==0)//打开目录成功
   {
      strcat((char*)dstpathname,(const char*)"/");//加入斜杠
     fn=get_src_dname(psrc);
    if(fn==0)//卷标拷贝
    {
     dstpathlen=strlen((const char*)dstpathname);
     dstpathname[dstpathlen]=psrc[0]; //记录卷标
     dstpathname[dstpathlen+1]=0;  //结束符
    }else strcat((char*)dstpathname,(const char*)fn);//加文件名  
    res=f_mkdir((const TCHAR*)dstpathname);//如果文件夹已经存在,就不创建.如果不存在就创建新的文件夹.
    if(res==FR_EXIST)res=0;
    while(res==0)//开始复制文件夹里面的东东
    {
           res=f_readdir(srcdir,finfo);     //读取目录下的一个文件
           if(res!=FR_OK||finfo->fname[0]==0)break;  //错误了/到末尾了,退出
           if(finfo->fname[0]=='.')continue;        //忽略上级目录
     fn=(u8*)(*finfo->lfname?finfo->lfname:finfo->fname);  //得到文件名
     dstpathlen=strlen((const char*)dstpathname); //得到当前目标路径的长度
     srcpathlen=strlen((const char*)srcpathname); //得到源路径长度

     strcat((char*)srcpathname,(const char*)"/");//源路径加斜杠
      if(finfo->fattrib&0X10)//是子目录   文件属性,0X20,归档文件;0X10,子目录;
     {
      strcat((char*)srcpathname,(const char*)fn);  //源路径加上子目录名字
       printf("\r\ncopy folder %s to %s\r\n",srcpathname,dstpathname);//拷贝文件
      res=mf_dcopy(srcpathname,dstpathname,fwmode); //拷贝文件夹
     }else //非目录
     {
      strcat((char*)dstpathname,(const char*)"/");//目标路径加斜杠
      strcat((char*)dstpathname,(const char*)fn); //目标路径加文件名
      strcat((char*)srcpathname,(const char*)fn); //源路径加文件名
      printf("\r\ncopy file %s to %s\r\n",srcpathname,dstpathname);//拷贝文件
      mf_copy(srcpathname,dstpathname,fwmode); //复制文件
     }
     srcpathname[srcpathlen]=0;//加入结束符
     dstpathname[dstpathlen]=0;//加入结束符    
    }
      
     myfree(SRAMIN,dstpathname);
    myfree(SRAMIN,srcpathname);
   myfree(SRAMIN,finfo->lfname);
  }
  }
 myfree(SRAMIN,srcdir);
 myfree(SRAMIN,dstdir);
 myfree(SRAMIN,finfo);
   return res;  
}


  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值