trussed-dev / trussed

Modern Cryptographic Firmware

Home Page:https://trussed.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Iteration stops on an empty file

szszszsz opened this issue · comments

Iterating over files can stop prematurally, when an empty (0-sized) file is encountered.
Perhaps the sys package misses that case, or interprets it wrongly.

Littlefs read call:

let read_dir_files_state = ReadDirFilesState {
real_dir: real_dir.clone(),
last: i,
location,
user_attribute,
};
// The semantics is that for a non-existent file, we return None (not an error)
let data = store::read(self.store, location, entry.path()).ok();
(data, read_dir_files_state)
})
// convert Option into Result, again because `read_dir_and_then` expects this
.ok_or(littlefs2::io::Error::Io)

Can you share some example code? I cannot reproduce the issue with this minimal test:

#![cfg(feature = "virt")]

use heapless_bytes::Bytes;
use trussed::{client::FilesystemClient, types::{Location, PathBuf}, syscall};

mod client;

#[test]
fn iterate_empty_file() {
    client::get(|client| {
        let l = Location::Internal;
        let n = 100;
        for i in 0..n {
            let path = PathBuf::from(format!("test{i}").as_str());
            let data = if i % 2 == 0 {
                Bytes::new()
            } else {
                Bytes::from_slice(&[0xde, 0xad, 0xbe, 0xef]).unwrap()
            };
            syscall!(client.write_file(l, path, data, None));
        }
        let mut msg = syscall!(client.read_dir_files_first(l, PathBuf::new(), None)).data;
        let mut count = 0;
        while msg.is_some() {
            count += 1;
            msg = syscall!(client.read_dir_files_next()).data;
        }
        assert_eq!(count, n);
    })
}

Please re-open if you can reproduce the problem.