Open subdirectory

JVKran
Posts: 18
Joined: Thu Mar 04, 2021 2:40 pm

Open subdirectory

Postby JVKran » Mon Aug 16, 2021 1:30 pm

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

ESP_Sprite
Posts: 9711
Joined: Thu Nov 26, 2015 4:08 am

Re: Open subdirectory

Postby ESP_Sprite » Tue Aug 17, 2021 1:01 am

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)

JVKran
Posts: 18
Joined: Thu Mar 04, 2021 2:40 pm

Re: Open subdirectory

Postby JVKran » Tue Aug 17, 2021 8:45 am

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.

JVKran
Posts: 18
Joined: Thu Mar 04, 2021 2:40 pm

Re: Open subdirectory

Postby JVKran » Tue Nov 30, 2021 5:12 pm

For the record; this is still not solved and happening in the latest IDF version.

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: Open subdirectory

Postby ESP_igrr » Wed Dec 01, 2021 8:42 pm

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.

Who is online

Users browsing this forum: No registered users and 108 guests