Skip to content

Commit 67ac3c2

Browse files
artembilanspring-builds
authored andcommitted
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))` (cherry picked from commit 8b88668)
1 parent cb5eafe commit 67ac3c2

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSession.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public boolean rmdir(String remoteDirectory) throws IOException {
189189
@Override
190190
public boolean exists(String path) {
191191
try {
192-
this.sftpClient.lstat(path);
192+
this.sftpClient.lstat(normalizePath(path));
193193
return true;
194194
}
195195
catch (SftpException ex) {

spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpOutboundTests.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -260,17 +260,25 @@ public void testSharedSession(boolean sharedSession) throws IOException {
260260
public void testExists() throws IOException {
261261
SftpClient sftpClient = mock(SftpClient.class);
262262

263+
willReturn("/exist")
264+
.given(sftpClient)
265+
.canonicalPath("exist");
266+
267+
willReturn("/notExist")
268+
.given(sftpClient)
269+
.canonicalPath("notExist");
270+
263271
willReturn(new SftpClient.Attributes())
264272
.given(sftpClient)
265-
.lstat(eq("exist"));
273+
.lstat("/exist");
266274

267275
willThrow(new SftpException(SftpConstants.SSH_FX_NO_SUCH_FILE, "notExist"))
268276
.given(sftpClient)
269-
.lstat(eq("notExist"));
277+
.lstat("/notExist");
270278

271279
willThrow(new SshException(SshConstants.SSH_OPEN_CONNECT_FAILED, "Connection lost."))
272280
.given(sftpClient)
273-
.lstat(and(not(eq("exist")), not(eq("notExist"))));
281+
.lstat(and(not(eq("/exist")), not(eq("/notExist"))));
274282

275283
SftpSession sftpSession = new SftpSession(sftpClient);
276284

spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpServerOutboundTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2022 the original author or authors.
2+
* Copyright 2013-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -701,7 +701,7 @@ public void testSessionExists() throws IOException {
701701
assertThatExceptionOfType(UncheckedIOException.class)
702702
.isThrownBy(() -> session.exists("any"))
703703
.withRootCauseInstanceOf(IOException.class)
704-
.withStackTraceContaining("lstat(any) client is closed");
704+
.withStackTraceContaining("canonicalPath(any) client is closed");
705705
}
706706

707707
@SuppressWarnings("unused")

0 commit comments

Comments
 (0)