Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow opening archive from IO instance #80

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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