@@ -13,7 +13,8 @@ use anyhow::{bail, Context, Result};
1313use futures_lite:: { Stream , StreamExt } ;
1414use iroh:: { key:: PublicKey , Endpoint , NodeAddr } ;
1515use iroh_blobs:: {
16- downloader:: Downloader , store:: EntryStatus , util:: local_pool:: LocalPoolHandle , Hash ,
16+ downloader:: Downloader , net_protocol:: ProtectCb , store:: EntryStatus ,
17+ util:: local_pool:: LocalPoolHandle , Hash ,
1718} ;
1819use iroh_gossip:: net:: Gossip ;
1920use serde:: { Deserialize , Serialize } ;
@@ -125,6 +126,34 @@ impl<D: iroh_blobs::store::Store> Engine<D> {
125126 } )
126127 }
127128
129+ /// Return a callback that can be added to blobs to protect the content of
130+ /// all docs from garbage collection.
131+ pub fn protect_cb ( & self ) -> ProtectCb {
132+ let this = self . clone ( ) ;
133+ Box :: new ( move |live| {
134+ let this = this. clone ( ) ;
135+ Box :: pin ( async move {
136+ let doc_hashes = match this. sync . content_hashes ( ) . await {
137+ Ok ( hashes) => hashes,
138+ Err ( err) => {
139+ tracing:: warn!( "Error getting doc hashes: {}" , err) ;
140+ return ;
141+ }
142+ } ;
143+ for hash in doc_hashes {
144+ match hash {
145+ Ok ( hash) => {
146+ live. insert ( hash) ;
147+ }
148+ Err ( err) => {
149+ tracing:: error!( "Error getting doc hash: {}" , err) ;
150+ }
151+ }
152+ }
153+ } )
154+ } )
155+ }
156+
128157 /// Get the blob store.
129158 pub fn blob_store ( & self ) -> & D {
130159 & self . blob_store
0 commit comments