Problems listing a directory in nitroFS

Post Reply
Almamu
Posts: 25
Joined: Tue Apr 07, 2009 8:45 pm

Problems listing a directory in nitroFS

Post by Almamu » 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.

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;
}
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.

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: Problems listing a directory in nitroFS

Post by mtheall » Thu Sep 27, 2012 10:36 pm

I don't see anything immediately problematic in this code. Possibly a bug in nitro filesystem code. Although there is one case in your code that you should be doing closedir():

Code: Select all

      if(num < 2)
      {
         Log::Debug("No songs found\n");
         closedir(pdir); // whoops you forgot to close here
         return 0;
      }
I will try to do some testing to see if I hit a similar problem. Can you make a minimal test case that shows the problem?

Almamu
Posts: 25
Joined: Tue Apr 07, 2009 8:45 pm

Re: Problems listing a directory in nitroFS

Post by Almamu » Thu Sep 27, 2012 10:41 pm

mtheall wrote:I don't see anything immediately problematic in this code. Possibly a bug in nitro filesystem code. Although there is one case in your code that you should be doing closedir():

Code: Select all

      if(num < 2)
      {
         Log::Debug("No songs found\n");
         closedir(pdir); // whoops you forgot to close here
         return 0;
      }
I will try to do some testing to see if I hit a similar problem.
Ok, thanks for that, Im using this function to free every entry and the list itself:

Code: Select all

void Sys_FreeSongList()
{
	// Free every entry
	for(uint32_t i = 0; i < songListMax; i ++)
	{
		free(songList[i]);
		free(songTitle[i]);

		songList[i] = NULL;
		songTitle[i] = NULL;
	}

	// Free the list placeholder
	free(songList);
	free(songTitle);

	songList = NULL;
	songTitle = NULL;

	songListMax = 0;
}
I dont think its a bug in nitroFS, because the nitrofs example works just fine...
mtheall wrote:Can you make a minimal test case that shows the problem?
I've tried the same code modifying the nitrofs example in devkitPro (and using the same files from my project) and the code does not hang, it works at it should, even If I run it twice. But when I run it in my project it crashes in closedir(pdir); lines

Almamu
Posts: 25
Joined: Tue Apr 07, 2009 8:45 pm

Re: Problems listing a directory in nitroFS

Post by Almamu » Thu Sep 27, 2012 11:02 pm

Sorry but I cant edit the post :\

I fixed the problem, the error wasnt there, for some reason, when I debugged it at first, the log stopped when closedir(pdir) was called, but then I noticed that there was something wrong in my menu code, and that code was calling NF_Error, I'm trying to fix it right now, anyway thanks for all ;)

Post Reply

Who is online

Users browsing this forum: No registered users and 32 guests