Skip to content

Commit

Permalink
[FIXED JENKINS-14922] TarArchiver.visitSymlink can throw undeclared P…
Browse files Browse the repository at this point in the history
…osixException.
  • Loading branch information
jglick committed Aug 24, 2012
1 parent f4c6563 commit 688eb6a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
3 changes: 3 additions & 0 deletions changelog.html
Expand Up @@ -55,6 +55,9 @@
<!-- Record your changes in the trunk here. -->
<div id="trunk" style="display:none"><!--=TRUNK-BEGIN=-->
<ul class=image>
<li class=bug>
<code>TarArchiver.visitSymlink</code> can throw undeclared <code>PosixException</code>.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-14922">issue 14922</a>)
<li class=>
</ul>
</div><!--=TRUNK-END=-->
Expand Down
7 changes: 5 additions & 2 deletions core/src/main/java/hudson/util/IOUtils.java
Expand Up @@ -2,6 +2,7 @@

import hudson.Functions;
import hudson.os.PosixAPI;
import hudson.os.PosixException;

import java.io.*;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -115,9 +116,11 @@ public static boolean isAbsolute(String path) {


/**
* 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();
}
Expand Down
11 changes: 9 additions & 2 deletions core/src/main/java/hudson/util/io/TarArchiver.java
Expand Up @@ -26,6 +26,7 @@

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;
Expand Down Expand Up @@ -65,8 +66,14 @@ public void flush() throws IOException {
@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);
Expand Down
16 changes: 13 additions & 3 deletions core/src/test/java/hudson/util/io/TarArchiverTest.java
Expand Up @@ -26,6 +26,9 @@
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;
Expand All @@ -34,10 +37,8 @@
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.
*/
Expand Down Expand Up @@ -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(), "**");
}

}

0 comments on commit 688eb6a

Please sign in to comment.