Skip to content

Commit 4063554

Browse files
committed
Canonicalize paths
1 parent 81e6deb commit 4063554

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/tarballer.rs

+6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ impl Tarballer {
6363
}
6464
for path in files {
6565
let src = Path::new(&self.work_dir).join(&path);
66+
let src = src
67+
.parent()
68+
.unwrap()
69+
.canonicalize()
70+
.map(|path| path.join(src.file_name().unwrap()))
71+
.unwrap_or_else(|_| src.to_owned());
6672
append_path(&mut builder, &src, &path)
6773
.with_context(|| format!("failed to tar file '{}'", src.display()))?;
6874
}

src/util.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ pub fn path_to_str(path: &Path) -> Result<&str> {
2121

2222
/// Wraps `fs::copy` with a nicer error message.
2323
pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<u64> {
24+
// Canonicalize the paths. On windows this results in `\\?\` paths for which the `MAX_PATH`
25+
// limit doesn't apply. Fallback to the original path if canonicalization fails. This can happen
26+
// on Windows when using a network drive.
27+
let from = from.as_ref().canonicalize().unwrap_or_else(|_| from.as_ref().to_owned());
28+
let to = to.as_ref().parent().unwrap().canonicalize().map(|path| path.join(to.as_ref().file_name().unwrap())).unwrap_or_else(|_| to.as_ref().to_owned());
29+
2430
if fs::symlink_metadata(&from)?.file_type().is_symlink() {
2531
let link = fs::read_link(&from)?;
2632
symlink_file(link, &to)?;
@@ -29,11 +35,11 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<u64> {
2935
let amt = fs::copy(&from, &to).with_context(|| {
3036
format!(
3137
"failed to copy '{}' ({}) to '{}' ({}, parent {})",
32-
from.as_ref().display(),
33-
if from.as_ref().exists() { "exists" } else { "doesn't exist" },
34-
to.as_ref().display(),
35-
if to.as_ref().exists() { "exists" } else { "doesn't exist" },
36-
if to.as_ref().parent().unwrap_or_else(|| Path::new("")).exists() {
38+
from.display(),
39+
if from.exists() { "exists" } else { "doesn't exist" },
40+
to.display(),
41+
if to.exists() { "exists" } else { "doesn't exist" },
42+
if to.parent().unwrap_or_else(|| Path::new("")).exists() {
3743
"exists"
3844
} else {
3945
"doesn't exist"

0 commit comments

Comments
 (0)