Skip to content

Commit

Permalink
[Truffle] Add io_truncate for File.truncate.
Browse files Browse the repository at this point in the history
  • Loading branch information
bjfish committed May 6, 2015
1 parent 9466e52 commit ae25618
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;
import jnr.constants.platform.Fcntl;
import org.jruby.RubyEncoding;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.core.RubyBasicObject;
Expand Down Expand Up @@ -106,6 +107,22 @@ public int open(RubyString path, int mode, int permission) {

}


@RubiniusPrimitive(name = "io_truncate", needsSelf = false)
public static abstract class IOTruncatePrimitiveNode extends RubiniusPrimitiveNode {

public IOTruncatePrimitiveNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

@Specialization
public int truncate(RubyString path, int offset) {
final String pathString = RubyEncoding.decodeUTF8(path.getByteList().getUnsafeBytes(), path.getByteList().getBegin(), path.getByteList().getRealSize());
return posix().truncate(pathString, offset);

This comment has been minimized.

Copy link
@nirvdrum

nirvdrum May 6, 2015

Contributor

I don't know if this is the cause of the issue you've reported, but the second argument to the POSIX call is the length, not the offset. You're passing a path, not a file descriptor, so there's no notion of position in the file.

This comment has been minimized.

Copy link
@nirvdrum

nirvdrum May 6, 2015

Contributor

Also, I'm not sure if we should be trying to decode the filename. The POSIX call just works on a char*. But I've never worked with C strings that weren't wchar or some similar wrapper, so I might have that wrong.

This comment has been minimized.

Copy link
@bjfish

bjfish May 6, 2015

Author Contributor

@nirvdrum yes I think I need to just rename offset, it's supposed to be length, not certain on the decoding either

}

}

@RubiniusPrimitive(name = "io_fnmatch", needsSelf = false)
public static abstract class IOFNMatchPrimitiveNode extends RubiniusPrimitiveNode {

Expand Down

1 comment on commit ae25618

@bjfish
Copy link
Contributor Author

@bjfish bjfish commented on ae25618 May 6, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nirvdrum It looks like truncate is working, I think the spec failures are an issue in the size method.

Please sign in to comment.