Skip to content

Commit 2a1d163

Browse files
committedMay 31, 2013
[FIXED JENKINS-14351] jna-posix → jnr-posix upgrade (opt in).
1 parent be5cd4f commit 2a1d163

9 files changed

+75
-28
lines changed
 

‎changelog.html

+3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
<li class=bug>
6262
Optimizations in fingerprint recording.
6363
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-16301">issue 16301</a>)
64+
<li class=bug>
65+
Using JNR-POSIX rather than JNA-POSIX for better platform support.
66+
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-14351">issue 14351</a>)
6467
<li class='major bug'>
6568
Errors searching build records when builds were misordered.
6669
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-15652">issue 15652</a>)

‎core/pom.xml

+6-1
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,16 @@ THE SOFTWARE.
112112
</exclusions>
113113
</dependency>
114114

115-
<dependency>
115+
<dependency> <!-- for compatibility only; all new code should use JNR -->
116116
<groupId>org.jruby.ext.posix</groupId>
117117
<artifactId>jna-posix</artifactId>
118118
<version>1.0.3</version>
119119
</dependency>
120+
<dependency>
121+
<groupId>com.github.jnr</groupId>
122+
<artifactId>jnr-posix</artifactId>
123+
<version>3.0.0</version>
124+
</dependency>
120125
<dependency>
121126
<groupId>org.kohsuke</groupId>
122127
<artifactId>trilead-putty-extension</artifactId>

‎core/src/main/java/hudson/Util.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
import org.apache.tools.ant.taskdefs.Chmod;
4141
import org.apache.tools.ant.taskdefs.Copy;
4242
import org.apache.tools.ant.types.FileSet;
43-
import org.jruby.ext.posix.FileStat;
44-
import org.jruby.ext.posix.POSIX;
43+
import jnr.posix.FileStat;
44+
import jnr.posix.POSIX;
4545
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
4646

4747
import javax.crypto.SecretKey;
@@ -281,7 +281,7 @@ private static void makeWritable(File f) {
281281
}
282282

283283
try {// try libc chmod
284-
POSIX posix = PosixAPI.get();
284+
POSIX posix = PosixAPI.jnr();
285285
String path = f.getAbsolutePath();
286286
FileStat stat = posix.stat(path);
287287
posix.chmod(path, stat.mode()|0200); // u+w
@@ -1073,13 +1073,16 @@ public static void createSymlink(File baseDir, String targetPath, String symlink
10731073
} catch (LinkageError e) {
10741074
// if JNA is unavailable, fall back.
10751075
// we still prefer to try JNA first as PosixAPI supports even smaller platforms.
1076-
if (PosixAPI.supportsNative()) {
1077-
r = PosixAPI.get().symlink(targetPath,symlinkFile.getAbsolutePath());
1076+
POSIX posix = PosixAPI.jnr();
1077+
if (posix.isNative()) {
1078+
// XXX should we rethrow PosixException as IOException here?
1079+
r = posix.symlink(targetPath,symlinkFile.getAbsolutePath());
10781080
}
10791081
}
10801082
}
10811083
if (r==null) {
10821084
// if all else fail, fall back to the most expensive approach of forking a process
1085+
// XXX is this really necessary? JavaPOSIX should do this automatically
10831086
r = new LocalProc(new String[]{
10841087
"ln","-s", targetPath, symlinkPath},
10851088
new String[0],listener.getLogger(), baseDir).join();
@@ -1221,7 +1224,7 @@ public static String resolveSymlink(File link) throws InterruptedException, IOEx
12211224
} catch (LinkageError e) {
12221225
// if JNA is unavailable, fall back.
12231226
// we still prefer to try JNA first as PosixAPI supports even smaller platforms.
1224-
return PosixAPI.get().readlink(filename);
1227+
return PosixAPI.jnr().readlink(filename);
12251228
}
12261229
}
12271230

‎core/src/main/java/hudson/cli/ClientAuthenticationCache.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public Void invoke(File f, VirtualChannel channel) throws IOException, Interrupt
118118
}
119119

120120
// try to protect this file from other users, if we can.
121-
PosixAPI.get().chmod(f.getAbsolutePath(),0600);
121+
PosixAPI.jnr().chmod(f.getAbsolutePath(),0600);
122122
return null;
123123
}
124124
});

‎core/src/main/java/hudson/os/PosixAPI.java

+48-16
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,74 @@
11
package hudson.os;
22

3-
import org.jruby.ext.posix.JavaPOSIX;
4-
import org.jruby.ext.posix.POSIX;
5-
import org.jruby.ext.posix.POSIXFactory;
6-
import org.jruby.ext.posix.POSIXHandler;
7-
import org.jruby.ext.posix.POSIX.ERRORS;
8-
93
import java.io.File;
104
import java.io.InputStream;
115
import java.io.PrintStream;
126
import java.util.Map;
137
import java.util.logging.Logger;
8+
import jnr.constants.platform.Errno;
9+
import jnr.posix.POSIX;
10+
import jnr.posix.POSIXFactory;
11+
import jnr.posix.util.DefaultPOSIXHandler;
1412

