littlefs-rust 0.1.0

Safe Rust API for the LittleFS embedded filesystem
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use alloc::boxed::Box;
use alloc::vec;
use alloc::vec::Vec;
use core::cell::RefCell;
use core::ffi::c_void;
use core::mem::{ManuallyDrop, MaybeUninit};

use littlefs_rust_core::{Lfs, LfsConfig, LfsInfo, LFS_ERR_IO};

use crate::config::Config;
use crate::dir::{dir_entry_from_info, ReadDir};
use crate::error::{from_lfs_result, from_lfs_size, Error};
use crate::file::File;
use crate::metadata::{DirEntry, Metadata, OpenFlags};
use crate::storage::Storage;

pub(crate) struct FsInner<S: Storage> {
    pub(crate) lfs: MaybeUninit<Lfs>,
    pub(crate) config: LfsConfig,
    pub(crate) storage: S,
    _read_buf: Vec<u8>,
    _prog_buf: Vec<u8>,
    _lookahead_buf: Vec<u8>,
    pub(crate) mounted: bool,
}

/// A mounted LittleFS filesystem.
///
/// All methods take `&self` via interior mutability, so multiple [`File`] and
/// [`ReadDir`] handles can coexist. The internal state is heap-allocated and
/// pinned so that core pointers remain stable across moves.
///
/// Use [`Filesystem::format`] to initialize storage, then [`Filesystem::mount`]
/// to obtain a `Filesystem`. Call [`Filesystem::unmount`] to cleanly unmount
/// and recover the storage, or let [`Drop`] handle it automatically.
///
/// `Filesystem` is `!Send` and `!Sync` (due to interior `RefCell`). This is
/// appropriate for single-threaded embedded use. If you need cross-thread
/// access, wrap it in a `Mutex`.
pub struct Filesystem<S: Storage> {
    pub(crate) inner: RefCell<Box<FsInner<S>>>,
}

// ── Trampolines ─────────────────────────────────────────────────────────────

unsafe extern "C" fn trampoline_read<S: Storage>(
    cfg: *const LfsConfig,
    block: u32,
    off: u32,
    buffer: *mut u8,
    size: u32,
) -> i32 {
    let storage = &mut *((*cfg).context as *mut S);
    let buf = core::slice::from_raw_parts_mut(buffer, size as usize);
    match storage.read(block, off, buf) {
        Ok(()) => 0,
        Err(_) => LFS_ERR_IO,
    }
}

unsafe extern "C" fn trampoline_prog<S: Storage>(
    cfg: *const LfsConfig,
    block: u32,
    off: u32,
    buffer: *const u8,
    size: u32,
) -> i32 {
    let storage = &mut *((*cfg).context as *mut S);
    let buf = core::slice::from_raw_parts(buffer, size as usize);
    match storage.write(block, off, buf) {
        Ok(()) => 0,
        Err(_) => LFS_ERR_IO,
    }
}

unsafe extern "C" fn trampoline_erase<S: Storage>(cfg: *const LfsConfig, block: u32) -> i32 {
    let storage = &mut *((*cfg).context as *mut S);
    match storage.erase(block) {
        Ok(()) => 0,
        Err(_) => LFS_ERR_IO,
    }
}

unsafe extern "C" fn trampoline_sync<S: Storage>(cfg: *const LfsConfig) -> i32 {
    let storage = &mut *((*cfg).context as *mut S);
    match storage.sync() {
        Ok(()) => 0,
        Err(_) => LFS_ERR_IO,
    }
}

// ── FsInner construction ────────────────────────────────────────────────────

