Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Ensure that files are properly closed even on processing errors.
  • Loading branch information
scrapper committed Aug 12, 2017
1 parent d464353 commit 2cf7800
Showing 1 changed file with 52 additions and 48 deletions.
100 changes: 52 additions & 48 deletions lib/fit4ruby/FitFile.rb
Expand Up @@ -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

Expand All @@ -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
Expand Down

1 comment on commit 2cf7800

@ahmgeek
Copy link

@ahmgeek ahmgeek commented on 2cf7800 Aug 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.