Skip to content

Commit

Permalink
Optimize the calculation of length and hashes
Browse files Browse the repository at this point in the history
After we had given the option to use or not hashes and length
for timestamp and snapshot roles, it's good to make sure we are
calculating them only when they are needed.

This optimization could be important for the bigger tuf adopters.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
  • Loading branch information
MVrachev committed Aug 17, 2020
1 parent 8577039 commit b0dda36
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions tuf/repository_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,28 @@ def _generate_targets_fileinfo(target_files, targets_directory,



def _get_hashes_and_length_if_needed(use_length, use_hashes, full_file_path,
storage_backend):
"""
Calculate length and hashes only if they are required.
"""

length = None
hashes = None
# We want to make sure we are calculating length and hashes only when
# they are needed. Otherwise, for adoptors of tuf with
# lots of delegations, this will cause unnecessary overhead.
if use_length:
length = securesystemslib.util.get_file_length(full_file_path, storage_backend)

if use_hashes:
hashes = securesystemslib.util.get_file_hashes(full_file_path,
tuf.settings.FILE_HASH_ALGORITHMS, storage_backend)

return length, hashes



def generate_snapshot_metadata(metadata_directory, version, expiration_date,
storage_backend, consistent_snapshot=False,
repository_name='default', use_length=False, use_hashes=False):
Expand Down Expand Up @@ -1604,12 +1626,8 @@ def generate_snapshot_metadata(metadata_directory, version, expiration_date,
# Targets, and all delegated roles of the repository.
fileinfodict = {}

length, hashes = securesystemslib.util.get_file_details(
os.path.join(metadata_directory, TARGETS_FILENAME),
tuf.settings.FILE_HASH_ALGORITHMS, storage_backend)

length = (use_length and length) or None
hashes = (use_hashes and hashes) or None
length, hashes = _get_hashes_and_length_if_needed(use_length, use_hashes,
os.path.join(metadata_directory, TARGETS_FILENAME), storage_backend)

targets_role = TARGETS_FILENAME[:-len(METADATA_EXTENSION)]

Expand Down Expand Up @@ -1645,18 +1663,8 @@ def generate_snapshot_metadata(metadata_directory, version, expiration_date,
if tuf.roledb.role_exists(rolename, repository_name) and \
rolename not in tuf.roledb.TOP_LEVEL_ROLES:

length = None
hashes = None
# We want to make sure we are calculating length and hashes only when
# at least one of them is needed. Otherwise, for adoptors of tuf with
# lots of delegations, this will cause unnecessary overhead.
if use_length or use_hashes:
length, hashes = securesystemslib.util.get_file_details(
os.path.join(metadata_directory, metadata_filename),
tuf.settings.FILE_HASH_ALGORITHMS)

length = (use_length and length) or None
hashes = (use_hashes and hashes) or None
length, hashes = _get_hashes_and_length_if_needed(use_length, use_hashes,
os.path.join(metadata_directory, metadata_filename), storage_backend)

file_version = get_metadata_versioninfo(rolename,
repository_name)
Expand Down Expand Up @@ -1752,11 +1760,8 @@ def generate_timestamp_metadata(snapshot_file_path, version, expiration_date,

snapshot_fileinfo = {}

length, hashes = securesystemslib.util.get_file_details(snapshot_file_path,
tuf.settings.FILE_HASH_ALGORITHMS, storage_backend)

length = (use_length and length) or None
hashes = (use_hashes and hashes) or None
length, hashes = _get_hashes_and_length_if_needed(use_length, use_hashes,
snapshot_file_path, storage_backend)

snapshot_filename = os.path.basename(snapshot_file_path)
# Retrieve the versioninfo of the Snapshot metadata file.
Expand Down

0 comments on commit b0dda36

Please sign in to comment.