gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Problem with directory scanning code

#168706 - Echo49 - Sat May 16, 2009 11:05 am

I'm trying to write some code to scan an entire directory which should be full of subdirectories, and inside those subdirectories are the files I want to have picked up by my code.

However, this code is failing at the first S_ISDIR statement. Both files and directories are returning 0 when testing with S_ISDIR. I've checked the value of st_mode and it's always 0, with both nitrofs on No$gba and libfat on hardware. Am I calling stat() incorrectly? I pretty much copypasted from the example.

Code:
void readDir()
{
   chdir(/*some directory*/);
   
   struct stat fileinfo;
   
   DIR* dir = opendir(".");
   struct dirent* entry;
   
   while ((entry = readdir(dir)) != NULL)
   {
      //ignore generic names
      if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
         continue;
      
      //get file info
      stat(entry->d_name, &fileinfo);
      
      //if this is a folder, find all the files inside
      if (S_ISDIR(fileinfo.st_mode))
      {
         DIR* subdir = opendir(entry->d_name);
         struct dirent* subentry;
         
         while ((subentry = readdir(subdir)) != NULL)
         {
            //reuse stat struct
            stat(subentry->d_name, &fileinfo);
            
            //if this is a file
            if (!S_ISDIR(fileinfo.st_mode))
            {
               char* ext = subentry->d_name;
               
               int length = strlen(ext);
               if (length < 4)
                  continue;
               
               //ext is now the extension of the current file
               ext += length - 4;
               
               if (strcmp(ext, ".ext") == 0)
                  ;//do stuff with this data
            }
         }
         
         closedir(subdir);
      }
   }
   
   closedir(dir);
}

#168712 - Dwedit - Sat May 16, 2009 5:06 pm

Warning: Some versions of devkitarm have a bug where the Stat struct you allocate is too small compared with what gets written there. Instant memory corruption. This bug may have been fixed already.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#168716 - nanou - Sat May 16, 2009 7:38 pm

You should check the return from stat() to see whether it's working at all. A similar problem came up before where a call to chdir() was needed prior to stat(). At a glance you look like you need it too.

You might want to try using ftw() instead if it's provided.
_________________
- nanou

#168721 - Echo49 - Sun May 17, 2009 5:44 am

It seems like emulator+nitrofs isn't processing the stat() call properly (returns -1), but worked fine on hardware+libfat.

I changed to diropen/dirnext/dirclose which worked fine on emulator BUT crashed on hardware ;.; (crashed on return from the readDir function - seemed to be something to do with dirnext)

So in the end I used opendir to check whether it is a directory or a file (NULL == file, otherwise dir). Took me 2-3 hours too >.>