Skip to content

Commit

Permalink
Remove cstr_core and cty dependencies and use littlefs2-sys 0.2.0
Browse files Browse the repository at this point in the history
CStr, c_char, c_void and c_int are now available in core::ffi so we no
longer need dependencies for them.  c_size_t is currently only available
in nightly so we use a typedef instead.

As littlefs2-sys 0.1 is still using cty, we also need to update it to
0.2.0.
  • Loading branch information
robin-nitrokey committed Jun 3, 2024
1 parent be44c0b commit 1289ee9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## Changed

- Enforced const evaluation for `path!`.
- Removed `cstr_core` and `cty` dependencies.
- Updated `littlefs2-sys` dependency to 0.2.0.

[#47]: https://github.com/trussed-dev/littlefs2/pull/47
[#57]: https://github.com/trussed-dev/littlefs2/pull/57
Expand Down
12 changes: 4 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,10 @@ documentation = "https://docs.rs/littlefs2"

[dependencies]
bitflags = "1"
cty = "0.2.1"
delog = "0.1.0"
generic-array = "0.14"
heapless = "0.7"

[dependencies.cstr_core]
default-features = false
version = "0.2"

[dependencies.littlefs2-sys]
version = "0.1.6"
littlefs2-sys = "0.2"

[dependencies.serde]
version = "1"
Expand Down Expand Up @@ -57,3 +50,6 @@ log-error = []
# member `char name[LFS_NAME_MAX+1]`.
# This means that if we change `traits::Storage::FILENAME_MAX_PLUS_ONE`,
# we need to pass this on!

[patch.crates-io]
littlefs2-sys = { git = "https://github.com/trussed-dev/littlefs2-sys.git", tag = "0.2.0" }
10 changes: 7 additions & 3 deletions src/c_stubs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
//!
//! Use this instead of linking to libc if you only need a handful of free functions

use cty::{c_char, c_void, size_t};
use core::ffi::{c_char, c_void};

// see core::ffi::c_size_t: currently, size_t is always usize
#[allow(non_camel_case_types)]
type c_size_t = usize;

extern "C" {
// provided by `compiler-builtins`
fn memcpy(dst: *mut c_void, src: *const c_void, n: size_t) -> *mut c_void;
fn memcpy(dst: *mut c_void, src: *const c_void, n: c_size_t) -> *mut c_void;
}

/// # Safety
Expand All @@ -19,7 +23,7 @@ unsafe fn strcpy(dst: *mut c_char, src: *const c_char) -> *mut c_char {

/// # Safety
/// `s` must point to valid memory; `s` will be treated as a null terminated string
pub unsafe fn strlen(mut s: *const c_char) -> size_t {
pub unsafe fn strlen(mut s: *const c_char) -> c_size_t {
let mut n = 0;
while *s != 0 {
s = s.add(1);
Expand Down
27 changes: 14 additions & 13 deletions src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Experimental Filesystem version using closures.

use bitflags::bitflags;
use core::ffi::{c_int, c_void};
use core::ptr::addr_of;
use core::ptr::addr_of_mut;
use core::{
Expand Down Expand Up @@ -457,7 +458,7 @@ impl<Storage: driver::Storage> Filesystem<'_, Storage> {
&mut self.alloc.borrow_mut().state,
path.as_ptr(),
id,
&mut attribute.data as *mut _ as *mut cty::c_void,
&mut attribute.data as *mut _ as *mut c_void,
attr_max,
)
};
Expand Down Expand Up @@ -489,7 +490,7 @@ impl<Storage: driver::Storage> Filesystem<'_, Storage> {
&mut self.alloc.borrow_mut().state,
path.as_ptr(),
attribute.id,
&attribute.data as *const _ as *const cty::c_void,
&attribute.data as *const _ as *const c_void,
attribute.size as u32,
)
};
Expand All @@ -503,9 +504,9 @@ impl<Storage: driver::Storage> Filesystem<'_, Storage> {
c: *const ll::lfs_config,
block: ll::lfs_block_t,
off: ll::lfs_off_t,
buffer: *mut cty::c_void,
buffer: *mut c_void,
size: ll::lfs_size_t,
) -> cty::c_int {
) -> c_int {
// println!("in lfs_config_read for {} bytes", size);
let storage = unsafe { &mut *((*c).context as *mut Storage) };
debug_assert!(!c.is_null());
Expand All @@ -522,9 +523,9 @@ impl<Storage: driver::Storage> Filesystem<'_, Storage> {
c: *const ll::lfs_config,
block: ll::lfs_block_t,
off: ll::lfs_off_t,
buffer: *const cty::c_void,
buffer: *const c_void,
size: ll::lfs_size_t,
) -> cty::c_int {
) -> c_int {
// println!("in lfs_config_prog");
let storage = unsafe { &mut *((*c).context as *mut Storage) };
debug_assert!(!c.is_null());
Expand All @@ -538,7 +539,7 @@ impl<Storage: driver::Storage> Filesystem<'_, Storage> {

/// C callback interface used by LittleFS to erase data with the lower level system below the
/// filesystem.
extern "C" fn lfs_config_erase(c: *const ll::lfs_config, block: ll::lfs_block_t) -> cty::c_int {
extern "C" fn lfs_config_erase(c: *const ll::lfs_config, block: ll::lfs_block_t) -> c_int {
// println!("in lfs_config_erase");
let storage = unsafe { &mut *((*c).context as *mut Storage) };
let off = block as usize * Storage::BLOCK_SIZE;
Expand Down Expand Up @@ -950,7 +951,7 @@ impl<S: driver::Storage> io::Read for File<'_, '_, S> {
ll::lfs_file_read(
&mut self.fs.alloc.borrow_mut().state,
addr_of_mut!((*(*self.alloc.borrow_mut())).state),
buf.as_mut_ptr() as *mut cty::c_void,
buf.as_mut_ptr() as *mut c_void,
buf.len() as u32,
)
};
Expand Down Expand Up @@ -984,7 +985,7 @@ impl<S: driver::Storage> io::Write for File<'_, '_, S> {
ll::lfs_file_write(
&mut self.fs.alloc.borrow_mut().state,
addr_of_mut!((*(*self.alloc.borrow_mut())).state),
buf.as_ptr() as *const cty::c_void,
buf.as_ptr() as *const c_void,
buf.len() as u32,
)
};
Expand Down Expand Up @@ -1214,11 +1215,11 @@ impl<'a, Storage: driver::Storage> Filesystem<'a, Storage> {

// Not public, user should use `mount`, possibly after `format`
fn new(alloc: &'a mut Allocation<Storage>, storage: &'a mut Storage) -> Self {
alloc.config.context = storage as *mut _ as *mut cty::c_void;
alloc.config.context = storage as *mut _ as *mut c_void;

alloc.config.read_buffer = alloc.cache.read.get() as *mut cty::c_void;
alloc.config.prog_buffer = alloc.cache.write.get() as *mut cty::c_void;
alloc.config.lookahead_buffer = alloc.cache.lookahead.get() as *mut cty::c_void;
alloc.config.read_buffer = alloc.cache.read.get() as *mut c_void;
alloc.config.prog_buffer = alloc.cache.write.get() as *mut c_void;
alloc.config.lookahead_buffer = alloc.cache.lookahead.get() as *mut c_void;

Filesystem {
alloc: RefCell::new(alloc),
Expand Down
14 changes: 9 additions & 5 deletions src/path.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//! Paths

use core::{cmp::Ordering, convert::TryFrom, fmt, iter::FusedIterator, ops, ptr, slice, str};

use cstr_core::CStr;
use cty::{c_char, size_t};
use core::{
cmp::Ordering,
convert::TryFrom,
ffi::{c_char, CStr},
fmt,
iter::FusedIterator,
ops, ptr, slice, str,
};

use crate::consts;

Expand Down Expand Up @@ -404,7 +408,7 @@ pub struct PathBuf {

/// # Safety
/// `s` must point to valid memory; `s` will be treated as a null terminated string
unsafe fn strlen(mut s: *const c_char) -> size_t {
unsafe fn strlen(mut s: *const c_char) -> usize {
let mut n = 0;
while *s != 0 {
s = s.add(1);
Expand Down

0 comments on commit 1289ee9

Please sign in to comment.