This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-wagon.git
The following commit(s) were added to refs/heads/master by this push:
new 0b22c340 Follow symlinked directories when copying into the SCM
checkout
0b22c340 is described below
commit 0b22c340f081c94bae529f221f9099ceb082cd1b
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Thu Nov 26 16:52:22 2026 +0000
Follow symlinked directories when copying into the SCM checkout
Restores the plexus-utils behavior lost in the JDK file API switch:
symlinked directories were copied empty, and a destination inside the source
was copied into itself.
---
.../apache/maven/wagon/providers/scm/ScmWagon.java | 52 ++++++++----
.../scm/ScmWagonCopyDirectoryStructureTest.java | 97 ++++++++++++++++++++++
2 files changed, 131 insertions(+), 18 deletions(-)
diff --git
a/wagon-providers/wagon-scm/src/main/java/org/apache/maven/wagon/providers/scm/ScmWagon.java
b/wagon-providers/wagon-scm/src/main/java/org/apache/maven/wagon/providers/scm/ScmWagon.java
index d39b6473..72671e3e 100644
---
a/wagon-providers/wagon-scm/src/main/java/org/apache/maven/wagon/providers/scm/ScmWagon.java
+++
b/wagon-providers/wagon-scm/src/main/java/org/apache/maven/wagon/providers/scm/ScmWagon.java
@@ -23,6 +23,7 @@
import java.io.File;
import java.io.IOException;
+import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -31,6 +32,7 @@
import java.nio.file.attribute.BasicFileAttributes;
import java.text.DecimalFormat;
import java.util.ArrayList;
+import java.util.EnumSet;
import java.util.List;
import java.util.Random;
@@ -780,26 +782,40 @@ private static void copyFile(File source, File
destination) throws IOException {
StandardCopyOption.COPY_ATTRIBUTES);
}
- /** JDK replacement for {@code FileUtils.copyDirectoryStructure}. */
- private static void copyDirectoryStructure(final File source, final File
target) throws IOException {
+ /**
+ * JDK replacement for {@code FileUtils.copyDirectoryStructure}. Like the
original it follows symbolic links to
+ * directories, throws if source and target are the same directory, and
silently skips the target when it lies
+ * inside the source instead of copying it into itself.
+ */
+ static void copyDirectoryStructure(final File source, final File target)
throws IOException {
final Path sourcePath = source.toPath();
- Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {
- @Override
- public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) throws IOException {
-
Files.createDirectories(target.toPath().resolve(sourcePath.relativize(dir)));
- return FileVisitResult.CONTINUE;
- }
+ final Path targetPath = target.toPath();
+ final Path absoluteSource = sourcePath.toAbsolutePath().normalize();
+ final Path absoluteTarget = targetPath.toAbsolutePath().normalize();
+ if (absoluteSource.equals(absoluteTarget)) {
+ throw new IOException("source and destination are the same
directory.");
+ }
+ Files.walkFileTree(
+ sourcePath, EnumSet.of(FileVisitOption.FOLLOW_LINKS),
Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
+ @Override
+ public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) throws IOException {
+ if
(dir.toAbsolutePath().normalize().equals(absoluteTarget)) {
+ return FileVisitResult.SKIP_SUBTREE;
+ }
+
Files.createDirectories(targetPath.resolve(sourcePath.relativize(dir)));
+ return FileVisitResult.CONTINUE;
+ }
- @Override
- public FileVisitResult visitFile(Path file, BasicFileAttributes
attrs) throws IOException {
- Files.copy(
- file,
- target.toPath().resolve(sourcePath.relativize(file)),
- StandardCopyOption.REPLACE_EXISTING,
- StandardCopyOption.COPY_ATTRIBUTES);
- return FileVisitResult.CONTINUE;
- }
- });
+ @Override
+ public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
+ Files.copy(
+ file,
+
targetPath.resolve(sourcePath.relativize(file)),
+ StandardCopyOption.REPLACE_EXISTING,
+ StandardCopyOption.COPY_ATTRIBUTES);
+ return FileVisitResult.CONTINUE;
+ }
+ });
}
/** JDK replacement for {@code FileUtils.deleteDirectory}. */
diff --git
a/wagon-providers/wagon-scm/src/test/java/org/apache/maven/wagon/providers/scm/ScmWagonCopyDirectoryStructureTest.java
b/wagon-providers/wagon-scm/src/test/java/org/apache/maven/wagon/providers/scm/ScmWagonCopyDirectoryStructureTest.java
new file mode 100644
index 00000000..9077af76
--- /dev/null
+++
b/wagon-providers/wagon-scm/src/test/java/org/apache/maven/wagon/providers/scm/ScmWagonCopyDirectoryStructureTest.java
@@ -0,0 +1,97 @@
+/*
+ * 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.maven.wagon.providers.scm;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.junit.jupiter.api.Assumptions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests the directory copy that {@link ScmWagon#putDirectory} uses to stage
files into the checkout.
+ */
+class ScmWagonCopyDirectoryStructureTest {
+
+ @TempDir
+ Path tempDir;
+
+ @Test
+ void copiesFilesBehindSymlinkedDirectory() throws IOException {
+ Path source = Files.createDirectories(tempDir.resolve("source"));
+ Path real = Files.createDirectories(tempDir.resolve("real"));
+ write(real.resolve("nested/data.txt"), "linked content");
+ write(source.resolve("plain.txt"), "plain content");
+
+ try {
+ Files.createSymbolicLink(source.resolve("link"), real);
+ } catch (UnsupportedOperationException | IOException |
SecurityException e) {
+ Assumptions.abort("Symbolic links are not supported here: " + e);
+ }
+
+ Path target = tempDir.resolve("target");
+ ScmWagon.copyDirectoryStructure(source.toFile(), target.toFile());
+
+ assertEquals("plain content", read(target.resolve("plain.txt")));
+ assertEquals("linked content",
read(target.resolve("link/nested/data.txt")));
+ assertFalse(Files.isSymbolicLink(target.resolve("link")));
+ }
+
+ @Test
+ @Timeout(30)
+ void doesNotCopyDestinationInsideSourceIntoItself() throws IOException {
+ Path source = Files.createDirectories(tempDir.resolve("source"));
+ write(source.resolve("a.txt"), "a");
+ write(source.resolve("sub/b.txt"), "b");
+ Path target = source.resolve("checkout");
+
+ ScmWagon.copyDirectoryStructure(source.toFile(), target.toFile());
+
+ assertEquals("a", read(target.resolve("a.txt")));
+ assertEquals("b", read(target.resolve("sub/b.txt")));
+ assertFalse(Files.exists(target.resolve("checkout")), "destination was
copied into itself");
+ }
+
+ @Test
+ void rejectsSameSourceAndDestination() throws IOException {
+ File source =
Files.createDirectories(tempDir.resolve("source")).toFile();
+
+ IOException e = assertThrows(IOException.class, () ->
ScmWagon.copyDirectoryStructure(source, source));
+ assertTrue(e.getMessage().contains("same directory"), e.getMessage());
+ }
+
+ private static void write(Path file, String content) throws IOException {
+ Files.createDirectories(file.getParent());
+ Files.write(file, content.getBytes(StandardCharsets.UTF_8));
+ }
+
+ private static String read(Path file) throws IOException {
+ return new String(Files.readAllBytes(file), StandardCharsets.UTF_8);
+ }
+}