Skip to content

Commit

Permalink
Use builtin posix vs creating one per regularfileresource. Besides ov…
Browse files Browse the repository at this point in the history
…erhead of lots of new jnr-posix instances we need to push this into resource because we do not know if native is enabled or disabled. On platforms with native unable to load lstat could not execute.
  • Loading branch information
enebo committed Aug 30, 2014
1 parent 38ea3c8 commit 0163e4b
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 36 deletions.
11 changes: 6 additions & 5 deletions core/src/main/java/org/jruby/RubyDir.java
Expand Up @@ -44,6 +44,7 @@
import java.util.zip.ZipEntry;
import jnr.posix.FileStat;

import jnr.posix.POSIX;
import org.jruby.anno.JRubyMethod;
import org.jruby.anno.JRubyClass;
import jnr.posix.util.Platform;
Expand Down Expand Up @@ -161,8 +162,9 @@ public IRubyObject initialize19(ThreadContext context, IRubyObject arg) {
private static List<ByteList> dirGlobs(ThreadContext context, String cwd, IRubyObject[] args, int flags) {
List<ByteList> dirs = new ArrayList<ByteList>();

POSIX posix = context.runtime.getPosix();
for (int i = 0; i < args.length; i++) {
dirs.addAll(Dir.push_glob(cwd, globArgumentAsByteList(context, args[i]), flags));
dirs.addAll(Dir.push_glob(posix, cwd, globArgumentAsByteList(context, args[i]), flags));
}

return dirs;
Expand Down Expand Up @@ -198,7 +200,7 @@ public static IRubyObject aref(ThreadContext context, IRubyObject recv, IRubyObj
Ruby runtime = context.runtime;
List<ByteList> dirs;
if (args.length == 1) {
dirs = Dir.push_glob(getCWD(runtime), globArgumentAsByteList(context, args[0]), 0);
dirs = Dir.push_glob(runtime.getPosix(), getCWD(runtime), globArgumentAsByteList(context, args[0]), 0);
} else {
dirs = dirGlobs(context, getCWD(runtime), args, 0);
}
Expand Down Expand Up @@ -226,7 +228,7 @@ public static IRubyObject glob(ThreadContext context, IRubyObject recv, IRubyObj
List<ByteList> dirs;
IRubyObject tmp = args[0].checkArrayType();
if (tmp.isNil()) {
dirs = Dir.push_glob(runtime.getCurrentDirectory(), globArgumentAsByteList(context, args[0]), flags);
dirs = Dir.push_glob(runtime.getPosix(), runtime.getCurrentDirectory(), globArgumentAsByteList(context, args[0]), flags);
} else {
dirs = dirGlobs(context, getCWD(runtime), ((RubyArray) tmp).toJavaArray(), flags);
}
Expand Down Expand Up @@ -618,7 +620,7 @@ public static IRubyObject exist(ThreadContext context, IRubyObject recv, IRubyOb
*
* @param path path for which to return the <code>File</code> object.
* @param mustExist is true the directory must exist. If false it must not.
* @throws IOError if <code>path</code> is not a directory.
* @throws java.io.IOError if <code>path</code> is not a directory.
*/
protected static JRubyFile getDir(final Ruby runtime, final String path, final boolean mustExist) {
String dir = dirFromPath(path, runtime);
Expand Down Expand Up @@ -646,7 +648,6 @@ protected static JRubyFile getDir(final Ruby runtime, final String path, final b
* Similar to getDir, but performs different checks to match rmdir behavior.
* @param runtime
* @param path
* @param mustExist
* @return
*/
protected static JRubyFile getDirForRmdir(final Ruby runtime, final String path) {
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/org/jruby/RubyFileStat.java
Expand Up @@ -112,13 +112,14 @@ private void setup(String filename, boolean lstat) {
filename += "/";
}

file = JRubyFile.createResource(getRuntime().getCurrentDirectory(), filename);
POSIX posix = getRuntime().getPosix();

file = JRubyFile.createResource(posix, getRuntime().getCurrentDirectory(), filename);

if (!file.exists()) {
throw getRuntime().newErrnoENOENTError("No such file or directory - " + filename);
}

POSIX posix = getRuntime().getPosix();
stat = lstat ? file.lstat(posix) : file.stat(posix);
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyFileTest.java
Expand Up @@ -524,7 +524,7 @@ private static RubyFileStat getFileStat(ThreadContext context, IRubyObject filen
RubyFileStat stat = null;
if (!(filename instanceof RubyFile)) {
RubyString path = get_path(context, filename);
FileResource file = JRubyFile.createResource(runtime.getCurrentDirectory(), path.getUnicodeValue());
FileResource file = JRubyFile.createResource(runtime.getPosix(), runtime.getCurrentDirectory(), path.getUnicodeValue());
if (file.exists()) {
stat = runtime.newFileStat(file.absolutePath(), false);
}
Expand Down
37 changes: 19 additions & 18 deletions core/src/main/java/org/jruby/util/Dir.java
Expand Up @@ -32,6 +32,7 @@
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import jnr.posix.POSIX;
import org.jruby.RubyEncoding;
import org.jruby.RubyFile;

Expand Down Expand Up @@ -296,10 +297,10 @@ public static int range(byte[] _pat, int pat, int pend, char test, int flags) {
return ok == not ? -1 : pat + 1;
}

public static List<ByteList> push_glob(String cwd, ByteList globByteList, int flags) {
public static List<ByteList> push_glob(POSIX posix, String cwd, ByteList globByteList, int flags) {
List<ByteList> result = new ArrayList<ByteList>();
if (globByteList.length() > 0) {
push_braces(cwd, result, new GlobPattern(globByteList, flags));
push_braces(posix, cwd, result, new GlobPattern(globByteList, flags));
}

return result;
Expand Down Expand Up @@ -414,7 +415,7 @@ public int call(byte[] ptr, int p, int len, Object ary) {
/*
* Process {}'s (example: Dir.glob("{jruby,jython}/README*")
*/
private static int push_braces(String cwd, List<ByteList> result, GlobPattern pattern) {
private static int push_braces(POSIX posix, String cwd, List<ByteList> result, GlobPattern pattern) {
pattern.reset();
int lbrace = pattern.indexOf((byte) '{'); // index of left-most brace
int rbrace = pattern.findClosingIndexOf(lbrace);// index of right-most brace
Expand All @@ -435,7 +436,7 @@ private static int push_braces(String cwd, List<ByteList> result, GlobPattern pa
unescaped.append(b);
}
}
return push_globs(cwd, result, unescaped.getUnsafeBytes(), unescaped.begin(), unescaped.length(), pattern.flags);
return push_globs(posix, cwd, result, unescaped.getUnsafeBytes(), unescaped.begin(), unescaped.length(), pattern.flags);
}

// Peel onion...make subpatterns out of outer layer of glob and recall with each subpattern
Expand All @@ -453,16 +454,16 @@ private static int push_braces(String cwd, List<ByteList> result, GlobPattern pa
buf.append(pattern.bytes, pattern.begin, lbrace - pattern.begin);
buf.append(pattern.bytes, middleRegionIndex, i - middleRegionIndex);
buf.append(pattern.bytes, rbrace + 1, pattern.end - (rbrace + 1));
int status = push_braces(cwd, result, new GlobPattern(buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize(),pattern.flags));
int status = push_braces(posix, cwd, result, new GlobPattern(buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize(),pattern.flags));
if(status != 0) return status;
}

return 0; // All braces pushed..
}

private static int push_globs(String cwd, List<ByteList> ary, byte[] pattern, int pbegin, int pend, int pflags) {
private static int push_globs(POSIX posix, String cwd, List<ByteList> ary, byte[] pattern, int pbegin, int pend, int pflags) {
pflags |= FNM_SYSCASE;
return glob_helper(cwd, pattern, pbegin, pend, -1, pflags, glob_caller, new GlobArgs(push_pattern, ary));
return glob_helper(posix, cwd, pattern, pbegin, pend, -1, pflags, glob_caller, new GlobArgs(push_pattern, ary));
}

private static boolean has_magic(byte[] bytes, int begin, int end, int flags) {
Expand Down Expand Up @@ -577,9 +578,9 @@ private static boolean isSpecialFile(String name) {
return c == '.' && name.charAt(2) == '/';
}

private static int addToResultIfExists(String cwd, byte[] bytes, int begin, int end, int flags, GlobFunc func, GlobArgs arg) {
private static int addToResultIfExists(POSIX posix, String cwd, byte[] bytes, int begin, int end, int flags, GlobFunc func, GlobArgs arg) {
String fileName = newStringFromUTF8(bytes, begin, end - begin);
FileResource file = JRubyFile.createResource(cwd, fileName);
FileResource file = JRubyFile.createResource(posix, cwd, fileName);

if (file.exists()) {
boolean trailingSlash = bytes[end - 1] == '/';
Expand Down Expand Up @@ -615,7 +616,7 @@ private static int addToResultIfExists(String cwd, byte[] bytes, int begin, int
return 0;
}

private static int glob_helper(String cwd, byte[] bytes, int begin, int end, int sub, int flags, GlobFunc func, GlobArgs arg) {
private static int glob_helper(POSIX posix, String cwd, byte[] bytes, int begin, int end, int sub, int flags, GlobFunc func, GlobArgs arg) {
int p,m;
int status = 0;
byte[] newpath = null;
Expand All @@ -637,9 +638,9 @@ private static int glob_helper(String cwd, byte[] bytes, int begin, int end, int
}

if (isAbsolutePath(bytes, begin, end)) {
status = addToResultIfExists(null, bytes, begin, end, flags, func, arg);
status = addToResultIfExists(posix, null, bytes, begin, end, flags, func, arg);
} else if ((end - begin) > 0) { // Length check is a hack. We should not be reeiving "" as a filename ever.
status = addToResultIfExists(cwd, bytes, begin, end, flags, func, arg);
status = addToResultIfExists(posix, cwd, bytes, begin, end, flags, func, arg);
}

return status;
Expand All @@ -658,7 +659,7 @@ private static int glob_helper(String cwd, byte[] bytes, int begin, int end, int
byte[] magic = extract_elem(bytes,p,end);
boolean recursive = false;

st = JRubyFile.createResource(cwd, newStringFromUTF8(dir));
st = JRubyFile.createResource(posix, cwd, newStringFromUTF8(dir));

if (st.isDirectory()) {
if(m != -1 && Arrays.equals(magic, DOUBLE_STAR)) {
Expand All @@ -667,7 +668,7 @@ private static int glob_helper(String cwd, byte[] bytes, int begin, int end, int
buf.length(0);
buf.append(base);
buf.append(bytes, (base.length > 0 ? m : m + 1), end - (base.length > 0 ? m : m + 1));
status = glob_helper(cwd, buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize(), n, flags, func, arg);
status = glob_helper(posix, cwd, buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize(), n, flags, func, arg);
if(status != 0) {
break finalize;
}
Expand All @@ -688,13 +689,13 @@ private static int glob_helper(String cwd, byte[] bytes, int begin, int end, int
buf.append(base);
buf.append(isRoot(base) ? EMPTY : SLASH );
buf.append(getBytesInUTF8(dirp[i]));
st = JRubyFile.createResource(cwd, newStringFromUTF8(buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize()));
st = JRubyFile.createResource(posix, cwd, newStringFromUTF8(buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize()));
if(!st.isSymLink() && st.isDirectory() && !".".equals(dirp[i]) && !"..".equals(dirp[i])) {
int t = buf.getRealSize();
buf.append(SLASH);
buf.append(DOUBLE_STAR);
buf.append(bytes, m, end - m);
status = glob_helper(cwd, buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize(), t, flags, func, arg);
status = glob_helper(posix, cwd, buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize(), t, flags, func, arg);
if(status != 0) {
break;
}
Expand Down Expand Up @@ -724,13 +725,13 @@ private static int glob_helper(String cwd, byte[] bytes, int begin, int end, int
for (DirGlobber globber : link) {
ByteList b = globber.link;
if (status == 0) {
st = JRubyFile.createResource(cwd, newStringFromUTF8(b.getUnsafeBytes(), 0, b.getRealSize()));
st = JRubyFile.createResource(posix, cwd, newStringFromUTF8(b.getUnsafeBytes(), 0, b.getRealSize()));
if(st.isDirectory()) {
int len = b.getRealSize();
buf.length(0);
buf.append(b);
buf.append(bytes, m, end - m);
status = glob_helper(cwd, buf.getUnsafeBytes(),0, buf.getRealSize(),len,flags,func,arg);
status = glob_helper(posix, cwd, buf.getUnsafeBytes(),0, buf.getRealSize(),len,flags,func,arg);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/java/org/jruby/util/JRubyFile.java
Expand Up @@ -35,6 +35,7 @@
import java.io.FilenameFilter;
import java.io.IOException;

import jnr.posix.POSIX;
import org.jruby.Ruby;
import org.jruby.RubyFile;
import org.jruby.runtime.ThreadContext;
Expand Down Expand Up @@ -63,10 +64,10 @@ public static FileResource createResource(ThreadContext context, String pathname
}

public static FileResource createResource(Ruby runtime, String pathname) {
return createResource(runtime.getCurrentDirectory(), pathname);
return createResource(runtime.getPosix(), runtime.getCurrentDirectory(), pathname);
}

public static FileResource createResource(String cwd, String pathname) {
public static FileResource createResource(POSIX posix, String cwd, String pathname) {
FileResource emptyResource = EmptyFileResource.create(pathname);
if (emptyResource != null) return emptyResource;

Expand All @@ -85,7 +86,7 @@ public static FileResource createResource(String cwd, String pathname) {
}

// If any other special resource types fail, count it as a filesystem backed resource.
return new RegularFileResource(create(cwd, pathname));
return new RegularFileResource(posix, create(cwd, pathname));
}

public static String normalizeSeps(String path) {
Expand Down
13 changes: 7 additions & 6 deletions core/src/main/java/org/jruby/util/RegularFileResource.java
Expand Up @@ -21,14 +21,15 @@
*/
class RegularFileResource implements FileResource {
private final JRubyFile file;
private final POSIX symlinkPosix = POSIXFactory.getPOSIX();
private final POSIX posix;

RegularFileResource(File file) {
this(file.getAbsolutePath());
RegularFileResource(POSIX posix, File file) {
this(posix, file.getAbsolutePath());
}

protected RegularFileResource(String filename) {
protected RegularFileResource(POSIX posix, String filename) {
this.file = new JRubyFile(filename);
this.posix = posix;
}

// TODO(ratnikov): This should likely be renamed to rubyPath, otherwise it's easy to get
Expand Down Expand Up @@ -74,9 +75,9 @@ public boolean isDirectory() {

@Override
public boolean isSymLink() {
FileStat stat = symlinkPosix.allocateStat();
FileStat stat = posix.allocateStat();

return symlinkPosix.lstat(file.getAbsolutePath(), stat) < 0 ?
return posix.lstat(file.getAbsolutePath(), stat) < 0 ?
false : stat.isSymlink();
}

Expand Down
Expand Up @@ -780,7 +780,7 @@ public static ChannelDescriptor open(String cwd, String path, ModeFlags flags, i
return new ChannelDescriptor(Channels.newChannel(is), flags);
}

return JRubyFile.createResource(cwd, path).openDescriptor(flags, posix, perm);
return JRubyFile.createResource(posix, cwd, path).openDescriptor(flags, posix, perm);
}

/**
Expand Down

0 comments on commit 0163e4b

Please sign in to comment.