Skip to content

Commit

Permalink
Close file descriptors when exception is raised (#18)
Browse files Browse the repository at this point in the history
Closes: #17
  • Loading branch information
dweil authored and xavierleroy committed Mar 19, 2019
1 parent f21afb0 commit 0e3fa45
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
6 changes: 5 additions & 1 deletion gzip.ml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ let open_in_chan ic =
in_crc = Int32.zero }

let open_in filename =
open_in_chan (Pervasives.open_in_bin filename)
let ic = Pervasives.open_in_bin filename in
try
open_in_chan ic
with exn ->
Pervasives.close_in ic; raise exn

let read_byte iz =
if iz.in_avail = 0 then begin
Expand Down
41 changes: 22 additions & 19 deletions zip.ml
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ let read_ecd filename ic =
let newlen = min (toread + len) 256 in
(* Search for magic number *)
let ofs = strrstr "PK\005\006" buf 0 newlen in
if ofs < 0 || newlen < 22 ||
(let comment_len =
if ofs < 0 || newlen < 22 ||
(let comment_len =
(Char.code (Bytes.get buf (ofs + 20)))
lor ((Char.code (Bytes.get buf (ofs + 21))) lsl 8) in
Int64.(add newpos (of_int (ofs + 22 + comment_len))) <> filelen) then
Expand Down Expand Up @@ -219,16 +219,19 @@ let read_cd filename ic cd_entries cd_offset cd_bound =

let open_in filename =
let ic = Pervasives.open_in_bin filename in
let (cd_entries, cd_size, cd_offset, cd_comment) = read_ecd filename ic in
let entries =
read_cd filename ic cd_entries cd_offset (Int32.add cd_offset cd_size) in
let dir = Hashtbl.create (cd_entries / 3) in
List.iter (fun e -> Hashtbl.add dir e.filename e) entries;
{ if_filename = filename;
if_channel = ic;
if_entries = entries;
if_directory = dir;
if_comment = cd_comment }
try
let (cd_entries, cd_size, cd_offset, cd_comment) = read_ecd filename ic in
let entries =
read_cd filename ic cd_entries cd_offset (Int32.add cd_offset cd_size) in
let dir = Hashtbl.create (cd_entries / 3) in
List.iter (fun e -> Hashtbl.add dir e.filename e) entries;
{ if_filename = filename;
if_channel = ic;
if_entries = entries;
if_directory = dir;
if_comment = cd_comment }
with exn ->
Pervasives.close_in ic; raise exn

(* Close a ZIP file opened for reading *)

Expand Down Expand Up @@ -307,7 +310,7 @@ let read_entry ifile e =
Bytes.unsafe_to_string res
with End_of_file ->
raise (Error(ifile.if_filename, e.filename, "truncated data"))

(* Write the contents of an entry into an out channel *)

let copy_entry_to_channel ifile e oc =
Expand Down Expand Up @@ -346,7 +349,7 @@ let copy_entry_to_channel ifile e oc =
raise (Error(ifile.if_filename, e.filename, "CRC mismatch"))
with End_of_file ->
raise (Error(ifile.if_filename, e.filename, "truncated data"))

(* Write the contents of an entry to a file *)

let copy_entry_to_file ifile e outfilename =
Expand Down Expand Up @@ -471,7 +474,7 @@ let add_data_descriptor ofile crc compr_size uncompr_size entry =

(* Add an entry with the contents of a string *)

let add_entry data ofile ?(extra = "") ?(comment = "")
let add_entry data ofile ?(extra = "") ?(comment = "")
?(level = 6) ?(mtime = Unix.time()) name =
let e = add_entry_header ofile extra comment level mtime name in
let crc = Zlib.update_crc_string Int32.zero data 0 (String.length data) in
Expand Down Expand Up @@ -502,7 +505,7 @@ let add_entry data ofile ?(extra = "") ?(comment = "")

(* Add an entry with the contents of an in channel *)

let copy_channel_to_entry ic ofile ?(extra = "") ?(comment = "")
let copy_channel_to_entry ic ofile ?(extra = "") ?(comment = "")
?(level = 6) ?(mtime = Unix.time()) name =
let e = add_entry_header ofile extra comment level mtime name in
let crc = ref Int32.zero in
Expand All @@ -529,7 +532,7 @@ let copy_channel_to_entry ic ofile ?(extra = "") ?(comment = "")
crc := Zlib.update_crc !crc buf 0 r;
in_pos := !in_pos + r;
r)
(fun buf n ->
(fun buf n ->
output ofile.of_channel buf 0 n;
out_pos := !out_pos + n);
(!out_pos, !in_pos)
Expand All @@ -540,11 +543,11 @@ let copy_channel_to_entry ic ofile ?(extra = "") ?(comment = "")

(* Add an entry with the contents of a file *)

let copy_file_to_entry infilename ofile ?(extra = "") ?(comment = "")
let copy_file_to_entry infilename ofile ?(extra = "") ?(comment = "")
?(level = 6) ?mtime name =
let ic = open_in_bin infilename in
let mtime' =
match mtime with
match mtime with
Some t -> mtime
| None ->
try Some((Unix.stat infilename).Unix.st_mtime)
Expand Down

0 comments on commit 0e3fa45

Please sign in to comment.