You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by bo...@apache.org on 2025/06/08 08:00:07 UTC

(ant) branch master updated (5e9795966 -> 4dec3b0f7)

This is an automated email from the ASF dual-hosted git repository.

bodewig pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/ant.git


    from 5e9795966 explicitly mention Laucher is going to terminate the VM
     new 6d2f06bd9 extract logic for detecting and stripping leading path separators
     new 9b9b69504 use extracted logic for detecting and stripping leading path separators
     new 4dec3b0f7 only strip leading character in getName when it actually is a path separator

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/main/org/apache/tools/ant/taskdefs/Expand.java |  6 ++--
 .../org/apache/tools/ant/types/ArchiveScanner.java | 11 +++----
 .../tools/ant/types/resources/URLResource.java     |  4 +--
 src/main/org/apache/tools/ant/util/FileUtils.java  | 35 +++++++++++++++++++---
 .../ant/types/resources/URLResourceTest.java}      | 29 +++++++-----------
 .../org/apache/tools/ant/util/FileUtilsTest.java   | 18 +++++++++++
 6 files changed, 69 insertions(+), 34 deletions(-)
 copy src/tests/junit/org/{example/junitlauncher/vintage/JUnit4SampleTest.java => apache/tools/ant/types/resources/URLResourceTest.java} (66%)


(ant) 01/03: extract logic for detecting and stripping leading path separators

Posted by bo...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

bodewig pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ant.git

commit 6d2f06bd9feea5b20c6999f026cc6cc2e6629476
Author: Stefan Bodewig <bo...@apache.org>
AuthorDate: Sun Jun 8 09:06:04 2025 +0200

    extract logic for detecting and stripping leading path separators
---
 src/main/org/apache/tools/ant/util/FileUtils.java  | 27 ++++++++++++++++++++++
 .../org/apache/tools/ant/util/FileUtilsTest.java   | 18 +++++++++++++++
 2 files changed, 45 insertions(+)

diff --git a/src/main/org/apache/tools/ant/util/FileUtils.java b/src/main/org/apache/tools/ant/util/FileUtils.java
index 15dd90ddd..1bf6d7e3e 100644
--- a/src/main/org/apache/tools/ant/util/FileUtils.java
+++ b/src/main/org/apache/tools/ant/util/FileUtils.java
@@ -1959,4 +1959,31 @@ public class FileUtils {
         }
         return Optional.of(caseSensitive);
     }
+
+    /**
+     * Tests whether the given path starts with a path separator.
+     * @param path path to check.
+     * @return true if the path starts with a path separator.
+     * @since Ant 1.10.16
+     */
+    public boolean startsWithPathSeparator(String path) {
+        if (path == null || path.length() == 0) {
+            return false;
+        }
+        char c = path.charAt(0);
+        return c == File.pathSeparatorChar || c == '/' || c == '\\';
+    }
+
+    /**
+     * Strips a leading path separator from path that actually start
+     * with one.
+     * @param path the whose leading file separator should be stripped.
+     * @return path with the leading path separator stripped off if
+     * path starts with a path separator - or the unchanged path
+     * otherwise.
+     * @since Ant 1.10.16
+     */
+    public String stripLeadingPathSeparator(String path) {
+        return startsWithPathSeparator(path) ? path.substring(1) : path;
+    }
 }
diff --git a/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java b/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java
index 5a168a514..bdf1f3245 100644
--- a/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java
+++ b/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java
@@ -861,6 +861,24 @@ public class FileUtilsTest {
                 ? "sensitive" : "insensitive"), expectedCaseSensitivity, actualCaseSensitivity.get());
     }
 
