Skip to content

Commit 6229fc6

Browse files
committed
Check concurrent batches conflict, add extra impl
1 parent d812261 commit 6229fc6

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

tuf/src/repository.rs

+45
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,29 @@ where
189189
}
190190
}
191191

192+
impl<T, D> RepositoryStorage<D> for &T
193+
where
194+
T: RepositoryStorage<D>,
195+
D: DataInterchange + Sync,
196+
{
197+
fn store_metadata<'a>(
198+
&'a self,
199+
meta_path: &MetadataPath,
200+
version: MetadataVersion,
201+
metadata: &'a mut (dyn AsyncRead + Send + Unpin),
202+
) -> BoxFuture<'a, Result<()>> {
203+
(**self).store_metadata(meta_path, version, metadata)
204+
}
205+
206+
fn store_target<'a>(
207+
&'a self,
208+
target_path: &TargetPath,
209+
target: &'a mut (dyn AsyncRead + Send + Unpin),
210+
) -> BoxFuture<'a, Result<()>> {
211+
(**self).store_target(target_path, target)
212+
}
213+
}
214+
192215
impl<T, D> RepositoryStorage<D> for &mut T
193216
where
194217
T: RepositoryStorage<D>,
@@ -317,6 +340,28 @@ where
317340
}
318341
}
319342

343+
impl<D> RepositoryStorage<D> for &dyn RepositoryStorage<D>
344+
where
345+
D: DataInterchange + Sync,
346+
{
347+
fn store_metadata<'a>(
348+
&'a self,
349+
meta_path: &MetadataPath,
350+
version: MetadataVersion,
351+
metadata: &'a mut (dyn AsyncRead + Send + Unpin),
352+
) -> BoxFuture<'a, Result<()>> {
353+
(**self).store_metadata(meta_path, version, metadata)
354+
}
355+
356+
fn store_target<'a>(
357+
&'a self,
358+
target_path: &TargetPath,
359+
target: &'a mut (dyn AsyncRead + Send + Unpin),
360+
) -> BoxFuture<'a, Result<()>> {
361+
(**self).store_target(target_path, target)
362+
}
363+
}
364+
320365
impl<D> RepositoryStorage<D> for &mut dyn RepositoryStorage<D>
321366
where
322367
D: DataInterchange + Sync,

tuf/src/repository/ephemeral.rs

+23
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,29 @@ mod test {
524524
.unwrap();
525525

526526
assert_matches!(batch.commit().await, Err(CommitError::Conflict));
527+
528+
// multiple batches should conflict.
529+
let batch1 = repo.batch_update();
530+
let batch2 = repo.batch_update();
531+
532+
batch1
533+
.store_target(
534+
&TargetPath::new("target4").unwrap(),
535+
&mut "target4".as_bytes(),
536+
)
537+
.await
538+
.unwrap();
539+
540+
batch2
541+
.store_target(
542+
&TargetPath::new("target5").unwrap(),
543+
&mut "target5".as_bytes(),
544+
)
545+
.await
546+
.unwrap();
547+
548+
assert_matches!(batch1.commit().await, Ok(()));
549+
assert_matches!(batch2.commit().await, Err(CommitError::Conflict));
527550
})
528551
}
529552
}

tuf/src/repository/file_system.rs

+23
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,29 @@ mod test {
765765
.unwrap();
766766

767767
assert_matches!(batch.commit().await, Err(CommitError::Conflict));
768+
769+
// multiple batches should conflict.
770+
let batch1 = repo.batch_update();
771+
let batch2 = repo.batch_update();
772+
773+
batch1
774+
.store_target(
775+
&TargetPath::new("target4").unwrap(),
776+
&mut "target4".as_bytes(),
777+
)
778+
.await
779+
.unwrap();
780+
781+
batch2
782+
.store_target(
783+
&TargetPath::new("target5").unwrap(),
784+
&mut "target5".as_bytes(),
785+
)
786+
.await
787+
.unwrap();
788+
789+
assert_matches!(batch1.commit().await, Ok(()));
790+
assert_matches!(batch2.commit().await, Err(CommitError::Conflict));
768791
})
769792
}
770793
}

0 commit comments

Comments
 (0)