@@ -131,12 +131,13 @@ def _generate_and_write_metadata(rolename, metadata_filename,
131131
132132
133133 elif rolename == 'snapshot' :
134+ metadata , fileinfodict = generate_snapshot_metadata (metadata_directory ,
135+ roleinfo ['version' ], roleinfo ['expires' ],
136+ storage_backend , consistent_snapshot , repository_name ,
137+ use_length = use_snapshot_length , use_hashes = use_snapshot_hashes )
138+
134139 if snapshot_merkle :
135- root , leaves , metadata = generate_snapshot_metadata (metadata_directory ,
136- roleinfo ['version' ], roleinfo ['expires' ],
137- storage_backend , consistent_snapshot , repository_name ,
138- use_length = use_snapshot_length , use_hashes = use_snapshot_hashes ,
139- snapshot_merkle = True )
140+ root , leaves = _build_merkle_tree (fileinfodict )
140141
141142 # Add the merkle tree root hash to the timestamp roleinfo
142143 timestamp_roleinfo = tuf .roledb .get_roleinfo ('timestamp' , repository_name )
@@ -147,12 +148,6 @@ def _generate_and_write_metadata(rolename, metadata_filename,
147148
148149 _write_merkle_paths (root , leaves , storage_backend , metadata_directory )
149150
150- else :
151- metadata = generate_snapshot_metadata (metadata_directory ,
152- roleinfo ['version' ], roleinfo ['expires' ],
153- storage_backend , consistent_snapshot , repository_name ,
154- use_length = use_snapshot_length , use_hashes = use_snapshot_hashes )
155-
156151
157152 _log_warning_if_expires_soon (SNAPSHOT_FILENAME , roleinfo ['expires' ],
158153 SNAPSHOT_EXPIRES_WARN_SECONDS )
@@ -1693,6 +1688,12 @@ def _write_merkle_paths(root, leaves, storage_backend, merkle_directory):
16931688 # the client and used for verification of the tree. For each
16941689 # step in the path, keep track of both the sibling node and
16951690 # Whether this is a left or a right child.
1691+
1692+ # Before writing each leaf, make sure the storage_backend
1693+ # is instantiated
1694+ if storage_backend is None :
1695+ storage_backend = securesystemslib .storage .FilesystemBackend ()
1696+
16961697 for l in leaves :
16971698 merkle_path = {}
16981699 current_node = l
@@ -1726,8 +1727,6 @@ def _write_merkle_paths(root, leaves, storage_backend, merkle_directory):
17261727 leaf_contents = l .contents ,
17271728 merkle_path = merkle_path ,
17281729 path_directions = path_directions )
1729- if storage_backend is None :
1730- storage_backend = securesystemslib .storage .FilesystemBackend ()
17311730 file_content = _get_written_metadata (file_contents )
17321731 file_object = tempfile .TemporaryFile ()
17331732 file_object .write (file_content )
@@ -1764,7 +1763,7 @@ def print_merkle_tree(root):
17641763
17651764def generate_snapshot_metadata (metadata_directory , version , expiration_date ,
17661765 storage_backend , consistent_snapshot = False ,
1767- repository_name = 'default' , use_length = False , use_hashes = False , snapshot_merkle = False ):
1766+ repository_name = 'default' , use_length = False , use_hashes = False ):
17681767 """
17691768 <Purpose>
17701769 Create the snapshot metadata. The minimum metadata must exist (i.e.,
@@ -1815,11 +1814,6 @@ def generate_snapshot_metadata(metadata_directory, version, expiration_date,
18151814 Read more at section 5.6 from the Mercury paper:
18161815 https://www.usenix.org/conference/atc17/technical-sessions/presentation/kuppusamy
18171816
1818- snapshot_merkle:
1819- Whether to generate snapshot merkle files in addition to snapshot.json
1820- metadata. If this is true, this function will return the root and leaves
1821- of the merkle tree in addition to the snapshot metadata.
1822-
18231817 <Exceptions>
18241818 securesystemslib.exceptions.FormatError, if the arguments are improperly
18251819 formatted.
@@ -1915,10 +1909,7 @@ def generate_snapshot_metadata(metadata_directory, version, expiration_date,
19151909 expires = expiration_date ,
19161910 meta = fileinfodict )
19171911
1918- if snapshot_merkle :
1919- root , leaves = _build_merkle_tree (fileinfodict )
1920- return root , leaves , metadata
1921- return metadata
1912+ return metadata , fileinfodict
19221913
19231914
19241915
0 commit comments