From 68fd0971d0b6fc268091916e904cec1f0da8e234 Mon Sep 17 00:00:00 2001 From: Arcterus Date: Tue, 2 Dec 2014 21:29:53 -0800 Subject: [PATCH 1/3] Fix build for latest Rust --- src/cp/cp.rs | 8 ++++---- src/du/du.rs | 6 +++--- src/realpath/realpath.rs | 2 +- src/tee/tee.rs | 1 - src/unlink/unlink.rs | 4 ++-- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/cp/cp.rs b/src/cp/cp.rs index a884ec9ce..52dc096d3 100644 --- a/src/cp/cp.rs +++ b/src/cp/cp.rs @@ -122,13 +122,13 @@ fn copy(matches: getopts::Matches) { panic!(); } } else { - if fs::stat(&dest).unwrap().kind != io::TypeDirectory { + if fs::stat(&dest).unwrap().kind != io::FileType::Directory { error!("error: TARGET must be a directory"); panic!(); } for source in sources.iter() { - if fs::stat(source).unwrap().kind != io::TypeFile { + if fs::stat(source).unwrap().kind != io::FileType::RegularFile { error!("error: \"{}\" is not a file", source.display().to_string()); continue; } @@ -165,12 +165,12 @@ pub fn paths_refer_to_same_file(p1: &Path, p2: &Path) -> io::IoResult { }; // We have to take symlinks and relative paths into account. - if p1_lstat.kind == io::TypeSymlink { + if p1_lstat.kind == io::FileType::Symlink { raw_p1 = fs::readlink(&raw_p1).unwrap(); } raw_p1 = os::make_absolute(&raw_p1).unwrap(); - if p2_lstat.kind == io::TypeSymlink { + if p2_lstat.kind == io::FileType::Symlink { raw_p2 = fs::readlink(&raw_p2).unwrap(); } raw_p2 = os::make_absolute(&raw_p2).unwrap(); diff --git a/src/du/du.rs b/src/du/du.rs index f07319cd0..819afa524 100644 --- a/src/du/du.rs +++ b/src/du/du.rs @@ -16,7 +16,7 @@ extern crate getopts; extern crate libc; extern crate time; -use std::io::{stderr, fs, FileStat, TypeDirectory}; +use std::io::{stderr, fs, FileStat, FileType}; use std::num::Float; use std::option::Option; use std::path::Path; @@ -48,7 +48,7 @@ fn du(path: &Path, mut my_stat: Stat, let mut stats = vec!(); let mut futures = vec!(); - if my_stat.fstat.kind == TypeDirectory { + if my_stat.fstat.kind == FileType::Directory { let read = match fs::readdir(path) { Ok(read) => read, Err(e) => { @@ -60,7 +60,7 @@ fn du(path: &Path, mut my_stat: Stat, for f in read.into_iter() { let this_stat = Stat{path: f.clone(), fstat: safe_unwrap!(fs::lstat(&f))}; - if this_stat.fstat.kind == TypeDirectory { + if this_stat.fstat.kind == FileType::Directory { let oa_clone = options.clone(); futures.push(Future::spawn(proc() { du(&f, this_stat, oa_clone, depth + 1) })) } else { diff --git a/src/realpath/realpath.rs b/src/realpath/realpath.rs index 6eabea142..6a2eba810 100644 --- a/src/realpath/realpath.rs +++ b/src/realpath/realpath.rs @@ -89,7 +89,7 @@ fn resolve_path(path: &str, strip: bool, zero: bool, quiet: bool) -> bool { } match std::io::fs::lstat(&result) { Err(_) => break, - Ok(ref s) if s.kind != std::io::TypeSymlink => break, + Ok(ref s) if s.kind != std::io::FileType::Symlink => break, Ok(_) => { links_left -= 1; match std::io::fs::readlink(&result) { diff --git a/src/tee/tee.rs b/src/tee/tee.rs index 40634d567..cd26e58e1 100644 --- a/src/tee/tee.rs +++ b/src/tee/tee.rs @@ -1,5 +1,4 @@ #![crate_name = "tee"] -#![license="MIT"] #![feature(phase)] #![feature(macro_rules)] diff --git a/src/unlink/unlink.rs b/src/unlink/unlink.rs index 899aa989e..36c38fa3d 100644 --- a/src/unlink/unlink.rs +++ b/src/unlink/unlink.rs @@ -64,8 +64,8 @@ pub fn uumain(args: Vec) -> int { let result = path.lstat().and_then(|info| { match info.kind { - io::TypeFile => Ok(()), - io::TypeSymlink => Ok(()), + io::FileType::RegularFile => Ok(()), + io::FileType::Symlink => Ok(()), _ => Err(io::IoError { kind: io::OtherIoError, desc: "is not a file or symlink", From db48d109e7dcbd359233d63836e9874c59766527 Mon Sep 17 00:00:00 2001 From: Arcterus Date: Wed, 3 Dec 2014 10:12:43 -0800 Subject: [PATCH 2/3] Use -O instead of --opt-level 3 (closes #194) --- Makefile | 2 +- src/rm/rm.rs | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index bc7916abe..7a507b4c6 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ TESTDIR := $(BASEDIR)/test TEMPDIR := $(BASEDIR)/tmp # Flags -RUSTCFLAGS := --opt-level=3 -L $(BUILDDIR)/ +RUSTCFLAGS := -O -L $(BUILDDIR)/ RMFLAGS := # Handle config setup diff --git a/src/rm/rm.rs b/src/rm/rm.rs index c157f4016..6fdc7916b 100644 --- a/src/rm/rm.rs +++ b/src/rm/rm.rs @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -#![feature(macro_rules)] +#![feature(if_let, macro_rules)] extern crate getopts; extern crate libc; @@ -114,10 +114,9 @@ pub fn uumain(args: Vec) -> int { return 0; } } - match remove(matches.free, force, interactive, one_fs, preserve_root, - recursive, dir, verbose) { - Ok(()) => ( /* pass */ ), - Err(e) => return e + if let Err(e) = remove(matches.free, force, interactive, one_fs, preserve_root, + recursive, dir, verbose) { + return e; } } From 1c2b7fc160cddace6ec399d9e65bfe0da69fa744 Mon Sep 17 00:00:00 2001 From: Arcterus Date: Wed, 3 Dec 2014 11:06:53 -0800 Subject: [PATCH 3/3] tail: rewrite tail_bytes and tail_lines as one macro --- src/tail/tail.rs | 100 ++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 57 deletions(-) diff --git a/src/tail/tail.rs b/src/tail/tail.rs index 8015295c6..83168fd0b 100644 --- a/src/tail/tail.rs +++ b/src/tail/tail.rs @@ -9,12 +9,12 @@ * */ -#![feature(macro_rules)] +#![feature(if_let, macro_rules)] extern crate getopts; use std::char::UnicodeChar; -use std::io::{stdin}; +use std::io::{stdin, stdout}; use std::io::{BufferedReader, BytesReader}; use std::io::fs::File; use std::path::Path; @@ -235,11 +235,44 @@ fn obsolete(options: &[String]) -> (Vec, Option) { (options, None) } -fn tail(reader: &mut BufferedReader, line_count: uint, byte_count: uint, beginning: bool, lines: bool, follow: bool, sleep_msec: u64) { +macro_rules! tail_impl ( + ($kind:ty, $kindfn:ident, $kindprint:ident, $reader:ident, $count:ident, $beginning:ident) => ({ + // read through each line and store them in a ringbuffer that always contains + // count lines/chars. When reaching the end of file, output the data in the + // ringbuf. + let mut ringbuf: RingBuf<$kind> = RingBuf::new(); + let mut data = $reader.$kindfn().skip( + if $beginning { + let temp = $count; + $count = ::std::uint::MAX; + temp - 1 + } else { + 0 + } + ); + for io_datum in data { + match io_datum { + Ok(datum) => { + if $count <= ringbuf.len() { + ringbuf.pop_front(); + } + ringbuf.push_back(datum); + } + Err(err) => panic!(err) + } + } + let mut stdout = stdout(); + for datum in ringbuf.iter() { + $kindprint(&mut stdout, datum); + } + }) +) + +fn tail(reader: &mut BufferedReader, mut line_count: uint, mut byte_count: uint, beginning: bool, lines: bool, follow: bool, sleep_msec: u64) { if lines { - tail_lines(reader, line_count, beginning); + tail_impl!(String, lines, print_string, reader, line_count, beginning); } else { - tail_bytes(reader, byte_count, beginning); + tail_impl!(u8, bytes, print_byte, reader, byte_count, beginning); } // if we follow the file, sleep a bit and print the rest if the file has grown. @@ -255,62 +288,15 @@ fn tail(reader: &mut BufferedReader, line_count: uint, byte_count: } #[inline] -fn tail_lines(reader: &mut BufferedReader, mut line_count: uint, beginning: bool) { - // read through each line and store them in a ringbuffer that always contains - // line_count lines. When reaching the end of file, output the lines in the - // ringbuf. - let mut ringbuf: RingBuf = RingBuf::new(); - let mut lines = reader.lines().skip( - if beginning { - let temp = line_count; - line_count = ::std::uint::MAX; - temp - 1 - } else { - 0 - } - ); - for io_line in lines { - match io_line { - Ok(line) => { - if line_count <= ringbuf.len() { - ringbuf.pop_front(); - } - ringbuf.push_back(line); - } - Err(err) => panic!(err) - } - } - for line in ringbuf.iter() { - print!("{}", line); +fn print_byte(stdout: &mut T, ch: &u8) { + if let Err(err) = stdout.write_u8(*ch) { + crash!(1, "{}", err); } } #[inline] -fn tail_bytes(reader: &mut BufferedReader, mut byte_count: uint, beginning: bool) { - let mut ringbuf: RingBuf = RingBuf::new(); - let mut bytes = reader.bytes().skip( - if beginning { - let temp = byte_count; - byte_count = ::std::uint::MAX; - temp - 1 - } else { - 0 - } - ); - for io_byte in bytes { - match io_byte { - Ok(byte) => { - if byte_count <= ringbuf.len() { - ringbuf.pop_front(); - } - ringbuf.push_back(byte as char); - } - Err(err) => panic!(err) - } - } - for byte in ringbuf.iter() { - print!("{}", byte); - } +fn print_string(_: &mut T, s: &String) { + print!("{}", s); } fn version () {