Skip to content
Snippets Groups Projects
Commit 3a7db8b7 authored by Thomas Wolf's avatar Thomas Wolf
Browse files

Simplify SshdFtpChannel


Apache MINA sshd has simpler API for reading directories, and it has a
functional interface suitable for us. So no need to use our own
interface, or to deal with low-level abstractions like CloseableHandle.

Change-Id: Ic125c587535670504983f157a696b41ed6a76bb7
Signed-off-by: default avatarThomas Wolf <thomas.wolf@paranor.ch>
parent c6d48ab2
Branches
Tags
No related merge requests found
...@@ -73,6 +73,7 @@ Import-Package: net.i2p.crypto.eddsa;version="[0.3.0,0.4.0)", ...@@ -73,6 +73,7 @@ Import-Package: net.i2p.crypto.eddsa;version="[0.3.0,0.4.0)",
org.apache.sshd.common.util.buffer;version="[2.7.0,2.8.0)", org.apache.sshd.common.util.buffer;version="[2.7.0,2.8.0)",
org.apache.sshd.common.util.closeable;version="[2.7.0,2.8.0)", org.apache.sshd.common.util.closeable;version="[2.7.0,2.8.0)",
org.apache.sshd.common.util.io;version="[2.7.0,2.8.0)", org.apache.sshd.common.util.io;version="[2.7.0,2.8.0)",
org.apache.sshd.common.util.io.functors;version="[2.7.0,2.8.0)",
org.apache.sshd.common.util.io.resource;version="[2.7.0,2.8.0)", org.apache.sshd.common.util.io.resource;version="[2.7.0,2.8.0)",
org.apache.sshd.common.util.logging;version="[2.7.0,2.8.0)", org.apache.sshd.common.util.logging;version="[2.7.0,2.8.0)",
org.apache.sshd.common.util.net;version="[2.7.0,2.8.0)", org.apache.sshd.common.util.net;version="[2.7.0,2.8.0)",
......
/* /*
* Copyright (C) 2018, 2020 Thomas Wolf <thomas.wolf@paranor.ch> and others * Copyright (C) 2018, 2021 Thomas Wolf <thomas.wolf@paranor.ch> and others
* *
* This program and the accompanying materials are made available under the * This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at * terms of the Eclipse Distribution License v. 1.0 which is available at
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
import java.util.Map; import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.regex.Pattern; import java.util.regex.Pattern;
...@@ -44,9 +43,9 @@ ...@@ -44,9 +43,9 @@
import org.apache.sshd.common.future.CloseFuture; import org.apache.sshd.common.future.CloseFuture;
import org.apache.sshd.common.future.SshFutureListener; import org.apache.sshd.common.future.SshFutureListener;
import org.apache.sshd.common.util.io.IoUtils; import org.apache.sshd.common.util.io.IoUtils;
import org.apache.sshd.common.util.io.functors.IOFunction;
import org.apache.sshd.common.util.net.SshdSocketAddress; import org.apache.sshd.common.util.net.SshdSocketAddress;
import org.apache.sshd.sftp.client.SftpClient; import org.apache.sshd.sftp.client.SftpClient;
import org.apache.sshd.sftp.client.SftpClient.CloseableHandle;
import org.apache.sshd.sftp.client.SftpClient.CopyMode; import org.apache.sshd.sftp.client.SftpClient.CopyMode;
import org.apache.sshd.sftp.client.SftpClientFactory; import org.apache.sshd.sftp.client.SftpClientFactory;
import org.apache.sshd.sftp.common.SftpException; import org.apache.sshd.sftp.common.SftpException;
...@@ -416,20 +415,6 @@ public void destroy() { ...@@ -416,20 +415,6 @@ public void destroy() {
} }
} }
/**
* Helper interface like {@link Supplier}, but possibly raising an
* {@link IOException}.
*
* @param <T>
* return type
*/
@FunctionalInterface
private interface FtpOperation<T> {
T call() throws IOException;
}
private class SshdFtpChannel implements FtpChannel { private class SshdFtpChannel implements FtpChannel {
private SftpClient ftp; private SftpClient ftp;
...@@ -485,9 +470,9 @@ private String absolute(String path) { ...@@ -485,9 +470,9 @@ private String absolute(String path) {
return path; return path;
} }
private <T> T map(FtpOperation<T> op) throws IOException { private <T> T map(IOFunction<Void, T> op) throws IOException {
try { try {
return op.call(); return op.apply(null);
} catch (IOException e) { } catch (IOException e) {
if (e instanceof SftpException) { if (e instanceof SftpException) {
throw new FtpChannel.FtpException(e.getLocalizedMessage(), throw new FtpChannel.FtpException(e.getLocalizedMessage(),
...@@ -499,7 +484,7 @@ private <T> T map(FtpOperation<T> op) throws IOException { ...@@ -499,7 +484,7 @@ private <T> T map(FtpOperation<T> op) throws IOException {
@Override @Override
public void cd(String path) throws IOException { public void cd(String path) throws IOException {
cwd = map(() -> ftp.canonicalPath(absolute(path))); cwd = map(x -> ftp.canonicalPath(absolute(path)));
if (cwd.isEmpty()) { if (cwd.isEmpty()) {
cwd += '/'; cwd += '/';
} }
...@@ -512,18 +497,9 @@ public String pwd() throws IOException { ...@@ -512,18 +497,9 @@ public String pwd() throws IOException {
@Override @Override
public Collection<DirEntry> ls(String path) throws IOException { public Collection<DirEntry> ls(String path) throws IOException {
return map(() -> { return map(x -> {
List<DirEntry> result = new ArrayList<>(); List<DirEntry> result = new ArrayList<>();
try (CloseableHandle handle = ftp.openDir(absolute(path))) { for (SftpClient.DirEntry remote : ftp.readDir(absolute(path))) {
AtomicReference<Boolean> atEnd = new AtomicReference<>(
Boolean.FALSE);
while (!atEnd.get().booleanValue()) {
List<SftpClient.DirEntry> chunk = ftp.readDir(handle,
atEnd);
if (chunk == null) {
break;
}
for (SftpClient.DirEntry remote : chunk) {
result.add(new DirEntry() { result.add(new DirEntry() {
@Override @Override
...@@ -533,8 +509,8 @@ public String getFilename() { ...@@ -533,8 +509,8 @@ public String getFilename() {
@Override @Override
public long getModifiedTime() { public long getModifiedTime() {
return remote.getAttributes() return remote.getAttributes().getModifyTime()
.getModifyTime().toMillis(); .toMillis();
} }
@Override @Override
...@@ -544,15 +520,13 @@ public boolean isDirectory() { ...@@ -544,15 +520,13 @@ public boolean isDirectory() {
}); });
} }
}
}
return result; return result;
}); });
} }
@Override @Override
public void rmdir(String path) throws IOException { public void rmdir(String path) throws IOException {
map(() -> { map(x -> {
ftp.rmdir(absolute(path)); ftp.rmdir(absolute(path));
return null; return null;
}); });
...@@ -561,7 +535,7 @@ public void rmdir(String path) throws IOException { ...@@ -561,7 +535,7 @@ public void rmdir(String path) throws IOException {
@Override @Override
public void mkdir(String path) throws IOException { public void mkdir(String path) throws IOException {
map(() -> { map(x -> {
ftp.mkdir(absolute(path)); ftp.mkdir(absolute(path));
return null; return null;
}); });
...@@ -569,17 +543,17 @@ public void mkdir(String path) throws IOException { ...@@ -569,17 +543,17 @@ public void mkdir(String path) throws IOException {
@Override @Override
public InputStream get(String path) throws IOException { public InputStream get(String path) throws IOException {
return map(() -> ftp.read(absolute(path))); return map(x -> ftp.read(absolute(path)));
} }
@Override @Override
public OutputStream put(String path) throws IOException { public OutputStream put(String path) throws IOException {
return map(() -> ftp.write(absolute(path))); return map(x -> ftp.write(absolute(path)));
} }
@Override @Override
public void rm(String path) throws IOException { public void rm(String path) throws IOException {
map(() -> { map(x -> {
ftp.remove(absolute(path)); ftp.remove(absolute(path));
return null; return null;
}); });
...@@ -587,7 +561,7 @@ public void rm(String path) throws IOException { ...@@ -587,7 +561,7 @@ public void rm(String path) throws IOException {
@Override @Override
public void rename(String from, String to) throws IOException { public void rename(String from, String to) throws IOException {
map(() -> { map(x -> {
String src = absolute(from); String src = absolute(from);
String dest = absolute(to); String dest = absolute(to);
try { try {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment