1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
use bytes::Bytes;
use iroh::{EndpointAddr, address_lookup::memory::MemoryLookup, endpoint::Endpoint};
use iroh_blobs::{
BlobFormat, BlobsProtocol, Hash,
api::{
Store as BlobApi,
blobs::{AddPathOptions, BlobReader, BlobStatus, ExportMode, ExportOptions, ImportMode},
},
protocol::GetRequest,
ticket::BlobTicket,
};
use n0_future::StreamExt;
use std::path::Path;
use crate::error::{Result, SyncwebError};
use crate::net::NetworkContext;
#[derive(Clone)]
pub struct BlobStore {
store: BlobApi,
address_lookup: MemoryLookup,
}
impl BlobStore {
#[must_use]
pub fn new(protocol: &BlobsProtocol) -> Self {
Self::new_with_address_lookup(protocol, MemoryLookup::new())
}
#[must_use]
pub fn new_with_address_lookup(protocol: &BlobsProtocol, address_lookup: MemoryLookup) -> Self {
Self {
store: protocol.store().clone(),
address_lookup,
}
}
#[must_use]
pub const fn inner(&self) -> &BlobApi {
&self.store
}
/// Create a reader for the given hash.
///
/// The reader implements [`tokio::io::AsyncRead`] and [`tokio::io::AsyncSeek`],
/// allowing O(1)-memory streaming to any sink.
#[must_use]
pub fn reader(&self, hash: impl Into<Hash>) -> BlobReader {
self.store.reader(hash)
}
/// # Errors
///
/// Returns an error if the blob fails to be added to the store.
pub async fn add_bytes(&self, data: impl AsRef<[u8]>) -> Result<Hash> {
Ok(self
.store
.add_bytes(Bytes::copy_from_slice(data.as_ref()))
.await
.map_err(|error| SyncwebError::operation("failed to add blob bytes", error))?
.hash)
}
/// # Errors
///
/// Returns an error if the file fails to be read or added to the store.
pub async fn add_file(&self, path: impl AsRef<Path>) -> Result<Hash> {
Ok(self
.store
.add_path(path)
.await
.map_err(|error| SyncwebError::operation("failed to add blob file", error))?
.hash)
}
/// Add a file using a reference to its original path instead of copying
/// its contents into the blob store.
///
/// The source file must remain available and unchanged for as long as the
/// blob store may need to read this blob.
///
/// # Errors
///
/// Returns an error if the file fails to be read or added to the store.
pub async fn add_file_ref(&self, path: impl AsRef<Path>) -> Result<Hash> {
Ok(self
.store
.add_path_with_opts(AddPathOptions {
path: path.as_ref().to_owned(),
mode: ImportMode::TryReference,
format: BlobFormat::Raw,
})
.await
.map_err(|error| SyncwebError::operation("failed to add blob file (reference)", error))?
.hash)
}
/// Export a complete blob to a temporary or caller-managed path.
///
/// Uses [`ExportMode::Copy`] — the blob stays in the store and a copy
/// (or `CoW` reflink) is written to `destination`.
///
/// # Errors
///
/// Returns an error if the blob cannot be read or written.
pub async fn export_to_path(&self, hash: Hash, destination: impl AsRef<Path>) -> Result<u64> {
self.export_to_path_with_mode(hash, destination, ExportMode::Copy).await
}
/// Export a complete blob with a specific [`ExportMode`].
///
/// `ExportMode::TryReference` moves the blob's internal file to
/// `destination` and updates the store to track it externally,
/// avoiding a copy. `ExportMode::Copy` leaves the blob in the store
/// and writes a copy (or `CoW` reflink) to `destination`.
///
/// # Errors
///
/// Returns an error if the blob cannot be read or written.
pub async fn export_to_path_with_mode(
&self,
hash: Hash,
destination: impl AsRef<Path>,
mode: ExportMode,
) -> Result<u64> {
self.store
.export_with_opts(ExportOptions {
hash,
mode,
target: destination.as_ref().to_owned(),
})
.await
.map_err(|error| SyncwebError::operation("failed to export blob", error))
}
/// # Errors
///
/// Returns an error if the store cannot be queried.
pub async fn has(&self, hash: Hash) -> Result<bool> {
self.store
.has(hash)
.await
.map_err(|error| SyncwebError::operation("failed to query blob store", error))
}
/// Get metadata (size) for a blob.
///
/// # Errors
///
/// Returns an error if the blob is not found or the store cannot be queried.
pub async fn stat(&self, hash: Hash) -> Result<BlobStatus> {
self.store
.blobs()
.status(hash)
.await
.map_err(|error| SyncwebError::operation("failed to stat blob", error))
}
/// # Errors
///
/// Returns an error if the blob cannot be found or read.
pub async fn get(&self, hash: Hash) -> Result<Bytes> {
self.store
.get_bytes(hash)
.await
.map_err(|error| SyncwebError::operation("failed to read blob", error))
}
#[must_use]
pub fn ticket(&self, endpoint: &Endpoint, hash: Hash) -> BlobTicket {
self.ticket_for_addr(endpoint.addr(), hash)
}
#[must_use]
pub fn ticket_for_addr(&self, addr: EndpointAddr, hash: Hash) -> BlobTicket {
BlobTicket::new(addr, hash, iroh_blobs::BlobFormat::Raw)
}
/// Pin a blob with a durable named tag so garbage collection cannot remove it.
///
/// # Errors
///
/// Returns an error if the tag cannot be written.
pub async fn pin(&self, name: impl AsRef<str>, hash: Hash) -> Result<()> {
self.store
.tags()
.set(name.as_ref(), hash)
.await
.map_err(|error| SyncwebError::operation("failed to pin blob", error))
}
/// Remove a durable pin from a blob.
///
/// # Errors
///
/// Returns an error if the tag cannot be removed.
pub async fn unpin(&self, name: impl AsRef<str>) -> Result<()> {
self.store
.tags()
.delete(name.as_ref())
.await
.map(|_deleted| ())
.map_err(|error| SyncwebError::operation("failed to unpin blob", error))
}
/// # Errors
///
/// Returns an error if the pin cannot be queried.
pub async fn is_pinned(&self, name: impl AsRef<str>, hash: Hash) -> Result<bool> {
self.store
.tags()
.get(name.as_ref())
.await
.map(|tag_info| tag_info.is_some_and(|tag| tag.hash == hash))
.map_err(|error| SyncwebError::operation("failed to query blob pin", error))
}
/// List all complete blobs currently in the store.
///
/// # Errors
///
/// Returns an error if the store cannot be queried.
pub async fn list_hashes(&self) -> Result<Vec<Hash>> {
self.store
.blobs()
.list()
.hashes()
.await
.map_err(|error| SyncwebError::operation("failed to list blobs", error))
}
/// List named pins whose names start with `prefix`.
///
/// # Errors
///
/// Returns an error if the store cannot be queried.
pub async fn list_pins(&self, prefix: impl AsRef<[u8]>) -> Result<Vec<(String, Hash)>> {
let mut tags = self
.store
.tags()
.list_prefix(prefix)
.await
.map_err(|error| SyncwebError::operation("failed to list blob pins", error))?;
let mut pins = Vec::new();
while let Some(tag_result) = tags.next().await {
let tag = tag_result.map_err(|error| SyncwebError::operation("failed to read blob pin", error))?;
let name = String::from_utf8(tag.name.as_ref().to_vec())
.map_err(|error| SyncwebError::operation("blob pin name is not UTF-8", error))?;
pins.push((name, tag.hash));
}
Ok(pins)
}
/// # Errors
///
/// Returns an error if the blob cannot be fetched from the remote endpoint.
pub async fn fetch(&self, endpoint: &Endpoint, ticket: &BlobTicket) -> Result<()> {
self.address_lookup.add_endpoint_info(ticket.addr().clone());
self.store
.downloader(endpoint)
.download(ticket.hash_and_format(), [ticket.addr().id])
.await
.map_err(|error| SyncwebError::operation("failed to fetch blob", error))?;
Ok(())
}
/// Fetch a blob with network access control gating.
///
/// Checks via `NetworkContext` that the local node can access the blob
/// through at least one network membership before downloading.
///
/// # Errors
///
/// Returns `AccessDenied` if the blob is not associated with any network
/// the local node belongs to. Returns other errors if the fetch itself fails.
pub async fn download_with_network(
&self,
endpoint: &Endpoint,
ticket: &BlobTicket,
ctx: &NetworkContext,
) -> Result<()> {
let hash = ticket.hash();
if !ctx.can_access_blob(&hash)? {
return Err(SyncwebError::access_denied("blob not accessible in any network"));
}
self.fetch(endpoint, ticket).await
}
/// Force-fetch a blob from a remote peer, re-downloading even if the blob
/// is already present locally. Uses `iroh-blobs`' low-level `execute_get`
/// API which does not check local availability.
///
/// # Errors
///
/// Returns an error if the connection or download fails.
pub async fn force_fetch(&self, endpoint: &Endpoint, ticket: &BlobTicket) -> Result<()> {
self.address_lookup.add_endpoint_info(ticket.addr().clone());
let hash = ticket.hash();
let conn = endpoint
.connect(ticket.addr().id, iroh_blobs::protocol::ALPN)
.await
.map_err(|error| SyncwebError::operation("failed to connect to provider", error))?;
self.force_fetch_from_conn(conn, hash).await
}
/// Force-fetch a blob from a known peer, connecting by public key alone.
///
/// # Errors
///
/// Returns an error if the connection or download fails.
pub async fn force_fetch_from_peer(&self, endpoint: &Endpoint, peer: &iroh::PublicKey, hash: Hash) -> Result<()> {
let conn = endpoint
.connect(*peer, iroh_blobs::protocol::ALPN)
.await
.map_err(|error| SyncwebError::operation("failed to connect to peer", error))?;
self.force_fetch_from_conn(conn, hash).await
}
async fn force_fetch_from_conn(&self, conn: iroh::endpoint::Connection, hash: Hash) -> Result<()> {
let request = GetRequest::blob(hash);
self.store
.remote()
.execute_get(conn, request)
.await
.map_err(|error| SyncwebError::operation("failed to force-fetch blob", error))?;
Ok(())
}
}