Page 1 of 1

Open subdirectory

Posted: Mon Aug 16, 2021 1:30 pm
by JVKran
Hey there!

Ok, so, I know this sounds very stupid, but I can't for the life of me figure out how to open a subdirectory. I've mounted an SD-card which I can succesfully use to play and serve files. Furthermore, I have the following function.

Code: Select all

void list_files(const char * dirpath){
    struct dirent *ep;     
    DIR *dp = opendir(dirpath);

    if (dp != NULL){
        while ((ep = readdir(dp)) != NULL) {
            ESP_LOGI(TAG, "Found %s.", ep->d_name);
        }
        (void) closedir (dp);
    } else {
        ESP_LOGE(TAG, "Failed to open directory \'%s\'!", dirpath);
    }

}
When I call this function like

Code: Select all

list_files("/sdcard/")
everything works and I'm presented with the names of all files in my terminal. Calling

Code: Select all

list_files("/sdcard/questions")
(which is an existing directory that contains files), however, results in opendir returning a NULL-pointer.

Am I overseeing something?

Kind regards,

Jochem

Re: Open subdirectory

Posted: Tue Aug 17, 2021 1:01 am
by ESP_Sprite
No clue why that goes wrong, but diropen() should set errno when it returns a null pointer; can you check what that value is? (perror() may help there)

Re: Open subdirectory

Posted: Tue Aug 17, 2021 8:45 am
by JVKran
Hi there. I've now updated the function to also include the error number as below.

Code: Select all

void list_files(const char * dirpath){
    struct dirent *ep;
    DIR *dp = opendir(dirpath);

    if (dp != NULL){
        while ((ep = readdir(dp)) != NULL) {
            ESP_LOGI(TAG, "Found %s.", ep->d_name);
        }
        (void) closedir (dp);
    } else {
        ESP_LOGE(TAG, "Failed to open directory \'%s\'! Errno is \'%s\.'", dirpath, strerror(errno));
    }
}
The error number represents 'Invalid Argument'... I imagine that not being very helpful... In the meantime, I'll go ahead and try some other SD-cards...

Kind regards,

Jochem

EDIT: Other (formatted) cards unfortunately show the exact same behaviour.

Re: Open subdirectory

Posted: Tue Nov 30, 2021 5:12 pm
by JVKran
For the record; this is still not solved and happening in the latest IDF version.

Re: Open subdirectory

Posted: Wed Dec 01, 2021 8:42 pm
by ESP_igrr
Just to check, have you enabled Long File Names support for FatFS in menuconfig (e.g. CONFIG_FATFS_LFN_HEAP)? Since the directory name is longer than 8 characters, without long file names support its path will be /sdcard/questi~1 instead of /sdcard/questions.