Skip to content

Commit d812261

Browse files
committed
Switch RepositoryStorage::store_* to take &self
During our migration to futures, we switched RepositoryStorage::store_metadata and RepositoryStorage::store_targets to take `&mut self` to make it easier to reason about changes. This change however prevented us from concurrently changing the repository, which can speed up large commits. This change switches the repositories back to use `&self` to enable this. One consequence of doing this is we need to change the batch update implementation `commit()` to now be fallible. This is implemented by incrementing a version number on every repository change, and storing the initial version number in the batch. We have a conflict if the versions don't match during commit.
1 parent f99fd7c commit d812261

File tree

7 files changed

+497
-163
lines changed

7 files changed

+497
-163
lines changed

tuf/src/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,7 +1797,7 @@ mod test {
17971797
fn constructor_succeeds_with_malformed_metadata() {
17981798
block_on(async {
17991799
// Store a malformed timestamp in the local repository.
1800-
let mut local = EphemeralRepository::<Json>::new();
1800+
let local = EphemeralRepository::<Json>::new();
18011801
let junk_timestamp = "junk timestamp";
18021802

18031803
local
@@ -2339,7 +2339,7 @@ mod test {
23392339
#[test]
23402340
fn client_can_update_with_unknown_len_and_hashes() {
23412341
block_on(async {
2342-
let mut repo = EphemeralRepository::<Json>::new();
2342+
let repo = EphemeralRepository::<Json>::new();
23432343

23442344
let root = RootMetadataBuilder::new()
23452345
.consistent_snapshot(true)

tuf/src/repo_builder.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,11 @@ where
14411441
let path = MetadataPath::targets();
14421442
self.ctx
14431443
.repo
1444-
.store_metadata(&path, MetadataVersion::None, &mut targets.raw.as_bytes())
1444+
.store_metadata(
1445+
&path.clone(),
1446+
MetadataVersion::None,
1447+
&mut targets.raw.as_bytes(),
1448+
)
14451449
.await?;
14461450

14471451
if consistent_snapshot {

tuf/src/repository.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,18 @@ where
117117
///
118118
/// [extension]: crate::interchange::DataInterchange::extension
119119
fn store_metadata<'a>(
120-
&'a mut self,
120+
&'a self,
121121
meta_path: &MetadataPath,
122122
version: MetadataVersion,
123-
metadata: &'a mut (dyn AsyncRead + Send + Unpin + 'a),
123+
metadata: &'a mut (dyn AsyncRead + Send + Unpin),
124124
) -> BoxFuture<'a, Result<()>>;
125125

126126
/// Store the provided `target` in a location identified by `target_path`, overwriting any
127127
/// existing target at that location.
128128
fn store_target<'a>(
129-
&'a mut self,
129+
&'a self,
130130
target_path: &TargetPath,
131-
target: &'a mut (dyn AsyncRead + Send + Unpin + 'a),
131+
target: &'a mut (dyn AsyncRead + Send + Unpin),
132132
) -> BoxFuture<'a, Result<()>>;
133133
}
134134

@@ -195,18 +195,18 @@ where
195195
D: DataInterchange + Sync,
196196
{
197197
fn store_metadata<'a>(
198-
&'a mut self,
198+
&'a self,
199199
meta_path: &MetadataPath,
200200
version: MetadataVersion,
201-
metadata: &'a mut (dyn AsyncRead + Send + Unpin + 'a),
201+
metadata: &'a mut (dyn AsyncRead + Send + Unpin),
202202
) -> BoxFuture<'a, Result<()>> {
203203
(**self).store_metadata(meta_path, version, metadata)
204204
}
205205

206206
fn store_target<'a>(
207-
&'a mut self,
207+
&'a self,
208208
target_path: &TargetPath,
209-
target: &'a mut (dyn AsyncRead + Send + Unpin + 'a),
209+
target: &'a mut (dyn AsyncRead + Send + Unpin),
210210
) -> BoxFuture<'a, Result<()>> {
211211
(**self).store_target(target_path, target)
212212
}
@@ -218,16 +218,16 @@ where
218218
D: DataInterchange + Sync,
219219
{
220220
fn store_metadata<'a>(
221-
&'a mut self,
221+
&'a self,
222222
meta_path: &MetadataPath,
223223
version: MetadataVersion,
224-
metadata: &'a mut (dyn AsyncRead + Send + Unpin + 'a),
224+
metadata: &'a mut (dyn AsyncRead + Send + Unpin),
225225
) -> BoxFuture<'a, Result<()>> {
226226
(**self).store_metadata(meta_path, version, metadata)
227227
}
228228

