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 > unlink & libfat not working?

#148080 - DSfriol - Wed Jan 02, 2008 12:49 am

I've compiled this simple program with libfat (the latest from CVS, where statvfs *seems* to work) and run it on my DS Lite with R4 (1Gb microSD):

Code:


...

void printFreeDiskSpace()
{
    struct statvfs st;
    statvfs("/.", &st);
   iprintf("free space [%ld] Kb",(st.f_bfree*st.f_bsize)/(1024));
}

int main(void)
{
   initDs();
   fatInitDefault();

   for (int i=0;i<4;i++)
   {
      iprintf("iteration [%d]\n",i);

      FILE* myf=fopen("/test.txt","wt");
      if (myf==NULL)
      {
         iprintf("error opening file\n");
         return 1;
      }

      for (int j=0;j<(1024*16);j++)
      {
         fprintf(myf,"0123456789abcdef");
      }

      fclose(myf);
      printFreeDiskSpace();

      if (unlink("/test.txt")==0)
      {
         iprintf("ok\n");
         printFreeDiskSpace();
      }
   }

   return 0;
}


Unfortunately, subsequent calls to "printFreeDiskSpace" show that space is *not* staying constant, but *decreases*, even if I call "unlink" on the created file.

What is not working, statvfs, unlink or...?

#148147 - DSfriol - Wed Jan 02, 2008 11:36 pm

Update: today I run chkdsk from Windows on the microSD and it found 14 lost files. So the problem seems to be in the unlink function. Anyone tried this on his DS?

#148149 - simonjhall - Thu Jan 03, 2008 12:07 am

Isn't unlink one of those internal functions that you're not meant to call? I've always used remove() and I (think I!) get the right results...
_________________
Big thanks to everyone who donated for Quake2

#148172 - DSfriol - Thu Jan 03, 2008 8:52 am

Same result with "remove" (the space is decreasing).

#148267 - Abcd1234 - Fri Jan 04, 2008 1:41 am

simonjhall wrote:
Isn't unlink one of those internal functions that you're not meant to call? I've always used remove() and I (think I!) get the right results...


I would hope not. unlink() is a standard POSIX libc function, defined in unistd.h, and is the only portable way to delete a file (as far as I'm aware). It should be no more internal than open(), close(), read(), etc.

#148270 - DSfriol - Fri Jan 04, 2008 3:10 am

I fear libfat has a problem, here.
The applications that are using the old lib_fat_nds are ok on my R4, instead. I wrote an email to chishm on the subject.

#148285 - chishm - Fri Jan 04, 2008 10:12 am

I'll take a look at this when I have my development PC up and running again.
_________________
http://chishm.drunkencoders.com
http://dldi.drunkencoders.com

#148328 - DSfriol - Fri Jan 04, 2008 4:34 pm

Can I help in any way in the mean time? Compiling libfat with debug, or something similar?

#148391 - chishm - Sat Jan 05, 2008 6:22 am

DSfriol wrote:
Can I help in any way in the mean time? Compiling libfat with debug, or something similar?
Nah, this is mostly a "try it and see what happens" type thing for me. Debugging will involve reproducing the problem, examining the on-disc data structures (the FAT mostly), then figuring out which function is screwing up.
_________________
http://chishm.drunkencoders.com
http://dldi.drunkencoders.com

#148395 - DSfriol - Sat Jan 05, 2008 10:23 am

Ok, I'll wait (remember that I have an R4, if that counts).