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

371 Views Asked by At

My folder structure is like this : I open a folder then I use f_chdir to change my directory to that folder. The problem is that f_chdir doesn't change my Directory Structure variable.

-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"
2

There are 2 best solutions below

6
On BEST ANSWER

f_readdir only works with the directory that has been opened. f_chdir does not affect your dir variable in any way. If you want to update dir, then re-open the needed directory:

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);
0
On

I have a developed a version f_chdir that changes directory. 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);
}