diff --git a/Cargo.toml b/Cargo.toml index 0605c3e..976ce3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,12 +24,12 @@ panic = 'unwind' [dependencies] zip = "^0.2.1" encoding = "^0.2" -duct = "^0.8.2" urlparse = "^0.7.3" requests = "^0.0.30" -stderr ="^0.7.0" -poolite ="^0.6.1" -app = { git = "https://github.com/biluohc/app-rs", branch="master", version ="^0.5.3" } +stderr = "^0.7.0" +poolite = "^0.6.1" +app = "^0.5.3" +# app = { git = "https://github.com/biluohc/app-rs", branch="master", version ="^0.5.3" } # app = { path = "../app" } diff --git a/readme.md b/readme.md index e1c34fd..fb227d9 100644 --- a/readme.md +++ b/readme.md @@ -9,7 +9,7 @@ ## Or ```sh -git clone https://github.com/biluohc/zipcs --depth 1 +git clone https://github.com/biluohc/zipcs cd zipcs cargo build --release diff --git a/src/coll/path.rs b/src/coll/path.rs index 09ae33f..e7a28ae 100644 --- a/src/coll/path.rs +++ b/src/coll/path.rs @@ -61,8 +61,8 @@ fn path_recurse(path: OsString, mut depth: i64, config: Arc) -> Result<() let path_decode_result = decode(&path, &config.charset); if config.charset != CharSet::UTF_8 && path_decode_result.is_ok() { let str = path_decode_result.unwrap(); - let noeq = noeq(&str, &path); - if config.store && noeq { + let ne = ne(&str, &path); + if config.store && ne { rename(&path, &str) .map_err(|e| format!("{:?} rename fails: {}", path, e.description()))?; println!("{:?} -> {:?}", path, str); @@ -111,11 +111,11 @@ fn decode(path: &OsString, cs: &CharSet) -> Result> { } #[cfg(unix)] -fn noeq(str: &str, path: &OsString) -> bool { +fn ne(str: &str, path: &OsString) -> bool { str.as_bytes() != path.as_bytes() } #[cfg(windows)] -fn noeq(str: &str, path: &OsString) -> bool { +fn ne(str: &str, path: &OsString) -> bool { str.as_bytes() != path.to_string_lossy().as_bytes() } \ No newline at end of file diff --git a/src/coll/ping.rs b/src/coll/ping.rs old mode 100644 new mode 100755 index 4c1e94b..c4b87a5 --- a/src/coll/ping.rs +++ b/src/coll/ping.rs @@ -1,7 +1,11 @@ -use duct::cmd; use poolite::{IntoPool, Pool}; use stderr::Loger; +#[cfg(windows)] +use super::consts::*; + +use std::process::Command as Cmd; +use std::process::Output; use std::net::ToSocketAddrs; use std::error::Error; use std::sync::Arc; @@ -90,33 +94,80 @@ fn ping(config: Arc, idx: usize, host_len_max: usize) { // host args.push(host); - match cmd("ping", &args[..]).read() { - Ok(o) => { - if config.only_line { - printf(&o, host, host_len_max); - } else { - println!("{}\n", o); - } - } - Err(e) => { - dbstln!("{:?}", e); - errln!("{}", e.description()); - } + let output = Cmd::new("ping") + .args(&args[..]) + .output() + .map_err(|e| panic!("exec ping fails: {}", e.description())) + .unwrap(); + if output.status.success() { + printf(&output, config.only_line, host, host_len_max); + } else { + printf_err(&output, host, host_len_max); } } -fn printf(msg: &str, host: &str, host_len_max: usize) { - let vs: Vec = msg.lines().map(|s| s.to_string()).collect(); +fn printf(msg: &Output, only_line: bool, host: &str, host_len_max: usize) { + assert!(!msg.stdout.is_empty()); + let msg = decode(&msg.stdout[..]); + let msg = msg.trim(); + // -l/--only-line + if !only_line { + println!("{}\n", msg); + return; + } + + let vs: Vec = msg.lines().map(|s| s.trim().to_string()).collect(); dbstln!("{:?}", msg); - let space_fix = |host: &str| { - let mut str = host.to_owned(); - while str.len() < host_len_max { - str += " "; - } - str - }; + + #[cfg(unix)] println!("{}: {} -> {}", - space_fix(host), + space_fix(host, host_len_max), vs[vs.len() - 1], vs[vs.len() - 2]); + + #[cfg(windows)] + println!("{}: {} -> {}", + space_fix(host, host_len_max), + vs[vs.len() - 1], + vs[vs.len() - 3]); +} + +fn printf_err(msg: &Output, host: &str, host_len_max: usize) { + assert!(!msg.stdout.is_empty()); + let msg = decode(&msg.stdout[..]); + let vs: Vec = msg.trim() + .lines() + .map(|s| s.trim().to_string()) + .collect(); + errln!("{}: {}", space_fix(host, host_len_max), vs[vs.len() - 1]); +} + +fn space_fix(msg: &str, host_len_max: usize) -> String { + let mut str = msg.to_owned(); + while str.len() < host_len_max { + str += " "; + } + str +} + +#[cfg(unix)] +fn decode(msg: &[u8]) -> String { + String::from_utf8_lossy(msg).into_owned().to_owned() +} + +#[cfg(windows)] +fn decode(msg: &[u8]) -> String { + let result = CharSet::GB18030.decode(msg); + if result.is_ok() { + return result.unwrap(); + } + let result = CharSet::BIG5_2003.decode(msg); + if result.is_ok() { + return result.unwrap(); + } + let result = CharSet::HZ.decode(msg); + if result.is_ok() { + return result.unwrap(); + } + String::from_utf8_lossy(msg).into_owned().to_owned() } diff --git a/src/main.rs b/src/main.rs index 51b8c29..a0d3baf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,13 @@ -extern crate duct; +extern crate encoding; +extern crate urlparse; +extern crate requests; +extern crate zip; + extern crate app; +extern crate poolite; #[macro_use] extern crate stderr; use stderr::Loger; -extern crate poolite; -extern crate encoding; -extern crate zip; -extern crate urlparse; -extern crate requests; mod coll; mod consts;