Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1579,7 +1579,7 @@ protected void doRemoveDirectory(Buffer buffer, int id) throws IOException {
}

protected void doRemoveDirectory(int id, String path) throws IOException {
Path p = resolveFile(path);
Path p = resolveDirectory(path);
if (log.isDebugEnabled()) {
log.debug("doRemoveDirectory({})[id={}] SSH_FXP_RMDIR (path={})[{}]", getServerSession(), id, path, p);
}
Expand Down Expand Up @@ -1643,7 +1643,7 @@ protected void doMakeDirectory(Buffer buffer, int id) throws IOException {
protected void doMakeDirectory(
int id, String path, Map<String, ?> attrs)
throws IOException {
Path resolvedPath = resolveFile(path);
Path resolvedPath = resolveDirectory(path);
ServerSession session = getServerSession();
if (log.isDebugEnabled()) {
log.debug("doMakeDirectory({})[id={}] SSH_FXP_MKDIR (path={}[{}], attrs={})",
Expand Down Expand Up @@ -3101,4 +3101,13 @@ protected Path resolveFile(String remotePath)
}
return localPath;
}

private Path resolveDirectory(String remotePath) throws IOException {
// Keep explicit dot components; only MKDIR and RMDIR ignore trailing slashes.
int length = remotePath.length();
while ((length > 1) && (remotePath.charAt(length - 1) == '/')) {
length--;
}
return resolveFile(remotePath.substring(0, length));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.sshd.sftp.client;

import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
import org.apache.sshd.common.util.OsUtils;
import org.apache.sshd.sftp.common.SftpException;
import org.apache.sshd.util.test.CommonTestSupportUtils;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class SftpDirectoryTest extends AbstractSftpClientTestSupport {
private Path root;

@BeforeEach
void setUp() throws Exception {
root = detectTargetFolder().resolve(getClass().getSimpleName()).resolve(getCurrentTestName());
CommonTestSupportUtils.deleteRecursive(root);
Files.createDirectories(root);
sshd.setFileSystemFactory(new VirtualFileSystemFactory(root));
}

@ParameterizedTest
@ValueSource(strings = { "/", "///" })
void makeDirectoryWithTrailingSlashes(String suffix) throws Exception {
try (SftpClient sftp = createSingleSessionClient()) {
sftp.mkdir("/parent" + suffix);
assertTrue(Files.isDirectory(root.resolve("parent")));
sftp.mkdir("parent/child" + suffix);
assertTrue(Files.isDirectory(root.resolve("parent/child")));
assertThrows(SftpException.class, () -> sftp.mkdir(suffix));
}
}

@ParameterizedTest
@ValueSource(strings = { "/", "///" })
void removeDirectoryWithTrailingSlashes(String suffix) throws Exception {
Path directory = Files.createDirectory(root.resolve("directory"));
try (SftpClient sftp = createSingleSessionClient()) {
sftp.rmdir("/directory" + suffix);
assertFalse(Files.exists(directory));
}
}

@Test
void directoryOperationsPreservePathComponents() throws Exception {
Assumptions.assumeFalse(OsUtils.isWin32(), "Windows resolves dot path components differently");
Path directory = Files.createDirectory(root.resolve("directory"));
try (SftpClient sftp = createSingleSessionClient()) {
assertThrows(SftpException.class, () -> sftp.mkdir("missing/../created/"));
assertFalse(Files.exists(root.resolve("created")));
assertThrows(SftpException.class, () -> sftp.mkdir("created/."));
assertFalse(Files.exists(root.resolve("created")));
assertThrows(SftpException.class, () -> sftp.rmdir("missing/../directory/"));
assertThrows(SftpException.class, () -> sftp.rmdir("directory/."));
assertTrue(Files.isDirectory(directory));
}
}

@Test
void directorySymlinkIsNotRemoved() throws Exception {
Assumptions.assumeFalse(OsUtils.isWin32(), "Symlinks are not supported on all Windows file systems");
Path directory = Files.createDirectory(root.resolve("directory"));
Path link = Files.createSymbolicLink(root.resolve("link"), directory.getFileName());
try (SftpClient sftp = createSingleSessionClient()) {
assertTrue(sftp.lstat("link").isSymbolicLink());
assertTrue(sftp.lstat("link/").isDirectory());
assertThrows(SftpException.class, () -> sftp.rmdir("link/"));
assertTrue(Files.isSymbolicLink(link));
assertTrue(Files.isDirectory(directory));
}
}
}