Armwrestling with Fat file system

15 Jul 2012

Well, I spent a few hours fighting this function... All I want to do is read the directory from an SD card and display a list of files with their sizes and time and date information.

I am using the opendir() function and then readdir("/sd") which gives, each time it is called, only a pointer to the current filename (or directoryname when present) and no attributes.

In FatDirHandle.cpp I see that the fileinfo is read in but only used to get access to the long filename. How do I get access to the other fileinfo from my program?

For testing I added a printf in FatDirHandle to show the available fileinfo:

struct dirent *FATDirHandle::readdir() {
    FILINFO finfo;

  #if _USE_LFN
    finfo.lfname = cur_entry.d_name;
    finfo.lfsize = sizeof(cur_entry.d_name);
#endif // _USE_LFN

    FRESULT res = f_readdir(&dir, &finfo);

    printf("File %s filesize: %d attribute %02x\n\r",finfo.fname,finfo.fsize,finfo.fattrib);

#if _USE_LFN
    if (res != 0 || finfo.fname[0]==0) {
        return NULL;
    } else {
        if (cur_entry.d_name[0]==0) {
            // No long filename so use short filename.
            memcpy(cur_entry.d_name, finfo.fname, sizeof(finfo.fname));
        }
        return &cur_entry;
    }
#else
    if (res != 0 || finfo.fname[0]==0) {
        return NULL;
    } else {
        memcpy(cur_entry.d_name, finfo.fname, sizeof(finfo.fname));
        return &cur_entry;
    }
#endif /* _USE_LFN */
}
15 Jul 2012

Tried using f_readdir() directly with a bit of succes, only lost long file name support (LFN).

Also when using:

void list_dir(){

   FILINFO finfo;
   FATFS_DIR mydj;
   FRESULT res;

   .....

The mbed compiler sets the finfo pointer to '1' so crashing the mbed when using the f_readdir(&mydj, &finfo); call... The FATFS_DIR pointer is ok. By declaring them outside the routine it seems to work OK... weird..