Skip to content

Commit

Permalink
feat: JENKINS-73837 - Adjusted DirScanner to allow empty directories …
Browse files Browse the repository at this point in the history
…in the TarArchiver
  • Loading branch information
DerSimeon committed Oct 6, 2024
1 parent 4d7b993 commit 82dbd2f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/src/main/java/hudson/util/DirScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
}

Expand Down
34 changes: 34 additions & 0 deletions core/src/test/java/hudson/util/io/TarArchiverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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;
Expand Down

0 comments on commit 82dbd2f

Please sign in to comment.