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 6affbd6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
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
8 changes: 7 additions & 1 deletion src/io/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@ class IO::FileDescriptor
check_open

flush
seek_value = LibC.lseek(@fd, offset, whence)

if whence.current?
seek_value = LibC.lseek(@fd, tell + offset, Seek::Set)
else
seek_value = LibC.lseek(@fd, offset, whence)
end

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

0 comments on commit 6affbd6

Please sign in to comment.