mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
Update for latest Rust
This commit is contained in:
parent
810936ac42
commit
9197373843
2 changed files with 72 additions and 39 deletions
|
@ -6,8 +6,8 @@ static TESTNAME: &'static str = "THISISARANDOMFILENAME";
|
||||||
fn make_file() -> io::File {
|
fn make_file() -> io::File {
|
||||||
while Path::new(TESTNAME).exists() { io::timer::sleep(1000); }
|
while Path::new(TESTNAME).exists() { io::timer::sleep(1000); }
|
||||||
match io::File::create(&Path::new(TESTNAME)) {
|
match io::File::create(&Path::new(TESTNAME)) {
|
||||||
Some(f) => f,
|
Ok(f) => f,
|
||||||
None => fail!()
|
Err(_) => fail!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ fn test_increase_file_size() {
|
||||||
fail!();
|
fail!();
|
||||||
}
|
}
|
||||||
file.seek(0, io::SeekEnd);
|
file.seek(0, io::SeekEnd);
|
||||||
if file.tell() != 5 * 1024 {
|
if file.tell().unwrap() != 5 * 1024 {
|
||||||
fail!();
|
fail!();
|
||||||
}
|
}
|
||||||
io::fs::unlink(&Path::new(TESTNAME));
|
io::fs::unlink(&Path::new(TESTNAME));
|
||||||
|
@ -32,8 +32,8 @@ fn test_decrease_file_size() {
|
||||||
fail!();
|
fail!();
|
||||||
}
|
}
|
||||||
file.seek(0, io::SeekEnd);
|
file.seek(0, io::SeekEnd);
|
||||||
if file.tell() != 6 {
|
if file.tell().unwrap() != 6 {
|
||||||
println!("{}", file.tell());
|
println!("{}", file.tell());
|
||||||
fail!();
|
fail!();
|
||||||
}
|
}
|
||||||
io::fs::unlink(&Path::new(TESTNAME));
|
io::fs::unlink(&Path::new(TESTNAME));
|
||||||
|
|
|
@ -9,13 +9,45 @@
|
||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#[feature(macro_rules)];
|
||||||
|
|
||||||
extern mod extra;
|
extern mod extra;
|
||||||
|
|
||||||
use std::io::{stderr, io_error, File, Open, ReadWrite, Writer, SeekEnd, SeekSet};
|
use std::io::{stderr, File, Open, ReadWrite, Writer, SeekEnd, SeekSet};
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::u64;
|
use std::u64;
|
||||||
use extra::getopts::groups;
|
use extra::getopts::groups;
|
||||||
|
|
||||||
|
macro_rules! get_file_size(
|
||||||
|
($file:ident, $action:expr) => ({
|
||||||
|
match $file.seek(0, SeekEnd) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(f) => {
|
||||||
|
writeln!(&mut stderr() as &mut Writer, "{}", f.to_str());
|
||||||
|
os::set_exit_status(1);
|
||||||
|
$action
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let size = match $file.tell() {
|
||||||
|
Ok(m) => m,
|
||||||
|
Err(f) => {
|
||||||
|
writeln!(&mut stderr() as &mut Writer, "{}", f.to_str());
|
||||||
|
os::set_exit_status(1);
|
||||||
|
$action
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match $file.seek(0, SeekSet) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(f) => {
|
||||||
|
writeln!(&mut stderr() as &mut Writer, "{}", f.to_str());
|
||||||
|
os::set_exit_status(1);
|
||||||
|
$action
|
||||||
|
}
|
||||||
|
}
|
||||||
|
size
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
#[deriving(Eq)]
|
#[deriving(Eq)]
|
||||||
enum TruncateMode {
|
enum TruncateMode {
|
||||||
Reference,
|
Reference,
|
||||||
|
@ -97,14 +129,15 @@ file based on its current size:
|
||||||
fn truncate(no_create: bool, io_blocks: bool, reference: Option<~str>, size: Option<~str>, filenames: ~[~str]) {
|
fn truncate(no_create: bool, io_blocks: bool, reference: Option<~str>, size: Option<~str>, filenames: ~[~str]) {
|
||||||
let (refsize, mode) = match reference {
|
let (refsize, mode) = match reference {
|
||||||
Some(rfilename) => {
|
Some(rfilename) => {
|
||||||
io_error::cond.trap(|err| {
|
let mut rfile = match File::open(&Path::new(rfilename.clone())) {
|
||||||
writeln!(&mut stderr() as &mut Writer, "{}", err.to_str());
|
Ok(m) => m,
|
||||||
os::set_exit_status(1);
|
Err(f) => {
|
||||||
}).inside(|| {
|
writeln!(&mut stderr() as &mut Writer, "{}", f.to_str());
|
||||||
let mut rfile = File::open(&Path::new(rfilename.clone()));
|
os::set_exit_status(1);
|
||||||
rfile.seek(0, SeekEnd);
|
return
|
||||||
(rfile.tell(), Reference)
|
}
|
||||||
})
|
};
|
||||||
|
(get_file_size!(rfile, return), Reference)
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
match parse_size(size.unwrap()) {
|
match parse_size(size.unwrap()) {
|
||||||
|
@ -116,34 +149,34 @@ fn truncate(no_create: bool, io_blocks: bool, reference: Option<~str>, size: Opt
|
||||||
for filename in filenames.iter() {
|
for filename in filenames.iter() {
|
||||||
let filename: &str = *filename;
|
let filename: &str = *filename;
|
||||||
let path = Path::new(filename);
|
let path = Path::new(filename);
|
||||||
io_error::cond.trap(|err| {
|
if path.exists() || !no_create {
|
||||||
writeln!(&mut stderr() as &mut Writer, "{}", err.to_str());
|
match File::open_mode(&path, Open, ReadWrite) {
|
||||||
}).inside(|| {
|
Ok(mut file) => {
|
||||||
if path.exists() || !no_create {
|
let fsize = get_file_size!(file, continue);
|
||||||
match File::open_mode(&path, Open, ReadWrite) {
|
let tsize = match mode {
|
||||||
Some(mut file) => {
|
Reference => refsize,
|
||||||
file.seek(0, SeekEnd);
|
Extend => fsize + refsize,
|
||||||
let fsize = file.tell();
|
Reduce => fsize - refsize,
|
||||||
file.seek(0, SeekSet);
|
AtMost => if fsize > refsize { refsize } else { fsize },
|
||||||
let tsize = match mode {
|
AtLeast => if fsize < refsize { refsize } else { fsize },
|
||||||
Reference => refsize,
|
RoundDown => fsize - fsize % refsize,
|
||||||
Extend => fsize + refsize,
|
RoundUp => fsize + fsize % refsize
|
||||||
Reduce => fsize - refsize,
|
};
|
||||||
AtMost => if fsize > refsize { refsize } else { fsize },
|
match file.truncate(tsize as i64) {
|
||||||
AtLeast => if fsize < refsize { refsize } else { fsize },
|
Ok(_) => {}
|
||||||
RoundDown => fsize - fsize % refsize,
|
Err(f) => {
|
||||||
RoundUp => fsize + fsize % refsize
|
writeln!(&mut stderr() as &mut Writer,
|
||||||
};
|
"{}", f.to_str());
|
||||||
file.truncate(tsize as i64);
|
os::set_exit_status(1);
|
||||||
}
|
}
|
||||||
None => {
|
|
||||||
writeln!(&mut stderr() as &mut Writer,
|
|
||||||
"Failed to open the file '{}'", filename);
|
|
||||||
os::set_exit_status(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Err(f) => {
|
||||||
|
writeln!(&mut stderr() as &mut Writer, "{}", f.to_str());
|
||||||
|
os::set_exit_status(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue