mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
Merge pull request #89 from Arcterus/truncate
truncate: get file size using file stat
This commit is contained in:
commit
146f507cdb
1 changed files with 4 additions and 19 deletions
|
@ -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())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue