Skip to content

Commit

Permalink
Add _find_bin_for_hash() helper to repository_tool
Browse files Browse the repository at this point in the history
Add a helper function to determine the name of a bin that a hashed
targetfile will be delegated to.

Based sketches by Lukas Puehringer in issues theupdateframework#994 & theupdateframework#995

Signed-off-by: Joshua Lock <jlock@vmware.com>
  • Loading branch information
joshuagl committed Mar 26, 2020
1 parent eb01374 commit 1b7a8ff
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions tuf/repository_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2824,6 +2824,59 @@ def _get_hash(target_filepath):



def _create_bin_name(low, high, prefix_len):
"""
<Purpose>
Create a string name of a delegated hash bin, where name will be a range of
zero-padded (up to prefix_len) strings i.e. for low=00, high=07,
prefix_len=3 the returned name would be '000-007'.
"""
if low == high:
return "{low:0{len}x}".format(low=low, len=prefix_len)

return "{low:0{len}x}-{high:0{len}x}".format(low=low, high=high,
len=prefix_len)





def _find_bin_for_hash(path_hash, number_of_bins):
"""
<Purpose>
For a given hashed filename, path_hash, calculate the name of a hashed bin
into which this file would be delegated given number_of_bins bins are in
use.
<Arguments>
path_hash:
The hash of the target file's path
number_of_bins:
The number of hashed_bins in use
<Returns>
The name of the hashed bin path_hash would be binned into.
"""

prefix_len = len("{:x}".format(number_of_bins - 1))
prefix_count = 16 ** prefix_len

if prefix_count % number_of_bins != 0:
raise securesystemslib.exceptions.Error('The "number_of_bins" argument'
' must be a power of 2.')

bin_size = prefix_count // number_of_bins
prefix = int(path_hash[:prefix_len], 16)

low = prefix - (prefix % bin_size)
high = (low + bin_size - 1)

return _create_bin_name(low, high, prefix_len)





def create_new_repository(repository_directory, repository_name='default'):
Expand Down

0 comments on commit 1b7a8ff

Please sign in to comment.