GH-9268: Fix regression in SMB Inbound for sub-dirs

Fixes: #9268

The new `SmbSession.list()` behavior prevents to pull files from sub-folders in inbound channel adapter

* Fix `SmbSession.list()` similar way to `SftpSession`: if `remoteFile.isFile()` then remove it as is.
Otherwise, perform `smbDir.listFiles()` on that sub-dir
* Modify `SmbTests.testSmbInboundFlow()` to poll files from the `smbSource/subSmbSource/`

**Auto-cherry-pick to `6.3.x` & `6.2.x`**
This commit is contained in:
Artem Bilan
2024-06-24 14:52:13 -04:00
parent 981f5761ca
commit f2399a4e13
2 changed files with 14 additions and 12 deletions

View File

@@ -119,25 +119,26 @@ public class SmbSession implements Session<SmbFile> {
}
/**
* Return the contents of the specified SMB resource as an array of SmbFile objects.
* Return the content of the specified SMB resource as an array of SmbFile objects.
* In case the remote resource does not exist, an empty array is returned.
* @param remotePath path to a remote directory or remote file path
* @param path path to a remote directory or remote file path
* @return array of SmbFile objects
* @throws IOException on error conditions returned by a CIFS server or if the remote resource is not a directory.
* @throws IOException on error conditions returned by a CIFS server.
*/
@Override
public SmbFile[] list(String remotePath) throws IOException {
SmbFile[] files;
public SmbFile[] list(String path) throws IOException {
String remotePath = StringUtils.trimTrailingCharacter(path, '/');
SmbFile[] files = null;
int lastIndex = StringUtils.hasText(remotePath) ? remotePath.lastIndexOf('/') : 0;
String remoteFileName = lastIndex > 0 ? remotePath.substring(lastIndex + 1) : null;
if (StringUtils.hasText(remoteFileName)) {
SmbFile remoteFile = createSmbFileObject(remotePath);
if (!remoteFile.isFile()) {
throw new IOException("[" + remotePath + "] is not a file.");
if (remoteFile.isFile()) {
files = new SmbFile[] {remoteFile};
}
files = new SmbFile[] {remoteFile};
}
else {
if (files == null) {
try {
SmbFile smbDir = createSmbDirectoryObject(remotePath);
if (!smbDir.exists()) {
@@ -154,6 +155,7 @@ public class SmbSession implements Session<SmbFile> {
throw new IOException("Failed to list in [" + remotePath + "].", _ex);
}
}
logListedFiles(remotePath, files);
return files;

View File

@@ -92,7 +92,7 @@ public class SmbTests extends SmbTestSupport {
DirectoryScanner scanner = new DefaultDirectoryScanner();
IntegrationFlow flow = IntegrationFlow.from(Smb.inboundAdapter(sessionFactory())
.preserveTimestamp(true)
.remoteDirectory("smbSource")
.remoteDirectory("smbSource/subSmbSource/")
.maxFetchSize(10)
.scanner(scanner)
.regexFilter(".*\\.txt$")
@@ -115,13 +115,13 @@ public class SmbTests extends SmbTestSupport {
Object payload = message.getPayload();
assertThat(payload).isInstanceOf(File.class);
File file = (File) payload;
assertThat(file.getName()).isIn("SMBSOURCE1.TXT.a", "SMBSOURCE2.TXT.a");
assertThat(file.getName()).isEqualTo("SUBSMBSOURCE1.TXT.a", "SUBSMBSOURCE2.TXT.a");
assertThat(file.getAbsolutePath()).contains("localTarget");
message = out.receive(10_000);
assertThat(message).isNotNull();
file = (File) message.getPayload();
assertThat(file.getName()).isIn("SMBSOURCE1.TXT.a", "SMBSOURCE2.TXT.a");
assertThat(file.getName()).isIn("SUBSMBSOURCE1.TXT.a", "SUBSMBSOURCE2.TXT.a");
assertThat(file.getAbsolutePath()).contains("localTarget");
assertThat(out.receive(10)).isNull();