Created
June 12, 2014 15:31
-
-
Save bollu/1044c871c1e7d9a7a58c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub struct CopyFileOperation { | |
src_path: &Path, | |
dest_path: &Path, | |
src_file: Option<File>, | |
dest_file: Option<File>, | |
KB_per_cycle: uint, | |
done: bool, | |
} | |
impl CopyFileOperation { | |
pub fn new(src_path: &Path, dest_path: &Path) -> CopyFileOperation { | |
let src_not_found_err = format!("invalid dource file | {} |", src_path.display()); | |
let dest_not_found_err = format!("invalid dest file | {} |", dest_path.display()); | |
CopyFileOperation { | |
src_path: src_path, | |
dest_path: dest_path. | |
src_file: None, | |
dest_file: None | |
//src: File::open_mode(src_path, Open, Read).ok().expect(src_not_found_err.as_slice()), | |
//dest: File::open_mode(dest_path, Open, Write).ok().expect(dest_not_found_err.as_slice()), | |
KB_per_cycle: 1000, | |
done: false, | |
} | |
} | |
} | |
impl Operation for CopyFileOperation { | |
fn run(&mut self) -> bool { | |
let mut buf = [0u8, ..1000]; | |
let result = self.src.read(buf); | |
match result { | |
Ok(num_bytes_read) => { | |
self.dest.write(buf.slice_to(num_bytes_read)).unwrap(); | |
self.done = false; | |
}, | |
Err(error) => { | |
if error.kind == EndOfFile { | |
self.done = true; | |
} | |
else { | |
self.done = false; | |
fail!("file write error: {}\n\t{}", error.kind, error.desc) | |
} | |
} | |
} | |
self.done | |
} | |
fn done(&self) -> bool { | |
self.done | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment