Skip to content

Commit

Permalink
[sonic_eeprom] If reading from what appears to be a corrupt cache fil…
Browse files Browse the repository at this point in the history
…e, delete file and read directly from EEPROM (sonic-net#10)

* [sonic_eeprom] If reading from what appears to be a corrupt cache file, delete file and read directly from EEPROM

* Refactor to eliminate recursion
  • Loading branch information
jleveque authored and lguohan committed Aug 10, 2018
1 parent d98600e commit 664d6cc
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions sonic_eeprom/eeprom_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,22 @@ def read_eeprom_bytes(self, byteCount, offset=0):
F = self.open_eeprom()
F.seek(self.s + offset)
o = F.read(byteCount)

# If we read from the cache file and the byte count isn't what we
# expect, the file may be corrupt. Delete it and try again, this
# time reading from the actual EEPROM.
if len(o) != byteCount and not self.cache_update_needed:
os.remove(self.cache_name)
self.cache_update_needed = True
F.close()
F = self.open_eeprom()
F.seek(self.s + offset)
o = F.read(byteCount)

if len(o) != byteCount:
raise RuntimeError("expected to read %d bytes from %s, " \
%(byteCount, self.p) +
"but only read %d" %(len(o)))
raise RuntimeError("Expected to read %d bytes from %s, "
% (byteCount, self.p) +
"but only read %d" % (len(o)))
F.close()
return o

Expand Down

0 comments on commit 664d6cc

Please sign in to comment.