mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-08 08:10:55 +03:00
Decoupling between SCP*Clients and SCPEngine
Encapsulate listener inside SCPEngine. Remove string path in UploadClient.
This commit is contained in:
committed by
Shikhar Bhushan
parent
aa9f4e192f
commit
286a22270b
@@ -148,7 +148,7 @@ public final class SCPDownloadClient {
|
|||||||
final String dirname = dMsgParts[2];
|
final String dirname = dMsgParts[2];
|
||||||
if (length != 0)
|
if (length != 0)
|
||||||
throw new IOException("Remote SCP command sent strange directory length: " + length);
|
throw new IOException("Remote SCP command sent strange directory length: " + length);
|
||||||
engine.listener.startedDir(dirname);
|
engine.startedDir(dirname);
|
||||||
{
|
{
|
||||||
f = FileTransferUtil.getTargetDirectory(f, dirname);
|
f = FileTransferUtil.getTargetDirectory(f, dirname);
|
||||||
engine.signal("ACK: D");
|
engine.signal("ACK: D");
|
||||||
@@ -157,7 +157,7 @@ public final class SCPDownloadClient {
|
|||||||
setAttributes(f, parsePermissions(dMsgParts[0]), tMsg);
|
setAttributes(f, parsePermissions(dMsgParts[0]), tMsg);
|
||||||
engine.signal("ACK: E");
|
engine.signal("ACK: E");
|
||||||
}
|
}
|
||||||
engine.listener.finishedDir();
|
engine.finishedDir();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processFile(String cMsg, String tMsg, File f)
|
private void processFile(String cMsg, String tMsg, File f)
|
||||||
@@ -165,7 +165,7 @@ public final class SCPDownloadClient {
|
|||||||
final String[] cMsgParts = tokenize(cMsg, 3); // C<perms> <size> <filename>
|
final String[] cMsgParts = tokenize(cMsg, 3); // C<perms> <size> <filename>
|
||||||
final long length = parseLong(cMsgParts[1], "length");
|
final long length = parseLong(cMsgParts[1], "length");
|
||||||
final String filename = cMsgParts[2];
|
final String filename = cMsgParts[2];
|
||||||
engine.listener.startedFile(filename, length);
|
engine.startedFile(length, filename);
|
||||||
{
|
{
|
||||||
f = FileTransferUtil.getTargetFile(f, filename);
|
f = FileTransferUtil.getTargetFile(f, filename);
|
||||||
engine.signal("Remote can start transfer");
|
engine.signal("Remote can start transfer");
|
||||||
@@ -179,7 +179,7 @@ public final class SCPDownloadClient {
|
|||||||
setAttributes(f, parsePermissions(cMsgParts[0]), tMsg);
|
setAttributes(f, parsePermissions(cMsgParts[0]), tMsg);
|
||||||
engine.signal("Transfer done");
|
engine.signal("Transfer done");
|
||||||
}
|
}
|
||||||
engine.listener.finishedFile();
|
engine.finishedFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setAttributes(File f, int perms, String tMsg)
|
private void setAttributes(File f, int perms, String tMsg)
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import net.schmizz.sshj.common.IOUtils;
|
|||||||
import net.schmizz.sshj.common.SSHException;
|
import net.schmizz.sshj.common.SSHException;
|
||||||
import net.schmizz.sshj.connection.channel.direct.Session.Command;
|
import net.schmizz.sshj.connection.channel.direct.Session.Command;
|
||||||
import net.schmizz.sshj.connection.channel.direct.SessionFactory;
|
import net.schmizz.sshj.connection.channel.direct.SessionFactory;
|
||||||
|
import net.schmizz.sshj.xfer.LocalFile;
|
||||||
import net.schmizz.sshj.xfer.TransferListener;
|
import net.schmizz.sshj.xfer.TransferListener;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -59,7 +60,7 @@ class SCPEngine {
|
|||||||
final Logger log = LoggerFactory.getLogger(getClass());
|
final Logger log = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
final SessionFactory host;
|
final SessionFactory host;
|
||||||
final TransferListener listener;
|
private final TransferListener listener;
|
||||||
|
|
||||||
Command scp;
|
Command scp;
|
||||||
int exitStatus;
|
int exitStatus;
|
||||||
@@ -184,4 +185,27 @@ class SCPEngine {
|
|||||||
throw new IOException("Had EOF before transfer completed");
|
throw new IOException("Had EOF before transfer completed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void startedDir(final String dirname) {
|
||||||
|
listener.startedDir(dirname);
|
||||||
|
}
|
||||||
|
|
||||||
|
void startedDir(LocalFile f) {
|
||||||
|
listener.startedDir(f.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
void finishedDir() {
|
||||||
|
listener.finishedDir();
|
||||||
|
}
|
||||||
|
|
||||||
|
void startedFile(final long length, final String filename) {
|
||||||
|
listener.startedFile(filename, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
void startedFile(LocalFile f) {
|
||||||
|
listener.startedFile(f.getName(), f.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
void finishedFile() {
|
||||||
|
listener.finishedFile();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,9 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package net.schmizz.sshj.xfer.scp;
|
package net.schmizz.sshj.xfer.scp;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileFilter;
|
import java.io.FileFilter;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
@@ -36,8 +34,6 @@ public final class SCPUploadClient {
|
|||||||
|
|
||||||
private final ModeGetter modeGetter;
|
private final ModeGetter modeGetter;
|
||||||
|
|
||||||
private FileFilter fileFilter;
|
|
||||||
|
|
||||||
private SCPEngine engine;
|
private SCPEngine engine;
|
||||||
|
|
||||||
SCPUploadClient(SessionFactory host, TransferListener listener, ModeGetter modeGetter) {
|
SCPUploadClient(SessionFactory host, TransferListener listener, ModeGetter modeGetter) {
|
||||||
@@ -58,7 +54,6 @@ public final class SCPUploadClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setFileFilter(FileFilter fileFilter) {
|
public void setFileFilter(FileFilter fileFilter) {
|
||||||
this.fileFilter = fileFilter;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void startCopy(LocalFile sourceFile, String targetPath)
|
private synchronized void startCopy(LocalFile sourceFile, String targetPath)
|
||||||
@@ -68,14 +63,6 @@ public final class SCPUploadClient {
|
|||||||
process(sourceFile);
|
process(sourceFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
private File[] getChildren(File f)
|
|
||||||
throws IOException {
|
|
||||||
File[] files = fileFilter == null ? f.listFiles() : f.listFiles(fileFilter);
|
|
||||||
if (files == null)
|
|
||||||
throw new IOException("Error listing files in directory: " + f);
|
|
||||||
return files;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void init(String target)
|
private void init(String target)
|
||||||
throws SSHException {
|
throws SSHException {
|
||||||
List<Arg> args = new LinkedList<Arg>();
|
List<Arg> args = new LinkedList<Arg>();
|
||||||
@@ -86,43 +73,20 @@ public final class SCPUploadClient {
|
|||||||
engine.execSCPWith(args, target);
|
engine.execSCPWith(args, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void process(File f)
|
|
||||||
throws IOException {
|
|
||||||
if (f.isDirectory()) {
|
|
||||||
engine.listener.startedDir(f.getName());
|
|
||||||
sendDirectory(f);
|
|
||||||
engine.listener.finishedDir();
|
|
||||||
} else if (f.isFile()) {
|
|
||||||
engine.listener.startedFile(f.getName(), f.length());
|
|
||||||
sendFile(f);
|
|
||||||
engine.listener.finishedFile();
|
|
||||||
} else
|
|
||||||
throw new IOException(f + " is not a regular file or directory");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void process(LocalFile f)
|
private void process(LocalFile f)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
if (f.isDirectory()) {
|
if (f.isDirectory()) {
|
||||||
engine.listener.startedDir(f.getName());
|
engine.startedDir(f);
|
||||||
sendDirectory(f);
|
sendDirectory(f);
|
||||||
engine.listener.finishedDir();
|
engine.finishedDir();
|
||||||
} else if (f.isFile()) {
|
} else if (f.isFile()) {
|
||||||
engine.listener.startedFile(f.getName(), f.length());
|
engine.startedFile(f);
|
||||||
sendFile(f);
|
sendFile(f);
|
||||||
engine.listener.finishedFile();
|
engine.finishedFile();
|
||||||
} else
|
} else
|
||||||
throw new IOException(f + " is not a regular file or directory");
|
throw new IOException(f + " is not a regular file or directory");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendDirectory(File f)
|
|
||||||
throws IOException {
|
|
||||||
preserveTimeIfPossible(f);
|
|
||||||
engine.sendMessage("D0" + getPermString(f) + " 0 " + f.getName());
|
|
||||||
for (File child : getChildren(f))
|
|
||||||
process(child);
|
|
||||||
engine.sendMessage("E");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void sendDirectory(LocalFile f)
|
private void sendDirectory(LocalFile f)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
preserveTimeIfPossible(f);
|
preserveTimeIfPossible(f);
|
||||||
@@ -132,20 +96,6 @@ public final class SCPUploadClient {
|
|||||||
engine.sendMessage("E");
|
engine.sendMessage("E");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendFile(File f)
|
|
||||||
throws IOException {
|
|
||||||
preserveTimeIfPossible(f);
|
|
||||||
final InputStream src = new FileInputStream(f);
|
|
||||||
try {
|
|
||||||
engine.sendMessage("C0" + getPermString(f) + " " + f.length() + " " + f.getName());
|
|
||||||
engine.transfer(src, engine.scp.getOutputStream(), engine.scp.getRemoteMaxPacketSize(), f.length());
|
|
||||||
engine.signal("Transfer done");
|
|
||||||
engine.check("Remote agrees transfer done");
|
|
||||||
} finally {
|
|
||||||
IOUtils.closeQuietly(src);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void sendFile(LocalFile f)
|
private void sendFile(LocalFile f)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
preserveTimeIfPossible(f);
|
preserveTimeIfPossible(f);
|
||||||
@@ -160,23 +110,12 @@ public final class SCPUploadClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void preserveTimeIfPossible(File f)
|
|
||||||
throws IOException {
|
|
||||||
if (modeGetter.preservesTimes())
|
|
||||||
engine.sendMessage("T" + modeGetter.getLastModifiedTime(f) + " 0 " + modeGetter.getLastAccessTime(f) + " 0");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void preserveTimeIfPossible(LocalFile f)
|
private void preserveTimeIfPossible(LocalFile f)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
if (modeGetter.preservesTimes())
|
if (modeGetter.preservesTimes())
|
||||||
engine.sendMessage("T" + modeGetter.getLastModifiedTime(f) + " 0 " + modeGetter.getLastAccessTime(f) + " 0");
|
engine.sendMessage("T" + modeGetter.getLastModifiedTime(f) + " 0 " + modeGetter.getLastAccessTime(f) + " 0");
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getPermString(File f)
|
|
||||||
throws IOException {
|
|
||||||
return Integer.toOctalString(modeGetter.getPermissions(f) & 07777);
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getPermString(LocalFile f)
|
private String getPermString(LocalFile f)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
return Integer.toOctalString(modeGetter.getPermissions(f) & 07777);
|
return Integer.toOctalString(modeGetter.getPermissions(f) & 07777);
|
||||||
|
|||||||
Reference in New Issue
Block a user