}
try {
PrintWriter out = new PrintWriter(new BufferWriter(getStdOut()));
VFS vfs = shell.getVFS();
FileName pwd = new FileName(shell.getEnvProperty("PWD"));
LinkedList paths = new LinkedList();
boolean force = false;
boolean errors = false;
for (int i = 0; i < params.length; i++) {
String param = params[i];
if ((i == 0) && param.equals(FORCE))
force = true;
else {
FileName path = new FileName(param);
if (path.isRelative())
path = pwd.absolutize(path);
// path = vfs.resolve(shell.getUserCtx(), path, true);
if (!vfs.exists(shell.getUserCtx(), path, true) && i != params.length - 1) {
out.println(param + ": Path does not exists!");
errors = true;
break;
} else
paths.add(path);
}
}
if ((paths.size() <= 1) || (errors && !force)) {
out.println("Usage: cp FILES DEST_DIR");
out.println(" cp FILE DEST_FILE");
} else {
FileName lastPath = (FileName) paths.removeLast();
boolean lastPathXsists = vfs.exists(shell.getUserCtx(), lastPath, true);
if (lastPathXsists) {
FileInfo lastInfo = vfs.getFileInfo(shell.getUserCtx(), lastPath, false);
if (lastInfo.isDirectory() && (force || !errors)) {
log.debug("1.Copying multiple files to dir.");
Iterator it = paths.iterator();
while (it.hasNext()) {
FileName path = (FileName) it.next();
FileInfo info = vfs.getFileInfo(shell.getUserCtx(), path, true);
if (copy(info, lastPath.absolutize(path.getName()), force, vfs, out) && !force) {
log.debug("done");
return;
}
}
} else if ((paths.size() == 1) && force) {
log.debug("1.Copying file (overwrite).");
FileName path = (FileName) paths.getFirst();
FileInfo info = vfs.getFileInfo(shell.getUserCtx(), path, true);
if (copy(info, lastPath, force, vfs, out) && !force) {
log.debug("done");
return;
}
} else
out.println(lastPath + ": Destination path is not a directory!");
} else if (paths.size() == 1) {
log.debug("1.Copying file");
FileName path = (FileName) paths.getFirst();
FileInfo info = vfs.getFileInfo(shell.getUserCtx(), path, true);
if (copy(info, lastPath, force, vfs, out) && !force) {
log.debug("done");
return;
}