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 dir-entry-path feature #80

Merged
merged 2 commits into from
Sep 13, 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
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ jobs:
cargo check --workspace --all-targets --all-features
cargo check --workspace --all-targets --no-default-features
cargo check --workspace --all-targets --no-default-features --features serde
cargo check --workspace --all-targets --no-default-features --features dir-entry-path

- name: Build
run: cargo build --workspace --release --verbose
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Removed `From<littlefs2::path::Error> for littlefs2::io::Error`.
- Removed `object_safe::OpenOptionsCallback`.
- Removed `consts::ATTRBYTES_MAX_TYPE`.
- Removed `dir-entry-path` feature (now always enabled).

[#47]: https://github.com/trussed-dev/littlefs2/pull/47
[#57]: https://github.com/trussed-dev/littlefs2/pull/57
Expand Down
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ serde = { version = "1.0", default-features = false, features = ["derive"] }
# trybuild = "1"

[features]
default = ["dir-entry-path", "serde"]
# use experimental closure-based API
dir-entry-path = ["littlefs2-core/dir-entry-path"]
default = ["serde"]
serde = ["littlefs2-core/serde"]
# enable assertions in backend C code
ll-assertions = ["littlefs2-sys/assertions"]
Expand Down
1 change: 0 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ heapless = "0.7"
serde = { version = "1", default-features = false, features = ["derive"], optional = true }

[features]
dir-entry-path = []
serde = ["dep:serde"]
10 changes: 1 addition & 9 deletions core/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,14 @@ impl<'a> Attribute<'a> {
pub struct DirEntry {
file_name: PathBuf,
metadata: Metadata,
#[cfg(feature = "dir-entry-path")]
path: PathBuf,
}

impl DirEntry {
pub fn new(
file_name: PathBuf,
metadata: Metadata,
#[cfg(feature = "dir-entry-path")] path: PathBuf,
) -> Self {
pub fn new(file_name: PathBuf, metadata: Metadata, path: PathBuf) -> Self {
Self {
file_name,
metadata,
#[cfg(feature = "dir-entry-path")]
path,
}
}
Expand All @@ -151,12 +145,10 @@ impl DirEntry {
/// Returns the full path to the file that this entry represents.
///
/// The full path is created by joining the original path to read_dir with the filename of this entry.
#[cfg(feature = "dir-entry-path")]
pub fn path(&self) -> &Path {
&self.path
}

#[cfg(feature = "dir-entry-path")]
#[doc(hidden)]
// This is used in `crypto-service` to "namespace" paths
// by mutating a DirEntry in-place.
Expand Down
2 changes: 0 additions & 2 deletions core/src/object_safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ pub trait DynFilesystem {
fn available_space(&self) -> Result<usize>;
fn remove(&self, path: &Path) -> Result<()>;
fn remove_dir(&self, path: &Path) -> Result<()>;
#[cfg(feature = "dir-entry-path")]
fn remove_dir_all(&self, path: &Path) -> Result<()>;
#[cfg(feature = "dir-entry-path")]
fn remove_dir_all_where(&self, path: &Path, predicate: Predicate<'_>) -> Result<usize>;
fn rename(&self, from: &Path, to: &Path) -> Result<()>;
fn exists(&self, path: &Path) -> bool;
Expand Down
65 changes: 22 additions & 43 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ fn metadata(info: ll::lfs_info) -> Metadata {
Metadata::new(file_type, info.size as usize)
}

#[cfg(feature = "dir-entry-path")]
struct RemoveDirAllProgress {
files_removed: usize,
skipped_any: bool,
Expand Down Expand Up @@ -274,13 +273,11 @@ impl<Storage: driver::Storage> Filesystem<'_, Storage> {

/// TODO: This method fails if some `println!` calls are removed.
/// Whyy?
#[cfg(feature = "dir-entry-path")]
pub fn remove_dir_all(&self, path: &Path) -> Result<()> {
self.remove_dir_all_where(path, &|_| true).map(|_| ())
}

/// Returns number of deleted files + whether the directory was fully deleted or not
#[cfg(feature = "dir-entry-path")]
fn remove_dir_all_where_inner<P>(
&self,
path: &Path,
Expand Down Expand Up @@ -338,7 +335,6 @@ impl<Storage: driver::Storage> Filesystem<'_, Storage> {
})
}

#[cfg(feature = "dir-entry-path")]
pub fn remove_dir_all_where<P>(&self, path: &Path, predicate: &P) -> Result<usize>
where
P: Fn(&DirEntry) -> bool,
Expand Down Expand Up @@ -927,8 +923,7 @@ pub struct ReadDir<'a, 'b, S: driver::Storage> {
// to the field alloc.state, so we cannot assert unique mutable access.
alloc: RefCell<*mut ReadDirAllocation>,
fs: &'b Filesystem<'a, S>,
#[cfg(feature = "dir-entry-path")]
path: PathBuf,
path: &'b Path,
}

impl<'a, 'b, S: driver::Storage> Iterator for ReadDir<'a, 'b, S> {
Expand All @@ -953,15 +948,9 @@ impl<'a, 'b, S: driver::Storage> Iterator for ReadDir<'a, 'b, S> {
let file_name = unsafe { PathBuf::from_buffer_unchecked(info.name) };
let metadata = metadata(info);

#[cfg(feature = "dir-entry-path")]
let path = self.path.join(&file_name);

let dir_entry = DirEntry::new(
file_name,
metadata,
#[cfg(feature = "dir-entry-path")]
path,
);
let dir_entry = DirEntry::new(file_name, metadata, path);
return Some(Ok(dir_entry));
}

Expand Down Expand Up @@ -1023,7 +1012,7 @@ impl<'a, Storage: driver::Storage> Filesystem<'a, Storage> {
pub unsafe fn read_dir<'b>(
&'b self,
alloc: &'b mut ReadDirAllocation,
path: &Path,
path: &'b Path,
) -> Result<ReadDir<'a, 'b, Storage>> {
// ll::lfs_dir_open stores a copy of the pointer to alloc.state, so
// we must use addr_of_mut! here, since &mut alloc.state asserts unique
Expand All @@ -1037,8 +1026,7 @@ impl<'a, Storage: driver::Storage> Filesystem<'a, Storage> {
let read_dir = ReadDir {
alloc: RefCell::new(alloc),
fs: self,
#[cfg(feature = "dir-entry-path")]
path: PathBuf::from(path),
path,
};

result_from(read_dir, return_code)
Expand Down Expand Up @@ -1254,7 +1242,6 @@ mod tests {
// unsafe { file.close() }
// }).unwrap();

#[cfg(feature = "dir-entry-path")]
fs.read_dir_and_then(b"/\0".try_into().unwrap(), |read_dir| {
for entry in read_dir {
let entry = entry?;
Expand All @@ -1274,24 +1261,20 @@ mod tests {
for entry in read_dir {
let entry = entry?;
println!("entry: {:?}", entry.file_name());
#[cfg(feature = "dir-entry-path")]
{
println!("path: {:?}", entry.path());

let attribute: &[u8] = if entry.file_type().is_dir() {
b"directory alarm"
} else {
// not 100% sure this is allowed, but if seems to work :)
fs.write(entry.path(), b"Alles neu macht n\xc3\xa4chstens der Mai")?;
b"ceci n'est pas une pipe"
};
fs.set_attribute(entry.path(), 37, attribute)?;
}
println!("path: {:?}", entry.path());

let attribute: &[u8] = if entry.file_type().is_dir() {
b"directory alarm"
} else {
// not 100% sure this is allowed, but if seems to work :)
fs.write(entry.path(), b"Alles neu macht n\xc3\xa4chstens der Mai")?;
b"ceci n'est pas une pipe"
};
fs.set_attribute(entry.path(), 37, attribute)?;
}
Ok(())
})?;

#[cfg(feature = "dir-entry-path")]
fs.read_dir_and_then(b"/tmp/test\0".try_into().unwrap(), |read_dir| {
for (i, entry) in read_dir.enumerate() {
let entry = entry?;
Expand Down Expand Up @@ -1334,17 +1317,14 @@ mod tests {
Ok(())
})?;

#[cfg(feature = "dir-entry-path")]
{
println!("\nDELETION SPREE\n");
// behaves veeryweirldy
// (...)
// entry = DirEntry { file_name: "test", metadata: Metadata { file_type: Dir, size: 0 }, path: "/tmp\u{0}/test" }
// (...)
// fs.remove_dir_all(&PathBuf::from(b"/tmp\0"))?;
// fs.remove_dir_all(&PathBuf::from(b"/tmp"))?;
fs.remove_dir_all(path!("/tmp"))?;
}
println!("\nDELETION SPREE\n");
// behaves veeryweirldy
// (...)
// entry = DirEntry { file_name: "test", metadata: Metadata { file_type: Dir, size: 0 }, path: "/tmp\u{0}/test" }
// (...)
// fs.remove_dir_all(&PathBuf::from(b"/tmp\0"))?;
// fs.remove_dir_all(&PathBuf::from(b"/tmp"))?;
fs.remove_dir_all(path!("/tmp"))?;

Ok(())
})
Expand All @@ -1356,7 +1336,6 @@ mod tests {
fs.write(path!("z.txt"), jackson5).unwrap();
}

#[cfg(feature = "dir-entry-path")]
#[test]
fn remove_dir_all() {
let mut test_storage = TestStorage::new();
Expand Down
2 changes: 0 additions & 2 deletions src/object_safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,10 @@ impl<S: Storage> DynFilesystem for Filesystem<'_, S> {
Filesystem::remove_dir(self, path)
}

#[cfg(feature = "dir-entry-path")]
fn remove_dir_all(&self, path: &Path) -> Result<()> {
Filesystem::remove_dir_all(self, path)
}

#[cfg(feature = "dir-entry-path")]
fn remove_dir_all_where(&self, path: &Path, predicate: Predicate<'_>) -> Result<usize> {
Filesystem::remove_dir_all_where(self, path, &|entry| predicate(entry))
}
Expand Down
1 change: 0 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,6 @@ fn test_fancy_open() {
}

#[test]
#[cfg(feature = "dir-entry-path")]
fn remove_dir_all_where() {
let mut backend = Ram::default();
let mut storage = RamStorage::new(&mut backend);
Expand Down
Loading