fn build_inner<S: Storage>(storage: S, config: &Config) -> FsInner<S> {
    let cache_size = config.resolve_cache_size() as usize;
    let lookahead_size = config.resolve_lookahead_size() as usize;

    let mut read_buf = vec![0u8; cache_size];
    let mut prog_buf = vec![0u8; cache_size];
    let mut lookahead_buf = vec![0u8; lookahead_size];

    let lfs_config = LfsConfig {
        context: core::ptr::null_mut(),
        read: Some(trampoline_read::<S>),
        prog: Some(trampoline_prog::<S>),
        erase: Some(trampoline_erase::<S>),
        sync: Some(trampoline_sync::<S>),
        read_size: config.read_size,
        prog_size: config.prog_size,
        block_size: config.block_size,
        block_count: config.block_count,
        block_cycles: config.block_cycles,
        cache_size: config.resolve_cache_size(),
        lookahead_size: config.resolve_lookahead_size(),
        compact_thresh: u32::MAX,
        read_buffer: read_buf.as_mut_ptr() as *mut c_void,
        prog_buffer: prog_buf.as_mut_ptr() as *mut c_void,
        lookahead_buffer: lookahead_buf.as_mut_ptr() as *mut c_void,
        name_max: config.name_max,
        file_max: config.file_max,
        attr_max: config.attr_max,
        metadata_max: 0,
        inline_max: 0,
    };

    FsInner {
        lfs: MaybeUninit::zeroed(),
        config: lfs_config,
        storage,
        _read_buf: read_buf,
        _prog_buf: prog_buf,
        _lookahead_buf: lookahead_buf,
        mounted: false,
    }
}

/// Wire `config.context` to point at `inner.storage`. Must be called after
/// `inner` is at its final address (i.e., inside the `RefCell`).
fn wire_context<S: Storage>(inner: &mut FsInner<S>) {
    inner.config.context = &mut inner.storage as *mut S as *mut c_void;
    inner.config.read_buffer = inner._read_buf.as_mut_ptr() as *mut c_void;
    inner.config.prog_buffer = inner._prog_buf.as_mut_ptr() as *mut c_void;
    inner.config.lookahead_buffer = inner._lookahead_buf.as_mut_ptr() as *mut c_void;
}

// ── Filesystem ──────────────────────────────────────────────────────────────

impl<S: Storage> Filesystem<S> {
    /// Format `storage` with a fresh LittleFS filesystem.
    ///
    /// This erases any existing data. The storage can be mounted afterwards
    /// with [`Filesystem::mount`].
    pub fn format(storage: &mut S, config: &Config) -> Result<(), Error> {
        let mut inner = build_inner_borrowed(storage, config);
        wire_context_borrowed(&mut inner);
        let rc = littlefs_rust_core::lfs_format(
            inner.lfs.as_mut_ptr(),
            &inner.config as *const LfsConfig,
        );
        from_lfs_result(rc)
    }

    /// Mount an existing filesystem. Takes ownership of the storage.
    ///
    /// On failure the storage is returned alongside the error so the caller
    /// can retry (e.g. format + mount).
    pub fn mount(storage: S, config: Config) -> Result<Self, (Error, S)> {
        let mut inner = Box::new(build_inner(storage, &config));
        wire_context(&mut inner);
        let rc = littlefs_rust_core::lfs_mount(
            inner.lfs.as_mut_ptr(),
            &inner.config as *const LfsConfig,
        );
        if rc != 0 {
            return Err((Error::from(rc), inner.storage));
        }
        inner.mounted = true;
        Ok(Filesystem {
            inner: RefCell::new(inner),
        })
    }

    /// Unmount and return the underlying storage.
    ///
    /// Prefer this over dropping when you need to check for errors or reuse
    /// the storage.
    pub fn unmount(self) -> Result<S, Error> {
        let this = ManuallyDrop::new(self);
        let mut inner = this.inner.borrow_mut();
        let rc = if inner.mounted {
            inner.mounted = false;
            littlefs_rust_core::lfs_unmount(inner.lfs.as_mut_ptr())
        } else {
            0
        };
        drop(inner);
        // Safety: we prevented Drop from running via ManuallyDrop, and we've
        // already unmounted. Take ownership of the RefCell's contents.
        let fs_inner = unsafe { core::ptr::read(&this.inner) }.into_inner();
        from_lfs_result(rc)?;
        Ok(fs_inner.storage)
    }

