为什么使用elm chan fatfs f_chdir更改目录不会影响目录结构?

c86crjj0  于 2023-01-01  发布在  其他
关注(0)|答案(2)|浏览(104)

我的文件夹结构是这样的:我打开一个文件夹,然后使用f_chdir将目录更改到该文件夹。问题是f_chdir不更改目录结构变量。

-A1
   | A11 
   |     |
   |     A11.mp3
   | A12
   |     |
   |       A12.mp3
   | A1.mp3

-A2
   | A21 
   |     |
   |     A21.mp3
   | A22
   |     |
   |       A22.mp3
   | A2.mp3
root_path = "/A1";
newPath = "/A1/A11";
f_opendir(dir,root_path );
f_chdir(newPath);
f_readdir(dir,fno);// This results in fno.fname = "/A12"

如何更改:

f_readdir(dir,fno);// This results in fno.fname = "/A12"

对这种行为?:

f_readdir(dir,fno);// Resulting in fno.fname = "A11.mp3"
anauzrmj

anauzrmj1#

f_readdir仅适用于已打开的目录。f_chdir不会以任何方式影响dir变量。如果要更新dir,请重新打开所需的目录:

f_closedir(dir);
f_opendir(dir, newPath);
f_readdir(dir, fno);

或使用点目录:

f_closedir(dir);
f_chdir(newPath);
f_opendir(dir, ".");
f_readdir(dir, fno);
r8xiu3jd

r8xiu3jd2#

我已经开发了一个版本的f_chdir,改变目录。它将不需要关闭一个目录,并重新打开它。我想知道是否有什么问题是与我的实现:

FRESULT f_chdir2 ( const TCHAR* path,DIR* dj)
{
  FRESULT res;
  FATFS *fs;
  _FDID *obj;
  DEF_NAMBUF   
  /* Get logical drive */
  obj = &dj->obj;
  res = find_volume(&path, &fs, 0);
  if (res == FR_OK) 
  {
    obj->fs = fs;
    dj->obj.fs = fs;

    INIT_NAMBUF(fs);
    res = follow_path(dj, path);        /* Follow the path */
    if (res == FR_OK) 
    {                   /* Follow completed */
      if (dj->fn[NSFLAG] & NS_NONAME)
      {
        fs->cdir = dj->obj.sclust;  /* It is the start directory itself */
#if _FS_EXFAT
        if (fs->fs_type == FS_EXFAT) 
        {
          fs->cdc_scl = dj->obj.c_scl;
          fs->cdc_size = dj->obj.c_size;
          fs->cdc_ofs = dj->obj.c_ofs;
        }
#endif
      } 
      else 
      {  
        if (obj->attr & AM_DIR)
        {   /* It is a sub-directory */
#if _FS_EXFAT
          if (fs->fs_type == FS_EXFAT)
          {
            fs->cdir = ld_dword(fs->dirbuf + XDIR_FstClus);     /* Sub-directory cluster */
            fs->cdc_scl = dj->obj.sclust;                       /* Save containing directory information */
            fs->cdc_size = ((DWORD)dj->obj.objsize & 0xFFFFFF00) | dj->obj.stat;
            fs->cdc_ofs = dj->blk_ofs;
            obj->c_scl = obj->sclust;                           /* Get containing directory inforamation */
            obj->c_size = ((DWORD)obj->objsize & 0xFFFFFF00) | obj->stat;
            obj->c_ofs = dj->blk_ofs;
            obj->sclust = ld_dword(fs->dirbuf + XDIR_FstClus);  /* Get object allocation info */
            obj->objsize = ld_qword(fs->dirbuf + XDIR_FileSize);
            obj->stat = fs->dirbuf[XDIR_GenFlags] & 2;
          } 
          else
#endif
          {
            obj->sclust = ld_clust(fs, dj->dir);    /* Get object allocation info */
            fs->cdir = ld_clust(fs, dj->dir);                   /* Sub-directory cluster */
          }
        } 
        else 
        {
          res = FR_NO_PATH;     /* Reached but a file */
        }
      }
      if (res == FR_OK)
      {
        obj->id = fs->id;
        res = dir_sdi(dj, 0);           /* Rewind directory */
      }
    }
    FREE_NAMBUF();
    if (res == FR_NO_FILE)
      res = FR_NO_PATH;
  }
  
  LEAVE_FF(fs, res);
}

相关问题