Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure missing directories are added to zips #785

Merged
merged 1 commit into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions private/tools/java/rules/jvm/external/jar/MergeJars.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ public static void main(String[] args) throws IOException {
Map<String, Path> fileToSourceJar = new TreeMap<>();
Map<String, byte[]> fileHashCodes = new HashMap<>();

Set<String> createdDirectories = new HashSet<>();
for (Path source : sources) {
try (InputStream fis = Files.newInputStream(source);
ZipInputStream zis = new ZipInputStream(fis)) {
Expand All @@ -144,10 +143,7 @@ public static void main(String[] args) throws IOException {
continue;
}

if (entry.isDirectory() && createdDirectories.add(entry.getName())) {
fileToSourceJar.put(entry.getName(), source);
createdDirectories.add(entry.getName());
} else {
if (!entry.isDirectory()) {
// Duplicate files, however may not be. We need the hash to determine
// whether we should do anything.
byte[] hash = hash(zis);
Expand Down Expand Up @@ -175,6 +171,9 @@ public static void main(String[] args) throws IOException {

// Now create the output jar
Files.createDirectories(out.getParent());

Set<String> createdDirectories = new HashSet<>();

try (OutputStream os = Files.newOutputStream(out);
JarOutputStream jos = new JarOutputStream(os)) {
jos.setMethod(DEFLATED);
Expand Down
24 changes: 23 additions & 1 deletion tests/com/jvm/external/jar/MergeJarsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,26 @@ public void orderingOfAutomaticallyCreatedDirectoriesIsConduciveToSensibleUnpack
assertTrue(indexOfQux > indexOfBaz);
}

@Test
public void shouldCreateIntermediateDirectoriesEvenIfTheyExistInTheSourceJar() throws IOException {
Path input = temp.newFile("example.jar").toPath();
createJar(input, ImmutableMap.of(
"foo/", "",
"foo/bar", "",
"foo/bar/baz.txt", "cheese"));

Path output = temp.newFile("out.jar").toPath();

MergeJars.main(new String[] {
"--output", output.toAbsolutePath().toString(),
"--sources", input.toAbsolutePath().toString(),
});

List<String> dirNames = readDirNames(output);
assertTrue(dirNames.contains("foo/"));
assertTrue(dirNames.contains("foo/bar/"));
}

@Test
public void mergedJarManifestSpecialAttributesAreHandled() throws IOException {
// This is required to allow JarInputStream to read the manifest properly
Expand Down Expand Up @@ -471,7 +491,9 @@ private void createJar(Path outputTo, Map<String, String> pathToContents) throws
for (Map.Entry<String, String> entry : pathToContents.entrySet()) {
ZipEntry ze = new StableZipEntry(entry.getKey());
zos.putNextEntry(ze);
zos.write(entry.getValue().getBytes(UTF_8));
if (!ze.isDirectory()) {
zos.write(entry.getValue().getBytes(UTF_8));
}
zos.closeEntry();
}
}
Expand Down