Skip to content

Commit

Permalink
Allow opening archive from io
Browse files Browse the repository at this point in the history
This is discrete from fd because if we pass an fd into libarchive then
the caller is reponsible for making sure the opened of the fd survives
for the lifetime of the archive instance, but passing in an IO and
holding a reference in an instance variable ties the lifetime to the
archive instance and allows GC to work as per normal.

Signed-off-by: Samuel Cochran <[email protected]>
  • Loading branch information
sj26 committed Aug 4, 2023
1 parent a6f8cc4 commit 3bafb2b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/ffi-libarchive/archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ def self.read_open_fd(fd, command = nil, &block)
Reader.open_fd fd, command, &block
end

def self.read_open_io(io, command = nil, &block)
Reader.open_io io, command, &block
end

def self.read_open_memory(string, command = nil, &block)
Reader.open_memory string, command, &block
end
Expand Down
22 changes: 22 additions & 0 deletions lib/ffi-libarchive/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ def self.open_fd(fd, command = nil, strip_components: 0)
end
end

def self.open_io(io, command = nil, strip_components: 0)
if block_given?
reader = open_io io, command, strip_components: strip_components
begin
yield reader
ensure
reader.close
end
else
new io: io, command: command, strip_components: strip_components
end
end

def self.open_memory(string, command = nil)
if block_given?
reader = open_memory string, command
Expand Down Expand Up @@ -81,6 +94,15 @@ def initialize(params = {})
raise Error, @archive if C.archive_read_open_filename(archive, params[:file_name], 1024) != C::OK
when params[:fd]
raise Error, @archive if C.archive_read_open_fd(archive, params[:fd], 1024) != C::OK
when params[:io]
# Hold a reference to the IO so Ruby understands we're using it while this object is alive
@io = params[:io]
raise ArgumentError, "Expected io to respond to #fileno" unless @io.respond_to?(:fileno)

fd = @io.fileno
raise ArgumentError, "Expected io.fileno to return an fd but got nil" if fd.nil?

raise Error, @archive if C.archive_read_open_fd(archive, fd, 1024) != C::OK
when params[:memory]
str = params[:memory]
@data = FFI::MemoryPointer.new(str.bytesize + 1)
Expand Down
41 changes: 41 additions & 0 deletions test/sets/ts_read.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "ffi-libarchive"
require "stringio"
require "tmpdir"
require "test/unit"

Expand Down Expand Up @@ -75,6 +76,46 @@ def test_read_tar_gz_from_fd_with_external_gunzip
end
end

def test_read_tar_gz_from_io
return if windows?

File.open("data/test.tar.gz", "r") do |f|
Archive.read_open_io(f) do |ar|
verify_content(ar)
end
end
end

def test_read_tar_gz_from_io_with_external_gunzip
return if windows?

File.open("data/test.tar.gz", "r") do |f|
Archive.read_open_io(f, "gunzip") do |ar|
verify_content(ar)
end
end
end

def test_read_tar_gz_from_io_no_fileno
return if windows?

string = File.read("data/test.tar.gz")

assert_raise ArgumentError, "Expected io to respond to #fileno" do
Archive.read_open_io(string)
end
end

def test_read_tar_gz_from_string_io
return if windows?

io = StringIO.new(File.read("data/test.tar.gz"))

assert_raise ArgumentError, "Expected io.fileno to return an fd but got nil" do
Archive.read_open_io(io)
end
end

def test_read_tar_gz_from_memory
return if windows?

Expand Down

0 comments on commit 3bafb2b

Please sign in to comment.