Problems listing a directory in nitroFS
Posted: Thu Sep 27, 2012 10:20 pm
Using directory functions with NitroFS results in unexpected behavior. For example, when I call closedir its like it calls a different function (probably is NF_Error, I'm using NFlib), If I comment that function the code works fine, until I call it again, then the program crashes.
At first I though that the way I allocate both lists and their items was the problem, but even commenting those lines the program hangs or calls an unexpected function. Any idea why this happens? It has stopped the development for some days, and I do not really know what happens. If it helps, I've tested it with a PC port using Cygwin, and the code works just fine.
Code: Select all
char** songList;
char** songTitle;
uint32_t Sys_SearchSongs()
{
DIR* pdir;
struct dirent *pent;
struct stat statbuf;
Log::Debug(" opening dir\n");
pdir = opendir("/ctb/songs/");
if(pdir)
{
uint32_t num = 0;
uint32_t current = 0;
// Go to the end
while(readdir(pdir) != NULL) num++;
if(num < 2)
{
Log::Debug("No songs found\n");
return 0;
}
num -= 2;
// Make enough space for pointers
songList = (char**)malloc(sizeof(char*) * num);
songTitle = (char**)malloc(sizeof(char*) * num);
rewinddir(pdir);
Log::Debug(" num songs found: %d\n", num);
while( (pent = readdir(pdir)) != NULL )
{
Log::Debug(" trying to parse stat for folder %s\n", pent->d_name);
stat(pent->d_name, &statbuf);
// Ignore the current and previous directory
if(strcmp(".", pent->d_name) == 0 || strcmp("..", pent->d_name) == 0)
continue;
if(S_ISDIR(statbuf.st_mode))
{
Log::Debug(" loading song data for %s\n", pent->d_name);
// Add the dir to the list
if(current > num) continue;
// Store the data
songList[current] = (char*)malloc(sizeof(char) * 768);
songTitle[current] = (char*)malloc(sizeof(char) * 256);
strcpy(songList[current], pent->d_name);
sprintf(path, "/ctb/songs/%s/%s.txt", songList[current], songList[current]);
FILE* fp = fopen(path, "rb+");
if(fp == NULL || ferror(fp))
{
strcpy(songTitle[current], "<ERROR ENTRY>");
}
else
{
fgets(title, 256, fp);
fclose(fp);
strcpy(songTitle[current], title);
}
Log::Debug(" song %d: %s\n", current, songTitle[current]);
current ++;
}
}
closedir(pdir);
Log::Debug(" parsed %d songs\n", current);
return current;
}
return 0;
}