From 02416e33763a805bee869b9e5a72d4e7e4bdae7a Mon Sep 17 00:00:00 2001 From: Joshua Lock Date: Wed, 11 Nov 2020 15:53:07 +0000 Subject: [PATCH] updater: more optimal file length checking Rather than read to the end of the file in order to determin its size, use the whence value of seek() to move the file object's position to the end of the file, then the tell() method of the file object to read the current position in bytes. Co-authored-by: Jussi Kukkonen Signed-off-by: Joshua Lock --- tuf/client/updater.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tuf/client/updater.py b/tuf/client/updater.py index 7d1f06b5..ef34c906 100755 --- a/tuf/client/updater.py +++ b/tuf/client/updater.py @@ -1244,8 +1244,10 @@ def _check_file_length(self, file_object, trusted_file_length): None. """ - file_object.seek(0) - observed_length = len(file_object.read()) + # seek to the end of the file; that is offset 0 from the end of the file, + # represented by a whence value of 2 + file_object.seek(0, 2) + observed_length = file_object.tell() # Return and log a message if the length 'file_object' is equal to # 'trusted_file_length', otherwise raise an exception. A hard check