Skip to content
Myr

Enhanced Windows support, see #70

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Use easier way to convert path components * Fix failing tests on Windows

authored by David Peter · committed by GitHub · parent a84536a · Browse files · 4 files+24 added−19 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+24 added−19 removed

  • tests:+5 added−6 removed
  • other:+19 added−13 removed

4 files+24 added−19 removed

modified:src/fshelper/mod.rs+10 added
1use std::path::{Path, PathBuf};2use std::io;34/// Get a relative path with respect to a certain base path.5/// See: https://stackoverflow.com/a/39343127/7048316pub fn path_relative_from(path: &Path, base: &Path) -> Option<PathBuf> {
34 unmodified lines
41 }42 Some(comps.iter().map(|c| c.as_os_str()).collect())43 }44}4546pub fn absolute_path(path: &Path) -> io::Result<PathBuf> {47 let path_buf = path.canonicalize()?;4849 #[cfg(windows)]50 let path_buf = Path::new(path_buf.as_path().to_string_lossy().trim_left_matches(r"\\?\")).to_path_buf();5152 Ok(path_buf)53}
modified:src/main.rs+9 added-13 removed
8 unmodified lines
9pub mod lscolors;10pub mod fshelper;11mod app;1213use std::borrow::Cow;13use std::env;14use std::error::Error;15use std::io::Write;16use std::ops::Deref;18#[cfg(target_family = "unix")]17#[cfg(unix)]18use std::os::unix::fs::PermissionsExt;20use std::path::{Component, Path, PathBuf};19use std::path::{Path, PathBuf};20use std::process;21use std::sync::Arc;22use std::sync::mpsc::channel;23use std::thread;
85 unmodified lines
109 Streaming,110}111112/// Root directory113#[cfg(unix)]114static ROOT_DIR: &'static str = "/";115116/// Parent directory117static PARENT_DIR: &'static str = "..";116#[cfg(windows)]117static ROOT_DIR: &'static str = "";118119/// Print a search result to the console.120fn print_entry(base: &Path, entry: &PathBuf, config: &FdOptions) {121 let path_full = base.join(entry);122123 let path_str = entry.to_string_lossy();124125 #[cfg(target_family = "unix")]125 #[cfg(unix)]126 let is_executable = |p: Option<&std::fs::Metadata>| {127 p.map(|f| f.permissions().mode() & 0o111 != 0)128 .unwrap_or(false)129 };130131 #[cfg(not(target_family = "unix"))]131 #[cfg(windows)]132 let is_executable = |_: Option<&std::fs::Metadata>| false;133134 let stdout = std::io::stdout();135 let mut handle = stdout.lock();
8 unmodified lines
144 }145146 // Traverse the path and colorize each component147 for component in entry.components() {148 let comp_str = match component {149 Component::Normal(p) => p.to_string_lossy(),150 Component::ParentDir => Cow::from(PARENT_DIR),151 _ => error("Error: unexpected path component.")152 };148 let comp_str = component.as_os_str().to_string_lossy();149150 component_path.push(Path::new(comp_str.deref()));151152 let metadata = component_path.metadata().ok();
215 unmodified lines
368 let path = Path::new(rd);369370 root_dir_is_absolute = path.is_absolute();371376 path.canonicalize().unwrap_or_else(372 fshelper::absolute_path(path).unwrap_or_else(373 |_| error(&format!("Error: could not find directory '{}'.", rd))374 )375 } else {376 current_dir_buf.clone()
78 unmodified lines
modified:tests/testenv/mod.rs+2 added
45 unmodified lines
4647 #[cfg(unix)]48 unix::fs::symlink(root.join("one/two"), root.join("symlink"))?;4950 // Note: creating symlinks on Windows requires the `SeCreateSymbolicLinkPrivilege` which51 // is by default only granted for administrators.52 #[cfg(windows)]53 windows::fs::symlink_dir(root.join("one/two"), root.join("symlink"))?;5455 fs::File::create(root.join(".ignore"))?.write_all(b"ignored.foo")?;
116 unmodified lines
modified:tests/tests.rs+3 added-6 removed
1//! Integration tests for the CLI interface of fd.23#![allow(dead_code, unused_imports)]43mod testenv;45use testenv::TestEnv;6
29 unmodified lines
36 symlink");37}3839/// Explicit root path42// TODO: Fails on windows43#[cfg_attr(windows, ignore)]40#[test]41fn test_explicit_root_path() {42 let te = TestEnv::new();43
190 unmodified lines
234 symlink");235}236237/// Absolute paths (--absolute-path)242// TODO: fails on windows243#[cfg_attr(windows, ignore)]238#[test]239fn test_absolute_path() {240 let te = TestEnv::new();241
1 unmodified line
243 .canonicalize().expect("absolute path")244 .to_str().expect("string")245 .to_string();246247 #[cfg(windows)]248 let abs_path = abs_path.trim_left_matches(r"\\?\");249250 te.assert_output(251 &["--absolute-path", "foo"],252 &format!(253 "{abs_path}/a.foo
71 unmodified lines