diff --git a/core/src/main/java/hudson/util/DirScanner.java b/core/src/main/java/hudson/util/DirScanner.java index a1c79c8c1a9b..90aeac773c0c 100644 --- a/core/src/main/java/hudson/util/DirScanner.java +++ b/core/src/main/java/hudson/util/DirScanner.java @@ -9,6 +9,7 @@ import java.io.IOException; import java.io.Serializable; import java.nio.file.OpenOption; +import java.util.Arrays; import java.util.HashSet; import java.util.Set; import org.apache.tools.ant.BuildException; @@ -146,6 +147,12 @@ public void scan(File dir, FileVisitor visitor) throws IOException { File file = new File(dir, f); scanSingle(file, f, visitor); } + for (String d : ds.getIncludedDirectories()) { + if (!d.isEmpty()) { + File file = new File(dir, d); + scanSingle(file, d, visitor); + } + } } } diff --git a/core/src/test/java/hudson/util/io/TarArchiverTest.java b/core/src/test/java/hudson/util/io/TarArchiverTest.java index c0f41981c27f..7522915fc71c 100644 --- a/core/src/test/java/hudson/util/io/TarArchiverTest.java +++ b/core/src/test/java/hudson/util/io/TarArchiverTest.java @@ -36,9 +36,18 @@ import hudson.util.StreamTaskListener; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import org.apache.tools.tar.TarEntry; +import org.apache.tools.tar.TarInputStream; +import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -141,6 +150,31 @@ private static void run(FilePath dir, String... cmds) throws InterruptedExceptio t1.join(); } + @Ignore("Empty Directory Test") + @Issue("JENKINS-73837") + @Test + public void emptyDirectory() throws Exception { + Path tar = tmp.newFile("test.tar").toPath(); + Path root = tmp.newFolder().toPath(); + Files.createDirectory(root.resolve("a")); + Files.createDirectory(root.resolve("b")); + + Files.writeString(root.resolve("a/file.txt"), "empty_dir_test"); + + try (OutputStream outputStream = Files.newOutputStream(tar)) { + new FilePath(root.toFile()).tar(outputStream, "**"); + } + + Set names = new HashSet<>(); + try (TarInputStream tarInputStream = new TarInputStream(Files.newInputStream(tar), StandardCharsets.UTF_8.name())) { + TarEntry tarEntry; + while ((tarEntry = tarInputStream.getNextEntry()) != null) { + names.add(tarEntry.getName()); + } + } + assertEquals(Set.of("a/", "b/", "a/file.txt"), names); + } + private static class GrowingFileRunnable implements Runnable { private boolean finish = false; private Exception ex = null;