1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 20:17:45 +00:00

Merge pull request #420 from skv-headless/move_util

move utility
This commit is contained in:
Alex Lyon 2014-10-19 11:50:15 -07:00
commit ea5d67f8dc
5 changed files with 120 additions and 0 deletions

View file

@ -111,6 +111,10 @@ path = "logname/logname.rs"
name = "mkdir" name = "mkdir"
path = "mkdir/mkdir.rs" path = "mkdir/mkdir.rs"
[[bin]]
name = "mv"
path = "mv/mv.rs"
[[bin]] [[bin]]
name = "mkfifo" name = "mkfifo"
path = "mkfifo/mkfifo.rs" path = "mkfifo/mkfifo.rs"

View file

@ -54,6 +54,7 @@ PROGS := \
link \ link \
hashsum \ hashsum \
mkdir \ mkdir \
mv \
nl \ nl \
paste \ paste \
printenv \ printenv \
@ -132,6 +133,7 @@ TEST_PROGS := \
cat \ cat \
cp \ cp \
mkdir \ mkdir \
mv \
nl \ nl \
seq \ seq \
sort \ sort \

89
src/mv/mv.rs Normal file
View file

@ -0,0 +1,89 @@
#![crate_name = "mv"]
/*
* This file is part of the uutils coreutils package.
*
* (c) Sokovikov Evgeniy <skv-headless@yandex.ru>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
#![feature(macro_rules)]
extern crate getopts;
use std::io::fs;
use getopts::{
getopts,
optflag,
usage,
};
static NAME: &'static str = "mv";
static VERSION: &'static str = "0.0.1";
#[path = "../common/util.rs"]
mod util;
pub fn uumain(args: Vec<String>) -> int {
let opts = [
optflag("h", "help", "display this help and exit"),
optflag("V", "version", "output version information and exit"),
];
let matches = match getopts(args.tail(), opts) {
Ok(m) => m,
Err(f) => crash!(1, "Invalid options\n{}", f)
};
let progname = &args[0];
let usage = usage("Move SOURCE to DEST", opts);
if matches.opt_present("version") {
println!("{}", VERSION);
return 0;
}
if matches.opt_present("help") {
help(progname.as_slice(), usage.as_slice());
return 0;
}
let source = if matches.free.len() < 1 {
println!("error: Missing SOURCE argument. Try --help.");
return 1;
} else {
Path::new(matches.free[0].as_slice())
};
let dest = if matches.free.len() < 2 {
println!("error: Missing DEST argument. Try --help.");
return 1;
} else {
Path::new(matches.free[1].as_slice())
};
mv(source, dest)
}
fn mv(source: Path, dest: Path) -> int {
let io_result = fs::rename(&source, &dest);
if io_result.is_err() {
let err = io_result.unwrap_err();
println!("error: {:s}", err.to_string());
1
} else {
0
}
}
fn help(progname: &str, usage: &str) {
let msg = format!("Usage: {0} SOURCE DEST\n \
or: {0} SOURCE... DIRECTORY\n \
\n\
{1}", progname, usage);
println!("{}", msg);
}

1
test/fixtures/mv/hello_world.txt vendored Normal file
View file

@ -0,0 +1 @@
Hello, World!

24
test/mv.rs Normal file
View file

@ -0,0 +1,24 @@
use std::io::process::Command;
use std::io::fs::{PathExtensions};
static EXE: &'static str = "./mv";
static TEST_HELLO_WORLD_SOURCE: &'static str = "hello_world.txt";
static TEST_HELLO_WORLD_DEST: &'static str = "move_of_hello_world.txt";
#[test]
fn test_mv() {
let prog = Command::new(EXE)
.arg(TEST_HELLO_WORLD_SOURCE)
.arg(TEST_HELLO_WORLD_DEST)
.status();
let exit_success = prog.unwrap().success();
assert_eq!(exit_success, true);
let dest = Path::new(TEST_HELLO_WORLD_DEST);
assert!(dest.exists() == true);
let source = Path::new(TEST_HELLO_WORLD_SOURCE);
assert!(source.exists() == false);
}