+    @Test
+    public void testStartsWithPathSeparator() {
+        assertTrue(getFileUtils().startsWithPathSeparator("/foo"));
+        assertTrue(getFileUtils().startsWithPathSeparator("\\foo"));
+        assertFalse(getFileUtils().startsWithPathSeparator("c:\\foo"));
+        assertFalse(getFileUtils().startsWithPathSeparator("foo"));
+        assertFalse(getFileUtils().startsWithPathSeparator("file:/foo"));
+    }
+
+    @Test
+    public void testStripLeadingPathSeparator() {
+        assertEquals("foo", getFileUtils().stripLeadingPathSeparator("/foo"));
+        assertEquals("foo", getFileUtils().stripLeadingPathSeparator("\\foo"));
+        assertEquals("c:\\foo", getFileUtils().stripLeadingPathSeparator("c:\\foo"));
+        assertEquals("foo", getFileUtils().stripLeadingPathSeparator("foo"));
+        assertEquals("file:/foo", getFileUtils().stripLeadingPathSeparator("file:/foo"));
+    }
+
     /**
      * adapt file separators to local conventions
      */


(ant) 03/03: only strip leading character in getName when it actually is a path separator

Posted by bo...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

bodewig pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ant.git

commit 4dec3b0f783fcd4a775d0252ec5229bda1d9b50d
Author: Stefan Bodewig <bo...@apache.org>
AuthorDate: Sun Jun 8 09:51:58 2025 +0200

    only strip leading character in getName when it actually is a path separator
    
    https://bz.apache.org/bugzilla/show_bug.cgi?id=69680
---
 .../tools/ant/types/resources/URLResource.java     |  4 +--
 .../tools/ant/types/resources/URLResourceTest.java | 36 ++++++++++++++++++++++
 2 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/src/main/org/apache/tools/ant/types/resources/URLResource.java b/src/main/org/apache/tools/ant/types/resources/URLResource.java
index 1772b4532..479d53f9c 100644
--- a/src/main/org/apache/tools/ant/types/resources/URLResource.java
+++ b/src/main/org/apache/tools/ant/types/resources/URLResource.java
@@ -183,7 +183,7 @@ public class URLResource extends Resource implements URLProvider {
             return getRef().getName();
         }
         String name = getURL().getFile();
-        return name.isEmpty() ? name : name.substring(1);
+        return FILE_UTILS.stripLeadingPathSeparator(name);
     }
 
     /**
@@ -196,7 +196,7 @@ public class URLResource extends Resource implements URLProvider {
     }
 
     /**
-     * Find out whether the URL exists .
+     * Find out whether the URL exists.
      * @return true if this resource exists.
      */
     public synchronized boolean isExists() {
diff --git a/src/tests/junit/org/apache/tools/ant/types/resources/URLResourceTest.java b/src/tests/junit/org/apache/tools/ant/types/resources/URLResourceTest.java
new file mode 100644
index 000000000..84bf23dc2
--- /dev/null
+++ b/src/tests/junit/org/apache/tools/ant/types/resources/URLResourceTest.java
@@ -0,0 +1,36 @@
+/*
+ *  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
+ *
+ *      https://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.tools.ant.types.resources;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class URLResourceTest {
+
+    /**
+     * @see "https://bz.apache.org/bugzilla/show_bug.cgi?id=69680"
+     */
+    @Test
+    public void getNameOnlyStripsLeadingPathSeparator() {
+        assertEquals("foo", new URLResource("file:/foo").getName());
+        assertEquals("foo", new URLResource("file://host/foo").getName());
+        assertEquals("file:/foo!/bar", new URLResource("jar:file:/foo!/bar").getName());
+    }
+}


(ant) 02/03: use extracted logic for detecting and stripping leading path separators

Posted by bo...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

bodewig pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ant.git

commit 9b9b69504ccd1a7a281a435e12179cb41cf0b524
Author: Stefan Bodewig <bo...@apache.org>
AuthorDate: Sun Jun 8 09:51:44 2025 +0200

    use extracted logic for detecting and stripping leading path separators
---
 src/main/org/apache/tools/ant/taskdefs/Expand.java      |  6 ++----
 src/main/org/apache/tools/ant/types/ArchiveScanner.java | 11 ++++++-----
 src/main/org/apache/tools/ant/util/FileUtils.java       |  8 ++++----
 3 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/src/main/org/apache/tools/ant/taskdefs/Expand.java b/src/main/org/apache/tools/ant/taskdefs/Expand.java