1513
/**
1614
* POSIX API wrapper.
17-
*
15+
* Formerly used the jna-posix library, but this has been superseded by jnr-posix.
1816
* @author Kohsuke Kawaguchi
1917
*/
2018
public class PosixAPI {
21-
public static POSIX get() {
19+
20+
private static POSIX posix;
21+
22+
/**
23+
* Load the JNR implementation of the POSIX APIs for the current platform.
24+
* Runtime exceptions will be of type {@link PosixException}.
25+
* @return some implementation (even on Windows or unsupported Unix)
26+
* @since 1.518
27+
*/
28+
public static synchronized POSIX jnr() {
29+
if (posix == null) {
30+
posix = POSIXFactory.getPOSIX(new DefaultPOSIXHandler() {
31+
@Override public void error(Errno error, String extraData) {
32+
throw new PosixException("native error " + error.description() + " " + extraData, convert(error));
33+
}
34+
@Override public void error(Errno error, String methodName, String extraData) {
35+
throw new PosixException("native error calling " + methodName + ": " + error.description() + " " + extraData, convert(error));
36+
}
37+
private org.jruby.ext.posix.POSIX.ERRORS convert(Errno error) {
38+
try {
39+
return org.jruby.ext.posix.POSIX.ERRORS.valueOf(error.name());
40+
} catch (IllegalArgumentException x) {
41+
return org.jruby.ext.posix.POSIX.ERRORS.EIO; // PosixException.message has real error anyway
42+
}
43+
}
44+
}, true);
45+
}
2246
return posix;
2347
}
2448

2549
/**
26-
* @deprecated as of 1.448
27-
* Use {@link #supportsNative()}.
50+
* @deprecated use {@link #jnr} and {@link POSIX#isNative}
2851
*/
52+
@Deprecated
2953
public boolean isNative() {
3054
return supportsNative();
3155
}
3256

3357
/**
34-
* Determine if the jna-posix library could not provide native support, and
35-
* used a fallback java implementation which does not support many operations.
58+
* @deprecated use {@link #jnr} and {@link POSIX#isNative}
3659
*/
60+
@Deprecated
3761
public static boolean supportsNative() {
38-
return !(posix instanceof JavaPOSIX);
62+
return !(jnaPosix instanceof org.jruby.ext.posix.JavaPOSIX);
3963
}
40-
41-
private static final POSIX posix = POSIXFactory.getPOSIX(new POSIXHandler() {
42-
public void error(ERRORS errors, String s) {
64+
65+
private static org.jruby.ext.posix.POSIX jnaPosix;
66+
/** @deprecated Use {@link #jnr} instead. */
67+
@Deprecated
68+
public static synchronized org.jruby.ext.posix.POSIX get() {
69+
if (jnaPosix == null) {
70+
jnaPosix = org.jruby.ext.posix.POSIXFactory.getPOSIX(new org.jruby.ext.posix.POSIXHandler() {
71+
public void error(org.jruby.ext.posix.POSIX.ERRORS errors, String s) {
4372
throw new PosixException(s,errors);
4473
}
4574

@@ -87,6 +116,9 @@ public PrintStream getErrorStream() {
87116
return System.err;
88117
}
89118
}, true);
119+
}
120+
return jnaPosix;
121+
}
90122

91123
private static final Logger LOGGER = Logger.getLogger(PosixAPI.class.getName());
92124
}

‎core/src/main/java/hudson/os/PosixException.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/**
66
* Indicates an error during POSIX API call.
7-
*
7+
* @see PosixAPI
88
* @author Kohsuke Kawaguchi
99
*/
1010
public class PosixException extends RuntimeException {
@@ -15,6 +15,8 @@ public PosixException(String message, ERRORS errors) {
1515
this.errors = errors;
1616
}
1717

18+
/** @deprecated Leaks reference to deprecated jna-posix API. */
19+
@Deprecated
1820
public ERRORS getErrorCode() {
1921
return errors;
2022
}

‎core/src/main/java/hudson/tools/ZipExtractionInstaller.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private void process(File f) {
137137
} catch (LinkageError e) {
138138
// if JNA is unavailable, fall back.
139139
// we still prefer to try JNA first as PosixAPI supports even smaller platforms.
140-
PosixAPI.get().chmod(f.getAbsolutePath(),0755);
140+
PosixAPI.jnr().chmod(f.getAbsolutePath(),0755);
141141
}
142142
}
143143
} else {

‎core/src/main/java/hudson/util/IOUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public static boolean isAbsolute(String path) {
122122
*/
123123
public static int mode(File f) throws PosixException {
124124
if(Functions.isWindows()) return -1;
125-
return PosixAPI.get().stat(f.getPath()).mode();
125+
return PosixAPI.jnr().stat(f.getPath()).mode();
126126
}
127127

128128
/**

‎core/src/main/java/hudson/util/jna/GNUCLibrary.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import com.sun.jna.Memory;
3131
import com.sun.jna.NativeLong;
3232
import com.sun.jna.ptr.IntByReference;
33+
import hudson.os.PosixAPI;
34+
import jnr.posix.POSIX;
3335
import org.jvnet.libpam.impl.CLibrary.passwd;
3436

3537
/**
@@ -38,7 +40,7 @@
3840
* <p>
3941
* Not available on all platforms (such as Linux/PPC, IBM mainframe, etc.), so the caller should recover gracefully
4042
* in case of {@link LinkageError}. See HUDSON-4820.
41-
*
43+
* <p>Consider deprecating all methods present also in {@link POSIX} (as obtained by {@link PosixAPI#jnr}).
4244
* @author Kohsuke Kawaguchi
4345
*/
4446
public interface GNUCLibrary extends Library {

0 commit comments

Comments
 (0)
Please sign in to comment.