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 > filesystem problem : using dirnext with long filenames

#120996 - Paco_777 - Thu Mar 08, 2007 12:45 am

Hi,
I'm scanning all the filesystem in order to find some specific files.
To achieve this, i'm using the dirnext method of the last devkitARM
I do it this usual way :
Code:
DIR_ITER* dir = diropen (nameTempBufferDir);
if (dir!=NULL) {
   while (dirnext(dir, nameTempBufferFile, &st) == 0) {
         // do stuffs
   }
}

I performed my tests on a Supercard SD.
I'm facing a problem : my loop stops with files which have a very big filename.
At least with the following filename in the root directory :
"this is a very big file ndkslepslamdoeldmsoemdoeld this is a very big file ndkslepslamdoeldmsoemdoeld this is a very big file ndkslepslamdoeldmsoemdoeld this is a very big file ndkslepslamdoeldmsothis is a very big file ndkslepslamdoeldmsoemdoeld .txt"


I made another version, trying to use errno in order to bypass incorrect files :
Code:
bool dirok;
DIR_ITER* dir = diropen (nameTempBufferDir);
if (dir!=NULL) {
   dirok = ((dirnext(dir, nameTempBufferFile, &st) == 0) || (errno!=ENOENT));
   while (dirok) {
      if (errno!=ENOENT){
         // do stuffs
      }
      dirok = ((dirnext(dir, nameTempBufferFile, &st) == 0) || (errno!=ENOENT));
   }
}

Unfortunately, the loop is endless and the 'bad' filename cannot be bypassed.

Is there a solution for that problem ?

By the way, dirnext should have a maxlength parameter (the size of the filename buffer) in order to avoid memory corruption when
the filename is written to the char *.
_________________
http://Gnese.free.fr/NDS/

#121022 - HyperHacker - Thu Mar 08, 2007 7:07 am

That's probably what's happening. Your huge file names are exceeding the buffer.
_________________
I'm a PSP hacker now, but I still <3 DS.

#121024 - chishm - Thu Mar 08, 2007 7:49 am

Max filename length is 256 chars. It should be defined somewhere, but I can't seem to find it.
_________________
http://chishm.drunkencoders.com
http://dldi.drunkencoders.com

#121542 - Paco_777 - Tue Mar 13, 2007 12:56 am

ok, thanks (luckyly, my buffer size was 256:) ).

Would it be possible to have dirnext skip files with filenames bigger that 256 characters ?
There is perhaps another way to list all the files in my filesystem (without stopping on some of them) , but i cannot figure how :/

well, of course removing the big filenames will do the job, but i cannot ensure that every user will have a clean card :)

(At least, i think that i can use chdir in order to reduce the total length of my filename+dirname).
_________________
http://Gnese.free.fr/NDS/

#121556 - chishm - Tue Mar 13, 2007 3:20 am

The FAT long file name specifications specify that a filename must be no more than 255 characters long. If you have a filename longer than this, it is outside of specs.
_________________
http://chishm.drunkencoders.com
http://dldi.drunkencoders.com

#121565 - HyperHacker - Tue Mar 13, 2007 5:06 am

So what does happen if we have a file with an excessively long name on our card?
_________________
I'm a PSP hacker now, but I still <3 DS.

#121573 - chishm - Tue Mar 13, 2007 6:54 am

It shouldn't pass a chkdsk/scandisk test. If you try to use it with libfat, it'll likely cause buffer overflows.
_________________
http://chishm.drunkencoders.com
http://dldi.drunkencoders.com

#121626 - tepples - Tue Mar 13, 2007 4:09 pm

What about a filename with "Pok?mon" repeated 36 times? That's 252 characters but it comes out to 288 bytes due to UTF-8 encoding.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#121631 - Diddl - Tue Mar 13, 2007 4:17 pm

nobody needs such long filenames. but buffer overflow should not happen, - it should be repaired by a new version of libfat.

#121725 - chishm - Wed Mar 14, 2007 9:51 am

tepples:
LFNs are encoded using UCS-2 in the FAT filename structures on disc. Libfat only uses the low byte, which can cause problems, but will be fixed in future. When that happens, the buffers passed to libfat likely need to be 768 bytes, with the names stored internally as UCS-2.

Diddl:
It's not that nobody needs the filenames. They shouldn't actually exist, since they are not allowed by the FAT specs. That begin said, I have added code to prevent buffer overflows due to excessively long LFNs.
_________________
http://chishm.drunkencoders.com
http://dldi.drunkencoders.com