* sftp get: unknown file type, assume it is a regular file

* minor refactoring
This commit is contained in:
Shikhar Bhushan
2010-03-11 23:53:40 +01:00
parent f572f8a51f
commit 447aa2f9f4
2 changed files with 29 additions and 26 deletions

View File

@@ -21,18 +21,22 @@ public class RemoteResourceInfo {
private final FileAttributes attrs; private final FileAttributes attrs;
public RemoteResourceInfo(String parent, String name, FileAttributes attrs) { public RemoteResourceInfo(String parent, String name, FileAttributes attrs) {
this.comps = new PathComponents(parent, name); this(new PathComponents(parent, name), attrs);
this.attrs = attrs;
} }
public String getParent() { public RemoteResourceInfo(PathComponents comps, FileAttributes attrs) {
return comps.getParent(); this.comps = comps;
this.attrs = attrs;
} }
public String getPath() { public String getPath() {
return comps.getPath(); return comps.getPath();
} }
public String getParent() {
return comps.getParent();
}
public String getName() { public String getName() {
return comps.getName(); return comps.getName();
} }
@@ -41,20 +45,12 @@ public class RemoteResourceInfo {
return attrs; return attrs;
} }
public boolean isType(FileMode.Type type) {
return attrs.getType() == type;
}
public boolean isRegularFile() { public boolean isRegularFile() {
return isType(FileMode.Type.REGULAR); return attrs.getType() == FileMode.Type.REGULAR;
} }
public boolean isDirectory() { public boolean isDirectory() {
return isType(FileMode.Type.DIRECTORY); return attrs.getType() == FileMode.Type.DIRECTORY;
}
public boolean isSymlink() {
return isType(FileMode.Type.SYMKLINK);
} }
@Override @Override

View File

@@ -64,9 +64,10 @@ public class SFTPFileTransfer
public void download(String source, String dest) public void download(String source, String dest)
throws IOException { throws IOException {
PathComponents src = pathHelper.getComponents(source); final PathComponents pathComponents = pathHelper.getComponents(source);
new Downloader(getModeSetter(), getDownloadFilter()).download(new RemoteResourceInfo(src.getParent(), src final FileAttributes attributes = sftp.stat(source);
.getName(), sftp.stat(source)), new File(dest)); new Downloader(getModeSetter(), getDownloadFilter())
.download(new RemoteResourceInfo(pathComponents, attributes), new File(dest));
} }
public void setUploadFilter(FileFilter uploadFilter) { public void setUploadFilter(FileFilter uploadFilter) {
@@ -136,12 +137,19 @@ public class SFTPFileTransfer
void download(RemoteResourceInfo remote, File local) void download(RemoteResourceInfo remote, File local)
throws IOException { throws IOException {
log.info("Downloading [{}] to [{}]", remote, local); log.info("Downloading [{}] to [{}]", remote, local);
if (remote.isDirectory()) switch (remote.getAttributes().getType()) {
case DIRECTORY:
downloadDir(remote, local); downloadDir(remote, local);
else if (remote.isRegularFile()) break;
case UNKNOWN: // ... BS servers like wodFTPD
log.warn("Server did not supply information about the type of file at `{}` -- assuming it is a regular file!");
case REGULAR:
downloadFile(remote, local); downloadFile(remote, local);
else break;
default:
throw new IOException(remote + " is not a regular file or directory"); throw new IOException(remote + " is not a regular file or directory");
}
} }
} }
@@ -240,8 +248,7 @@ public class SFTPFileTransfer
try { try {
final FileInputStream fis = new FileInputStream(local); final FileInputStream fis = new FileInputStream(local);
try { try {
StreamCopier.copy(fis, // StreamCopier.copy(fis, rf.getOutputStream(), sftp.getSubsystem().getRemoteMaxPacketSize()
rf.getOutputStream(), sftp.getSubsystem().getRemoteMaxPacketSize()
- rf.getOutgoingPacketOverhead(), false); - rf.getOutgoingPacketOverhead(), false);
} finally { } finally {
fis.close(); fis.close();