    pub(crate) fn cache_size(&self) -> u32 {
        self.inner.borrow().config.cache_size
    }

    // ── File access ─────────────────────────────────────────────────────

    /// Open a file with the given [`OpenFlags`].
    ///
    /// Common combinations: `READ`, `WRITE | CREATE | TRUNC`,
    /// `WRITE | CREATE | APPEND`.
    pub fn open(&self, path: &str, flags: OpenFlags) -> Result<File<'_, S>, Error> {
        File::open(self, path, flags)
    }

    // ── Convenience file I/O ────────────────────────────────────────────

    /// Read an entire file into a `Vec<u8>`.
    pub fn read_to_vec(&self, path: &str) -> Result<Vec<u8>, Error> {
        let file = self.open(path, OpenFlags::READ)?;
        let size = file.size() as usize;
        let mut buf = vec![0u8; size];
        if size > 0 {
            let n = file.read(&mut buf)?;
            buf.truncate(n as usize);
        }
        Ok(buf)
    }

    /// Write `data` to a file, creating or truncating it.
    pub fn write_file(&self, path: &str, data: &[u8]) -> Result<(), Error> {
        let file = self.open(
            path,
            OpenFlags::WRITE | OpenFlags::CREATE | OpenFlags::TRUNC,
        )?;
        let mut offset = 0;
        while offset < data.len() {
            let n = file.write(&data[offset..])? as usize;
            offset += n;
        }
        Ok(())
    }

    // ── Path operations ─────────────────────────────────────────────────

    /// Create a directory. Fails if it already exists.
    pub fn mkdir(&self, path: &str) -> Result<(), Error> {
        let path_bytes = null_terminate(path);
        let mut inner = self.inner.borrow_mut();
        let rc = littlefs_rust_core::lfs_mkdir(inner.lfs.as_mut_ptr(), path_bytes.as_ptr());
        from_lfs_result(rc)
    }

    /// Remove a file or empty directory.
    pub fn remove(&self, path: &str) -> Result<(), Error> {
        let path_bytes = null_terminate(path);
        let mut inner = self.inner.borrow_mut();
        let rc = littlefs_rust_core::lfs_remove(inner.lfs.as_mut_ptr(), path_bytes.as_ptr());
        from_lfs_result(rc)
    }

    /// Rename or move a file or directory.
    pub fn rename(&self, from: &str, to: &str) -> Result<(), Error> {
        let from_bytes = null_terminate(from);
        let to_bytes = null_terminate(to);
        let mut inner = self.inner.borrow_mut();
        let rc = littlefs_rust_core::lfs_rename(
            inner.lfs.as_mut_ptr(),
            from_bytes.as_ptr(),
            to_bytes.as_ptr(),
        );
        from_lfs_result(rc)
    }

    /// Get metadata for a file or directory.
    pub fn stat(&self, path: &str) -> Result<Metadata, Error> {
        let path_bytes = null_terminate(path);
        let mut info = MaybeUninit::<LfsInfo>::zeroed();
        {
            let mut inner = self.inner.borrow_mut();
            let rc = littlefs_rust_core::lfs_stat(
                inner.lfs.as_mut_ptr(),
                path_bytes.as_ptr(),
                info.as_mut_ptr(),
            );
            from_lfs_result(rc)?;
        }
        let entry = dir_entry_from_info(unsafe { &*info.as_ptr() });
        Ok(Metadata {
            name: entry.name,
            file_type: entry.file_type,
            size: entry.size,
        })
    }

    /// Returns `true` if `path` exists.
    pub fn exists(&self, path: &str) -> bool {
        self.stat(path).is_ok()
    }

    // ── Directory listing ───────────────────────────────────────────────

    /// Open a directory for iteration. The returned [`ReadDir`] is an
    /// [`Iterator`] that skips `.` and `..` entries.
    pub fn read_dir(&self, path: &str) -> Result<ReadDir<'_, S>, Error> {
        ReadDir::open(self, path)
    }

    /// Collect all entries in a directory into a `Vec`.
    pub fn list_dir(&self, path: &str) -> Result<Vec<DirEntry>, Error> {
        let dir = self.read_dir(path)?;
        dir.collect()
    }

    // ── FS-level ────────────────────────────────────────────────────────

    /// Return the number of allocated blocks.
    pub fn fs_size(&self) -> Result<u32, Error> {
        let mut inner = self.inner.borrow_mut();
        let rc = littlefs_rust_core::lfs_fs_size(inner.lfs.as_mut_ptr());
        from_lfs_size(rc)
    }

    /// Run garbage collection to reclaim unused blocks.
    pub fn gc(&self) -> Result<(), Error> {
        let mut inner = self.inner.borrow_mut();
        let rc = littlefs_rust_core::lfs_fs_gc(inner.lfs.as_mut_ptr());
        from_lfs_result(rc)
    }
}

