Getting a "PermissionError" while scanning directories inside "/spiflash"

Hasan Shadi
Posts: 26
Joined: Tue Mar 31, 2020 7:26 pm

Getting a "PermissionError" while scanning directories inside "/spiflash"

Postby Hasan Shadi » Sun May 31, 2020 6:22 am

I am sure that I have written multiple files inside the "/spiflash" folder, I always get the error, but I did not get it when I scanned the root directory which is "/" and I got nothing on the console while scanning it. Based on my searches, this error is related to the python tool that is communicating with the ESP32. It only happened to me when I am reading the directories and printing them to the console. And this is the traceback:

Code: Select all

I (317) example: Mounting FAT filesystem
      name | type | fileno

Traceback (most recent call last):
  File "C:\Users\Hassan\Documents\Arduino\ESP\esp-idf\tools/idf_monitor.py", lin
e 865, in <module>
    main()
  File "C:\Users\Hassan\Documents\Arduino\ESP\esp-idf\tools/idf_monitor.py", lin
e 777, in main
    monitor.main_loop()
  File "C:\Users\Hassan\Documents\Arduino\ESP\esp-idf\tools/idf_monitor.py", lin
e 379, in main_loop
    self.handle_serial_input(data)
  File "C:\Users\Hassan\Documents\Arduino\ESP\esp-idf\tools/idf_monitor.py", lin
e 439, in handle_serial_input
    self._print(line + b'\n')
  File "C:\Users\Hassan\Documents\Arduino\ESP\esp-idf\tools/idf_monitor.py", lin
e 681, in _print
    console_printer(string)
  File "C:\Users\Hassan\.espressif\python_env\idf4.0_py3.7_env\lib\site-packages
\serial\tools\miniterm.py", line 64, in write_bytes
    self.byte_output.flush()
  File "C:\Users\Hassan\Documents\Arduino\ESP\esp-idf\tools/idf_monitor.py", lin
e 862, in flush
    self.output.flush()
PermissionError: [WinError 31] A device attached to the system is not functionin
g
????????.??? |    2 |  0
idf_monitor failed with exit code 1
Notice that I have "????????.???" as the name of the file, so weird. This is the application code, so simple:

Code: Select all

#include <stdlib.h>
#include <stdio.h>
#include "esp_vfs.h"
#include "esp_vfs_fat.h"
#include "esp_system.h"

static const char *TAG = "example";

static wl_handle_t s_wl_handle = WL_INVALID_HANDLE;

const char *base_path = "/spiflash";

extern "C" {
    void app_main(void );
}

void app_main(void)
{
    ESP_LOGI(TAG, "Mounting FAT filesystem");
    const esp_vfs_fat_mount_config_t mount_config = {
            .format_if_mount_failed = false,
            .max_files = 4,
            .allocation_unit_size = CONFIG_WL_SECTOR_SIZE
    };
    esp_err_t err = esp_vfs_fat_spiflash_mount(base_path, "storage", &mount_config, &s_wl_handle);
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
        return;
    }

    DIR *d;
    struct dirent *dir;
    d = opendir("/spiflash");
    if (d)
    {
        printf("%10s | %4s | %s\n", "name", "type", "fileno");
        dir = readdir(d);
        while ((dir = readdir(d)) != NULL)
        {
            printf("%10s | %4d | %2d\n", dir->d_name, dir->d_type, dir->d_ino);
        }
        closedir(d);
    } else {
        printf("The file is empty!\n");
    }

    ESP_LOGI(TAG, "Unmounting FAT filesystem");
    ESP_ERROR_CHECK( esp_vfs_fat_spiflash_unmount(base_path, s_wl_handle));

    ESP_LOGI(TAG, "Done");
}
If there is another way to scan the directories easily, please enlighten me. I will appreciate anything that helps to solve my issue.

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

Re: Getting a "PermissionError" while scanning directories inside "/spiflash"

Postby ESP_Sprite » Sun May 31, 2020 9:11 am

Do you actually have files in the spiflash structure? Asking because there is a bug in your code: on an empty directory, the first readdir will return NULL and the result of the second readdir will be undefined.

Hasan Shadi
Posts: 26
Joined: Tue Mar 31, 2020 7:26 pm

Re: Getting a "PermissionError" while scanning directories inside "/spiflash"

Postby Hasan Shadi » Mon Jun 01, 2020 4:50 am

ESP_Sprite wrote:
Sun May 31, 2020 9:11 am
Do you actually have files in the spiflash structure? Asking because there is a bug in your code: on an empty directory, the first readdir will return NULL and the result of the second readdir will be undefined.
I have tried it without the "readdir()" function, same results. It was not written before I have posted this question, I have added so I can see if I can get different results. I have also tried to create and write and then use the "opendir()" function, same results.

Who is online

Users browsing this forum: No registered users and 148 guests