Skip to content

Commit

Permalink
Fix for IO errors due to non-closing of file handles in Windows OS.
Browse files Browse the repository at this point in the history
Special handling of BufferedStreams removed.
Intermediate decoders will close the input stream as buffer sources are no
longer used.
  • Loading branch information
sambitdash committed May 24, 2018
1 parent 1215fc4 commit 1d9f9d0
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 48 deletions.
4 changes: 1 addition & 3 deletions src/CosDoc.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using ..Common

import Base: close

export CosDoc,
cosDocOpen,
cosDocClose,
Expand Down Expand Up @@ -76,7 +74,7 @@ opened the document by 'cosDocOpen'. Documents opened with `pdDocOpen` do not ne
this method.
"""
function cosDocClose(doc::CosDocImpl)
close(doc.ps)
util_close(doc.ps)
for path in doc.tmpfiles
rm(path)
end
Expand Down
4 changes: 2 additions & 2 deletions src/CosObjStream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function read_xref_stream(xrefstm::CosObject,

input = get(xrefstm)
data = read(input)
close(input)
util_close(input)
datasize = length(data)

w_n = get(w,true) #This size is 3
Expand Down Expand Up @@ -121,6 +121,6 @@ function cosObjectStreamGetObject(stm::CosObjectStream,
obj = parse_value(ps)
return obj
finally
close(ps)
util_close(ps)
end
end
16 changes: 8 additions & 8 deletions src/CosReader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import Base: peek

#This function is for testing only
function parse_data(filename)
ps=IOStream(util_open(filename,"r"))
try
while(!eof(ps))
println(parse_value(ps))
chomp_space!(ps)
ps=util_open(filename,"r")
try
while(!eof(ps))
println(parse_value(ps))
chomp_space!(ps)
end
finally
util_close(ps)
end
finally
close(ps)
end
end

"""
Expand Down
11 changes: 8 additions & 3 deletions src/CosStream.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Base:eof,close
import Base: eof

export cosStreamRemoveFilters,
merge_streams,
Expand All @@ -17,6 +17,7 @@ end

function decode_flate(input, parms)
io = inflate(input)
util_close(input)
return apply_flate_params(io, parms)
end

Expand Down Expand Up @@ -46,7 +47,7 @@ function cosStreamRemoveFilters(stm::CosObject)
if (filters != CosNull)
bufstm = decode(stm)
data = read(bufstm)
close(bufstm)
util_close(bufstm)
filename = get(stm, CosName("F"))
write(filename |> get |> String, data)
set!(stm, CosName("FFilter"), CosNull)
Expand All @@ -64,7 +65,7 @@ function merge_streams(stms::CosArray)
for stm in v
bufstm = decode(stm)
data = read(bufstm)
close(bufstm)
util_close(bufstm)
write(io, data)
end
return ret
Expand Down Expand Up @@ -173,6 +174,7 @@ function apply_flate_params(io::IO, pred::Int, col::Int)
write(iob, curr)
nline += 1
end
util_close(io)
return seekstart(iob)
end

Expand All @@ -192,6 +194,7 @@ function decode_rle(input::IO)
end
b = read(input, UInt8)
end
util_close(input)
return seekstart(iob)
end

Expand All @@ -211,6 +214,7 @@ function decode_asciihex(input::IO)
end
k = !k
end
util_close(input)
resize!(data, j)
return IOBuffer(data)
end
Expand Down Expand Up @@ -270,6 +274,7 @@ function decode_ascii85(input::IO)
end
j += (k - 1)
end
util_close(input)
resize!(data, j)
return IOBuffer(data)
end
5 changes: 3 additions & 2 deletions src/PDFonts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,15 @@ function merge_encoding!(fum::FontUnicodeMapping, doc::CosDoc, font::CosObject)
merge_encoding!(fum, toUnicode, doc, font)
end

function merge_encoding!(fum::FontUnicodeMapping, cmap::CosIndirectObject{CosStream},
function merge_encoding!(fum::FontUnicodeMapping,
cmap::CosIndirectObject{CosStream},
doc::CosDoc, font::CosObject)
stm_cmap = get(cmap)
try
fum.cmap = read_cmap(stm_cmap)
fum.hasCMap = true
finally
close(stm_cmap)
util_close(stm_cmap)
end
return fum
end
Expand Down
2 changes: 1 addition & 1 deletion src/PDPage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function load_page_objects(page::PDPageImpl, stm::CosObject)
try
load_objects(get(page.content_objects), bufstm)
finally
close(bufstm)
util_close(bufstm)
end
return nothing
end
Expand Down
2 changes: 1 addition & 1 deletion src/Utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ end

get_tempfilepath()=mktemp(get_tempdir())
util_open(filename, mode)=open(filename, mode)
util_close(handle::IOStream)=close(handle)
util_close(handle)=close(handle)
utilPrintOpenFiles()=[]

import Base: zero
Expand Down
60 changes: 32 additions & 28 deletions test/debugIO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,56 @@ are not affected.

const DEBUG=false

if DEBUG
@static if DEBUG

import PDFIO.Common: get_tempfilepath,
util_open, util_close, utilPrintOpenFiles

import Base: close

IODebug=[0,Vector{Tuple{AbstractString,IOStream}}()]
IODebug=[0,Vector{Tuple{AbstractString,IO}}()]

function get_tempfilepath()
global IODebug
IODebug[1]+=1
path = joinpath(get_tempdir(), string(IODebug[1]))
return (path, util_open(path,"w"))
global IODebug
IODebug[1]+=1
path = joinpath(get_tempdir(), string(IODebug[1]))
return (path, util_open(path,"w"))
end

function util_open(filename, mode)
global IODebug
io=open(filename, mode)
@printf("Opening file: %s\n",filename)
push!(IODebug[2], (filename,io))
return io
global IODebug
io=open(filename, mode)
@printf "Opening file: %s\n" filename
push!(IODebug[2], (filename, io))
return io
end

function util_close(handle::IOStream)
global IODebug
idx=1
for file in IODebug[2]
if (handle === file[2])
@printf("Closing file: %s, %d\n", file[1],idx)
close(handle)
deleteat!(IODebug[2],idx)
return
global IODebug
idx=1
for file in IODebug[2]
if (handle === file[2])
@printf "Closing file: %s, %d\n" file[1] idx
close(handle)
deleteat!(IODebug[2],idx)
return
end
idx+=1
end
idx+=1
end
error("IO handle not found")
error("IO handle not found")
end

util_close(handle::IOBuffer)=close(handle)

function utilPrintOpenFiles()
global IODebug
println("The following files are opened currently:")
println("-----------------------------------------")
println(IODebug[2])
println("-----------------------------------------")
IODebug[2]
global IODebug
println("The following files are opened currently:")
println("-----------------------------------------")
for f in IODebug[2]
println(f)
end
println("-----------------------------------------")
return IODebug[2]
end

end
Expand Down

0 comments on commit 1d9f9d0

Please sign in to comment.