Skip to content

Unable to list files in a SPIFFS partition #117

Closed
@bugadani

Description

@bugadani

Given is the following test code, built on a CMake setup (with some details omitted):

# Cargo.toml

...

[dependencies]
c_str_macro = "1"
// main.rs
use std::os::raw::c_char;
use std::ffi::CStr;
use c_str_macro::c_str;

#[repr(C)]
pub struct esp_vfs_spiffs_conf_t {
    pub base_path: *const c_char,
    pub partition_label: *const c_char,
    pub max_files: usize,
    pub format_if_mount_failed: bool,
}

extern "C" {
    pub fn esp_vfs_spiffs_register(conf: *const esp_vfs_spiffs_conf_t) -> i32;
}

fn init_partition(path: &CStr, label: &CStr, max_files: usize) {
    let storage_conf = esp_vfs_spiffs_conf_t {
        base_path: path.as_ptr(),
        partition_label: label.as_ptr(),
        max_files,
        format_if_mount_failed: true,
    };

    unsafe { esp_vfs_spiffs_register(&storage_conf) };
}

fn test() {
    let path = std::path::Path::new("/storage/foobar");

    if let Err(err) = std::fs::File::create(path) {
        println!("Failed to create file: {err}");
        return;
    }

    match std::fs::read_dir("/storage") {
        Ok(dir) => {
            for entry in dir {
                println!("{entry:?}");
            }
        }
        Err(err) => println!("Failed to read root: {err}"),
    }

    if let Err(err) = std::fs::remove_file(path) {
        println!("Failed to remove file: {err}");
    }
}

#[no_mangle]
pub extern "C" fn app_main() {
    init_partition(c_str!("/storage"), c_str!("storage"), 3);

    test();
}

I expect the following output:

Ok(DirEntry("/storage/foobar"))

Instead, the output I get is the following:

Ok(DirEntry("/storage/"))
Err(Os { code: 5, kind: Uncategorized, message: "I/O error" })

The OS error is unrelated (SPIFFS seems to indicate the end of folder iteration by returning a value that gets turned into EIO), but note that the path in the DirEntry has an empty filename.

This issue is present on:

rustc verion esp-idf 4.4.1
Rustc 1.60.0.0 x
Rustc 1.59.0.1 x
Rustc 1.59.0.0 not affected

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions