简体   繁体   English

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

[英]Why changing directory using elm chan fatfs f_chdir doesnt effect directory structure?

My folder structure is like this: I open a folder then I use f_chdir to change my directory to that folder.我的文件夹结构是这样的:我打开一个文件夹,然后使用 f_chdir 将我的目录更改为该文件夹。 The problem is that f_chdir doesn't change my Directory Structure variable.问题是 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"

How can I change:我该如何改变:

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

to this behavior?:对于这种行为?:

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

f_readdir only works with the directory that has been opened. f_readdir仅适用于已打开的目录。 f_chdir does not affect your dir variable in any way. f_chdir不会以任何方式影响您的dir变量。 If you want to update dir , then re-open the needed directory:如果你想更新dir ,然后重新打开所需的目录:

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

or using the dot directory:或使用点目录:

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

I have a developed a version f_chdir that changes directory.我开发了一个可以更改目录的版本 f_chdir。 It would doesn't need to close a directory and reopen it.它不需要关闭目录并重新打开它。 I was wondering if anything is wrong is with my Implementation:我想知道我的实现是否有问题:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM