GH-9114: SFTP: Use canonicalPath exists operation
Fixes: #9114 If path is not in normalized presentation, the SFTP operation might fail like: ``` Caused by: SFTP error (SSH_FX_NO_SUCH_PATH): The file path does not exist or is invalid. at org.apache.sshd.sftp.client.impl.AbstractSftpClient.throwStatusException(AbstractSftpClient.java:277) at org.apache.sshd.sftp.client.impl.AbstractSftpClient.checkAttributesResponse(AbstractSftpClient.java:333) at org.apache.sshd.sftp.client.impl.AbstractSftpClient.checkAttributes(AbstractSftpClient.java:325) at org.apache.sshd.sftp.client.impl.AbstractSftpClient.lstat(AbstractSftpClient.java:1010) at org.springframework.integration.sftp.session.SftpSession.exists(SftpSession.java:191) ``` * Use it now like this `this.sftpClient.lstat(normalizePath(path))` **Auto-cherry-pick to `6.2.x` & `6.1.x`**
This commit is contained in:
@@ -189,7 +189,7 @@ public class SftpSession implements Session<SftpClient.DirEntry> {
|
||||
@Override
|
||||
public boolean exists(String path) {
|
||||
try {
|
||||
this.sftpClient.lstat(path);
|
||||
this.sftpClient.lstat(normalizePath(path));
|
||||
return true;
|
||||
}
|
||||
catch (SftpException ex) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -260,17 +260,25 @@ public class SftpOutboundTests {
|
||||
public void testExists() throws IOException {
|
||||
SftpClient sftpClient = mock(SftpClient.class);
|
||||
|
||||
willReturn("/exist")
|
||||
.given(sftpClient)
|
||||
.canonicalPath("exist");
|
||||
|
||||
willReturn("/notExist")
|
||||
.given(sftpClient)
|
||||
.canonicalPath("notExist");
|
||||
|
||||
willReturn(new SftpClient.Attributes())
|
||||
.given(sftpClient)
|
||||
.lstat(eq("exist"));
|
||||
.lstat("/exist");
|
||||
|
||||
willThrow(new SftpException(SftpConstants.SSH_FX_NO_SUCH_FILE, "notExist"))
|
||||
.given(sftpClient)
|
||||
.lstat(eq("notExist"));
|
||||
.lstat("/notExist");
|
||||
|
||||
willThrow(new SshException(SshConstants.SSH_OPEN_CONNECT_FAILED, "Connection lost."))
|
||||
.given(sftpClient)
|
||||
.lstat(and(not(eq("exist")), not(eq("notExist"))));
|
||||
.lstat(and(not(eq("/exist")), not(eq("/notExist"))));
|
||||
|
||||
SftpSession sftpSession = new SftpSession(sftpClient);
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2022 the original author or authors.
|
||||
* Copyright 2013-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -701,7 +701,7 @@ public class SftpServerOutboundTests extends SftpTestSupport {
|
||||
assertThatExceptionOfType(UncheckedIOException.class)
|
||||
.isThrownBy(() -> session.exists("any"))
|
||||
.withRootCauseInstanceOf(IOException.class)
|
||||
.withStackTraceContaining("lstat(any) client is closed");
|
||||
.withStackTraceContaining("canonicalPath(any) client is closed");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
|
||||
Reference in New Issue
Block a user