Use dunce to canonicalize paths

This commit is contained in:
Jason Shirk 2020-04-08 23:19:52 -07:00
parent 0aeba8d09b
commit 55825f6082
4 changed files with 12 additions and 30 deletions

7
Cargo.lock generated
View File

@ -136,6 +136,11 @@ dependencies = [
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "dunce"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.1.14" version = "0.1.14"
@ -432,6 +437,7 @@ dependencies = [
"bstr 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", "bstr 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
"dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"dunce 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)",
"structopt 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)",
"uuid 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -456,6 +462,7 @@ dependencies = [
"checksum crossbeam-utils 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" "checksum crossbeam-utils 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8"
"checksum dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "13aea89a5c93364a98e9b37b2fa237effbb694d5cfe01c5b70941f7eb087d5e3" "checksum dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "13aea89a5c93364a98e9b37b2fa237effbb694d5cfe01c5b70941f7eb087d5e3"
"checksum dirs-sys 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "afa0b23de8fd801745c471deffa6e12d248f962c9fd4b4c33787b055599bde7b" "checksum dirs-sys 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "afa0b23de8fd801745c471deffa6e12d248f962c9fd4b4c33787b055599bde7b"
"checksum dunce 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d0ad6bf6a88548d1126045c413548df1453d9be094a8ab9fd59bf1fdd338da4f"
"checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" "checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
"checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" "checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205"
"checksum hermit-abi 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "725cf19794cf90aa94e65050cb4191ff5d8fa87a498383774c47b332e3af952e" "checksum hermit-abi 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "725cf19794cf90aa94e65050cb4191ff5d8fa87a498383774c47b332e3af952e"

View File

@ -17,6 +17,7 @@ anyhow = "1.0.28"
bincode = "1.2.1" bincode = "1.2.1"
clap = "2.33.0" clap = "2.33.0"
dirs = "2.0.2" dirs = "2.0.2"
dunce = "1.0"
serde = { version = "1.0.106", features = ["derive"] } serde = { version = "1.0.106", features = ["derive"] }
structopt = "0.3.12" structopt = "0.3.12"
uuid = { version = "0.8.1", features = ["v4"] } uuid = { version = "0.8.1", features = ["v4"] }

View File

@ -1,6 +1,5 @@
use crate::config; use crate::config;
use crate::dir::{Dir, Epoch, Rank}; use crate::dir::{Dir, Epoch, Rank};
use crate::util;
use anyhow::{anyhow, bail, Context, Result}; use anyhow::{anyhow, bail, Context, Result};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -145,7 +144,7 @@ impl DB {
continue; continue;
} }
}; };
let path_abs = match Path::new(path_str).canonicalize() { let path_abs = match dunce::canonicalize(Path::new(path_str)) {
Ok(path) => path, Ok(path) => path,
Err(e) => { Err(e) => {
eprintln!("invalid path '{}' at line {}: {}", path_str, line_number, e); eprintln!("invalid path '{}' at line {}: {}", path_str, line_number, e);
@ -186,10 +185,7 @@ impl DB {
} }
pub fn add<P: AsRef<Path>>(&mut self, path: P, max_age: Rank, now: Epoch) -> Result<()> { pub fn add<P: AsRef<Path>>(&mut self, path: P, max_age: Rank, now: Epoch) -> Result<()> {
let path_abs = path let path_abs = dunce::canonicalize(&path)
.as_ref()
.canonicalize()
.and_then(|path| Ok(util::remove_verbatim_disk_in_path(path)))
.with_context(|| anyhow!("could not access directory: {}", path.as_ref().display()))?; .with_context(|| anyhow!("could not access directory: {}", path.as_ref().display()))?;
match self.data.dirs.iter_mut().find(|dir| dir.path == path_abs) { match self.data.dirs.iter_mut().find(|dir| dir.path == path_abs) {
@ -259,7 +255,7 @@ impl DB {
} }
pub fn remove<P: AsRef<Path>>(&mut self, path: P) -> Result<()> { pub fn remove<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
if let Ok(path_abs) = path.as_ref().canonicalize() { if let Ok(path_abs) = dunce::canonicalize(&path) {
self.remove_exact(path_abs) self.remove_exact(path_abs)
.or_else(|_| self.remove_exact(path)) .or_else(|_| self.remove_exact(path))
} else { } else {

View File

@ -7,7 +7,7 @@ use anyhow::{anyhow, bail, Context, Result};
use std::cmp::Reverse; use std::cmp::Reverse;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::path::{Component, Path, PathBuf, Prefix}; use std::path::Path;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
use std::time::SystemTime; use std::time::SystemTime;
@ -26,28 +26,6 @@ pub fn path_to_bytes<P: AsRef<Path>>(path: &P) -> Result<&[u8]> {
} }
} }
#[cfg(not(windows))]
pub fn remove_verbatim_disk_in_path(path: PathBuf) -> PathBuf {
path
}
#[cfg(windows)]
pub fn remove_verbatim_disk_in_path(path: PathBuf) -> PathBuf {
let mut new_path = PathBuf::new();
for component in path.components() {
if let Component::Prefix(prefix) = component {
if let Prefix::VerbatimDisk(drive) = prefix.kind() {
new_path.push(format!("{}:", drive as char));
continue;
}
}
new_path.push(component);
}
new_path
}
#[cfg(unix)] #[cfg(unix)]
pub fn bytes_to_path(bytes: &[u8]) -> Result<&Path> { pub fn bytes_to_path(bytes: &[u8]) -> Result<&Path> {
use std::ffi::OsStr; use std::ffi::OsStr;