Skip to content

Commit 02416e3

Browse files
joshuaglJussi Kukkonen
andcommitted
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 <jkukkonen@vmware.com> Signed-off-by: Joshua Lock <jlock@vmware.com>
1 parent ad1335b commit 02416e3

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

tuf/client/updater.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,8 +1244,10 @@ def _check_file_length(self, file_object, trusted_file_length):
12441244
None.
12451245
"""
12461246

1247-
file_object.seek(0)
1248-
observed_length = len(file_object.read())
1247+
# seek to the end of the file; that is offset 0 from the end of the file,
1248+
# represented by a whence value of 2
1249+
file_object.seek(0, 2)
1250+
observed_length = file_object.tell()
12491251

12501252
# Return and log a message if the length 'file_object' is equal to
12511253
# 'trusted_file_length', otherwise raise an exception. A hard check

0 commit comments

Comments
 (0)