1
+
1
2
#!/usr/bin/env python
2
3
3
4
# Copyright 2013 - 2017, New York University and the TUF contributors
@@ -2529,7 +2530,7 @@ def delegate_hashed_bins(self, list_of_targets, keys_of_hashed_bins,
2529
2530
securesystemslib .formats .ANYKEYLIST_SCHEMA .check_match (keys_of_hashed_bins )
2530
2531
tuf .formats .NUMBINS_SCHEMA .check_match (number_of_bins )
2531
2532
2532
- prefix_length , prefix_count , bin_size = _get_bin_numbers (number_of_bins )
2533
+ prefix_length , prefix_count , bin_size = repo_lib . get_bin_numbers (number_of_bins )
2533
2534
2534
2535
logger .info ('Creating hashed bin delegations.\n ' +
2535
2536
repr (len (list_of_targets )) + ' total targets.\n ' +
@@ -2543,7 +2544,7 @@ def delegate_hashed_bins(self, list_of_targets, keys_of_hashed_bins,
2543
2544
ordered_roles = []
2544
2545
for idx in range (0 , prefix_count , bin_size ):
2545
2546
high = idx + bin_size - 1
2546
- name = _create_bin_name (idx , high , prefix_length )
2547
+ name = repo_lib . create_bin_name (idx , high , prefix_length )
2547
2548
if bin_size == 1 :
2548
2549
target_hash_prefixes = [name ]
2549
2550
else :
@@ -2675,7 +2676,7 @@ def add_target_to_bin(self, target_filepath, number_of_bins=DEFAULT_NUM_BINS,
2675
2676
# TODO: check target_filepath is sane
2676
2677
2677
2678
path_hash = repo_lib .get_target_hash (target_filepath )
2678
- bin_name = _find_bin_for_hash (path_hash , number_of_bins )
2679
+ bin_name = repo_lib . find_bin_for_hash (path_hash , number_of_bins )
2679
2680
2680
2681
# Ensure the Targets object has delegated to hashed bins
2681
2682
if not self ._delegated_roles .get (bin_name , None ):
@@ -2737,7 +2738,7 @@ def remove_target_from_bin(self, target_filepath,
2737
2738
# TODO: check target_filepath is sane?
2738
2739
2739
2740
path_hash = repo_lib .get_target_hash (target_filepath )
2740
- bin_name = _find_bin_for_hash (path_hash , number_of_bins )
2741
+ bin_name = repo_lib . find_bin_for_hash (path_hash , number_of_bins )
2741
2742
2742
2743
# Ensure the Targets object has delegated to hashed bins
2743
2744
if not self ._delegated_roles .get (bin_name , None ):
@@ -2839,90 +2840,6 @@ def _keys_to_keydict(keys):
2839
2840
2840
2841
2841
2842
2842
-
2843
- def _create_bin_name (low , high , prefix_len ):
2844
- """
2845
- <Purpose>
2846
- Create a string name of a delegated hash bin, where name will be a range of
2847
- zero-padded (up to prefix_len) strings i.e. for low=00, high=07,
2848
- prefix_len=3 the returned name would be '000-007'.
2849
- """
2850
- if low == high :
2851
- return "{low:0{len}x}" .format (low = low , len = prefix_len )
2852
-
2853
- return "{low:0{len}x}-{high:0{len}x}" .format (low = low , high = high ,
2854
- len = prefix_len )
2855
-
2856
-
2857
-
2858
-
2859
-
2860
- def _get_bin_numbers (number_of_bins ):
2861
- """
2862
- Given the desired number of bins (number_of_bins) calculate the prefix length
2863
- (prefix_length), total number of prefixes (prefix_count) and the number of
2864
- prefixes to be stored in each bin (bin_size).
2865
- Example: number_of_bins = 32
2866
- prefix_length = 2
2867
- prefix_count = 256
2868
- bin_size = 8
2869
- That is, each of the 32 hashed bins are responsible for 8 hash prefixes, i.e.
2870
- 00-07, 08-0f, ..., f8-ff.
2871
- """
2872
- # Convert 'number_of_bins' to hexadecimal and determine the number of
2873
- # hexadecimal digits needed by each hash prefix
2874
- prefix_length = len ("{:x}" .format (number_of_bins - 1 ))
2875
- # Calculate the total number of hash prefixes (e.g., 000 - FFF total values)
2876
- prefix_count = 16 ** prefix_length
2877
- # Determine how many prefixes to assign to each bin
2878
- bin_size = prefix_count // number_of_bins
2879
-
2880
- # For simplicity, ensure that 'prefix_count' (16 ^ n) can be evenly
2881
- # distributed over 'number_of_bins' (must be 2 ^ n). Each bin will contain
2882
- # (prefix_count / number_of_bins) hash prefixes.
2883
- if prefix_count % number_of_bins != 0 :
2884
- # Note: x % y != 0 does not guarantee that y is not a power of 2 for
2885
- # arbitrary x and y values. However, due to the relationship between
2886
- # number_of_bins and prefix_count, it is true for them.
2887
- raise securesystemslib .exceptions .Error ('The "number_of_bins" argument'
2888
- ' must be a power of 2.' )
2889
-
2890
- return prefix_length , prefix_count , bin_size
2891
-
2892
-
2893
-
2894
-
2895
- def _find_bin_for_hash (path_hash , number_of_bins ):
2896
- """
2897
- <Purpose>
2898
- For a given hashed filename, path_hash, calculate the name of a hashed bin
2899
- into which this file would be delegated given number_of_bins bins are in
2900
- use.
2901
-
2902
- <Arguments>
2903
- path_hash:
2904
- The hash of the target file's path
2905
-
2906
- number_of_bins:
2907
- The number of hashed_bins in use
2908
-
2909
- <Returns>
2910
- The name of the hashed bin path_hash would be binned into.
2911
- """
2912
-
2913
- prefix_length , _ , bin_size = _get_bin_numbers (number_of_bins )
2914
-
2915
- prefix = int (path_hash [:prefix_length ], 16 )
2916
-
2917
- low = prefix - (prefix % bin_size )
2918
- high = (low + bin_size - 1 )
2919
-
2920
- return _create_bin_name (low , high , prefix_length )
2921
-
2922
-
2923
-
2924
-
2925
-
2926
2843
def create_new_repository (repository_directory , repository_name = 'default' ,
2927
2844
storage_backend = None ):
2928
2845
"""
0 commit comments