1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

truncate: get file size using file stat

This commit is contained in:
Arcterus 2014-02-07 19:05:53 -08:00
parent 8a04ee296b
commit 44b66c5b32

View file

@ -14,7 +14,7 @@
extern mod extra; extern mod extra;
extern mod getopts; extern mod getopts;
use std::io::{File, Open, ReadWrite, SeekEnd, SeekSet}; use std::io::{File, Open, ReadWrite, fs};
use std::os; use std::os;
use std::u64; use std::u64;
@ -23,28 +23,13 @@ mod util;
macro_rules! get_file_size( macro_rules! get_file_size(
($file:ident, $action:expr) => ({ ($file:ident, $action:expr) => ({
match $file.seek(0, SeekEnd) { match fs::stat($file.path()) {
Ok(_) => {} Ok(stat) => stat.size,
Err(f) => { Err(f) => {
show_error!(1, "{}", f.to_str()); show_error!(1, "{}", f.to_str());
$action $action
} }
} }
let size = match $file.tell() {
Ok(m) => m,
Err(f) => {
show_error!(1, "{}", f.to_str());
$action
}
};
match $file.seek(0, SeekSet) {
Ok(_) => {}
Err(f) => {
show_error!(1, "{}", f.to_str());
$action
}
}
size
}) })
) )
@ -126,7 +111,7 @@ 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) => {
let mut rfile = match File::open(&Path::new(rfilename.clone())) { let rfile = match File::open(&Path::new(rfilename.clone())) {
Ok(m) => m, Ok(m) => m,
Err(f) => { Err(f) => {
crash!(1, "{}", f.to_str()) crash!(1, "{}", f.to_str())