-
-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3615 from jruby/truffle-minimal-java-posix
[Truffle] Minimal Java POSIX
- 9.4.12.0
- 9.4.11.0
- 9.4.10.0
- 9.4.9.0
- 9.4.8.0
- 9.4.7.0
- 9.4.6.0
- 9.4.5.0
- 9.4.4.0
- 9.4.3.0
- 9.4.2.0
- 9.4.1.0
- 9.4.0.0
- 9.3.15.0
- 9.3.14.0
- 9.3.13.0
- 9.3.12.0
- 9.3.11.0
- 9.3.10.0
- 9.3.9.0
- 9.3.8.0
- 9.3.7.0
- 9.3.6.0
- 9.3.5.0
- 9.3.4.0
- 9.3.3.0
- 9.3.2.0
- 9.3.1.0
- 9.3.0.0
- 9.2.21.0
- 9.2.20.1
- 9.2.20.0
- 9.2.19.0
- 9.2.18.0
- 9.2.17.0
- 9.2.16.0
- 9.2.15.0
- 9.2.14.0
- 9.2.13.0
- 9.2.12.0
- 9.2.11.1
- 9.2.11.0
- 9.2.10.0
- 9.2.9.0
- 9.2.8.0
- 9.2.7.0
- 9.2.6.0
- 9.2.5.0
- 9.2.4.1
- 9.2.4.0
- 9.2.3.0
- 9.2.2.0
- 9.2.1.0
- 9.2.0.0
- 9.1.17.0
- 9.1.16.0
- 9.1.15.0
- 9.1.14.0
- 9.1.13.0
- 9.1.12.0
- 9.1.11.0
- 9.1.10.0
- 9.1.9.0
- 9.1.8.0
- 9.1.7.0
- 9.1.6.0
- 9.1.5.0
- 9.1.4.0
- 9.1.3.0
- 9.1.2.0
- 9.1.1.0
- 9.1.0.0
Showing
8 changed files
with
1,506 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
540 changes: 540 additions & 0 deletions
540
truffle/src/main/java/org/jruby/truffle/runtime/BaseLibC.java
Large diffs are not rendered by default.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
truffle/src/main/java/org/jruby/truffle/runtime/JavaLibC.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This | ||
* code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
* redistribute it and/or modify it under the terms of the: | ||
* | ||
* Eclipse Public License version 1.0 | ||
* GNU General Public License version 2 | ||
* GNU Lesser General Public License version 2.1 | ||
*/ | ||
package org.jruby.truffle.runtime; | ||
|
||
import jnr.posix.LibC; | ||
|
||
public class JavaLibC extends BaseLibC implements LibC { | ||
|
||
public static final JavaLibC INSTANCE = new JavaLibC(); | ||
|
||
private JavaLibC() { | ||
} | ||
|
||
@Override | ||
public int isatty(int fd) { | ||
return System.console() != null ? 1 : 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
636 changes: 636 additions & 0 deletions
636
truffle/src/main/java/org/jruby/truffle/runtime/POSIXDelegator.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
truffle/src/main/java/org/jruby/truffle/runtime/TruffleJavaFileStat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This | ||
* code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
* redistribute it and/or modify it under the terms of the: | ||
* | ||
* Eclipse Public License version 1.0 | ||
* GNU General Public License version 2 | ||
* GNU Lesser General Public License version 2.1 | ||
*/ | ||
package org.jruby.truffle.runtime; | ||
|
||
import jnr.posix.FileStat; | ||
import jnr.posix.JavaFileStat; | ||
import jnr.posix.POSIX; | ||
import jnr.posix.POSIXHandler; | ||
|
||
import java.io.File; | ||
|
||
public class TruffleJavaFileStat extends JavaFileStat { | ||
|
||
private boolean executable = false; | ||
|
||
public TruffleJavaFileStat(POSIX posix, POSIXHandler handler) { | ||
super(posix, handler); | ||
} | ||
|
||
@Override | ||
public void setup(String path) { | ||
super.setup(path); | ||
|
||
executable = new File(path).canExecute(); | ||
} | ||
|
||
@Override | ||
public int gid() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public int mode() { | ||
int mode = super.mode(); | ||
|
||
if (executable) { | ||
mode |= FileStat.S_IXOTH; | ||
} | ||
|
||
return mode; | ||
} | ||
|
||
} |
248 changes: 248 additions & 0 deletions
248
truffle/src/main/java/org/jruby/truffle/runtime/TruffleJavaPOSIX.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
/* | ||
* Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This | ||
* code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
* redistribute it and/or modify it under the terms of the: | ||
* | ||
* Eclipse Public License version 1.0 | ||
* GNU General Public License version 2 | ||
* GNU Lesser General Public License version 2.1 | ||
*/ | ||
package org.jruby.truffle.runtime; | ||
|
||
import jnr.constants.platform.Errno; | ||
import jnr.constants.platform.Fcntl; | ||
import jnr.constants.platform.OpenFlags; | ||
import jnr.posix.FileStat; | ||
import jnr.posix.LibC; | ||
import jnr.posix.POSIX; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.io.RandomAccessFile; | ||
import java.nio.ByteBuffer; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class TruffleJavaPOSIX extends POSIXDelegator implements POSIX { | ||
|
||
private static class OpenFile { | ||
|
||
private final RandomAccessFile randomAccessFile; | ||
private final int flags; | ||
|
||
private OpenFile(RandomAccessFile randomAccessFile, int flags) { | ||
this.randomAccessFile = randomAccessFile; | ||
this.flags = flags; | ||
} | ||
|
||
public RandomAccessFile getRandomAccessFile() { | ||
return randomAccessFile; | ||
} | ||
|
||
public int getFlags() { | ||
return flags; | ||
} | ||
} | ||
|
||
private static final int STDIN = 0; | ||
private static final int STDOUT = 1; | ||
private static final int STDERR = 2; | ||
|
||
private final RubyContext context; | ||
|
||
private final AtomicInteger nextFileHandle = new AtomicInteger(3); | ||
private final Map<Integer, OpenFile> fileHandles = new ConcurrentHashMap<>(); | ||
|
||
public TruffleJavaPOSIX(RubyContext context, POSIX delegateTo) { | ||
super(delegateTo); | ||
this.context = context; | ||
} | ||
|
||
@Override | ||
public int fcntlInt(int fd, Fcntl fcntlConst, int arg) { | ||
if (fcntlConst.longValue() == Fcntl.F_GETFL.longValue()) { | ||
switch (fd) { | ||
case STDIN: | ||
return OpenFlags.O_RDONLY.intValue(); | ||
case STDOUT: | ||
case STDERR: | ||
return OpenFlags.O_WRONLY.intValue(); | ||
} | ||
|
||
final OpenFile openFile = fileHandles.get(fd); | ||
|
||
if (openFile != null) { | ||
return openFile.getFlags(); | ||
} | ||
} | ||
|
||
return super.fcntlInt(fd, fcntlConst, arg); | ||
} | ||
|
||
@Override | ||
public int getpid() { | ||
return context.hashCode(); | ||
} | ||
|
||
@Override | ||
public LibC libc() { | ||
return JavaLibC.INSTANCE; | ||
} | ||
|
||
@Override | ||
public int open(CharSequence path, int flags, int perm) { | ||
if (perm != 0666) { | ||
return super.open(path, flags, perm); | ||
} | ||
|
||
final int fileHandle = nextFileHandle.getAndIncrement(); | ||
|
||
if (fileHandle < 0) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
final int basicMode = flags & 3; | ||
final String mode; | ||
|
||
if (basicMode == OpenFlags.O_RDONLY.intValue()) { | ||
mode = "r"; | ||
} else if (basicMode == OpenFlags.O_WRONLY.intValue()) { | ||
mode = "w"; | ||
} else if (basicMode == OpenFlags.O_RDWR.intValue()) { | ||
mode = "rw"; | ||
} else { | ||
return super.open(path, flags, perm); | ||
} | ||
|
||
final RandomAccessFile randomAccessFile; | ||
|
||
try { | ||
randomAccessFile = new RandomAccessFile(path.toString(), mode); | ||
} catch (FileNotFoundException e) { | ||
return -1; | ||
} | ||
|
||
fileHandles.put(fileHandle, new OpenFile(randomAccessFile, flags)); | ||
|
||
return fileHandle; | ||
} | ||
|
||
@Override | ||
public int read(int fd, ByteBuffer buf, int n) { | ||
return pread(fd, buf.array(), n, buf.arrayOffset()); | ||
} | ||
|
||
@Override | ||
public int read(int fd, byte[] buf, int n) { | ||
return pread(fd, buf, n, 0); | ||
} | ||
|
||
@Override | ||
public int pread(int fd, byte[] buf, int n, int offset) { | ||
if (fd == STDIN) { | ||
try { | ||
System.in.read(buf, offset, n); | ||
} catch (IOException e) { | ||
return -1; | ||
} | ||
|
||
return n; | ||
} | ||
|
||
final OpenFile openFile = fileHandles.get(fd); | ||
|
||
if (openFile != null) { | ||
final int read; | ||
|
||
try { | ||
read = openFile.getRandomAccessFile().read(buf, offset, n); | ||
} catch (IOException e) { | ||
return -1; | ||
} | ||
|
||
if (read == -1) { | ||
errno(Errno.ETIMEDOUT.intValue()); | ||
} | ||
|
||
return read; | ||
} | ||
|
||
return super.pwrite(fd, buf, n, offset); | ||
} | ||
|
||
@Override | ||
public int write(int fd, ByteBuffer buf, int n) { | ||
return pwrite(fd, buf.array(), n, buf.arrayOffset()); | ||
} | ||
|
||
@Override | ||
public int write(int fd, byte[] buf, int n) { | ||
return pwrite(fd, buf, n, 0); | ||
} | ||
|
||
@Override | ||
public int pwrite(int fd, byte[] buf, int n, int offset) { | ||
if (fd == STDOUT || fd == STDERR) { | ||
final PrintStream stream; | ||
|
||
switch (fd) { | ||
case STDOUT: | ||
stream = System.out; | ||
break; | ||
case STDERR: | ||
stream = System.err; | ||
break; | ||
default: | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
stream.write(buf, offset, n); | ||
|
||
return n; | ||
} | ||
|
||
return super.pwrite(fd, buf, n, offset); | ||
} | ||
|
||
@Override | ||
public int close(int fd) { | ||
final OpenFile openFile = fileHandles.get(fd); | ||
|
||
if (openFile != null) { | ||
fileHandles.remove(fd); | ||
|
||
try { | ||
openFile.getRandomAccessFile().close(); | ||
} catch (IOException e) { | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
return super.close(fd); | ||
} | ||
|
||
@Override | ||
public int getgid() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public FileStat allocateStat() { | ||
return new TruffleJavaFileStat(this, null); | ||
} | ||
|
||
@Override | ||
public String getenv(String envName) { | ||
final String javaValue = System.getenv(envName); | ||
|
||
if (javaValue != null) { | ||
return javaValue; | ||
} | ||
|
||
return super.getenv(envName); | ||
} | ||
} |