Skip to content

Commit

Permalink
[JENKINS-19456] There is a case that a zip file does not contain entr…
Browse files Browse the repository at this point in the history
…ies for directories.
  • Loading branch information
ikedam committed Sep 9, 2013
1 parent db52e3a commit ca25b4e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 15 deletions.
Expand Up @@ -249,7 +249,10 @@ public FormValidation doCheckInitialPath(@QueryParameter String artifactsPattern
ZipFile file = null;
try {
file = new ZipFile(artifact);
if (file.getEntry(value) == null) {
if (
!ArtifactsDocLinksDocument.isDirectory(file, value)
&& file.getEntry(value) == null
) {
return FormValidation.warning(
Messages.ArtifactsDocLinksConfig_initialPath_notfound(artifactName, build.getFullDisplayName())
);
Expand Down
Expand Up @@ -27,6 +27,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
Expand Down Expand Up @@ -235,12 +236,9 @@ public void doDynamic(StaplerRequest req, StaplerResponse resp) throws IOExcepti
* @throws IOException
*/
private ZipEntry getFileEntry(ZipFile zip, String path) throws IOException {
if (!StringUtils.isEmpty(path)) {
if (!isDirectory(zip, path)) {
ZipEntry entry = zip.getEntry(path);
if (entry == null) {
return null;
}
if (!isDirectory(zip, entry)) {
if (entry != null) {
return entry;
}
}
Expand Down Expand Up @@ -268,7 +266,7 @@ private ZipEntry getFileEntry(ZipFile zip, String path) throws IOException {
* @return
* @throws IOException
*/
private boolean isDirectory(ZipFile zip, ZipEntry entry) throws IOException {
private static boolean isDirectory(ZipFile zip, ZipEntry entry) throws IOException {
if (entry.isDirectory()) {
return true;
}
Expand All @@ -286,14 +284,31 @@ private boolean isDirectory(ZipFile zip, ZipEntry entry) throws IOException {
* @return
* @throws IOException
*/
private boolean isDirectory(ZipFile zip, String path) throws IOException {
if (StringUtils.isEmpty(path)) {
public static boolean isDirectory(ZipFile zip, String path) throws IOException {
if (StringUtils.isEmpty(path) || "/".equals(path)) {
return true;
}
ZipEntry entry = zip.getEntry(path);
if (entry == null) {
return false;
if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}

{
ZipEntry entry = zip.getEntry(path);
if (entry != null) {
return isDirectory(zip, entry);
}
}

String dirPrefix = String.format("%s/", path);

Enumeration<? extends ZipEntry> entryEnum = zip.entries();
while (entryEnum.hasMoreElements()) {
ZipEntry entry = entryEnum.nextElement();
if (entry.getName().startsWith(dirPrefix)) {
return true;
}
}
return isDirectory(zip, entry);

return false;
}
}
Expand Up @@ -264,4 +264,21 @@ public void testDescriptor_doCheckInitialPath() throws Exception {
descriptor.doCheckInitialPath("**/*.zip", "nosuchdir", p).kind
);
}

public void testDescriptor_doCheckInitialPathWithoutDirectoryEntry() throws Exception {
DescriptorImpl descriptor = getDescriptor();
FreeStyleProject p = createFreeStyleProject();

// build with an artifact.
p.getBuildersList().clear();
p.getBuildersList().add(new CleanupBuilder());
p.getBuildersList().add(new TestZipBuilder("artifact1.zip", true));
p.getPublishersList().clear();
p.getPublishersList().add(new ArtifactArchiver("artifact1.zip", "", false));
assertBuildStatusSuccess(p.scheduleBuild2(0).get());
assertEquals(
FormValidation.Kind.OK,
descriptor.doCheckInitialPath("**/*.zip", "subdir", p).kind
);
}
}
Expand Up @@ -175,6 +175,29 @@ public void testIndexFile() throws Exception {
}
}

public void testIndexFileWithoutDirectoryEntry() throws Exception {
WebClient wc = getWebClient();

FreeStyleProject p = createFreeStyleProject();

p.getBuildersList().clear();
p.getBuildersList().add(new TestZipBuilder("artifact1.zip", true));
p.getPublishersList().clear();
p.getPublishersList().add(new ArtifactArchiver("artifact1.zip", "", false));
p.getPublishersList().add(new ArtifactsDocLinksPublisher(Arrays.asList(
new ArtifactsDocLinksConfig("Test", "artifact1.zip", null, null)
)));
p.save();

FreeStyleBuild build = p.scheduleBuild2(0).get(BUILD_TIMEOUT, TimeUnit.SECONDS);
ArtifactsDocLinksAction action = build.getAction(ArtifactsDocLinksAction.class);
ArtifactsDocLinksDocument doc = action.getArtifactsDocLinksDocumentList().get(0);

// index.html
HtmlPage page = wc.getPage(build, String.format("%s/%s/subdir", action.getUrlName(), doc.getUrl()));
assertTrue(page.asText(), page.asText().contains("Page in a sub directory."));
}

public void testLinksInDocuments() throws Exception {
WebClient wc = getWebClient();

Expand Down
Expand Up @@ -47,9 +47,15 @@
public class TestZipBuilder extends Builder
{
private String filename;
private boolean noEntryForDirectories;

public TestZipBuilder(String filename) {
this(filename, false);
}

public TestZipBuilder(String filename, boolean noEntryForDirectories) {
this.filename = filename;
this.noEntryForDirectories = noEntryForDirectories;
}

@Override
Expand Down Expand Up @@ -98,8 +104,10 @@ private void compress(ZipOutputStream zos, File dir, String relative) throws IOE
File file = new File(dir, filename);
String path = StringUtils.isEmpty(relative)?filename:String.format("%s/%s", relative, filename);
if (file.isDirectory()) {
ZipEntry entry = new ZipEntry(String.format("%s/", path));
zos.putNextEntry(entry);
if (!noEntryForDirectories) {
ZipEntry entry = new ZipEntry(String.format("%s/", path));
zos.putNextEntry(entry);
}
compress(zos, file, path);
} else {
ZipEntry entry = new ZipEntry(path);
Expand Down

0 comments on commit ca25b4e

Please sign in to comment.