From 688eb6ab5d0a0623ac256447387ec264b4704bd4 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Fri, 24 Aug 2012 12:48:24 -0400 Subject: [PATCH] [FIXED JENKINS-14922] TarArchiver.visitSymlink can throw undeclared PosixException. --- changelog.html | 3 +++ core/src/main/java/hudson/util/IOUtils.java | 7 +++++-- .../main/java/hudson/util/io/TarArchiver.java | 11 +++++++++-- .../java/hudson/util/io/TarArchiverTest.java | 16 +++++++++++++--- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/changelog.html b/changelog.html index 4e3abaa4b2..2bacddd7d8 100644 --- a/changelog.html +++ b/changelog.html @@ -55,6 +55,9 @@ Upcoming changes diff --git a/core/src/main/java/hudson/util/IOUtils.java b/core/src/main/java/hudson/util/IOUtils.java index 975448dc58..4d046b7541 100644 --- a/core/src/main/java/hudson/util/IOUtils.java +++ b/core/src/main/java/hudson/util/IOUtils.java @@ -2,6 +2,7 @@ package hudson.util; import hudson.Functions; import hudson.os.PosixAPI; +import hudson.os.PosixException; import java.io.*; import java.util.regex.Pattern; @@ -115,9 +116,11 @@ public class IOUtils extends org.apache.commons.io.IOUtils { /** - * Gets the mode of a file/directory, if appropriate. Returns -1 if not on Unix. + * Gets the mode of a file/directory, if appropriate. + * @return a file mode, or -1 if not on Unix + * @throws PosixException if the file could not be statted, e.g. broken symlink */ - public static int mode(File f) { + public static int mode(File f) throws PosixException { if(Functions.isWindows()) return -1; return PosixAPI.get().stat(f.getPath()).mode(); } diff --git a/core/src/main/java/hudson/util/io/TarArchiver.java b/core/src/main/java/hudson/util/io/TarArchiver.java index 790c94e1ea..6bd89c6edd 100644 --- a/core/src/main/java/hudson/util/io/TarArchiver.java +++ b/core/src/main/java/hudson/util/io/TarArchiver.java @@ -26,6 +26,7 @@ package hudson.util.io; import hudson.Functions; import hudson.org.apache.tools.tar.TarOutputStream; +import hudson.os.PosixException; import hudson.util.FileVisitor; import hudson.util.IOException2; import hudson.util.IOUtils; @@ -65,8 +66,14 @@ final class TarArchiver extends Archiver { @Override public void visitSymlink(File link, String target, String relativePath) throws IOException { TarEntry e = new TarEntry(relativePath, LF_SYMLINK); - int mode = IOUtils.mode(link); - if (mode!=-1) e.setMode(mode); + try { + int mode = IOUtils.mode(link); + if (mode != -1) { + e.setMode(mode); + } + } catch (PosixException x) { + // ignore + } try { StringBuffer linkName = (StringBuffer) LINKNAME_FIELD.get(e); diff --git a/core/src/test/java/hudson/util/io/TarArchiverTest.java b/core/src/test/java/hudson/util/io/TarArchiverTest.java index 2d968be8f3..a4cbfd5874 100644 --- a/core/src/test/java/hudson/util/io/TarArchiverTest.java +++ b/core/src/test/java/hudson/util/io/TarArchiverTest.java @@ -26,6 +26,9 @@ package hudson.util.io; import hudson.FilePath; import hudson.Functions; import hudson.Launcher.LocalLauncher; +import hudson.Util; +import hudson.model.TaskListener; +import hudson.util.NullStream; import hudson.util.StreamTaskListener; import java.io.File; import java.io.FileOutputStream; @@ -34,10 +37,8 @@ import static org.junit.Assume.*; import org.junit.Test; import org.jvnet.hudson.test.Bug; -/** - * @author Kohsuke Kawaguchi - */ public class TarArchiverTest { + /** * Makes sure that permissions are properly stored in the tar file. */ @@ -92,4 +93,13 @@ public class TarArchiverTest { dir.deleteRecursive(); } } + + @Bug(14922) + @Test public void brokenSymlinks() throws Exception { + assumeTrue(!Functions.isWindows()); + File dir = Util.createTempDir(); + Util.createSymlink(dir, "nonexistent", "link", TaskListener.NULL); + new FilePath(dir).tar(new NullStream(), "**"); + } + } -- GitLab