229229
fn store_target<'a>(
230-
&'a mut self,
230+
&'a self,
231231
target_path: &TargetPath,
232232
target: &'a mut (dyn AsyncRead + Send + Unpin + 'a),
233233
) -> BoxFuture<'a, Result<()>> {
@@ -322,18 +322,18 @@ where
322322
D: DataInterchange + Sync,
323323
{
324324
fn store_metadata<'a>(
325-
&'a mut self,
325+
&'a self,
326326
meta_path: &MetadataPath,
327327
version: MetadataVersion,
328-
metadata: &'a mut (dyn AsyncRead + Send + Unpin + 'a),
328+
metadata: &'a mut (dyn AsyncRead + Send + Unpin),
329329
) -> BoxFuture<'a, Result<()>> {
330330
(**self).store_metadata(meta_path, version, metadata)
331331
}
332332

333333
fn store_target<'a>(
334-
&'a mut self,
334+
&'a self,
335335
target_path: &TargetPath,
336-
target: &'a mut (dyn AsyncRead + Send + Unpin + 'a),
336+
target: &'a mut (dyn AsyncRead + Send + Unpin),
337337
) -> BoxFuture<'a, Result<()>> {
338338
(**self).store_target(target_path, target)
339339
}
@@ -486,7 +486,7 @@ where
486486
/// [extension]: crate::interchange::DataInterchange::extension
487487
pub async fn store_metadata<'a, M>(
488488
&'a mut self,
489-
path: &'a MetadataPath,
489+
path: &MetadataPath,
490490
version: MetadataVersion,
491491
metadata: &'a RawSignedMetadata<D, M>,
492492
) -> Result<()>
@@ -503,7 +503,7 @@ where
503503
/// Store the provided `target` in a location identified by `target_path`.
504504
pub async fn store_target<'a>(
505505
&'a mut self,
506-
target_path: &'a TargetPath,
506+
target_path: &TargetPath,
507507
target: &'a mut (dyn AsyncRead + Send + Unpin + 'a),
508508
) -> Result<()> {
509509
self.repository.store_target(target_path, target).await
@@ -580,7 +580,7 @@ mod test {
580580
let _metadata = RawSignedMetadata::<Json, RootMetadata>::new(data.to_vec());
581581
let data_hash = crypto::calculate_hash(data, &HashAlgorithm::Sha256);
582582

583-
let mut repo = EphemeralRepository::new();
583+
let repo = EphemeralRepository::new();
584584
repo.store_metadata(&path, version, &mut &*data)
585585
.await
586586
.unwrap();
@@ -608,7 +608,7 @@ mod test {
608608
let version = MetadataVersion::None;
609609
let data: &[u8] = b"corrupt metadata";
610610

611-
let mut repo = EphemeralRepository::new();
611+
let repo = EphemeralRepository::new();
612612
repo.store_metadata(&path, version, &mut &*data)
613613
.await
614614
.unwrap();
@@ -637,7 +637,7 @@ mod test {
637637
let data: &[u8] = b"reasonably sized metadata";
638638
let _metadata = RawSignedMetadata::<Json, RootMetadata>::new(data.to_vec());
639639

640-
let mut repo = EphemeralRepository::new();
640+
let repo = EphemeralRepository::new();
641641
repo.store_metadata(&path, version, &mut &*data)
642642
.await
643643
.unwrap();
@@ -660,7 +660,7 @@ mod test {
660660
let version = MetadataVersion::None;
661661
let data: &[u8] = b"very big metadata";
662662

663-
let mut repo = EphemeralRepository::new();
663+
let repo = EphemeralRepository::new();
664664
repo.store_metadata(&path, version, &mut &*data)
665665
.await
666666
.unwrap();

0 commit comments

Comments
 (0)