2018-08-22 01:47:12 +02:00
|
|
|
extern crate crossbeam_channel as channel;
|
2016-10-12 01:57:09 +02:00
|
|
|
extern crate ignore;
|
|
|
|
extern crate walkdir;
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::io::{self, Write};
|
2016-11-06 03:44:15 +02:00
|
|
|
use std::path::Path;
|
|
|
|
use std::thread;
|
2016-10-12 01:57:09 +02:00
|
|
|
|
2016-11-06 03:44:15 +02:00
|
|
|
use ignore::WalkBuilder;
|
2016-10-12 01:57:09 +02:00
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
|
|
|
fn main() {
|
2016-11-06 03:44:15 +02:00
|
|
|
let mut path = env::args().nth(1).unwrap();
|
|
|
|
let mut parallel = false;
|
|
|
|
let mut simple = false;
|
2018-08-22 01:47:12 +02:00
|
|
|
let (tx, rx) = channel::bounded::<DirEntry>(100);
|
2016-11-06 03:44:15 +02:00
|
|
|
if path == "parallel" {
|
|
|
|
path = env::args().nth(2).unwrap();
|
|
|
|
parallel = true;
|
|
|
|
} else if path == "walkdir" {
|
|
|
|
path = env::args().nth(2).unwrap();
|
|
|
|
simple = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
let stdout_thread = thread::spawn(move || {
|
|
|
|
let mut stdout = io::BufWriter::new(io::stdout());
|
2018-08-22 01:47:12 +02:00
|
|
|
for dent in rx {
|
2016-11-06 03:44:15 +02:00
|
|
|
write_path(&mut stdout, dent.path());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if parallel {
|
|
|
|
let walker = WalkBuilder::new(path).threads(6).build_parallel();
|
|
|
|
walker.run(|| {
|
2018-08-22 01:47:12 +02:00
|
|
|
let tx = tx.clone();
|
2016-11-06 03:44:15 +02:00
|
|
|
Box::new(move |result| {
|
|
|
|
use ignore::WalkState::*;
|
|
|
|
|
2018-12-15 15:37:52 +02:00
|
|
|
tx.send(DirEntry::Y(result.unwrap())).unwrap();
|
2016-11-06 03:44:15 +02:00
|
|
|
Continue
|
|
|
|
})
|
|
|
|
});
|
|
|
|
} else if simple {
|
|
|
|
let walker = WalkDir::new(path);
|
|
|
|
for result in walker {
|
2018-12-15 15:37:52 +02:00
|
|
|
tx.send(DirEntry::X(result.unwrap())).unwrap();
|
2016-11-06 03:44:15 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let walker = WalkBuilder::new(path).build();
|
|
|
|
for result in walker {
|
2018-12-15 15:37:52 +02:00
|
|
|
tx.send(DirEntry::Y(result.unwrap())).unwrap();
|
2016-11-06 03:44:15 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-22 01:47:12 +02:00
|
|
|
drop(tx);
|
2016-11-06 03:44:15 +02:00
|
|
|
stdout_thread.join().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
enum DirEntry {
|
|
|
|
X(walkdir::DirEntry),
|
|
|
|
Y(ignore::DirEntry),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DirEntry {
|
|
|
|
fn path(&self) -> &Path {
|
|
|
|
match *self {
|
|
|
|
DirEntry::X(ref x) => x.path(),
|
|
|
|
DirEntry::Y(ref y) => y.path(),
|
|
|
|
}
|
2016-10-12 01:57:09 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-06 03:44:15 +02:00
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn write_path<W: Write>(mut wtr: W, path: &Path) {
|
|
|
|
use std::os::unix::ffi::OsStrExt;
|
|
|
|
wtr.write(path.as_os_str().as_bytes()).unwrap();
|
|
|
|
wtr.write(b"\n").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
fn write_path<W: Write>(mut wtr: W, path: &Path) {
|
|
|
|
wtr.write(path.to_string_lossy().as_bytes()).unwrap();
|
|
|
|
wtr.write(b"\n").unwrap();
|
|
|
|
}
|