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 > Getting files to NOT show up as a directory

#165273 - DiscoStew - Mon Dec 15, 2008 10:31 pm

I just got finished with using Maxmod to stream WAV files (PCM specific) from a specific location using libfat, but now I want to be able to allow the user to go through directories to search for compatible WAV files to play.

I started with the libfat example that scans the root directory for folders and files and displays them as such, but I'm having a bit of trouble when opendir doesn't use the root directory. Everything read with a different directory is showing up as folders, both folders and files.

This is the simple change I made...
Code:
DIR *pdir;
   
   struct dirent *pent;
   struct stat statbuf;

   //pdir=opendir("/");
   pdir=opendir("/data/");   <-----------------------
   if (pdir)
   {
      while ((pent=readdir(pdir))!=NULL)
      {
         stat(pent->d_name,&statbuf);
          if(strcmp(".", pent->d_name) == 0 || strcmp("..", pent->d_name) == 0)
             continue;
          if(S_ISDIR(statbuf.st_mode))
            iprintf("%s <dir>\n", pent->d_name);
            if(!(S_ISDIR(statbuf.st_mode)))
               iprintf("%s %ld\n", pent->d_name, statbuf.st_size);
      }
      closedir(pdir);
   } else {
      iprintf ("opendir() failure; terminating\n");
   }

   while(1)
      swiWaitForVBlank();


Am I doing something wrong? Also, if I am to switch to a new directory, would I need to close the current one, and then reopen the new one with opendir?
_________________
DS - It's all about DiscoStew

#165274 - nanou - Mon Dec 15, 2008 11:08 pm

That's ... odd. Aside from the fact that everything is listed as a directory, are you getting the correct list of items?

If you're throwing out the directory handle, then yes, you should certainly close it first. IMO, unless you're still in the middle of processing the contents (i.e. in the main while loop there--which would only cause trouble anyway) then you should close it before opening up a new one.
_________________
- nanou

#165278 - DiscoStew - Mon Dec 15, 2008 11:47 pm

nanou wrote:
That's ... odd. Aside from the fact that everything is listed as a directory, are you getting the correct list of items?

If you're throwing out the directory handle, then yes, you should certainly close it first. IMO, unless you're still in the middle of processing the contents (i.e. in the main while loop there--which would only cause trouble anyway) then you should close it before opening up a new one.


So far, I'm seeing all the files in the current directory used, but unless opendir() is given just "/", S_ISDIR() is returning true for everything.

With switching to a different directory, I was just making sure that I needed to close then open, rather than doing something else like chdir or other things of that nature.
_________________
DS - It's all about DiscoStew

#165281 - nanou - Tue Dec 16, 2008 1:33 am

Okay, I feel stupid for not noticing this earlier.

Code:
stat(pent->d_name,&statbuf);


This assumes you're at the root level. The call to stat() fails since the directory is missing from the path, so statbuf is never getting updated (after checking . and .., which are directories.)

You can either use the full path when calling stat() or chdir() then opendir(".") should work.
_________________
- nanou

#165286 - DiscoStew - Tue Dec 16, 2008 5:14 am

Thanks, that helped a lot.
_________________
DS - It's all about DiscoStew