Skip to content


[Truffle] Add more Dir.{open,entries}.
Browse files Browse the repository at this point in the history
  • Loading branch information
bjfish committed Apr 30, 2015
1 parent 1ddc236 commit 69b8e67
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 14 deletions.
4 changes: 0 additions & 4 deletions spec/truffle/tags/core/dir/entries_tags.txt
@@ -1,7 +1,3 @@
fails:Dir.entries returns an Array of filenames in an existing directory including dotfiles
fails:Dir.entries calls #to_path on non-String arguments
fails:Dir.entries accepts an options Hash
fails:Dir.entries returns entries encoded with the filesystem encoding by default
fails:Dir.entries returns entries encoded with the specified encoding
fails:Dir.entries returns entries transcoded to the default internal encoding
fails:Dir.entries raises a SystemCallError if called with a nonexistent diretory
8 changes: 0 additions & 8 deletions spec/truffle/tags/core/dir/open_tags.txt
@@ -1,10 +1,2 @@ returns a Dir instance representing the specified directory raises a SystemCallError if the directory does not exist may take a block which is yielded to with the Dir instance returns the value of the block if a block is given closes the Dir instance when the block exits if given a block closes the Dir instance when the block exits the block even due to an exception calls #to_path on non-String arguments accepts an options Hash calls #to_hash to convert the options object ignores the :encoding option if it is nil
Expand Up @@ -21,6 +21,7 @@
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.core.RubyBasicObject;
import org.jruby.truffle.runtime.core.RubyClass;
import org.jruby.truffle.runtime.core.RubyEncoding;
import org.jruby.truffle.runtime.core.RubyNilClass;
import org.jruby.truffle.runtime.core.RubyString;

Expand Down Expand Up @@ -82,6 +83,13 @@ public RubyNilClass open(RubyBasicObject dir, RubyString path, RubyNilClass enco
return nil();

public RubyNilClass open(RubyBasicObject dir, RubyString path, RubyEncoding encoding) {
// TODO BJF 30-APR-2015 HandleEncoding
return open(dir, path, nil());


@RubiniusPrimitive(name = "dir_read")
Expand Down
Expand Up @@ -12,6 +12,7 @@
import jnr.constants.platform.Fcntl;
import org.jruby.RubyEncoding;
import org.jruby.platform.Platform;
import org.jruby.truffle.nodes.core.CoreClass;
import org.jruby.truffle.nodes.core.CoreMethod;
Expand All @@ -23,6 +24,7 @@
import org.jruby.util.unsafe.UnsafeHolder;
import sun.misc.Unsafe;

import java.nio.charset.StandardCharsets;

@CoreClass(name = "Rubinius::FFI::Platform::POSIX")
Expand Down Expand Up @@ -193,7 +195,21 @@ public UnlinkNode(RubyContext context, SourceSection sourceSection) {

public int unlink(RubyString path) {
return posix().unlink(path.toString());
return posix().unlink(RubyEncoding.decodeUTF8(path.getByteList().getUnsafeBytes(), path.getByteList().getBegin(), path.getByteList().getRealSize()));


@CoreMethod(names = "umask", isModuleFunction = true, required = 1)
public abstract static class UmaskNode extends CoreMethodArrayArgumentsNode {

public UmaskNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);

public int umask(int mask) {
return posix().umask(mask);

Expand Down
Expand Up @@ -13,6 +13,7 @@
import jnr.posix.FileStat;
import org.jruby.RubyEncoding;
import org.jruby.truffle.nodes.objectstorage.ReadHeadObjectFieldNode;
import org.jruby.truffle.nodes.objectstorage.WriteHeadObjectFieldNode;
import org.jruby.truffle.runtime.RubyContext;
Expand All @@ -36,7 +37,8 @@ public StatStatPrimitiveNode(RubyContext context, SourceSection sourceSection) {
public int stat(RubyBasicObject rubyStat, RubyString path) {
final FileStat stat = posix().allocateStat();
final int code = posix().stat(path.toString(), stat);
final String pathString = RubyEncoding.decodeUTF8(path.getByteList().getUnsafeBytes(), path.getByteList().getBegin(), path.getByteList().getRealSize());
final int code = posix().stat(pathString, stat);

if (code == 0) {
writeStatNode.execute(rubyStat, stat);
Expand Down
27 changes: 27 additions & 0 deletions truffle/src/main/ruby/core/rubinius/common/dir.rb
Expand Up @@ -34,6 +34,33 @@ class Dir

FFI = Rubinius::FFI

def, options=undefined)
dir = new path, options
if block_given?
value = yield dir

return value
return dir

def self.entries(path, options=undefined)
ret = []

open(path, options) do |dir|
while s =
ret << s


def self.[](*patterns)
if patterns.size == 1
pattern = Rubinius::Type.coerce_to_path(patterns[0])
Expand Down

0 comments on commit 69b8e67

Please sign in to comment.