Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove panicking path conversions #64

Merged
merged 4 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,31 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

## Added
### Added
- Added object-safe traits `DynFile`, `DynFilesystem` and `DynStorage` for
accessing `Storage`, `Filesystem` and `File` implementations for any storage.
- Added `Filesystem::mount_or_else` function ([#57][])
- Marked `Path::is_empty`, `Path::from_bytes_with_nul`, `Path::from_cstr`, `Path::from_cstr_unchecked`, `Path::as_str_ref_with_trailing_nul`, `Path::as_str`, and `PathBuf::new` as `const`.

## Fixed
### Fixed

- Fixed macro hygiene for `path!`.
- Fixed build error that would occur on Windows systems.
- Fixed compilation without default features.
- Added path iteration utilities ([#47][])

## Changed
### Changed

- Enforced const evaluation for `path!`.
- Removed `cstr_core` and `cty` dependencies.
- Updated `littlefs2-sys` dependency to 0.2.0.
- Replace all panicking `Path`/`PathBuf` conversions with fallible alternatives:
- Return a `Result` from `Path::from_str_with_nul`.
- Replace the `From<_>` implementations for `Path` and `PathBuf` with `TryFrom<_>`, except for `From<&Path> for PathBuf`.

### Removed

- Removed `Path::from_bytes_with_nul_unchecked`. Use `CStr::from_bytes_with_nul_unchecked` and `Path::from_cstr_unchecked` instead.

[#47]: https://github.com/trussed-dev/littlefs2/pull/47
[#57]: https://github.com/trussed-dev/littlefs2/pull/57
Expand Down
7 changes: 4 additions & 3 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ impl<'a, Storage: driver::Storage> Filesystem<'a, Storage> {
let path_slice = path.as_ref().as_bytes();
for i in 0..path_slice.len() {
if path_slice[i] == b'/' {
let dir = PathBuf::from(&path_slice[..i]);
let dir = PathBuf::try_from(&path_slice[..i])?;
#[cfg(test)]
println!("generated PathBuf dir {:?} using i = {}", &dir, i);
match self.create_dir(&dir) {
Expand Down Expand Up @@ -1362,6 +1362,7 @@ impl<'a, Storage: driver::Storage> Filesystem<'a, Storage> {
#[cfg(test)]
mod tests {
use super::*;
use crate::path;
use core::convert::TryInto;
use driver::Storage as LfsStorage;
use io::Result as LfsResult;
Expand Down Expand Up @@ -1483,7 +1484,7 @@ mod tests {
// (...)
// fs.remove_dir_all(&PathBuf::from(b"/tmp\0"))?;
// fs.remove_dir_all(&PathBuf::from(b"/tmp"))?;
fs.remove_dir_all(&PathBuf::from("/tmp"))?;
fs.remove_dir_all(path!("/tmp"))?;
}

Ok(())
Expand All @@ -1493,7 +1494,7 @@ mod tests {
let mut alloc = Allocation::new();
let fs = Filesystem::mount(&mut alloc, &mut test_storage).unwrap();
// fs.write(b"/z.txt\0".try_into().unwrap(), &jackson5).unwrap();
fs.write(&PathBuf::from("z.txt"), jackson5).unwrap();
fs.write(path!("z.txt"), jackson5).unwrap();
}

#[cfg(feature = "dir-entry-path")]
Expand Down
9 changes: 6 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Separately, keeping track of the allocations is a chore, we hope that
```
# use littlefs2::fs::{Filesystem, File, OpenOptions};
# use littlefs2::io::prelude::*;
# use littlefs2::path::PathBuf;
# use littlefs2::path;
#
# use littlefs2::{consts, ram_storage, driver, io::Result};
#
Expand All @@ -113,7 +113,7 @@ let mut fs = Filesystem::mount(&mut alloc, &mut storage).unwrap();
let mut buf = [0u8; 11];
fs.open_file_with_options_and_then(
|options| options.read(true).write(true).create(true),
&PathBuf::from(b"example.txt"),
path!("example.txt"),
|file| {
file.write(b"Why is black smoke coming out?!")?;
file.seek(SeekFrom::End(-24)).unwrap();
Expand Down Expand Up @@ -199,7 +199,10 @@ pub struct Version {
macro_rules! path {
($path:literal) => {{
const _PATH: &$crate::path::Path =
$crate::path::Path::from_str_with_nul(::core::concat!($path, "\0"));
match $crate::path::Path::from_str_with_nul(::core::concat!($path, "\0")) {
Ok(path) => path,
Err(_) => panic!("invalid littlefs2 path"),
};
_PATH
}};
}
Expand Down
Loading
Loading