Skip to content

Commit

Permalink
[JENKINS-12119] Fix for symlinks not created when native libs don't work
Browse files Browse the repository at this point in the history
  • Loading branch information
tivv authored and kohsuke committed Jan 3, 2012
1 parent e36f423 commit 867b67a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 48 deletions.
73 changes: 26 additions & 47 deletions core/src/main/java/hudson/Util.java
Expand Up @@ -23,81 +23,52 @@
*/
package hudson;

import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import hudson.Proc.LocalProc;
import hudson.model.TaskListener;
import jenkins.model.Jenkins;
import static hudson.util.jna.GNUCLibrary.LIBC;

import hudson.os.PosixAPI;
import hudson.util.IOException2;
import hudson.util.QuotedStringTokenizer;
import hudson.util.VariableResolver;
import hudson.Proc.LocalProc;
import hudson.os.PosixAPI;
import jenkins.model.Jenkins;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.time.FastDateFormat;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.taskdefs.Chmod;
import org.apache.tools.ant.taskdefs.Copy;
import org.apache.commons.lang.time.FastDateFormat;
import org.apache.commons.io.IOUtils;
import org.apache.tools.ant.types.FileSet;
import org.jruby.ext.posix.FileStat;
import org.jruby.ext.posix.POSIX;
import org.kohsuke.stapler.Stapler;
import org.jvnet.animal_sniffer.IgnoreJRERequirement;
import org.kohsuke.stapler.Stapler;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.io.PrintStream;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.SimpleTimeZone;
import java.util.StringTokenizer;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.nio.charset.Charset;

import com.sun.jna.Native;
import com.sun.jna.Memory;
import com.sun.jna.NativeLong;
import static hudson.util.jna.GNUCLibrary.LIBC;

/**
* Various utility methods that don't have more proper home.
Expand Down Expand Up @@ -1020,23 +991,31 @@ public static void createSymlink(File baseDir, String targetPath, String symlink
// ignore a failure.
new LocalProc(new String[]{"rm","-rf", symlinkPath},new String[0],listener.getLogger(), baseDir).join();

int r;
int r = -1;
boolean operationDone = false;
if (!SYMLINK_ESCAPEHATCH) {
try {
r = LIBC.symlink(targetPath,symlinkFile.getAbsolutePath());
if (r!=0) {
r = Native.getLastError();
errmsg = LIBC.strerror(r);
}
operationDone = true;
} catch (LinkageError e) {
// if JNA is unavailable, fall back.
// we still prefer to try JNA first as PosixAPI supports even smaller platforms.
r = PosixAPI.get().symlink(targetPath,symlinkFile.getAbsolutePath());
if (PosixAPI.isNative()) {
r = PosixAPI.get().symlink(targetPath,symlinkFile.getAbsolutePath());
operationDone = true;
}
}
} else // escape hatch, until we know that the above works well.
}
if (!operationDone) {
// escape hatch, until we know that the above works well.
r = new LocalProc(new String[]{
"ln","-s", targetPath, symlinkPath},
new String[0],listener.getLogger(), baseDir).join();
}
if(r!=0)
listener.getLogger().println(String.format("ln -s %s %s failed: %d %s",targetPath, symlinkFile, r, errmsg));
} catch (IOException e) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/os/PosixAPI.java
Expand Up @@ -25,7 +25,7 @@ public static POSIX get() {
* Determine if the jna-posix library could not provide native support, and
* used a fallback java implementation which does not support many operations.
*/
public boolean isNative() {
public static boolean isNative() {
return !(posix instanceof JavaPOSIX);
}

Expand Down

0 comments on commit 867b67a

Please sign in to comment.