Skip to content

Commit

Permalink
convert windows path separators
Browse files Browse the repository at this point in the history
  • Loading branch information
pwinckles committed Feb 25, 2022
1 parent 5dd6129 commit fc1b275
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* -text
19 changes: 17 additions & 2 deletions src/bagit/bag.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use chrono::Local;
use std::borrow::Cow;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::fmt::{Display, Formatter};
Expand Down Expand Up @@ -510,11 +511,11 @@ fn write_manifests<P: AsRef<Path>>(
file_meta.sort_by(|a, b| a.path.cmp(&b.path));

for meta in file_meta {
// TODO on windows, `\` must be converted to `/`
let path = meta.path.to_str().ok_or_else(|| InvalidUtf8Path {
path: meta.path.to_path_buf(),
})?;
let encoded = percent_encode(path);
let normalized = convert_path_separator(encoded.as_ref());

for algorithm in algorithms {
let digest = meta
Expand All @@ -524,7 +525,7 @@ fn write_manifests<P: AsRef<Path>>(
let manifest = manifests
.get_mut(algorithm)
.expect("Missing expected file digest");
writeln!(manifest, "{digest} {encoded}").context(IoGeneralSnafu {})?;
writeln!(manifest, "{digest} {normalized}").context(IoGeneralSnafu {})?;
}
}

Expand Down Expand Up @@ -652,3 +653,17 @@ fn is_hidden_file(name: &OsStr) -> bool {
.map(|name| name.starts_with('.') && name != "." && name != "..")
.unwrap_or(false)
}

#[cfg(target_os = "windows")]
fn convert_path_separator(path: &str) -> Cow<str> {
if path.contains('\\') {
Cow::Owned(path.replace('\\', "/"))
} else {
path.into()
}
}

#[cfg(not(target_os = "windows"))]
fn convert_path_separator(path: &str) -> Cow<str> {
path.into()
}

0 comments on commit fc1b275

Please sign in to comment.