Skip to content

Commit

Permalink
Fix: IO::FileDescriptor#seek from current position
Browse files Browse the repository at this point in the history
Trying to seek from the current position failed because the system
position of a buffered IO is different from the actual position in
the stream.
  • Loading branch information
ysbaddaden committed Jun 13, 2017
1 parent ca47207 commit a862c51
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
7 changes: 7 additions & 0 deletions spec/std/file_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,13 @@ describe "File" do
file.gets(4).should eq("ello")
end

it "seeks from the current position" do
file = File.new("#{__DIR__}/data/test_file.txt")
file.gets(5)
file.seek(-4, IO::Seek::Current)
file.tell.should eq(1)
end

it "raises if invoking seek with a closed file" do
file = File.new("#{__DIR__}/data/test_file.txt")
file.close
Expand Down
2 changes: 2 additions & 0 deletions src/io/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ class IO::FileDescriptor
check_open

flush
offset -= @in_buffer_rem.size if whence.current?
seek_value = LibC.lseek(@fd, offset, whence)

if seek_value == -1
raise Errno.new "Unable to seek"
end
Expand Down

0 comments on commit a862c51

Please sign in to comment.