mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Merge pull request #174 from ebfe/fix-cp
Add cp back to Makefile and fix build.
This commit is contained in:
commit
f1f8b7252d
2 changed files with 25 additions and 24 deletions
1
Makefile
1
Makefile
|
@ -7,6 +7,7 @@ PROGS := \
|
||||||
cat \
|
cat \
|
||||||
cksum \
|
cksum \
|
||||||
comm \
|
comm \
|
||||||
|
cp \
|
||||||
dirname \
|
dirname \
|
||||||
echo \
|
echo \
|
||||||
env \
|
env \
|
||||||
|
|
48
cp/cp.rs
48
cp/cp.rs
|
@ -1,6 +1,6 @@
|
||||||
#[crate_id(name="cp", vers="1.0.0", author="Jordy Dickinson")];
|
#![crate_id(name="cp", vers="1.0.0", author="Jordy Dickinson")]
|
||||||
#[feature(macro_rules)];
|
#![feature(macro_rules)]
|
||||||
#[feature(phase)];
|
#![feature(phase)]
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the uutils coreutils package.
|
* This file is part of the uutils coreutils package.
|
||||||
|
@ -32,7 +32,7 @@ pub enum Mode {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = os::args();
|
let args: Vec<StrBuf> = os::args().iter().map(|x| x.to_strbuf()).collect();
|
||||||
let opts = ~[
|
let opts = ~[
|
||||||
optflag("h", "help", "display this help and exit"),
|
optflag("h", "help", "display this help and exit"),
|
||||||
optflag("", "version", "output version information and exit"),
|
optflag("", "version", "output version information and exit"),
|
||||||
|
@ -45,7 +45,7 @@ fn main() {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let progname = args[0].clone();
|
let progname = args.get(0);
|
||||||
let usage = usage("Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.", opts);
|
let usage = usage("Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.", opts);
|
||||||
let mode = if matches.opt_present("version") {
|
let mode = if matches.opt_present("version") {
|
||||||
Version
|
Version
|
||||||
|
@ -57,7 +57,7 @@ fn main() {
|
||||||
|
|
||||||
match mode {
|
match mode {
|
||||||
Copy => copy(matches),
|
Copy => copy(matches),
|
||||||
Help => help(progname, usage),
|
Help => help(progname.as_slice(), usage.as_slice()),
|
||||||
Version => version(),
|
Version => version(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,27 +76,27 @@ fn help(progname: &str, usage: &str) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn copy(matches: getopts::Matches) {
|
fn copy(matches: getopts::Matches) {
|
||||||
let sources = if matches.free.len() < 1 {
|
let sources : Vec<Path> = if matches.free.len() < 1 {
|
||||||
error!("error: Missing SOURCE argument. Try --help.");
|
error!("error: Missing SOURCE argument. Try --help.");
|
||||||
fail!()
|
fail!()
|
||||||
} else {
|
} else {
|
||||||
// All but the last argument:
|
// All but the last argument:
|
||||||
matches.free.slice(0, matches.free.len() - 2)
|
matches.free.slice(0, matches.free.len() - 2).iter()
|
||||||
.map(|arg| ~Path::new(arg.clone()))
|
.map(|arg| Path::new(arg.clone())).collect()
|
||||||
};
|
};
|
||||||
let dest = if matches.free.len() < 2 {
|
let dest = if matches.free.len() < 2 {
|
||||||
error!("error: Missing DEST argument. Try --help.");
|
error!("error: Missing DEST argument. Try --help.");
|
||||||
fail!()
|
fail!()
|
||||||
} else {
|
} else {
|
||||||
// Only the last argument:
|
// Only the last argument:
|
||||||
~Path::new(matches.free[matches.free.len() - 1].clone())
|
Path::new(matches.free.get(matches.free.len() - 1).as_slice())
|
||||||
};
|
};
|
||||||
|
|
||||||
assert!(sources.len() >= 1);
|
assert!(sources.len() >= 1);
|
||||||
|
|
||||||
if sources.len() == 1 {
|
if sources.len() == 1 {
|
||||||
let source = sources[0].clone();
|
let source = sources.get(0);
|
||||||
let same_file = match paths_refer_to_same_file(source, dest) {
|
let same_file = match paths_refer_to_same_file(source, &dest) {
|
||||||
Ok(b) => b,
|
Ok(b) => b,
|
||||||
Err(e) => if e.kind == io::FileNotFound {
|
Err(e) => if e.kind == io::FileNotFound {
|
||||||
false
|
false
|
||||||
|
@ -113,7 +113,7 @@ fn copy(matches: getopts::Matches) {
|
||||||
fail!();
|
fail!();
|
||||||
}
|
}
|
||||||
|
|
||||||
let io_result = fs::copy(source, dest);
|
let io_result = fs::copy(source, &dest);
|
||||||
|
|
||||||
if io_result.is_err() {
|
if io_result.is_err() {
|
||||||
let err = io_result.unwrap_err();
|
let err = io_result.unwrap_err();
|
||||||
|
@ -121,13 +121,13 @@ fn copy(matches: getopts::Matches) {
|
||||||
fail!();
|
fail!();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if fs::stat(dest).unwrap().kind != io::TypeDirectory {
|
if fs::stat(&dest).unwrap().kind != io::TypeDirectory {
|
||||||
error!("error: TARGET must be a directory");
|
error!("error: TARGET must be a directory");
|
||||||
fail!();
|
fail!();
|
||||||
}
|
}
|
||||||
|
|
||||||
for source in sources.iter() {
|
for source in sources.iter() {
|
||||||
if fs::stat(*source).unwrap().kind != io::TypeFile {
|
if fs::stat(source).unwrap().kind != io::TypeFile {
|
||||||
error!("error: \"{:s}\" is not a file", source.display().to_str());
|
error!("error: \"{:s}\" is not a file", source.display().to_str());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -138,7 +138,7 @@ fn copy(matches: getopts::Matches) {
|
||||||
|
|
||||||
println!("{:s}", full_dest.display().to_str());
|
println!("{:s}", full_dest.display().to_str());
|
||||||
|
|
||||||
let io_result = fs::copy(*source, full_dest);
|
let io_result = fs::copy(source, &full_dest);
|
||||||
|
|
||||||
if io_result.is_err() {
|
if io_result.is_err() {
|
||||||
let err = io_result.unwrap_err();
|
let err = io_result.unwrap_err();
|
||||||
|
@ -150,29 +150,29 @@ fn copy(matches: getopts::Matches) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn paths_refer_to_same_file(p1: &Path, p2: &Path) -> io::IoResult<bool> {
|
pub fn paths_refer_to_same_file(p1: &Path, p2: &Path) -> io::IoResult<bool> {
|
||||||
let mut raw_p1 = ~p1.clone();
|
let mut raw_p1 = p1.clone();
|
||||||
let mut raw_p2 = ~p2.clone();
|
let mut raw_p2 = p2.clone();
|
||||||
|
|
||||||
let p1_lstat = match fs::lstat(raw_p1) {
|
let p1_lstat = match fs::lstat(&raw_p1) {
|
||||||
Ok(stat) => stat,
|
Ok(stat) => stat,
|
||||||
Err(e) => return Err(e),
|
Err(e) => return Err(e),
|
||||||
};
|
};
|
||||||
|
|
||||||
let p2_lstat = match fs::lstat(raw_p2) {
|
let p2_lstat = match fs::lstat(&raw_p2) {
|
||||||
Ok(stat) => stat,
|
Ok(stat) => stat,
|
||||||
Err(e) => return Err(e),
|
Err(e) => return Err(e),
|
||||||
};
|
};
|
||||||
|
|
||||||
// We have to take symlinks and relative paths into account.
|
// We have to take symlinks and relative paths into account.
|
||||||
if p1_lstat.kind == io::TypeSymlink {
|
if p1_lstat.kind == io::TypeSymlink {
|
||||||
raw_p1 = ~fs::readlink(raw_p1).unwrap();
|
raw_p1 = fs::readlink(&raw_p1).unwrap();
|
||||||
}
|
}
|
||||||
raw_p1 = ~os::make_absolute(raw_p1);
|
raw_p1 = os::make_absolute(&raw_p1);
|
||||||
|
|
||||||
if p2_lstat.kind == io::TypeSymlink {
|
if p2_lstat.kind == io::TypeSymlink {
|
||||||
raw_p2 = ~fs::readlink(raw_p2).unwrap();
|
raw_p2 = fs::readlink(&raw_p2).unwrap();
|
||||||
}
|
}
|
||||||
raw_p2 = ~os::make_absolute(raw_p2);
|
raw_p2 = os::make_absolute(&raw_p2);
|
||||||
|
|
||||||
Ok(raw_p1 == raw_p2)
|
Ok(raw_p1 == raw_p2)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue