Permalink
Browse files

Ensure that files are properly closed even on processing errors.

  • Loading branch information...
scrapper committed Aug 12, 2017
1 parent d464353 commit 2cf7800b75d74e3b7b15b4aadc738bebcdca121f
Showing with 52 additions and 48 deletions.
  1. +52 −48 lib/fit4ruby/FitFile.rb
@@ -38,38 +38,40 @@ def read(file_name, filter = nil)
end
entities = []
while !io.eof?
offset = io.pos
header = FitHeader.read(io)
header.check
check_crc(io, io.pos, offset + header.end_pos)
entity = FitFileEntity.new
# This Array holds the raw data of the records that may be needed to
# dump a human readable form of the FIT file.
records = []
# This hash will hold a counter for each record type. The counter is
# incremented each time the corresponding record type is found.
record_counters = Hash.new { 0 }
while io.pos < offset + header.end_pos
record = FitRecord.new(definitions)
record.read(io, entity, filter, record_counters)
records << record if filter
begin
while !io.eof?
offset = io.pos
header = FitHeader.read(io)
header.check
check_crc(io, io.pos, offset + header.end_pos)
entity = FitFileEntity.new
# This Array holds the raw data of the records that may be needed to
# dump a human readable form of the FIT file.
records = []
# This hash will hold a counter for each record type. The counter is
# incremented each time the corresponding record type is found.
record_counters = Hash.new { 0 }
while io.pos < offset + header.end_pos
record = FitRecord.new(definitions)
record.read(io, entity, filter, record_counters)
records << record if filter
end
# Skip the 2 CRC bytes
io.seek(2, :CUR)
header.dump if filter && filter.record_numbers.nil?
dump_records(records) if filter
entity.check
entities << entity
end
# Skip the 2 CRC bytes
io.seek(2, :CUR)
header.dump if filter && filter.record_numbers.nil?
dump_records(records) if filter
entity.check
entities << entity
ensure
io.close
end
io.close
entities[0].top_level_record
end
@@ -80,25 +82,27 @@ def write(file_name, top_level_record)
Log.fatal "Cannot open FIT file '#{file_name}': #{e.message}"
end
# Create a header object, but don't yet write it into the file.
header = FitHeader.new
start_pos = header.header_size
# Move the pointer behind the header section.
io.seek(start_pos)
id_mapper = FitMessageIdMapper.new
top_level_record.write(io, id_mapper)
end_pos = io.pos
crc = write_crc(io, start_pos, end_pos)
# Complete the data of the header section and write it at the start of
# the file.
header.data_size = end_pos - start_pos
header.crc = crc
io.seek(0)
header.write(io)
io.close
begin
# Create a header object, but don't yet write it into the file.
header = FitHeader.new
start_pos = header.header_size
# Move the pointer behind the header section.
io.seek(start_pos)
id_mapper = FitMessageIdMapper.new
top_level_record.write(io, id_mapper)
end_pos = io.pos
crc = write_crc(io, start_pos, end_pos)
# Complete the data of the header section and write it at the start of
# the file.
header.data_size = end_pos - start_pos
header.crc = crc
io.seek(0)
header.write(io)
ensure
io.close
end
end
private

1 comment on commit 2cf7800

@ahmgeek

This comment has been minimized.

ahmgeek commented on 2cf7800 Aug 16, 2017

I guess that's inspired by my closed PR 😛 👍

Please sign in to comment.