2

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"
Clifford
  • 88,407
  • 13
  • 85
  • 165
Nima Aghayan
  • 109
  • 1
  • 7

2 Answers2

2

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);
Armandas
  • 2,276
  • 1
  • 22
  • 27
  • So what is the benefit of f_chdir if i need to reopen a directory – Nima Aghayan Dec 31 '22 at 08:27
  • 1
    To be honest, I don't see a great benefit. Probably that's why these functions are optional. But with relative paths enabled, you can use `.` and `..` directory names, so I guess you could do something like `f_chdir(".."); f_opendir(dir, ".");` – Armandas Dec 31 '22 at 08:31
  • I hoped to use this to change folders structure because f_opendir takes about average 20 milis second to which is alot of time for me – Nima Aghayan Dec 31 '22 at 08:35
  • 1
    @NimaAghayan I guess if you were inside a deeply nested directory, using a relative path could save you some time, as it would not have to follow the path from the root directory every time. But I guess your bottle neck is IO operations anyway, so `f_chdir` will not help there. – Armandas Dec 31 '22 at 08:45
  • How can I improve the speed of IO operations ? – Nima Aghayan Dec 31 '22 at 08:46
  • 1
    @NimaAghayan Firstly, my comment was just a guess, so you should measure and figure out what takes the time. If it turns out to be the bus access, then you could try to see if you're using the fastest clock that your interface can handle. If the speed is really critical, you could try changing the code to only use a single directory or even trying different file system altogether. Another thing you could try is opening the necessary directories in advance (i.e. during initialization phase). – Armandas Dec 31 '22 at 08:55
0

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);
}
Nima Aghayan
  • 109
  • 1
  • 7