From 4f7639c6623ba3dd295ba26849da342161d19f58 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Thu, 9 Aug 2018 21:45:52 +0000 Subject: [PATCH 1/2] [sonic_eeprom] If reading from what appears to be a corrupt cache file, delete file and read directly from EEPROM --- sonic_eeprom/eeprom_base.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/sonic_eeprom/eeprom_base.py b/sonic_eeprom/eeprom_base.py index 27d7b5dfe..dacd43412 100644 --- a/sonic_eeprom/eeprom_base.py +++ b/sonic_eeprom/eeprom_base.py @@ -233,9 +233,17 @@ def read_eeprom_bytes(self, byteCount, offset=0): 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))) + # If we read from the cache file, it may be corrupt. Delete it + # and try again, this time reading from the actual EEPROM. + if not self.cache_update_needed: + os.remove(self.cache_name) + self.cache_update_needed = True + F.close() + return self.read_eeprom_bytes(byteCount, offset) + else: + raise RuntimeError("Expected to read %d bytes from %s, " \ + %(byteCount, self.p) + + "but only read %d" %(len(o))) F.close() return o From d4af7e5e4a1bd1d17b8be2b58e1236b687935a2c Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Thu, 9 Aug 2018 23:22:41 +0000 Subject: [PATCH 2/2] Refactor to eliminate recursion --- sonic_eeprom/eeprom_base.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/sonic_eeprom/eeprom_base.py b/sonic_eeprom/eeprom_base.py index dacd43412..faa9c071b 100644 --- a/sonic_eeprom/eeprom_base.py +++ b/sonic_eeprom/eeprom_base.py @@ -232,18 +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: - # If we read from the cache file, it may be corrupt. Delete it - # and try again, this time reading from the actual EEPROM. - if not self.cache_update_needed: - os.remove(self.cache_name) - self.cache_update_needed = True - F.close() - return self.read_eeprom_bytes(byteCount, offset) - else: - 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