From: Eric Barboni Date: Mon, 17 Feb 2020 14:25:42 +0000 (+0100) Subject: Change configuration to handle windows path restriction X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=23737aaa3d19a79eb9471e4e9d3e67b92b4594ef;p=archiva.git Change configuration to handle windows path restriction --- diff --git a/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/DefaultArchivaConfiguration.java b/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/DefaultArchivaConfiguration.java index 97777d051..11ae70569 100644 --- a/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/DefaultArchivaConfiguration.java +++ b/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/DefaultArchivaConfiguration.java @@ -46,6 +46,7 @@ import javax.inject.Inject; import javax.inject.Named; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; @@ -553,10 +554,9 @@ public class DefaultArchivaConfiguration * @param contents the contents to write. * @return true if write successful. */ - private boolean writeFile(String filetype, String path, String contents, boolean createDirs) { - Path file = Paths.get(path); - + private boolean writeFile(String filetype, String path, String contents, boolean createDirs) { try { + Path file = Paths.get(path); // Check parent directory (if it is declared) final Path parent = file.getParent(); if (parent != null) { @@ -574,6 +574,9 @@ public class DefaultArchivaConfiguration } catch (IOException e) { log.error("Unable to create {} file: {}", filetype, e.getMessage(), e); return false; + } catch (InvalidPathException ipe) { + log.error("Unable to read {} file: {}", path, ipe.getMessage(), ipe); + return false; } } diff --git a/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/util/PathMatcher.java b/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/util/PathMatcher.java index 2e77b38a0..11e5ed519 100644 --- a/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/util/PathMatcher.java +++ b/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/util/PathMatcher.java @@ -20,6 +20,7 @@ package org.apache.archiva.configuration.util; */ import java.io.File; +import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.nio.file.Paths; @@ -226,8 +227,8 @@ public class PathMatcher * or false otherwise. */ public static boolean matchPath(String pattern, String str) { - String[] patDirs = tokenizePathAsArray(pattern); - return matchPath(patDirs, tokenizePathAsArray(str), true); + String[] patDirs = tokenizePathAsArray( pattern, false ); + return matchPath(patDirs, tokenizePathAsArray( str, true ), true); } /** @@ -248,20 +249,30 @@ public class PathMatcher */ public static boolean matchPath(String pattern, String str, boolean isCaseSensitive) { - String[] patDirs = tokenizePathAsArray(pattern); - return matchPath(patDirs, tokenizePathAsArray(str), isCaseSensitive); + String[] patDirs = tokenizePathAsArray( pattern, false ); + return matchPath(patDirs, tokenizePathAsArray( str, false ), isCaseSensitive); } - - static String[] tokenizePathAsArray(String path) { + /** + * + * @param path + * @param osspecific + * @return + */ + static String[] tokenizePathAsArray(String path, boolean osSpecific) { Path root = null; - Path fsPath = Paths.get( path ); - - if ( fsPath.isAbsolute()) { - root = fsPath.getRoot( ); - path = root.relativize( fsPath ).toString(); + try + { + Path fsPath = Paths.get( path ); + if ( fsPath.isAbsolute() ) { + root = fsPath.getRoot(); + path = root.relativize( fsPath ).toString(); + } + } catch (InvalidPathException ipe ) + { + // invalid path, windauze hate **/* } - char sep = File.separatorChar; + char sep = osSpecific ? File.separatorChar : '/'; int start = 0; int len = path.length(); int count = 0; diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java index 625c4574a..8dfcdb22d 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java @@ -19,6 +19,7 @@ package org.apache.archiva.consumers.core.repository; * under the License. */ +import java.io.File; import org.apache.archiva.metadata.model.ArtifactMetadata; import org.apache.archiva.metadata.repository.MetadataRepository; import org.apache.archiva.metadata.repository.RepositorySession; @@ -163,7 +164,8 @@ public abstract class AbstractRepositoryPurgeTest atf.setRetentionPeriod( Period.ofDays( TEST_DAYS_OLDER) ); String path = AbstractRepositoryPurgeTest.fixPath( basePath.resolve( repoId ).toAbsolutePath().toString() ); - config.setLocation( new URI( path ) ); + File file = new File( path ); + config.setLocation( file.toURI() ); atf.setDeleteReleasedSnapshots( true ); atf.setRetentionCount( TEST_RETENTION_COUNT );