Skip to content

Commit

Permalink
Filesystem: require forward slash in group paths
Browse files Browse the repository at this point in the history
Output appropriate separators on Windows for platform-correct URIs.
  • Loading branch information
aschampion committed Dec 6, 2022
1 parent f3615e2 commit 1bbd7ac
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]
### Changed
- `\` is no longer accepted as a group or dataset separator on Windows filesystem. N5 key paths must use `/`.
- The filesystem backend now returns platform correct `\`-separated URIs on Windows.
- Increase MSRV 1.39 -> 1.56 for tool and dependency upgrades.
- Updated `ndarray` from 0.13 to 0.15.

Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ categories = ["encoding", "filesystem", "science"]
default = ["bzip", "filesystem", "gzip", "lz", "use_ndarray", "xz"]

bzip = ["bzip2"]
filesystem = ["fs2", "walkdir"]
filesystem = ["fs2", "relative-path", "walkdir"]
gzip = ["flate2"]
lz = ["lz4"]
lz_pure = ["lz-fear"]
Expand All @@ -35,6 +35,7 @@ lz4 = { version = "1.23", optional = true }
lz-fear = { version = "0.1.1", optional = true }
ndarray = { version = "0.15", optional = true }
num-traits = { version = "0.2", optional = true }
relative-path = { version = "1.7", optional = true }
serde = { version = "1.0", features = ["derive"] }
smallvec = { version = "1", features = ["serde"] }
walkdir = { version = "2", optional = true }
Expand Down
24 changes: 8 additions & 16 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,13 @@ impl N5Filesystem {
// Note: cannot use `canonicalize` on both the constructed dataset path
// and `base_path` and check `starts_with`, because `canonicalize` also
// requires the path exist.
use std::path::{
Component,
Path,
};
use relative_path::RelativePath;
use std::path::Path;

// Normalize the path to be relative.
let mut components = Path::new(path_name).components();
while components.as_path().has_root() {
use std::path::Component;
match components.next() {
Some(Component::Prefix(_)) => {
return Err(Error::new(
Expand All @@ -141,16 +140,14 @@ impl N5Filesystem {
_ => return Err(Error::new(ErrorKind::NotFound, "Path is malformed")),
}
}
let unrooted_path = components.as_path();
let relative_path = RelativePath::from_path(components.as_path())
.map_err(|_| Error::new(ErrorKind::NotFound, "Path is malformed"))?;

// Check that the path is inside the container's base path.
let mut nest: i32 = 0;
for component in unrooted_path.components() {
for component in relative_path.components() {
use relative_path::Component;
match component {
// This should be unreachable.
Component::Prefix(_) | Component::RootDir => {
return Err(Error::new(ErrorKind::NotFound, "Path is malformed"))
}
Component::CurDir => continue,
Component::ParentDir => nest -= 1,
Component::Normal(_) => nest += 1,
Expand All @@ -163,7 +160,7 @@ impl N5Filesystem {
"Path name is outside this N5 filesystem",
))
} else {
Ok(self.base_path.join(unrooted_path))
Ok(relative_path.to_path(&self.base_path))
}
}

Expand Down Expand Up @@ -516,11 +513,6 @@ mod tests {
}

#[test]
// TODO: this test is ignored on windows because the dataset path in the returned URI still includes the unix slash.
// This will be fixed by parsing dataset paths as unix paths in `get_path`, then translating to platform-native
// `PathBuf`s. However, the only way to do this at the moment with the `typed_paths` crate depends on unstable
// features. See also rust issue #66621.
#[cfg_attr(windows, ignore)]
fn test_get_block_uri() {
let dir = TempDir::new("rust_n5_tests").unwrap();
let path_str = dir.path().to_str().unwrap();
Expand Down

0 comments on commit 1bbd7ac

Please sign in to comment.