Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Commit

Permalink
Fix. Propagate error from reader.
Browse files Browse the repository at this point in the history
```lua
function reader() return nil, "some error" end
local ok, err = arc:write('test.txt', fileDesc, reader)
assert(ok == nil)
assert(err == "some error")
-- !!! But zip archive contain test.txt file with zero size
```
  • Loading branch information
moteus committed Feb 26, 2014
1 parent 577d29d commit d316026
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lua/ZipWriter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ function ZipWriter:write(
size = 0
csize = 0

local reader_error -- error from reader (e.g. access error to file)
if fileDesc.isfile then
-- create stream for file data
local stream = ZipWriter_as_stream(self)
Expand Down Expand Up @@ -730,6 +731,7 @@ function ZipWriter:write(
size = size + #chunk
chunk, ctx = reader(ctx)
end
reader_error = ctx
end

csize = stream:close()
Expand Down Expand Up @@ -806,6 +808,9 @@ function ZipWriter:write(
utfpath, cdextra, utfcomment
)
table.insert(self.private_.headers, cdh)

if reader_error then return nil, reader_error end

return true
end

Expand Down
42 changes: 42 additions & 0 deletions test/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,48 @@ test_best = Make('BEST')

end

local _ENV = TEST_CASE'ZipWriter reader interface' do

function test_context()
local out = Stream:new()
local writer = ZipWriter.new()
writer:open_stream(out)

local function make_reader()
local ctx = {}
local i = 0
return function(o)
if i == 0 then
i = 1
assert_nil(o)
return "data", ctx
end
assert_equal(ctx, o)
end
end

assert_true(writer:write('test.txt', fileDesc, make_reader()))
assert_equal(1, writer:close())
end

function test_error()
local out = Stream:new()
local writer = ZipWriter.new()
writer:open_stream(out)

local ERR = {}

local function reader()
return nil, ERR
end

local ok, err = assert_nil(writer:write('test.txt', fileDesc, reader))
assert_equal(ERR, err)
assert_equal(1, writer:close())
end

end

local _ENV = TEST_CASE'ZipWriter sink' do

local function Make(lvl) return function()
Expand Down

0 comments on commit d316026

Please sign in to comment.