impl<S: Storage> Drop for Filesystem<S> {
    fn drop(&mut self) {
        if let Ok(mut inner) = self.inner.try_borrow_mut() {
            if inner.mounted {
                let _ = littlefs_rust_core::lfs_unmount(inner.lfs.as_mut_ptr());
                inner.mounted = false;
            }
        }
    }
}

// ── format helper (borrows storage instead of taking ownership) ─────────────

struct BorrowedFsInner<'a, S: Storage> {
    lfs: MaybeUninit<Lfs>,
    config: LfsConfig,
    storage: &'a mut S,
    _read_buf: Vec<u8>,
    _prog_buf: Vec<u8>,
    _lookahead_buf: Vec<u8>,
}

fn build_inner_borrowed<'a, S: Storage>(
    storage: &'a mut S,
    config: &Config,
) -> BorrowedFsInner<'a, S> {
    let cache_size = config.resolve_cache_size() as usize;
    let lookahead_size = config.resolve_lookahead_size() as usize;

    let mut read_buf = vec![0u8; cache_size];
    let mut prog_buf = vec![0u8; cache_size];
    let mut lookahead_buf = vec![0u8; lookahead_size];

    let lfs_config = LfsConfig {
        context: core::ptr::null_mut(),
        read: Some(trampoline_read::<S>),
        prog: Some(trampoline_prog::<S>),
        erase: Some(trampoline_erase::<S>),
        sync: Some(trampoline_sync::<S>),
        read_size: config.read_size,
        prog_size: config.prog_size,
        block_size: config.block_size,
        block_count: config.block_count,
        block_cycles: config.block_cycles,
        cache_size: config.resolve_cache_size(),
        lookahead_size: config.resolve_lookahead_size(),
        compact_thresh: u32::MAX,
        read_buffer: read_buf.as_mut_ptr() as *mut c_void,
        prog_buffer: prog_buf.as_mut_ptr() as *mut c_void,
        lookahead_buffer: lookahead_buf.as_mut_ptr() as *mut c_void,
        name_max: config.name_max,
        file_max: config.file_max,
        attr_max: config.attr_max,
        metadata_max: 0,
        inline_max: 0,
    };

    BorrowedFsInner {
        lfs: MaybeUninit::zeroed(),
        config: lfs_config,
        storage,
        _read_buf: read_buf,
        _prog_buf: prog_buf,
        _lookahead_buf: lookahead_buf,
    }
}

fn wire_context_borrowed<S: Storage>(inner: &mut BorrowedFsInner<'_, S>) {
    inner.config.context = inner.storage as *mut S as *mut c_void;
    inner.config.read_buffer = inner._read_buf.as_mut_ptr() as *mut c_void;
    inner.config.prog_buffer = inner._prog_buf.as_mut_ptr() as *mut c_void;
    inner.config.lookahead_buffer = inner._lookahead_buf.as_mut_ptr() as *mut c_void;
}

fn null_terminate(s: &str) -> Vec<u8> {
    let mut v: Vec<u8> = s.bytes().collect();
    v.push(0);
    v
}