}
final File dir = new File(directory);
sub.beginTask("Move files from staging server to DFS", 1);
ChannelExec exec =
exec(" dfs " + s_whichFS
+ " -moveFromLocal /tmp/hadoop_scp_" + guid + " \""
+ getPath() + "/" + dir.getName() + "\"");
BufferedReader reader =
new BufferedReader(new InputStreamReader(
new BufferedInputStream(exec.getInputStream())));
if (!monitor.isCanceled()) {
exec.connect();
String line = reader.readLine();
sub.worked(1);
}
if (exec.isConnected()) {
exec.disconnect();
}
sub.done();
monitor.done();
doRefresh();
} catch (Exception e) {
log.log(Level.SEVERE, "", e);
throw new InvocationTargetException(e);
}
}
public void scp(String from, String to, IProgressMonitor monitor) {
File file = new File(from);
ChannelExec channel = null;
monitor.beginTask("scp from " + from + " to " + to, 100 * (file
.isDirectory() ? file.list().length + 1 : 1));
if (monitor.isCanceled()) {
return;
}
if (file.isDirectory()) {
// mkdir
try {
channel = (ChannelExec) getSession().openChannel("exec");
channel.setCommand(" mkdir " + to);
InputStream in = channel.getInputStream();
channel.connect();
// in.read(); // wait for a response, which
// we'll then ignore
} catch (JSchException e) {
// BUG(jz) abort operation and display error
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (channel.isConnected()) {
channel.disconnect();
}
}
monitor.worked(100);
String[] children = file.list();
for (int i = 0; i < children.length; i++) {
File child = new File(file, children[i]);
// recurse
scp(new File(file, children[i]).getAbsolutePath(), to + "/"
+ children[i], new SubProgressMonitor(monitor, 100));
}
} else {
InputStream filein = null;
try {
channel = (ChannelExec) getSession().openChannel("exec");
(channel).setCommand("scp -p -t " + to);
BufferedOutputStream out =
new BufferedOutputStream(channel.getOutputStream());
InputStream in = channel.getInputStream();
channel.connect();
if (in.read() == 0) {
int step = (int) (100 / new File(from).length());
out.write(("C0644 " + new File(from).length() + " "
+ new File(to).getName() + "\n").getBytes());
out.flush();
if (in.read() != 0) {
throw new RuntimeException("Copy failed");
}
filein =
new BufferedInputStream(new FileInputStream(from));
byte[] buffer = new byte[1024];
int bytes;
while ((bytes = filein.read(buffer)) > -1) {
if (monitor.isCanceled()) {
return;
}
out.write(buffer, 0, bytes);
monitor.worked(step);
}
out.write("\0".getBytes());
out.flush();
if (in.read() != 0) {
throw new RuntimeException("Copy failed");
}
out.close();
} else {
// problems with copy
throw new RuntimeException("Copy failed");
}
} catch (JSchException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (channel.isConnected()) {
channel.disconnect();
}
try {
filein.close();
} catch (IOException e) {
}