You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2018/10/25 09:10:59 UTC
[3/9] mina-sshd git commit: [SSHD-842] Moved a few more common
classes to sshd-common artifact
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/98ac7c2c/sshd-common/src/test/java/org/apache/sshd/common/file/util/BasePathTest.java
----------------------------------------------------------------------
diff --git a/sshd-common/src/test/java/org/apache/sshd/common/file/util/BasePathTest.java b/sshd-common/src/test/java/org/apache/sshd/common/file/util/BasePathTest.java
new file mode 100644
index 0000000..baaa1e6
--- /dev/null
+++ b/sshd-common/src/test/java/org/apache/sshd/common/file/util/BasePathTest.java
@@ -0,0 +1,548 @@
+/*
+ * 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.common.file.util;
+
+import java.io.IOException;
+import java.net.URI;
+import java.nio.file.FileSystem;
+import java.nio.file.LinkOption;
+import java.nio.file.Path;
+import java.nio.file.attribute.UserPrincipalLookupService;
+import java.nio.file.spi.FileSystemProvider;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.sshd.common.util.GenericUtils;
+import org.apache.sshd.util.test.JUnitTestSupport;
+import org.apache.sshd.util.test.NoIoTestCase;
+import org.junit.Before;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runners.MethodSorters;
+import org.mockito.Mockito;
+
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+@Category({ NoIoTestCase.class })
+@SuppressWarnings("checkstyle:MethodCount")
+public class BasePathTest extends JUnitTestSupport {
+ private TestFileSystem fileSystem;
+
+ public BasePathTest() {
+ super();
+ }
+
+ @Before
+ public void setUp() {
+ fileSystem = new TestFileSystem(Mockito.mock(FileSystemProvider.class));
+ }
+
+ @Test
+ public void testBasicPathParsing() {
+ assertPathEquals("/", "/");
+ assertPathEquals("/foo", "/foo");
+ assertPathEquals("/foo", "/", "foo");
+ assertPathEquals("/foo/bar", "/foo/bar");
+ assertPathEquals("/foo/bar", "/", "foo", "bar");
+ assertPathEquals("/foo/bar", "/foo", "bar");
+ assertPathEquals("/foo/bar", "/", "foo/bar");
+ assertPathEquals("foo/bar/baz", "foo/bar/baz");
+ assertPathEquals("foo/bar/baz", "foo", "bar", "baz");
+ assertPathEquals("foo/bar/baz", "foo/bar", "baz");
+ assertPathEquals("foo/bar/baz", "foo", "bar/baz");
+ }
+
+ @Test
+ public void testPathParsingWithExtraSeparators() {
+ assertPathEquals("/foo/bar", "///foo/bar");
+ assertPathEquals("/foo/bar", "/foo///bar//");
+ assertPathEquals("/foo/bar/baz", "/foo", "/bar", "baz/");
+ //assertPathEquals("/foo/bar/baz", "/foo\\/bar//\\\\/baz\\/");
+ }
+
+ @Test
+ public void testRootPath() {
+ new PathTester(fileSystem, "/")
+ .root("/")
+ .test("/");
+ }
+
+ @Test
+ public void testRelativePathSingleName() {
+ new PathTester(fileSystem, "test")
+ .names("test")
+ .test("test");
+
+ Path path = parsePath("test");
+ assertEquals(path, path.getFileName());
+ }
+
+ @Test
+ public void testRelativePathTwoNames() {
+ new PathTester(fileSystem, "foo/bar")
+ .names("foo", "bar")
+ .test("foo/bar");
+ }
+
+ @Test
+ public void testRelativePathFourNames() {
+ new PathTester(fileSystem, "foo/bar/baz/test")
+ .names("foo", "bar", "baz", "test")
+ .test("foo/bar/baz/test");
+ }
+
+ @Test
+ public void testAbsolutePathSingleName() {
+ new PathTester(fileSystem, "/foo")
+ .root("/")
+ .names("foo")
+ .test("/foo");
+ }
+
+ @Test
+ public void testAbsolutePathTwoNames() {
+ new PathTester(fileSystem, "/foo/bar")
+ .root("/")
+ .names("foo", "bar")
+ .test("/foo/bar");
+ }
+
+ @Test
+ public void testAbsoluteMultiNamePathFourNames() {
+ new PathTester(fileSystem, "/foo/bar/baz/test")
+ .root("/")
+ .names("foo", "bar", "baz", "test")
+ .test("/foo/bar/baz/test");
+ }
+
+ @Test
+ public void testResolveFromRoot() {
+ Path root = parsePath("/");
+ assertResolvedPathEquals("/foo", root, "foo");
+ assertResolvedPathEquals("/foo/bar", root, "foo/bar");
+ assertResolvedPathEquals("/foo/bar", root, "foo", "bar");
+ assertResolvedPathEquals("/foo/bar/baz/test", root, "foo/bar/baz/test");
+ assertResolvedPathEquals("/foo/bar/baz/test", root, "foo", "bar/baz", "test");
+ }
+
+ @Test
+ public void testResolveFromAbsolute() {
+ Path path = parsePath("/foo");
+ assertResolvedPathEquals("/foo/bar", path, "bar");
+ assertResolvedPathEquals("/foo/bar/baz/test", path, "bar/baz/test");
+ assertResolvedPathEquals("/foo/bar/baz/test", path, "bar/baz", "test");
+ assertResolvedPathEquals("/foo/bar/baz/test", path, "bar", "baz", "test");
+ }
+
+ @Test
+ public void testResolveFromRelative() {
+ Path path = parsePath("foo");
+ assertResolvedPathEquals("foo/bar", path, "bar");
+ assertResolvedPathEquals("foo/bar/baz/test", path, "bar/baz/test");
+ assertResolvedPathEquals("foo/bar/baz/test", path, "bar", "baz", "test");
+ assertResolvedPathEquals("foo/bar/baz/test", path, "bar/baz", "test");
+ }
+
+ @Test
+ public void testResolveWithThisAndParentDirNames() {
+ Path path = parsePath("/foo");
+ assertResolvedPathEquals("/foo/bar/../baz", path, "bar/../baz");
+ assertResolvedPathEquals("/foo/bar/../baz", path, "bar", "..", "baz");
+ assertResolvedPathEquals("/foo/./bar/baz", path, "./bar/baz");
+ assertResolvedPathEquals("/foo/./bar/baz", path, ".", "bar/baz");
+ }
+
+ @Test
+ public void testResolveGivenAbsolutePath() {
+ assertResolvedPathEquals("/test", parsePath("/foo"), "/test");
+ assertResolvedPathEquals("/test", parsePath("foo"), "/test");
+ }
+
+ @Test
+ public void testResolveGivenEmptyPath() {
+ assertResolvedPathEquals("/foo", parsePath("/foo"), "");
+ assertResolvedPathEquals("foo", parsePath("foo"), "");
+ }
+
+ @Test
+ public void testResolveAgainstEmptyPath() {
+ assertResolvedPathEquals("foo/bar", parsePath(""), "foo/bar");
+ }
+
+ @Test
+ public void testResolveSiblingGivenEmptyPath() {
+ Path path = parsePath("foo/bar");
+ Path resolved = path.resolveSibling("");
+ assertPathEquals("foo", resolved);
+
+ path = parsePath("foo");
+ resolved = path.resolveSibling("");
+ assertPathEquals("", resolved);
+ }
+
+ @Test
+ public void testResolveSiblingAgainstEmptyPath() {
+ Path path = parsePath("");
+ Path resolved = path.resolveSibling("foo");
+ assertPathEquals("foo", resolved);
+
+ path = parsePath("");
+ resolved = path.resolveSibling("");
+ assertPathEquals("", resolved);
+ }
+
+ @Test
+ public void testRelativizeBothAbsolute() {
+ assertRelativizedPathEquals("b/c", parsePath("/a"), "/a/b/c");
+ assertRelativizedPathEquals("c/d", parsePath("/a/b"), "/a/b/c/d");
+ }
+
+ @Test
+ public void testRelativizeBothRelative() {
+ assertRelativizedPathEquals("b/c", parsePath("a"), "a/b/c");
+ assertRelativizedPathEquals("d", parsePath("a/b/c"), "a/b/c/d");
+ }
+
+ @Test
+ public void testRelativizeAgainstEmptyPath() {
+ assertRelativizedPathEquals("foo/bar", parsePath(""), "foo/bar");
+ }
+
+ @Test
+ public void testRelativizeOneAbsoluteOneRelative() {
+ try {
+ Path result = parsePath("/foo/bar").relativize(parsePath("foo"));
+ fail("Unexpected 2-level result: " + result);
+ } catch (IllegalArgumentException expected) {
+ // ignored
+ }
+
+ try {
+ Path result = parsePath("foo").relativize(parsePath("/foo/bar"));
+ fail("Unexpected 1-level result: " + result);
+ } catch (IllegalArgumentException expected) {
+ // ignored
+ }
+ }
+
+ @Test
+ public void testNormalizeWithParentDirName() {
+ assertNormalizedPathEquals("/foo/baz", "/foo/bar/../baz");
+ assertNormalizedPathEquals("/foo/baz", "/foo", "bar", "..", "baz");
+ }
+
+ @Test
+ public void testNormalizeWithThisDirName() {
+ assertNormalizedPathEquals("/foo/bar/baz", "/foo/bar/./baz");
+ assertNormalizedPathEquals("/foo/bar/baz", "/foo", "bar", ".", "baz");
+ }
+
+ @Test
+ public void testNormalizeWithThisAndParentDirNames() {
+ assertNormalizedPathEquals("foo/test", "foo/./bar/../././baz/../test");
+ }
+
+ @Test
+ public void testNormalizeWithLeadingParentDirNames() {
+ assertNormalizedPathEquals("../../foo/baz", "../../foo/bar/../baz");
+ }
+
+ @Test
+ public void testNormalizeWithLeadingThisAndParentDirNames() {
+ assertNormalizedPathEquals("../../foo/baz", "./.././.././foo/bar/../baz");
+ }
+
+ @Test
+ public void testNormalizeWithExtraParentDirNamesAtRoot() {
+ assertNormalizedPathEquals("/", "/..");
+ assertNormalizedPathEquals("/", "/../../..");
+ assertNormalizedPathEquals("/", "/foo/../../..");
+ assertNormalizedPathEquals("/", "/../foo/../../bar/baz/../../../..");
+ }
+
+ @Test
+ public void testPathWithExtraSlashes() {
+ assertPathEquals("/foo/bar/baz", parsePath("/foo/bar/baz/"));
+ assertPathEquals("/foo/bar/baz", parsePath("/foo//bar///baz"));
+ assertPathEquals("/foo/bar/baz", parsePath("///foo/bar/baz"));
+ }
+
+ private void assertResolvedPathEquals(
+ String expected, Path path, String firstResolvePath, String... moreResolvePaths) {
+ Path resolved = path.resolve(firstResolvePath);
+ for (String additionalPath : moreResolvePaths) {
+ resolved = resolved.resolve(additionalPath);
+ }
+ assertPathEquals(expected, resolved);
+
+ Path relative = parsePath(firstResolvePath, moreResolvePaths);
+ resolved = path.resolve(relative);
+ assertPathEquals(expected, resolved);
+
+ // assert the invariant that p.relativize(p.resolve(q)).equals(q) when q does not have a root
+ // p = path, q = relative, p.resolve(q) = resolved
+ if (relative.getRoot() == null) {
+ assertEquals(relative, path.relativize(resolved));
+ }
+ }
+
+ private void assertRelativizedPathEquals(String expected, Path path, String relativizePath) {
+ Path relativized = path.relativize(parsePath(relativizePath));
+ assertPathEquals(expected, relativized);
+ }
+
+ private void assertNormalizedPathEquals(String expected, String first, String... more) {
+ assertPathEquals(expected, parsePath(first, more).normalize());
+ }
+
+ private void assertPathEquals(String expected, String first, String... more) {
+ assertPathEquals(expected, parsePath(first, more));
+ }
+
+ private void assertPathEquals(String expected, Path path) {
+ assertEquals(parsePath(expected), path);
+ }
+
+ private Path parsePath(String first, String... more) {
+ return fileSystem.getPath(first, more);
+ }
+
+ private static class TestFileSystem extends BaseFileSystem<TestPath> {
+ TestFileSystem(FileSystemProvider fileSystemProvider) {
+ super(fileSystemProvider);
+ }
+
+ @Override
+ protected TestPath create(String root, List<String> names) {
+ return new TestPath(this, root, names);
+ }
+
+ @Override
+ public void close() throws IOException {
+ // ignored
+ }
+
+ @Override
+ public boolean isOpen() {
+ return false;
+ }
+
+ @Override
+ public Set<String> supportedFileAttributeViews() {
+ return null;
+ }
+
+ @Override
+ public UserPrincipalLookupService getUserPrincipalLookupService() {
+ return null;
+ }
+ }
+
+ private static class TestPath extends BasePath<TestPath, TestFileSystem> {
+ TestPath(TestFileSystem fileSystem, String root, List<String> names) {
+ super(fileSystem, root, names);
+ }
+
+ @Override
+ protected TestPath create(String root, List<String> names) {
+ return new TestPath(getFileSystem(), root, names);
+ }
+
+ @Override
+ public URI toUri() {
+ return null;
+ }
+
+ @Override
+ public Path toRealPath(LinkOption... options) throws IOException {
+ return null;
+ }
+ }
+
+ public static class PathTester {
+ private final FileSystem fileSystem;
+ private final String string;
+ private String root;
+ private List<String> names = Collections.emptyList();
+
+ public PathTester(FileSystem fileSystem, String string) {
+ this.fileSystem = fileSystem;
+ this.string = string;
+ }
+
+ public PathTester root(String root) {
+ this.root = root;
+ return this;
+ }
+
+ public PathTester names(Collection<String> names) {
+ this.names = GenericUtils.unmodifiableList(names);
+ return this;
+ }
+
+ public PathTester names(String... names) {
+ return names(Arrays.asList(names));
+ }
+
+ public void test(String first, String... more) {
+ Path path = fileSystem.getPath(first, more);
+ test(path);
+ }
+
+ public void test(Path path) {
+ assertEquals("Mismatched path value", string, path.toString());
+
+ testRoot(path);
+ testNames(path);
+ testParents(path);
+ testStartsWith(path);
+ testEndsWith(path);
+ testSubpaths(path);
+ }
+
+ protected void testRoot(Path path) {
+ if (root != null) {
+ assertTrue(path + ".isAbsolute() ?", path.isAbsolute());
+ assertNotNull(path + ".getRoot() <> null ?", path.getRoot());
+ assertEquals("Mismatched root path value", root, path.getRoot().toString());
+ } else {
+ assertFalse(path + ".is(Not)Absolute() ?", path.isAbsolute());
+ assertNull(path + ".getRoot() == null ?", path.getRoot());
+ }
+ }
+
+ protected void testNames(Path path) {
+ assertEquals("Mismatched names count", names.size(), path.getNameCount());
+ assertListEquals("Mismatched path names", names, names(path));
+
+ for (int i = 0; i < names.size(); i++) {
+ String nameAtIndex = names.get(i);
+ Path pathAtIndex = path.getName(i);
+ assertEquals("Mismatched component name at index=" + i, nameAtIndex, pathAtIndex.toString());
+ // don't test individual names if this is an individual name
+ if (names.size() > 1) {
+ new PathTester(fileSystem, nameAtIndex)
+ .names(nameAtIndex)
+ .test(pathAtIndex);
+ }
+ }
+
+ if (names.size() > 0) {
+ String fileName = names.get(names.size() - 1);
+ assertEquals("Mismatched last component name", fileName, path.getFileName().toString());
+ // don't test individual names if this is an individual name
+ if (names.size() > 1) {
+ new PathTester(fileSystem, fileName)
+ .names(fileName)
+ .test(path.getFileName());
+ }
+ }
+ }
+
+ protected void testParents(Path path) {
+ Path parent = path.getParent();
+ if (((root != null) && (names.size() >= 1)) || (names.size() > 1)) {
+ assertNotNull("No parent", parent);
+ }
+
+ if (parent != null) {
+ String parentName = names.size() == 1 ? root : string.substring(0, string.lastIndexOf('/'));
+ new PathTester(fileSystem, parentName)
+ .root(root)
+ .names(names.subList(0, names.size() - 1))
+ .test(parent);
+ }
+ }
+
+ protected void testSubpaths(Path path) {
+ int nameCount = path.getNameCount();
+ if (path.getRoot() == null) {
+ assertEquals(path, path.subpath(0, nameCount));
+ }
+
+ if (nameCount > 1) {
+ String stringWithoutRoot = root == null ? string : string.substring(root.length());
+
+ // test start + 1 to end and start to end - 1 subpaths... this recursively tests all subpaths
+ // actually tests most possible subpaths multiple times but... eh
+ Path startSubpath = path.subpath(1, nameCount);
+ List<String> startNames = split(stringWithoutRoot, '/')
+ .subList(1, nameCount);
+ new PathTester(fileSystem, GenericUtils.join(startNames, '/'))
+ .names(startNames)
+ .test(startSubpath);
+
+ Path endSubpath = path.subpath(0, nameCount - 1);
+ List<String> endNames = split(stringWithoutRoot, '/')
+ .subList(0, nameCount - 1);
+ new PathTester(fileSystem, GenericUtils.join(endNames, '/'))
+ .names(endNames)
+ .test(endSubpath);
+ }
+ }
+
+ protected void testStartsWith(Path path) {
+ // empty path doesn't start with any path
+ if ((root != null) || (!names.isEmpty())) {
+ Path other = path;
+ while (other != null) {
+ assertTrue(path + ".startsWith(" + other + ")[path] ?", path.startsWith(other));
+ assertTrue(path + ".startsWith(" + other + ")[string] ?", path.startsWith(other.toString()));
+ other = other.getParent();
+ }
+ }
+ }
+
+ protected void testEndsWith(Path path) {
+ // empty path doesn't start with any path
+ if ((root != null) || (!names.isEmpty())) {
+ Path other = path;
+ while (other != null) {
+ assertTrue(path + ".endsWith(" + other + ")[path] ?", path.endsWith(other));
+ assertTrue(path + ".endsWith(" + other + ")[string] ?", path.endsWith(other.toString()));
+
+ int otherNameCount = other.getNameCount();
+ if ((other.getRoot() != null) && (otherNameCount > 0)) {
+ other = other.subpath(0, otherNameCount);
+ } else if (otherNameCount > 1) {
+ other = other.subpath(1, otherNameCount);
+ } else {
+ other = null;
+ }
+ }
+ }
+ }
+
+ private static List<String> names(Path path) {
+ List<String> list = new ArrayList<>();
+ for (Path p : path) {
+ list.add(p.toString());
+ }
+ return list;
+ }
+
+ private static List<String> split(String string, char sep) {
+ return Arrays.asList(GenericUtils.split(string, sep));
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/98ac7c2c/sshd-core/src/main/java/org/apache/sshd/common/file/FileSystemAware.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/file/FileSystemAware.java b/sshd-core/src/main/java/org/apache/sshd/common/file/FileSystemAware.java
deleted file mode 100644
index e73c37d..0000000
--- a/sshd-core/src/main/java/org/apache/sshd/common/file/FileSystemAware.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * 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.common.file;
-
-import java.nio.file.FileSystem;
-
-/**
- * Interface that can be implemented by a command to be able to access the
- * file system in which this command will be used.
- */
-@FunctionalInterface
-public interface FileSystemAware {
- /**
- * Set the file system in which this shell will be executed.
- *
- * @param fileSystem the file system
- */
- void setFileSystem(FileSystem fileSystem);
-}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/98ac7c2c/sshd-core/src/main/java/org/apache/sshd/common/file/root/RootedFileSystem.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/file/root/RootedFileSystem.java b/sshd-core/src/main/java/org/apache/sshd/common/file/root/RootedFileSystem.java
deleted file mode 100644
index 1b9a71c..0000000
--- a/sshd-core/src/main/java/org/apache/sshd/common/file/root/RootedFileSystem.java
+++ /dev/null
@@ -1,101 +0,0 @@
-/*
- * 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.common.file.root;
-
-import java.io.IOException;
-import java.nio.file.FileStore;
-import java.nio.file.FileSystem;
-import java.nio.file.Path;
-import java.nio.file.attribute.UserPrincipalLookupService;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Set;
-
-import org.apache.sshd.common.file.util.BaseFileSystem;
-
-/**
- * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
- */
-public class RootedFileSystem extends BaseFileSystem<RootedPath> {
-
- private final Path rootPath;
- private final FileSystem rootFs;
-
- public RootedFileSystem(RootedFileSystemProvider fileSystemProvider, Path root, Map<String, ?> env) {
- super(fileSystemProvider);
- this.rootPath = Objects.requireNonNull(root, "No root path");
- this.rootFs = root.getFileSystem();
- }
-
- public FileSystem getRootFileSystem() {
- return rootFs;
- }
-
- public Path getRoot() {
- return rootPath;
- }
-
- @Override
- public RootedFileSystemProvider provider() {
- return (RootedFileSystemProvider) super.provider();
- }
-
- @Override
- public void close() throws IOException {
- if (log.isDebugEnabled()) {
- log.debug("close({})", this);
- }
- }
-
- @Override
- public boolean isOpen() {
- return rootFs.isOpen();
- }
-
- @Override
- public boolean isReadOnly() {
- return rootFs.isReadOnly();
- }
-
- @Override
- public Set<String> supportedFileAttributeViews() {
- return rootFs.supportedFileAttributeViews();
- }
-
- @Override
- public UserPrincipalLookupService getUserPrincipalLookupService() {
- return rootFs.getUserPrincipalLookupService();
- }
-
- @Override
- protected RootedPath create(String root, List<String> names) {
- return new RootedPath(this, root, names);
- }
-
- @Override
- public Iterable<FileStore> getFileStores() {
- return rootFs.getFileStores();
- }
-
- @Override
- public String toString() {
- return rootPath.toString();
- }
-}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/98ac7c2c/sshd-core/src/main/java/org/apache/sshd/common/file/root/RootedFileSystemProvider.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/file/root/RootedFileSystemProvider.java b/sshd-core/src/main/java/org/apache/sshd/common/file/root/RootedFileSystemProvider.java
deleted file mode 100644
index 8765f47..0000000
--- a/sshd-core/src/main/java/org/apache/sshd/common/file/root/RootedFileSystemProvider.java
+++ /dev/null
@@ -1,504 +0,0 @@
-/*
- * 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.common.file.root;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.nio.channels.AsynchronousFileChannel;
-import java.nio.channels.FileChannel;
-import java.nio.channels.SeekableByteChannel;
-import java.nio.file.AccessMode;
-import java.nio.file.CopyOption;
-import java.nio.file.DirectoryStream;
-import java.nio.file.FileStore;
-import java.nio.file.FileSystem;
-import java.nio.file.FileSystemAlreadyExistsException;
-import java.nio.file.FileSystemNotFoundException;
-import java.nio.file.Files;
-import java.nio.file.InvalidPathException;
-import java.nio.file.LinkOption;
-import java.nio.file.OpenOption;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.nio.file.ProviderMismatchException;
-import java.nio.file.attribute.BasicFileAttributes;
-import java.nio.file.attribute.FileAttribute;
-import java.nio.file.attribute.FileAttributeView;
-import java.nio.file.spi.FileSystemProvider;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Set;
-import java.util.concurrent.ExecutorService;
-
-import org.apache.sshd.common.util.ValidateUtils;
-import org.apache.sshd.common.util.io.IoUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * File system provider which provides a rooted file system.
- * The file system only gives access to files under the root directory.
- *
- * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
- */
-public class RootedFileSystemProvider extends FileSystemProvider {
- protected final Logger log;
- private final Map<Path, RootedFileSystem> fileSystems = new HashMap<>();
-
- public RootedFileSystemProvider() {
- log = LoggerFactory.getLogger(getClass());
- }
-
- @Override
- public String getScheme() {
- return "root";
- }
-
- @Override
- public FileSystem newFileSystem(URI uri, Map<String, ?> env) throws IOException {
- return newFileSystem(uri, uriToPath(uri), env);
- }
-
- @Override
- public FileSystem getFileSystem(URI uri) {
- return getFileSystem(uriToPath(uri));
- }
-
- @Override
- public FileSystem newFileSystem(Path path, Map<String, ?> env) throws IOException {
- return newFileSystem(path, path, env);
- }
-
- protected FileSystem newFileSystem(Object src, Path path, Map<String, ?> env) throws IOException {
- Path root = ensureDirectory(path).toRealPath();
- RootedFileSystem rootedFs = null;
- synchronized (fileSystems) {
- if (!this.fileSystems.containsKey(root)) {
- rootedFs = new RootedFileSystem(this, path, env);
- this.fileSystems.put(root, rootedFs);
- }
- }
-
- // do all the throwing outside the synchronized block to minimize its lock time
- if (rootedFs == null) {
- throw new FileSystemAlreadyExistsException("newFileSystem(" + src + ") already mapped " + root);
- }
-
- if (log.isTraceEnabled()) {
- log.trace("newFileSystem({}): {}", src, rootedFs);
- }
-
- return rootedFs;
- }
-
- protected Path uriToPath(URI uri) {
- String scheme = uri.getScheme();
- String expected = getScheme();
- if ((scheme == null) || (!scheme.equalsIgnoreCase(expected))) {
- throw new IllegalArgumentException("URI scheme (" + scheme + ") is not '" + expected + "'");
- }
-
- String root = uri.getRawSchemeSpecificPart();
- int i = root.indexOf("!/");
- if (i != -1) {
- root = root.substring(0, i);
- }
-
- try {
- return Paths.get(new URI(root)).toAbsolutePath();
- } catch (URISyntaxException e) {
- throw new IllegalArgumentException(root + ": " + e.getMessage(), e);
- }
- }
-
- private static Path ensureDirectory(Path path) {
- return IoUtils.ensureDirectory(path, IoUtils.getLinkOptions(true));
- }
-
- @Override
- public Path getPath(URI uri) {
- String str = uri.getSchemeSpecificPart();
- int i = str.indexOf("!/");
- if (i == -1) {
- throw new IllegalArgumentException("URI: " + uri + " does not contain path info - e.g., root:file://foo/bar!/");
- }
-
- FileSystem fs = getFileSystem(uri);
- String subPath = str.substring(i + 1);
- Path p = fs.getPath(subPath);
- if (log.isTraceEnabled()) {
- log.trace("getPath({}): {}", uri, p);
- }
- return p;
- }
-
- @Override
- public InputStream newInputStream(Path path, OpenOption... options) throws IOException {
- Path r = unroot(path);
- FileSystemProvider p = provider(r);
- return p.newInputStream(r, options);
- }
-
- @Override
- public OutputStream newOutputStream(Path path, OpenOption... options) throws IOException {
- Path r = unroot(path);
- FileSystemProvider p = provider(r);
- return p.newOutputStream(r, options);
- }
-
- @Override
- public FileChannel newFileChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
- Path r = unroot(path);
- FileSystemProvider p = provider(r);
- return p.newFileChannel(r, options, attrs);
- }
-
- @Override
- public AsynchronousFileChannel newAsynchronousFileChannel(
- Path path, Set<? extends OpenOption> options, ExecutorService executor, FileAttribute<?>... attrs)
- throws IOException {
- Path r = unroot(path);
- FileSystemProvider p = provider(r);
- return p.newAsynchronousFileChannel(r, options, executor, attrs);
- }
-
- @Override
- public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
- Path r = unroot(path);
- FileSystemProvider p = provider(r);
- return p.newByteChannel(r, options, attrs);
- }
-
- @Override
- public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException {
- Path r = unroot(dir);
- FileSystemProvider p = provider(r);
- return root(((RootedPath) dir).getFileSystem(), p.newDirectoryStream(r, filter));
- }
-
- protected DirectoryStream<Path> root(RootedFileSystem rfs, DirectoryStream<Path> ds) {
- return new DirectoryStream<Path>() {
- @Override
- public Iterator<Path> iterator() {
- return root(rfs, ds.iterator());
- }
-
- @Override
- public void close() throws IOException {
- ds.close();
- }
- };
- }
-
- protected Iterator<Path> root(RootedFileSystem rfs, Iterator<Path> iter) {
- return new Iterator<Path>() {
- @Override
- public boolean hasNext() {
- return iter.hasNext();
- }
-
- @Override
- public Path next() {
- return root(rfs, iter.next());
- }
- };
- }
-
- @Override
- public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException {
- Path r = unroot(dir);
- FileSystemProvider p = provider(r);
- p.createDirectory(r, attrs);
- }
-
- @Override
- public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs) throws IOException {
- createLink(link, target, true, attrs);
- }
-
- @Override
- public void createLink(Path link, Path existing) throws IOException {
- createLink(link, existing, false);
- }
-
- protected void createLink(Path link, Path target, boolean symLink, FileAttribute<?>... attrs) throws IOException {
- Path l = unroot(link);
- Path t = unroot(target);
- /*
- * For a symbolic link preserve the relative path
- */
- if (symLink && (!target.isAbsolute())) {
- RootedFileSystem rfs = ((RootedPath) target).getFileSystem();
- Path root = rfs.getRoot();
- t = root.relativize(t);
- }
-
- FileSystemProvider p = provider(l);
- if (symLink) {
- p.createSymbolicLink(l, t, attrs);
- } else {
- p.createLink(l, t);
- }
-
- if (log.isDebugEnabled()) {
- log.debug("createLink(symbolic={}) {} => {}", symLink, l, t);
- }
- }
-
- @Override
- public void delete(Path path) throws IOException {
- Path r = unroot(path);
- if (log.isTraceEnabled()) {
- log.trace("delete({}): {}", path, r);
- }
- FileSystemProvider p = provider(r);
- p.delete(r);
- }
-
- @Override
- public boolean deleteIfExists(Path path) throws IOException {
- Path r = unroot(path);
- if (log.isTraceEnabled()) {
- log.trace("deleteIfExists({}): {}", path, r);
- }
- FileSystemProvider p = provider(r);
- return p.deleteIfExists(r);
- }
-
- @Override
- public Path readSymbolicLink(Path link) throws IOException {
- Path r = unroot(link);
- FileSystemProvider p = provider(r);
- Path t = p.readSymbolicLink(r);
- Path target = root((RootedFileSystem) link.getFileSystem(), t);
- if (log.isTraceEnabled()) {
- log.trace("readSymbolicLink({})[{}]: {}[{}]", link, r, target, t);
- }
- return target;
- }
-
- @Override
- public void copy(Path source, Path target, CopyOption... options) throws IOException {
- Path s = unroot(source);
- Path t = unroot(target);
- if (log.isTraceEnabled()) {
- log.trace("copy({})[{}]: {}[{}]", source, s, target, t);
- }
- FileSystemProvider p = provider(s);
- p.copy(s, t, options);
- }
-
- @Override
- public void move(Path source, Path target, CopyOption... options) throws IOException {
- Path s = unroot(source);
- Path t = unroot(target);
- if (log.isTraceEnabled()) {
- log.trace("move({})[{}]: {}[{}]", source, s, target, t);
- }
- FileSystemProvider p = provider(s);
- p.move(s, t, options);
- }
-
- @Override
- public boolean isSameFile(Path path, Path path2) throws IOException {
- Path r = unroot(path);
- Path r2 = unroot(path2);
- FileSystemProvider p = provider(r);
- return p.isSameFile(r, r2);
- }
-
- @Override
- public boolean isHidden(Path path) throws IOException {
- Path r = unroot(path);
- FileSystemProvider p = provider(r);
- return p.isHidden(r);
- }
-
- @Override
- public FileStore getFileStore(Path path) throws IOException {
- RootedFileSystem fileSystem = getFileSystem(path);
- Path root = fileSystem.getRoot();
- return Files.getFileStore(root);
- }
-
- protected RootedFileSystem getFileSystem(Path path) throws FileSystemNotFoundException {
- Path real = unroot(path);
- Path rootInstance = null;
- RootedFileSystem fsInstance = null;
- synchronized (fileSystems) {
- // Cannot use forEach because the referenced variable are not effectively final
- for (Map.Entry<Path, RootedFileSystem> fse : fileSystems.entrySet()) {
- Path root = fse.getKey();
- RootedFileSystem fs = fse.getValue();
- if (real.equals(root)) {
- return fs; // we were lucky to have the root
- }
-
- if (!real.startsWith(root)) {
- continue;
- }
-
- // if already have a candidate prefer the longer match since both are prefixes of the real path
- if ((rootInstance == null) || (rootInstance.getNameCount() < root.getNameCount())) {
- rootInstance = root;
- fsInstance = fs;
- }
- }
- }
-
- if (fsInstance == null) {
- throw new FileSystemNotFoundException(path.toString());
- }
-
- if (log.isTraceEnabled()) {
- log.trace("getFileSystem({}): {}", path, fsInstance);
- }
-
- return fsInstance;
- }
-
- @Override
- public void checkAccess(Path path, AccessMode... modes) throws IOException {
- Path r = unroot(path);
- FileSystemProvider p = provider(r);
- p.checkAccess(r, modes);
- }
-
- @Override
- public <V extends FileAttributeView> V getFileAttributeView(Path path, Class<V> type, LinkOption... options) {
- Path r = unroot(path);
- FileSystemProvider p = provider(r);
- return p.getFileAttributeView(r, type, options);
- }
-
- @Override
- public <A extends BasicFileAttributes> A readAttributes(Path path, Class<A> type, LinkOption... options) throws IOException {
- Path r = unroot(path);
- if (log.isTraceEnabled()) {
- log.trace("readAttributes({})[{}] type={}", path, r, type.getSimpleName());
- }
-
- FileSystemProvider p = provider(r);
- return p.readAttributes(r, type, options);
- }
-
- @Override
- public Map<String, Object> readAttributes(Path path, String attributes, LinkOption... options) throws IOException {
- Path r = unroot(path);
- FileSystemProvider p = provider(r);
- Map<String, Object> attrs = p.readAttributes(r, attributes, options);
- if (log.isTraceEnabled()) {
- log.trace("readAttributes({})[{}] {}: {}", path, r, attributes, attrs);
- }
- return attrs;
- }
-
- @Override
- public void setAttribute(Path path, String attribute, Object value, LinkOption... options) throws IOException {
- Path r = unroot(path);
- if (log.isTraceEnabled()) {
- log.trace("setAttribute({})[{}] {}={}", path, r, attribute, value);
- }
- FileSystemProvider p = provider(r);
- p.setAttribute(r, attribute, value, options);
- }
-
- protected FileSystemProvider provider(Path path) {
- FileSystem fs = path.getFileSystem();
- return fs.provider();
- }
-
- protected Path root(RootedFileSystem rfs, Path nat) {
- if (nat.isAbsolute()) {
- Path root = rfs.getRoot();
- Path rel = root.relativize(nat);
- return rfs.getPath("/" + rel.toString());
- } else {
- return rfs.getPath(nat.toString());
- }
- }
-
- /**
- * @param path The original (rooted) {@link Path}
- * @return The actual <U>absolute <B>local</B></U> {@link Path} represented
- * by the rooted one
- * @see #resolveLocalPath(RootedPath)
- * @throws IllegalArgumentException if {@code null} path argument
- * @throws ProviderMismatchException if not a {@link RootedPath}
- */
- protected Path unroot(Path path) {
- Objects.requireNonNull(path, "No path to unroot");
- if (!(path instanceof RootedPath)) {
- throw new ProviderMismatchException("unroot(" + path + ") is not a " + RootedPath.class.getSimpleName()
- + " but rather a " + path.getClass().getSimpleName());
- }
-
- return resolveLocalPath((RootedPath) path);
- }
-
- /**
- * @param path The original {@link RootedPath} - never {@code null}
- * @return The actual <U>absolute <B>local</B></U> {@link Path} represented
- * by the rooted one
- * @throws InvalidPathException If the resolved path is not a proper sub-path
- * of the rooted file system
- */
- protected Path resolveLocalPath(RootedPath path) {
- RootedPath absPath = Objects.requireNonNull(path, "No rooted path to resolve").toAbsolutePath();
- RootedFileSystem rfs = absPath.getFileSystem();
- Path root = rfs.getRoot();
- FileSystem lfs = root.getFileSystem();
-
- String rSep = ValidateUtils.checkNotNullAndNotEmpty(rfs.getSeparator(), "No rooted file system separator");
- ValidateUtils.checkTrue(rSep.length() == 1, "Bad rooted file system separator: %s", rSep);
- char rootedSeparator = rSep.charAt(0);
-
- String lSep = ValidateUtils.checkNotNullAndNotEmpty(lfs.getSeparator(), "No local file system separator");
- ValidateUtils.checkTrue(lSep.length() == 1, "Bad local file system separator: %s", lSep);
- char localSeparator = lSep.charAt(0);
-
- String r = absPath.toString();
- String subPath = r.substring(1);
- if (rootedSeparator != localSeparator) {
- subPath = subPath.replace(rootedSeparator, localSeparator);
- }
-
- Path resolved = root.resolve(subPath);
- resolved = resolved.normalize();
- resolved = resolved.toAbsolutePath();
- if (log.isTraceEnabled()) {
- log.trace("resolveLocalPath({}): {}", absPath, resolved);
- }
-
- /*
- * This can happen for Windows since we represent its paths as /C:/some/path,
- * so substring(1) yields C:/some/path - which is resolved as an absolute path
- * (which we don't want).
- */
- if (!resolved.startsWith(root)) {
- throw new InvalidPathException(r, "Not under root");
- }
- return resolved;
- }
-}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/98ac7c2c/sshd-core/src/main/java/org/apache/sshd/common/file/root/RootedPath.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/file/root/RootedPath.java b/sshd-core/src/main/java/org/apache/sshd/common/file/root/RootedPath.java
deleted file mode 100644
index e708aed..0000000
--- a/sshd-core/src/main/java/org/apache/sshd/common/file/root/RootedPath.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * 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.common.file.root;
-
-import java.io.File;
-import java.io.IOException;
-import java.nio.file.FileSystem;
-import java.nio.file.LinkOption;
-import java.nio.file.Path;
-import java.nio.file.spi.FileSystemProvider;
-import java.util.List;
-
-import org.apache.sshd.common.file.util.BasePath;
-
-/**
- * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
- */
-public class RootedPath extends BasePath<RootedPath, RootedFileSystem> {
- public RootedPath(RootedFileSystem fileSystem, String root, List<String> names) {
- super(fileSystem, root, names);
- }
-
- @Override
- public File toFile() {
- RootedPath absolute = toAbsolutePath();
- RootedFileSystem fs = getFileSystem();
- Path path = fs.getRoot();
- for (String n : absolute.names) {
- path = path.resolve(n);
- }
- return path.toFile();
- }
-
- @Override
- public RootedPath toRealPath(LinkOption... options) throws IOException {
- RootedPath absolute = toAbsolutePath();
- FileSystem fs = getFileSystem();
- FileSystemProvider provider = fs.provider();
- provider.checkAccess(absolute);
- return absolute;
- }
-}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/98ac7c2c/sshd-core/src/main/java/org/apache/sshd/common/file/util/BaseFileSystem.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/file/util/BaseFileSystem.java b/sshd-core/src/main/java/org/apache/sshd/common/file/util/BaseFileSystem.java
deleted file mode 100644
index 6287ce1..0000000
--- a/sshd-core/src/main/java/org/apache/sshd/common/file/util/BaseFileSystem.java
+++ /dev/null
@@ -1,256 +0,0 @@
-/*
- * 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.common.file.util;
-
-import java.io.IOException;
-import java.nio.file.FileStore;
-import java.nio.file.FileSystem;
-import java.nio.file.Path;
-import java.nio.file.PathMatcher;
-import java.nio.file.WatchService;
-import java.nio.file.spi.FileSystemProvider;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.Objects;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import org.apache.sshd.common.util.GenericUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public abstract class BaseFileSystem<T extends Path> extends FileSystem {
- protected final Logger log;
- private final FileSystemProvider fileSystemProvider;
-
- public BaseFileSystem(FileSystemProvider fileSystemProvider) {
- this.log = LoggerFactory.getLogger(getClass());
- this.fileSystemProvider = Objects.requireNonNull(fileSystemProvider, "No file system provider");
- }
-
- public T getDefaultDir() {
- return getPath("/");
- }
-
- @Override
- public boolean isReadOnly() {
- return false;
- }
-
- @Override
- public FileSystemProvider provider() {
- return fileSystemProvider;
- }
-
- @Override
- public String getSeparator() {
- return "/";
- }
-
- @Override
- public Iterable<Path> getRootDirectories() {
- return Collections.singleton(create("/"));
- }
-
- @Override
- public Iterable<FileStore> getFileStores() {
- throw new UnsupportedOperationException("No file stores available");
- }
-
- @Override
- public T getPath(String first, String... more) {
- StringBuilder sb = new StringBuilder();
- if (!GenericUtils.isEmpty(first)) {
- appendDedupSep(sb, first.replace('\\', '/')); // in case we are running on Windows
- }
-
- if (GenericUtils.length(more) > 0) {
- for (String segment : more) {
- if ((sb.length() > 0) && (sb.charAt(sb.length() - 1) != '/')) {
- sb.append('/');
- }
- // in case we are running on Windows
- appendDedupSep(sb, segment.replace('\\', '/'));
- }
- }
-
- if ((sb.length() > 1) && (sb.charAt(sb.length() - 1) == '/')) {
- sb.setLength(sb.length() - 1);
- }
-
- String path = sb.toString();
- String root = null;
- if (path.startsWith("/")) {
- root = "/";
- path = path.substring(1);
- }
-
- String[] names = GenericUtils.split(path, '/');
- T p = create(root, names);
- if (log.isTraceEnabled()) {
- log.trace("getPath({}, {}): {}", first, Arrays.toString(more), p);
- }
-
- return p;
- }
-
- protected void appendDedupSep(StringBuilder sb, CharSequence s) {
- for (int i = 0; i < s.length(); i++) {
- char ch = s.charAt(i);
- if ((ch != '/') || (sb.length() == 0) || (sb.charAt(sb.length() - 1) != '/')) {
- sb.append(ch);
- }
- }
- }
-
- @Override
- public PathMatcher getPathMatcher(String syntaxAndPattern) {
- int colonIndex = Objects.requireNonNull(syntaxAndPattern, "No argument").indexOf(':');
- if ((colonIndex <= 0) || (colonIndex == syntaxAndPattern.length() - 1)) {
- throw new IllegalArgumentException("syntaxAndPattern must have form \"syntax:pattern\" but was \"" + syntaxAndPattern + "\"");
- }
-
- String syntax = syntaxAndPattern.substring(0, colonIndex);
- String pattern = syntaxAndPattern.substring(colonIndex + 1);
- String expr;
- switch (syntax) {
- case "glob":
- expr = globToRegex(pattern);
- break;
- case "regex":
- expr = pattern;
- break;
- default:
- throw new UnsupportedOperationException("Unsupported path matcher syntax: \'" + syntax + "\'");
- }
- if (log.isTraceEnabled()) {
- log.trace("getPathMatcher({}): {}", syntaxAndPattern, expr);
- }
-
- Pattern regex = Pattern.compile(expr);
- return path -> {
- Matcher m = regex.matcher(path.toString());
- return m.matches();
- };
- }
-
- protected String globToRegex(String pattern) {
- StringBuilder sb = new StringBuilder(Objects.requireNonNull(pattern, "No pattern").length());
- int inGroup = 0;
- int inClass = 0;
- int firstIndexInClass = -1;
- char[] arr = pattern.toCharArray();
- for (int i = 0; i < arr.length; i++) {
- char ch = arr[i];
- switch (ch) {
- case '\\':
- i++;
- if (i >= arr.length) {
- sb.append('\\');
- } else {
- char next = arr[i];
- switch (next) {
- case ',':
- // escape not needed
- break;
- case 'Q':
- case 'E':
- // extra escape needed
- sb.append("\\\\");
- break;
- default:
- sb.append('\\');
- break;
- }
- sb.append(next);
- }
- break;
- case '*':
- sb.append((inClass == 0) ? ".*" : "*");
- break;
- case '?':
- sb.append((inClass == 0) ? '.' : '?');
- break;
- case '[':
- inClass++;
- firstIndexInClass = i + 1;
- sb.append('[');
- break;
- case ']':
- inClass--;
- sb.append(']');
- break;
- case '.':
- case '(':
- case ')':
- case '+':
- case '|':
- case '^':
- case '$':
- case '@':
- case '%':
- if ((inClass == 0) || ((firstIndexInClass == i) && (ch == '^'))) {
- sb.append('\\');
- }
- sb.append(ch);
- break;
- case '!':
- sb.append((firstIndexInClass == i) ? '^' : '!');
- break;
- case '{':
- inGroup++;
- sb.append('(');
- break;
- case '}':
- inGroup--;
- sb.append(')');
- break;
- case ',':
- sb.append((inGroup > 0) ? '|' : ',');
- break;
- default:
- sb.append(ch);
- }
- }
-
- String regex = sb.toString();
- if (log.isTraceEnabled()) {
- log.trace("globToRegex({}): {}", pattern, regex);
- }
-
- return regex;
- }
-
- @Override
- public WatchService newWatchService() throws IOException {
- throw new UnsupportedOperationException("Watch service N/A");
- }
-
- protected T create(String root, String... names) {
- return create(root, GenericUtils.unmodifiableList(names));
- }
-
- protected T create(String root, Collection<String> names) {
- return create(root, GenericUtils.unmodifiableList(names));
- }
-
- protected abstract T create(String root, List<String> names);
-}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/98ac7c2c/sshd-core/src/main/java/org/apache/sshd/common/file/util/BasePath.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/file/util/BasePath.java b/sshd-core/src/main/java/org/apache/sshd/common/file/util/BasePath.java
deleted file mode 100644
index d073548..0000000
--- a/sshd-core/src/main/java/org/apache/sshd/common/file/util/BasePath.java
+++ /dev/null
@@ -1,423 +0,0 @@
-/*
- * 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.common.file.util;
-
-import java.io.File;
-import java.io.IOException;
-import java.net.URI;
-import java.nio.file.FileSystem;
-import java.nio.file.Path;
-import java.nio.file.ProviderMismatchException;
-import java.nio.file.WatchEvent;
-import java.nio.file.WatchKey;
-import java.nio.file.WatchService;
-import java.util.AbstractList;
-import java.util.ArrayDeque;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Deque;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Objects;
-
-import org.apache.sshd.common.util.GenericUtils;
-
-public abstract class BasePath<T extends BasePath<T, FS>, FS extends BaseFileSystem<T>> implements Path {
-
- protected final String root;
- protected final List<String> names;
- private final FS fileSystem;
- private String strValue;
- private int hashValue;
-
- public BasePath(FS fileSystem, String root, List<String> names) {
- this.fileSystem = Objects.requireNonNull(fileSystem, "No file system provided");
- this.root = root;
- this.names = names;
- }
-
- @SuppressWarnings("unchecked")
- protected T asT() {
- return (T) this;
- }
-
- protected T create(String root, String... names) {
- return create(root, GenericUtils.unmodifiableList(names));
- }
-
- protected T create(String root, Collection<String> names) {
- return create(root, GenericUtils.unmodifiableList(names));
- }
-
- protected T create(String root, List<String> names) {
- return fileSystem.create(root, names);
- }
-
- @Override
- public FS getFileSystem() {
- return fileSystem;
- }
-
- @Override
- public boolean isAbsolute() {
- return root != null;
- }
-
- @Override
- public T getRoot() {
- if (isAbsolute()) {
- return create(root);
- }
- return null;
- }
-
- @Override
- public T getFileName() {
- if (!names.isEmpty()) {
- return create(null, names.get(names.size() - 1));
- }
- return null;
- }
-
- @Override
- public T getParent() {
- if (names.isEmpty() || ((names.size() == 1) && (root == null))) {
- return null;
- }
- return create(root, names.subList(0, names.size() - 1));
- }
-
- @Override
- public int getNameCount() {
- return names.size();
- }
-
- @Override
- public T getName(int index) {
- int maxIndex = getNameCount();
- if ((index < 0) || (index >= maxIndex)) {
- throw new IllegalArgumentException("Invalid name index " + index + " - not in range [0-" + maxIndex + "]");
- }
- return create(null, names.subList(index, index + 1));
- }
-
- @Override
- public T subpath(int beginIndex, int endIndex) {
- int maxIndex = getNameCount();
- if ((beginIndex < 0) || (beginIndex >= maxIndex) || (endIndex > maxIndex) || (beginIndex >= endIndex)) {
- throw new IllegalArgumentException("subpath(" + beginIndex + "," + endIndex + ") bad index range - allowed [0-" + maxIndex + "]");
- }
- return create(null, names.subList(beginIndex, endIndex));
- }
-
- protected boolean startsWith(List<?> list, List<?> other) {
- return list.size() >= other.size() && list.subList(0, other.size()).equals(other);
- }
-
- @Override
- public boolean startsWith(Path other) {
- T p1 = asT();
- T p2 = checkPath(other);
- return Objects.equals(p1.getFileSystem(), p2.getFileSystem())
- && Objects.equals(p1.root, p2.root)
- && startsWith(p1.names, p2.names);
- }
-
- @Override
- public boolean startsWith(String other) {
- return startsWith(getFileSystem().getPath(other));
- }
-
- protected boolean endsWith(List<?> list, List<?> other) {
- return other.size() <= list.size() && list.subList(list.size() - other.size(), list.size()).equals(other);
- }
-
- @Override
- public boolean endsWith(Path other) {
- T p1 = asT();
- T p2 = checkPath(other);
- if (p2.isAbsolute()) {
- return p1.compareTo(p2) == 0;
- }
- return endsWith(p1.names, p2.names);
- }
-
- @Override
- public boolean endsWith(String other) {
- return endsWith(getFileSystem().getPath(other));
- }
-
- protected boolean isNormal() {
- int count = getNameCount();
- if ((count == 0) || ((count == 1) && !isAbsolute())) {
- return true;
- }
- boolean foundNonParentName = isAbsolute(); // if there's a root, the path doesn't start with ..
- boolean normal = true;
- for (String name : names) {
- if (name.equals("..")) {
- if (foundNonParentName) {
- normal = false;
- break;
- }
- } else {
- if (name.equals(".")) {
- normal = false;
- break;
- }
- foundNonParentName = true;
- }
- }
- return normal;
- }
-
- @Override
- public T normalize() {
- if (isNormal()) {
- return asT();
- }
-
- Deque<String> newNames = new ArrayDeque<>();
- for (String name : names) {
- if (name.equals("..")) {
- String lastName = newNames.peekLast();
- if (lastName != null && !lastName.equals("..")) {
- newNames.removeLast();
- } else if (!isAbsolute()) {
- // if there's a root and we have an extra ".." that would go up above the root, ignore it
- newNames.add(name);
- }
- } else if (!name.equals(".")) {
- newNames.add(name);
- }
- }
-
- return newNames.equals(names) ? asT() : create(root, newNames);
- }
-
- @Override
- public T resolve(Path other) {
- T p1 = asT();
- T p2 = checkPath(other);
- if (p2.isAbsolute()) {
- return p2;
- }
- if (p2.names.isEmpty()) {
- return p1;
- }
- String[] names = new String[p1.names.size() + p2.names.size()];
- int index = 0;
- for (String p : p1.names) {
- names[index++] = p;
- }
- for (String p : p2.names) {
- names[index++] = p;
- }
- return create(p1.root, names);
- }
-
- @Override
- public T resolve(String other) {
- return resolve(getFileSystem().getPath(other, GenericUtils.EMPTY_STRING_ARRAY));
- }
-
- @Override
- public Path resolveSibling(Path other) {
- Objects.requireNonNull(other, "Missing sibling path argument");
- T parent = getParent();
- return parent == null ? other : parent.resolve(other);
- }
-
- @Override
- public Path resolveSibling(String other) {
- return resolveSibling(getFileSystem().getPath(other, GenericUtils.EMPTY_STRING_ARRAY));
- }
-
- @Override
- public T relativize(Path other) {
- T p1 = asT();
- T p2 = checkPath(other);
- if (!Objects.equals(p1.getRoot(), p2.getRoot())) {
- throw new IllegalArgumentException("Paths have different roots: " + this + ", " + other);
- }
- if (p2.equals(p1)) {
- return create(null);
- }
- if (p1.root == null && p1.names.isEmpty()) {
- return p2;
- }
- // Common subsequence
- int sharedSubsequenceLength = 0;
- for (int i = 0; i < Math.min(p1.names.size(), p2.names.size()); i++) {
- if (p1.names.get(i).equals(p2.names.get(i))) {
- sharedSubsequenceLength++;
- } else {
- break;
- }
- }
- int extraNamesInThis = Math.max(0, p1.names.size() - sharedSubsequenceLength);
- List<String> extraNamesInOther = (p2.names.size() <= sharedSubsequenceLength)
- ? Collections.emptyList()
- : p2.names.subList(sharedSubsequenceLength, p2.names.size());
- List<String> parts = new ArrayList<>(extraNamesInThis + extraNamesInOther.size());
- // add .. for each extra name in this path
- parts.addAll(Collections.nCopies(extraNamesInThis, ".."));
- // add each extra name in the other path
- parts.addAll(extraNamesInOther);
- return create(null, parts);
- }
-
- @Override
- public T toAbsolutePath() {
- if (isAbsolute()) {
- return asT();
- }
- return fileSystem.getDefaultDir().resolve(this);
- }
-
- @Override
- public URI toUri() {
- File file = toFile();
- return file.toURI();
- }
-
- @Override
- public File toFile() {
- throw new UnsupportedOperationException("To file " + toAbsolutePath() + " N/A");
- }
-
- @Override
- public WatchKey register(WatchService watcher, WatchEvent.Kind<?>... events) throws IOException {
- return register(watcher, events, (WatchEvent.Modifier[]) null);
- }
-
- @Override
- public WatchKey register(WatchService watcher, WatchEvent.Kind<?>[] events, WatchEvent.Modifier... modifiers) throws IOException {
- throw new UnsupportedOperationException("Register to watch " + toAbsolutePath() + " N/A");
- }
-
- @Override
- public Iterator<Path> iterator() {
- return new AbstractList<Path>() {
- @Override
- public Path get(int index) {
- return getName(index);
- }
-
- @Override
- public int size() {
- return getNameCount();
- }
- }.iterator();
- }
-
- @Override
- public int compareTo(Path paramPath) {
- T p1 = asT();
- T p2 = checkPath(paramPath);
- int c = compare(p1.root, p2.root);
- if (c != 0) {
- return c;
- }
- for (int i = 0; i < Math.min(p1.names.size(), p2.names.size()); i++) {
- String n1 = p1.names.get(i);
- String n2 = p2.names.get(i);
- c = compare(n1, n2);
- if (c != 0) {
- return c;
- }
- }
- return p1.names.size() - p2.names.size();
- }
-
- protected int compare(String s1, String s2) {
- if (s1 == null) {
- return s2 == null ? 0 : -1;
- } else {
- return s2 == null ? +1 : s1.compareTo(s2);
- }
- }
-
- @SuppressWarnings("unchecked")
- protected T checkPath(Path paramPath) {
- Objects.requireNonNull(paramPath, "Missing path argument");
- if (paramPath.getClass() != getClass()) {
- throw new ProviderMismatchException("Path is not of this class: " + paramPath + "[" + paramPath.getClass().getSimpleName() + "]");
- }
- T t = (T) paramPath;
-
- FileSystem fs = t.getFileSystem();
- if (fs.provider() != this.fileSystem.provider()) {
- throw new ProviderMismatchException("Mismatched providers for " + t);
- }
- return t;
- }
-
- @Override
- public int hashCode() {
- synchronized (this) {
- if (hashValue == 0) {
- hashValue = calculatedHashCode();
- if (hashValue == 0) {
- hashValue = 1;
- }
- }
- }
-
- return hashValue;
- }
-
- protected int calculatedHashCode() {
- return Objects.hash(getFileSystem(), root, names);
- }
-
- @Override
- public boolean equals(Object obj) {
- return (obj instanceof Path) && (compareTo((Path) obj) == 0);
- }
-
- @Override
- public String toString() {
- synchronized (this) {
- if (strValue == null) {
- strValue = asString();
- }
- }
-
- return strValue;
- }
-
- protected String asString() {
- StringBuilder sb = new StringBuilder();
- if (root != null) {
- sb.append(root);
- }
-
- String separator = getFileSystem().getSeparator();
- for (String name : names) {
- if ((sb.length() > 0) && (sb.charAt(sb.length() - 1) != '/')) {
- sb.append(separator);
- }
- sb.append(name);
- }
-
- return sb.toString();
- }
-}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/98ac7c2c/sshd-core/src/main/java/org/apache/sshd/common/file/util/MockFileSystem.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/file/util/MockFileSystem.java b/sshd-core/src/main/java/org/apache/sshd/common/file/util/MockFileSystem.java
deleted file mode 100644
index 9181c2d..0000000
--- a/sshd-core/src/main/java/org/apache/sshd/common/file/util/MockFileSystem.java
+++ /dev/null
@@ -1,114 +0,0 @@
-/*
- * 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.common.file.util;
-
-import java.io.File;
-import java.io.IOException;
-import java.nio.file.FileStore;
-import java.nio.file.FileSystem;
-import java.nio.file.Path;
-import java.nio.file.PathMatcher;
-import java.nio.file.WatchService;
-import java.nio.file.attribute.UserPrincipalLookupService;
-import java.nio.file.spi.FileSystemProvider;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Set;
-import java.util.concurrent.atomic.AtomicBoolean;
-
-/**
- * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
- */
-public class MockFileSystem extends FileSystem {
- private final AtomicBoolean open = new AtomicBoolean(true);
- private final String name;
-
- public MockFileSystem(String name) {
- this.name = name;
- }
-
- @Override
- public FileSystemProvider provider() {
- throw new UnsupportedOperationException("provider() N/A");
- }
-
- @Override
- public void close() throws IOException {
- if (open.getAndSet(false)) {
- //noinspection UnnecessaryReturnStatement
- return; // debug breakpoint
- }
- }
-
- @Override
- public boolean isOpen() {
- return open.get();
- }
-
- @Override
- public boolean isReadOnly() {
- return true;
- }
-
- @Override
- public String getSeparator() {
- return File.separator;
- }
-
- @Override
- public Iterable<Path> getRootDirectories() {
- return Collections.emptyList();
- }
-
- @Override
- public Iterable<FileStore> getFileStores() {
- return Collections.emptyList();
- }
-
- @Override
- public Set<String> supportedFileAttributeViews() {
- return Collections.emptySet();
- }
-
- @Override
- public Path getPath(String first, String... more) {
- throw new UnsupportedOperationException("getPath(" + first + ") " + Arrays.toString(more));
- }
-
- @Override
- public PathMatcher getPathMatcher(String syntaxAndPattern) {
- throw new UnsupportedOperationException("getPathMatcher(" + syntaxAndPattern + ")");
- }
-
- @Override
- public UserPrincipalLookupService getUserPrincipalLookupService() {
- throw new UnsupportedOperationException("getUserPrincipalLookupService() N/A");
- }
-
- @Override
- public WatchService newWatchService() throws IOException {
- throw new IOException("newWatchService() N/A");
- }
-
- @Override
- public String toString() {
- return name;
- }
-}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/98ac7c2c/sshd-core/src/main/java/org/apache/sshd/common/file/util/MockPath.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/file/util/MockPath.java b/sshd-core/src/main/java/org/apache/sshd/common/file/util/MockPath.java
deleted file mode 100644
index 2f781b8..0000000
--- a/sshd-core/src/main/java/org/apache/sshd/common/file/util/MockPath.java
+++ /dev/null
@@ -1,186 +0,0 @@
-/*
- * 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.common.file.util;
-
-import java.io.File;
-import java.io.IOException;
-import java.net.URI;
-import java.nio.file.FileSystem;
-import java.nio.file.LinkOption;
-import java.nio.file.Path;
-import java.nio.file.WatchEvent.Kind;
-import java.nio.file.WatchEvent.Modifier;
-import java.nio.file.WatchKey;
-import java.nio.file.WatchService;
-import java.util.Collections;
-import java.util.Iterator;
-
-/**
- * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
- */
-public class MockPath implements Path {
- private final String path;
- private final FileSystem fs;
-
- public MockPath(String path) {
- this.path = path;
- this.fs = new MockFileSystem(path);
- }
-
- @Override
- public FileSystem getFileSystem() {
- return fs;
- }
-
- @Override
- public boolean isAbsolute() {
- return true;
- }
-
- @Override
- public Path getRoot() {
- return this;
- }
-
- @Override
- public Path getFileName() {
- return this;
- }
-
- @Override
- public Path getParent() {
- return null;
- }
-
- @Override
- public int getNameCount() {
- return 0;
- }
-
- @Override
- public Path getName(int index) {
- if (index == 0) {
- return this;
- } else {
- throw new IllegalArgumentException("getName - bad index: " + index);
- }
- }
-
- @Override
- public Path subpath(int beginIndex, int endIndex) {
- throw new UnsupportedOperationException("subPath(" + beginIndex + "," + endIndex + ") N/A");
- }
-
- @Override
- public boolean startsWith(Path other) {
- return startsWith(other.toString());
- }
-
- @Override
- public boolean startsWith(String other) {
- return path.startsWith(other);
- }
-
- @Override
- public boolean endsWith(Path other) {
- return endsWith(other.toString());
- }
-
- @Override
- public boolean endsWith(String other) {
- return path.endsWith(other);
- }
-
- @Override
- public Path normalize() {
- return this;
- }
-
- @Override
- public Path resolve(Path other) {
- return resolve(other.toString());
- }
-
- @Override
- public Path resolve(String other) {
- throw new UnsupportedOperationException("resolve(" + other + ") N/A");
- }
-
- @Override
- public Path resolveSibling(Path other) {
- return resolveSibling(other.toString());
- }
-
- @Override
- public Path resolveSibling(String other) {
- throw new UnsupportedOperationException("resolveSibling(" + other + ") N/A");
- }
-
- @Override
- public Path relativize(Path other) {
- throw new UnsupportedOperationException("relativize(" + other + ") N/A");
- }
-
- @Override
- public URI toUri() {
- throw new UnsupportedOperationException("toUri() N/A");
- }
-
- @Override
- public Path toAbsolutePath() {
- return this;
- }
-
- @Override
- public Path toRealPath(LinkOption... options) throws IOException {
- return this;
- }
-
- @Override
- public File toFile() {
- throw new UnsupportedOperationException("toFile() N/A");
- }
-
- @Override
- public WatchKey register(WatchService watcher, Kind<?>... events) throws IOException {
- return register(watcher, events, (Modifier[]) null);
- }
-
- @Override
- public WatchKey register(WatchService watcher, Kind<?>[] events, Modifier... modifiers) throws IOException {
- throw new IOException("register(" + path + ") N/A");
- }
-
- @Override
- public Iterator<Path> iterator() {
- return Collections.<Path>singleton(this).iterator();
- }
-
- @Override
- public int compareTo(Path other) {
- return path.compareTo(other.toString());
- }
-
- @Override
- public String toString() {
- return path;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/98ac7c2c/sshd-core/src/test/java/org/apache/sshd/common/file/root/AssertableFile.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/file/root/AssertableFile.java b/sshd-core/src/test/java/org/apache/sshd/common/file/root/AssertableFile.java
deleted file mode 100644
index 3d056ae..0000000
--- a/sshd-core/src/test/java/org/apache/sshd/common/file/root/AssertableFile.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * 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.common.file.root;
-
-import java.nio.file.Files;
-import java.nio.file.Path;
-
-import org.apache.sshd.common.util.NumberUtils;
-import org.apache.sshd.util.test.BaseTestSupport;
-
-/**
- * TODO upgrade to default methods in JDK 8
- * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
- */
-public abstract class AssertableFile extends BaseTestSupport {
- protected AssertableFile() {
- super();
- }
-
- public static boolean notExists(Path p) {
- boolean cond = !Files.exists(p);
- assertTrue(p + " does not exist", cond);
- return cond;
- }
-
- public static boolean exists(Path p) {
- boolean cond = Files.exists(p);
- assertTrue(p + " exists", cond);
- return cond;
- }
-
- public static boolean isDir(Path p) {
- boolean cond = Files.isDirectory(p);
- assertTrue(p + " is directory", cond);
- return cond;
- }
-
- public static boolean isReadable(Path p) {
- boolean cond = Files.isReadable(p);
- assertTrue(p + " is readable by user", cond);
- return cond;
- }
-
- public static boolean isNonEmpty(byte[] bytes) {
- boolean cond = !NumberUtils.isEmpty(bytes);
- assertTrue("bytes are non empty", cond);
- return cond;
- }
-
- public static boolean isRootedAt(Path root, Path check) {
- boolean cond = check.toAbsolutePath().normalize().startsWith(root.toAbsolutePath().normalize());
- assertTrue(check + " is subpath of parent " + root, cond);
- return cond;
- }
-}