From 6c98dd80096fbee36472d2aeb16f38e2d347b11a Mon Sep 17 00:00:00 2001 From: Rob Spoor Date: Wed, 31 Jul 2019 15:34:12 +0200 Subject: [PATCH] Check file attributes if . is not part of a directory listing --- .../filesystems/sftp/SFTPFileSystem.java | 7 +++- .../sftp/AbstractSFTPFileSystemTest.java | 7 +++- .../SFTPFileSystemWithoutSystemDirsTest.java | 32 +++++++++++++++++++ .../sftp/server/FixedSftpSubsystem.java | 29 +++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/github/robtimus/filesystems/sftp/SFTPFileSystemWithoutSystemDirsTest.java diff --git a/src/main/java/com/github/robtimus/filesystems/sftp/SFTPFileSystem.java b/src/main/java/com/github/robtimus/filesystems/sftp/SFTPFileSystem.java index 78a3ac2..fa61d51 100644 --- a/src/main/java/com/github/robtimus/filesystems/sftp/SFTPFileSystem.java +++ b/src/main/java/com/github/robtimus/filesystems/sftp/SFTPFileSystem.java @@ -338,7 +338,12 @@ DirectoryStream newDirectoryStream(final SFTPPath path, Filter>asList(new FixedSftpSubsystem.Factory())); + sshServer.setSubsystemFactories(Arrays.>asList(subSystemFactory)); rootPath = Files.createTempDirectory("sftp-fs"); defaultDir = rootPath.resolve("home"); diff --git a/src/test/java/com/github/robtimus/filesystems/sftp/SFTPFileSystemWithoutSystemDirsTest.java b/src/test/java/com/github/robtimus/filesystems/sftp/SFTPFileSystemWithoutSystemDirsTest.java new file mode 100644 index 0000000..42acb75 --- /dev/null +++ b/src/test/java/com/github/robtimus/filesystems/sftp/SFTPFileSystemWithoutSystemDirsTest.java @@ -0,0 +1,32 @@ +/* + * SFTPFileSystemWithoutSystemDirsTest.java + * Copyright 2019 Rob Spoor + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.robtimus.filesystems.sftp; + +import java.io.IOException; +import java.security.NoSuchAlgorithmException; +import org.junit.BeforeClass; +import com.github.robtimus.filesystems.sftp.server.FixedSftpSubsystem; + +@SuppressWarnings("javadoc") +public class SFTPFileSystemWithoutSystemDirsTest extends SFTPFileSystemTest { + + @BeforeClass + public static void setupClass() throws NoSuchAlgorithmException, IOException { + setupClass(new FixedSftpSubsystem.FactoryWithoutSystemDirs()); + } +} diff --git a/src/test/java/com/github/robtimus/filesystems/sftp/server/FixedSftpSubsystem.java b/src/test/java/com/github/robtimus/filesystems/sftp/server/FixedSftpSubsystem.java index eb1b64a..c3f340b 100644 --- a/src/test/java/com/github/robtimus/filesystems/sftp/server/FixedSftpSubsystem.java +++ b/src/test/java/com/github/robtimus/filesystems/sftp/server/FixedSftpSubsystem.java @@ -25,6 +25,9 @@ import org.apache.sshd.common.util.GenericUtils; import org.apache.sshd.common.util.io.IoUtils; import org.apache.sshd.server.Command; +import org.apache.sshd.server.session.ServerSession; +import org.apache.sshd.server.subsystem.sftp.DirectoryHandle; +import org.apache.sshd.server.subsystem.sftp.Handle; import org.apache.sshd.server.subsystem.sftp.SftpEventListener; import org.apache.sshd.server.subsystem.sftp.SftpSubsystem; import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory; @@ -63,4 +66,30 @@ public Command create() { return subsystem; } } + + public static final class FactoryWithoutSystemDirs extends SftpSubsystemFactory { + + @Override + public Command create() { + SftpSubsystem subsystem = new FixedSftpSubsystem(getExecutorService(), isShutdownOnExit(), getUnsupportedAttributePolicy()); + Collection listeners = getRegisteredListeners(); + if (GenericUtils.size(listeners) > 0) { + for (SftpEventListener l : listeners) { + subsystem.addSftpEventListener(l); + } + } + subsystem.addSftpEventListener(new SftpEventListener() { + @Override + public void open(ServerSession session, String remoteHandle, Handle localHandle) throws IOException { + if (localHandle instanceof DirectoryHandle) { + DirectoryHandle directoryHandle = (DirectoryHandle) localHandle; + directoryHandle.markDotSent(); + directoryHandle.markDotDotSent(); + } + } + }); + + return subsystem; + } + } }