Skip to content

Commit

Permalink
ping: fix it on windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
biluohc committed Apr 13, 2017
1 parent 981a81a commit 80a1827
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 38 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions src/coll/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ fn path_recurse(path: OsString, mut depth: i64, config: Arc<Paths>) -> 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);
Expand Down Expand Up @@ -111,11 +111,11 @@ fn decode(path: &OsString, cs: &CharSet) -> Result<String, Cow<'static, str>> {
}

#[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()
}
97 changes: 74 additions & 23 deletions src/coll/ping.rs
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -90,33 +94,80 @@ fn ping(config: Arc<Pings>, 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<String> = 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<String> = 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<String> = 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()
}
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 80a1827

Please sign in to comment.