Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit 1602590

Browse files
author
Jorge Aparicio
committed
fallback to regular Files on Windows
closes #2
1 parent 711460b commit 1602590

File tree

1 file changed

+52
-21
lines changed

1 file changed

+52
-21
lines changed

src/bin/itmdump.rs

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@ extern crate ref_slice;
55
#[macro_use]
66
extern crate error_chain;
77

8+
use std::io::{Read, Write};
9+
use std::path::PathBuf;
10+
use std::time::Duration;
11+
use std::{env, fs, io, process, thread};
12+
13+
#[cfg(not(unix))]
14+
use std::fs::OpenOptions;
15+
16+
#[cfg(unix)]
817
use std::ffi::CString;
18+
#[cfg(unix)]
919
use std::fs::File;
10-
use std::io::{Read, Write};
20+
#[cfg(unix)]
1121
use std::os::unix::ffi::OsStringExt;
12-
use std::path::PathBuf;
13-
use std::{env, fs, io, process};
1422

1523
use clap::{App, Arg};
1624
use ref_slice::ref_slice_mut;
@@ -22,6 +30,10 @@ mod errors {
2230
}
2331

2432
fn main() {
33+
fn show_backtrace() -> bool {
34+
env::var("RUST_BACKTRACE").as_ref().map(|s| &s[..]) == Ok("1")
35+
}
36+
2537
if let Err(e) = run() {
2638
let stderr = io::stderr();
2739
let mut stderr = stderr.lock();
@@ -34,7 +46,6 @@ fn main() {
3446

3547
if show_backtrace() {
3648
if let Some(backtrace) = e.backtrace() {
37-
writeln!(stderr, "backtrace:").ok();
3849
writeln!(stderr, "{:?}", backtrace).ok();
3950
}
4051
}
@@ -57,20 +68,37 @@ fn run() -> Result<()> {
5768
.chain_err(|| format!("couldn't remove {}", pipe_)));
5869
}
5970

60-
let cpipe = try!(CString::new(pipe.clone().into_os_string().into_vec())
61-
.chain_err(|| format!("error converting {} to a C string", pipe_)));
71+
let mut stream = match () {
72+
#[cfg(unix)]
73+
() => {
74+
let cpipe =
75+
try!(CString::new(pipe.clone().into_os_string().into_vec())
76+
.chain_err(|| {
77+
format!("error converting {} to a C string", pipe_)
78+
}));
79+
80+
match unsafe { libc::mkfifo(cpipe.as_ptr(), 0o644) } {
81+
0 => {}
82+
e => {
83+
try!(Err(io::Error::from_raw_os_error(e)).chain_err(|| {
84+
format!("couldn't create a named pipe in {}", pipe_)
85+
}))
86+
}
87+
}
6288

63-
match unsafe { libc::mkfifo(cpipe.as_ptr(), 0o644) } {
64-
0 => {}
65-
e => {
66-
try!(Err(io::Error::from_raw_os_error(e)).chain_err(|| {
67-
format!("couldn't create a named pipe in {}", pipe_)
68-
}))
89+
let mut stream = try!(File::open(&pipe)
90+
.chain_err(|| format!("couldn't open {}", pipe_)));
6991
}
70-
}
71-
72-
let mut stream = try!(File::open(&pipe)
73-
.chain_err(|| format!("couldn't open {}", pipe_)));
92+
#[cfg(not(unix))]
93+
() => {
94+
try!(OpenOptions::new()
95+
.create(true)
96+
.read(true)
97+
.write(true)
98+
.open(&pipe)
99+
.chain_err(|| format!("couldn't open {}", pipe_)))
100+
}
101+
};
74102

75103
let mut header = 0;
76104

@@ -108,11 +136,14 @@ fn run() -> Result<()> {
108136
}
109137
}
110138
})() {
111-
writeln!(stderr, "error: {}", e).ok();
139+
match e.kind() {
140+
io::ErrorKind::UnexpectedEof => {
141+
thread::sleep(Duration::from_millis(100));
142+
}
143+
_ => {
144+
writeln!(stderr, "error: {:?}", e.kind()).ok();
145+
}
146+
}
112147
}
113148
}
114149
}
115-
116-
fn show_backtrace() -> bool {
117-
env::var("RUST_BACKTRACE").as_ref().map(|s| &s[..]) == Ok("1")
118-
}

0 commit comments

Comments
 (0)