Skip to content
Permalink
Browse files
[JENKINS-19456] There is a case that a zip file does not contain entr…
…ies for directories.
  • Loading branch information
ikedam committed Sep 9, 2013
1 parent db52e3a commit ca25b4ed246565775e0ede041ffb477c3eaf64e6
@@ -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())
);
@@ -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;
@@ -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;
}
}
@@ -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;
}
@@ -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;
}
}
@@ -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
);
}
}
@@ -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();

@@ -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
@@ -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);

0 comments on commit ca25b4e

Please sign in to comment.