From f9b4466fc10fea98afabf8bc6bc7b7f858f2bee3 Mon Sep 17 00:00:00 2001 From: Russ Poetker Date: Wed, 1 Nov 2023 17:00:38 -0400 Subject: [PATCH] Fix dir creation logic --- .../transport/sftp/SftpTransportSession.java | 37 +++++++++++++------ .../pass/deposit/transport/sftp/SftpTest.java | 2 +- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/transport/sftp/SftpTransportSession.java b/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/transport/sftp/SftpTransportSession.java index 56142558..a51b7743 100644 --- a/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/transport/sftp/SftpTransportSession.java +++ b/pass-deposit-services/deposit-core/src/main/java/org/eclipse/pass/deposit/transport/sftp/SftpTransportSession.java @@ -22,12 +22,14 @@ import static org.eclipse.pass.deposit.transport.Transport.TRANSPORT_USERNAME; import static org.eclipse.pass.deposit.transport.sftp.SftpTransport.SFTP_BASE_DIRECTORY; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.time.OffsetDateTime; import java.time.ZoneId; import java.util.Map; +import java.util.regex.Pattern; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; @@ -38,6 +40,7 @@ import org.apache.sshd.sftp.client.impl.DefaultSftpClientFactory; import org.apache.sshd.sftp.common.SftpConstants; import org.apache.sshd.sftp.common.SftpException; +import org.eclipse.pass.deposit.DepositServiceRuntimeException; import org.eclipse.pass.deposit.assembler.PackageStream; import org.eclipse.pass.deposit.transport.TransportResponse; import org.eclipse.pass.deposit.transport.TransportSession; @@ -90,7 +93,7 @@ private TransportResponse writeFile(ClientSession clientSession, PackageStream p try (InputStream inputStream = packageStream.open()) { String baseDir = getBaseDir(); createBaseDirIfNeeded(baseDir, sftpClient); - try (OutputStream outputStream = sftpClient.write(baseDir + "/" + fileName)) { + try (OutputStream outputStream = sftpClient.write(baseDir + File.separator + fileName)) { IOUtils.copy(inputStream, outputStream); return new SftpTransportResponse(true); } @@ -98,35 +101,45 @@ private TransportResponse writeFile(ClientSession clientSession, PackageStream p } } + private String getBaseDir() { + String baseDir = transportProps.get(SFTP_BASE_DIRECTORY); + if (StringUtils.isBlank(baseDir)) { + throw new DepositServiceRuntimeException("Sftp requires \"default-directory\" in protocol-binding"); + } + String cleanedDirName = baseDir.startsWith(File.separator) ? baseDir.substring(1) : baseDir; + return cleanedDirName.contains("%s") + ? String.format(cleanedDirName, OffsetDateTime.now(ZoneId.of("UTC")).format(ISO_LOCAL_DATE)) + : cleanedDirName; + } + private void createBaseDirIfNeeded(String path, SftpClient sftpClient) throws IOException { if (StringUtils.isNotEmpty(path)) { - String[] dirs = path.split("/"); + String splitRegex = Pattern.quote(System.getProperty("file.separator")); + String[] dirs = path.split(splitRegex); StringBuilder fullPath = new StringBuilder(); for (String dir : dirs) { if (!fullPath.isEmpty()) { - fullPath.append("/"); + fullPath.append(File.separator); } fullPath.append(dir); try { sftpClient.stat(fullPath.toString()); } catch (SftpException e) { if (e.getStatus() != SftpConstants.SSH_FX_NO_SUCH_FILE) { - throw new RuntimeException(e); + throw new RuntimeException("Error creating dir named " + fullPath, e); } - sftpClient.mkdir(fullPath.toString()); + createDir(fullPath.toString(), sftpClient); } } } } - private String getBaseDir() { - String baseDir = transportProps.get(SFTP_BASE_DIRECTORY); - if (StringUtils.isNotEmpty(baseDir)) { - return baseDir.contains("%s") - ? String.format(baseDir, OffsetDateTime.now(ZoneId.of("UTC")).format(ISO_LOCAL_DATE)) - : baseDir; + private void createDir(String dirName, SftpClient sftpClient) { + try { + sftpClient.mkdir(dirName); + } catch (IOException e) { + throw new RuntimeException("Error creating dir named " + dirName, e); } - return ""; } @Override diff --git a/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/transport/sftp/SftpTest.java b/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/transport/sftp/SftpTest.java index bd7ccec3..5d3dfd9e 100644 --- a/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/transport/sftp/SftpTest.java +++ b/pass-deposit-services/deposit-core/src/test/java/org/eclipse/pass/deposit/transport/sftp/SftpTest.java @@ -83,7 +83,7 @@ public void testCreateFile() { TRANSPORT_SERVER_PORT, String.valueOf(sshd.getPort()), TRANSPORT_USERNAME, "dummyUser", TRANSPORT_PASSWORD, "dummyPass", - SFTP_BASE_DIRECTORY, "upload/test/%s" + SFTP_BASE_DIRECTORY, "/upload/test/%s" ); String testFileName = System.currentTimeMillis() + "package.tar.gz"; NullInputStream content = new NullInputStream(ONE_MIB);