index 19f27fb4b..e607a6a4a 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Expand.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Expand.java
@@ -257,10 +257,8 @@ public class Expand extends Task {
                                boolean isDirectory, FileNameMapper mapper)
                                throws IOException {
 
-        final boolean entryNameStartsWithPathSpec = !entryName.isEmpty()
-            && (entryName.charAt(0) == File.separatorChar
-                || entryName.charAt(0) == '/'
-                || entryName.charAt(0) == '\\');
+        final boolean entryNameStartsWithPathSpec =
+            FILE_UTILS.startsWithPathSeparator(entryName);
         if (stripAbsolutePathSpec && entryNameStartsWithPathSpec) {
             log("stripped absolute path spec from " + entryName,
                 Project.MSG_VERBOSE);
diff --git a/src/main/org/apache/tools/ant/types/ArchiveScanner.java b/src/main/org/apache/tools/ant/types/ArchiveScanner.java
index ead0005e8..f5ab9f95e 100644
--- a/src/main/org/apache/tools/ant/types/ArchiveScanner.java
+++ b/src/main/org/apache/tools/ant/types/ArchiveScanner.java
@@ -28,6 +28,7 @@ import org.apache.tools.ant.Project;
 import org.apache.tools.ant.types.resources.FileProvider;
 import org.apache.tools.ant.types.resources.FileResource;
 import org.apache.tools.ant.types.resources.FileResourceIterator;
+import org.apache.tools.ant.util.FileUtils;
 
 /**
  * ArchiveScanner accesses the pattern matching algorithm in DirectoryScanner,
@@ -48,6 +49,8 @@ public abstract class ArchiveScanner extends DirectoryScanner {
 
     // CheckStyle:VisibilityModifier ON
 
+    private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
+
     /**
      * The archive resource which should be scanned.
      */
@@ -257,11 +260,9 @@ public abstract class ArchiveScanner extends DirectoryScanner {
     public boolean match(String path) {
         String vpath = path;
         if (!path.isEmpty()) {
-            vpath = path.replace('/', File.separatorChar)
-                    .replace('\\', File.separatorChar);
-            if (vpath.charAt(0) == File.separatorChar) {
-                vpath = vpath.substring(1);
-            }
+            vpath = FILE_UTILS.stripLeadingPathSeparator(path)
+                .replace('/', File.separatorChar)
+                .replace('\\', File.separatorChar);
         }
         return isIncluded(vpath) && !isExcluded(vpath);
     }
diff --git a/src/main/org/apache/tools/ant/util/FileUtils.java b/src/main/org/apache/tools/ant/util/FileUtils.java
index 1bf6d7e3e..087ab703d 100644
--- a/src/main/org/apache/tools/ant/util/FileUtils.java
+++ b/src/main/org/apache/tools/ant/util/FileUtils.java
@@ -684,13 +684,13 @@ public class FileUtils {
         if (filename.isEmpty()) {
             return false;
         }
+        if (!ON_DOS && !ON_NETWARE) {
+            return PRIMARY_INSTANCE.startsWithPathSeparator(filename);
+        }
         int len = filename.length();
         char sep = File.separatorChar;
         filename = filename.replace('/', sep).replace('\\', sep);
         char c = filename.charAt(0);
-        if (!ON_DOS && !ON_NETWARE) {
-            return c == sep;
-        }
         if (c == sep) {
             // CheckStyle:MagicNumber OFF
             if (!ON_DOS || len <= 4 || filename.charAt(1) != sep) {
@@ -859,7 +859,7 @@ public class FileUtils {
         String osPath;
         String path = normalize(f.getAbsolutePath()).getPath();
         String name = f.getName();
-        boolean isAbsolute = path.charAt(0) == File.separatorChar;
+        boolean isAbsolute = startsWithPathSeparator(path);
         // treat directories specified using .DIR syntax as files
         // CheckStyle:MagicNumber OFF
         boolean isDirectory = f.isDirectory()