Skip to content

Commit

Permalink
[Truffle] Add File.truncate.
Browse files Browse the repository at this point in the history
  • Loading branch information
bjfish committed May 6, 2015
1 parent 884428c commit cff40b3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
13 changes: 0 additions & 13 deletions spec/truffle/tags/core/file/truncate_tags.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,2 @@
fails:File.truncate truncates a file
fails:File.truncate truncate a file size to 0
fails:File.truncate truncate a file size to 5
fails:File.truncate truncates to a larger file size than the original file
fails:File.truncate truncates to the same size as the original file
fails:File.truncate raises an Errno::EINVAL if the length argument is not valid
fails:File.truncate accepts an object that has a #to_path method
fails:File#truncate does not move the file write pointer to the specified byte offset
fails:File#truncate does not move the file read pointer to the specified byte offset
fails:File#truncate truncates a file
fails:File#truncate truncates a file size to 0
fails:File#truncate truncates a file size to 5
fails:File#truncate truncates a file to a larger size than the original file
fails:File#truncate truncates a file to the same size as the original file
fails:File#truncate raises an IOError if file is closed
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,41 @@ public IOTruncatePrimitiveNode(RubyContext context, SourceSection sourceSection)
}

@Specialization
public int truncate(RubyString path, int offset) {
public int truncate(RubyString path, int length) {
return truncate(path, (long) length);
}

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

}

@RubiniusPrimitive(name = "io_ftruncate")
public static abstract class IOFTruncatePrimitiveNode extends RubiniusPrimitiveNode {

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

@Specialization
public int ftruncate(VirtualFrame frame, RubyBasicObject io, int length) {
return ftruncate(frame, io, (long) length);
}

@Specialization
public int ftruncate(VirtualFrame frame, RubyBasicObject io, long length) {
final int fd = (int) rubyWithSelf(frame, io, "@descriptor");
return posix().ftruncate(fd, length);
}

}




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

Expand Down

2 comments on commit cff40b3

@bjfish
Copy link
Contributor Author

@bjfish bjfish commented on cff40b3 May 6, 2015

Choose a reason for hiding this comment

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

@nirvdrum Specs started working too , not sure why but that's good.

@nirvdrum
Copy link
Contributor

Choose a reason for hiding this comment

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

You might have not gotten an updated maven snapshot. If you don't force it with -U, I find it's rather random about when to check for snapshot updates.

Please sign in to comment.