iroh_blobs/store/
fs.rs

1//! # File based blob store.
2//!
3//! A file based blob store needs a writeable directory to work with.
4//!
5//! General design:
6//!
7//! The file store consists of two actors.
8//!
9//! # The main actor
10//!
11//! The purpose of the main actor is to handle user commands and own a map of
12//! handles for hashes that are currently being worked on.
13//!
14//! It also owns tasks for ongoing import and export operations, as well as the
15//! database actor.
16//!
17//! Handling a command almost always involves either forwarding it to the
18//! database actor or creating a hash context and spawning a task.
19//!
20//! # The database actor
21//!
22//! The database actor is responsible for storing metadata about each hash,
23//! as well as inlined data and outboard data for small files.
24//!
25//! In addition to the metadata, the database actor also stores tags.
26//!
27//! # Tasks
28//!
29//! Tasks do not return a result. They are responsible for sending an error
30//! to the requester if possible. Otherwise, just dropping the sender will
31//! also fail the receiver, but without a descriptive error message.
32//!
33//! Tasks are usually implemented as an impl fn that does return a result,
34//! and a wrapper (named `..._task`) that just forwards the error, if any.
35//!
36//! That way you can use `?` syntax in the task implementation. The impl fns
37//! are also easier to test.
38//!
39//! # Context
40//!
41//! The main actor holds a TaskContext that is needed for almost all tasks,
42//! such as the config and a way to interact with the database.
43//!
44//! For tasks that are specific to a hash, a HashContext combines the task
45//! context with a slot from the table of the main actor that can be used
46//! to obtain an unique handle for the hash.
47//!
48//! # Runtime
49//!
50//! The fs store owns and manages its own tokio runtime. Dropping the store
51//! will clean up the database and shut down the runtime. However, some parts
52//! of the persistent state won't make it to disk, so operations involving large
53//! partial blobs will have a large initial delay on the next startup.
54//!
55//! It is also not guaranteed that all write operations will make it to disk.
56//! The on-disk store will be in a consistent state, but might miss some writes
57//! in the last seconds before shutdown.
58//!
59//! To avoid this, you can use the [`crate::api::Store::shutdown`] method to
60//! cleanly shut down the store and save ephemeral state to disk.
61//!
62//! Note that if you use the store inside a [`iroh::protocol::Router`] and shut
63//! down the router using [`iroh::protocol::Router::shutdown`], the store will be
64//! safely shut down as well. Any store refs you are holding will be inoperable
65//! after this.
66use std::{
67    fmt::{self, Debug},
68    fs,
69    future::Future,
70    io::Write,
71    num::NonZeroU64,
72    ops::Deref,
73    path::{Path, PathBuf},
74    sync::{
75        atomic::{AtomicU64, Ordering},
76        Arc,
77    },
78};
79
80use bao_tree::{
81    blake3,
82    io::{
83        mixed::{traverse_ranges_validated, EncodedItem, ReadBytesAt},
84        outboard::PreOrderOutboard,
85        sync::ReadAt,
86        BaoContentItem, Leaf,
87    },
88    BaoTree, ChunkNum, ChunkRanges,
89};
90use bytes::Bytes;
91use delete_set::{BaoFilePart, ProtectHandle};
92use entity_manager::{EntityManagerState, SpawnArg};
93use entry_state::{DataLocation, OutboardLocation};
94use import::{ImportEntry, ImportSource};
95use irpc::{channel::mpsc, RpcMessage};
96use meta::list_blobs;
97use n0_error::{Result, StdResultExt};
98use n0_future::{future::yield_now, io};
99use nested_enum_utils::enum_conversions;
100use range_collections::range_set::RangeSetRange;
101use tokio::task::{JoinError, JoinSet};
102use tracing::{error, instrument, trace};
103
104use crate::{
105    api::{
106        proto::{
107            self, bitfield::is_validated, BatchMsg, BatchResponse, Bitfield, Command,
108            CreateTempTagMsg, ExportBaoMsg, ExportBaoRequest, ExportPathMsg, ExportPathRequest,
109            ExportRangesItem, ExportRangesMsg, ExportRangesRequest, HashSpecific, ImportBaoMsg,
110            ImportBaoRequest, ObserveMsg, Scope,
111        },
112        ApiClient,
113    },
114    protocol::ChunkRangesExt,
115    store::{
116        fs::{
117            bao_file::{
118                BaoFileStorage, BaoFileStorageSubscriber, CompleteStorage, DataReader,
119                OutboardReader,
120            },
121            util::entity_manager::{self, ActiveEntityState},
122        },
123        gc::run_gc,
124        util::{BaoTreeSender, FixedSize, MemOrFile, ValueOrPoisioned},
125        IROH_BLOCK_SIZE,
126    },
127    util::{
128        channel::oneshot,
129        temp_tag::{TagDrop, TempTag, TempTagScope, TempTags},
130    },
131    Hash,
132};
133mod bao_file;
134use bao_file::BaoFileHandle;
135mod delete_set;
136mod entry_state;
137mod import;
138mod meta;
139pub mod options;
140pub(crate) mod util;
141use entry_state::EntryState;
142use import::{import_byte_stream, import_bytes, import_path, ImportEntryMsg};
143use options::Options;
144use tracing::Instrument;
145
146use crate::{
147    api::{
148        self,
149        blobs::{AddProgressItem, ExportMode, ExportProgressItem},
150        Store,
151    },
152    HashAndFormat,
153};
154
155/// Maximum number of external paths we track per blob.
156const MAX_EXTERNAL_PATHS: usize = 8;
157
158/// Create a 16 byte unique ID.
159fn new_uuid() -> [u8; 16] {
160    use rand::RngCore;
161    let mut rng = rand::rng();
162    let mut bytes = [0u8; 16];
163    rng.fill_bytes(&mut bytes);
164    bytes
165}
166
167/// Create temp file name based on a 16 byte UUID.
168fn temp_name() -> String {
169    format!("{}.temp", hex::encode(new_uuid()))
170}
171
172#[derive(Debug)]
173#[enum_conversions()]
174pub(crate) enum InternalCommand {
175    Dump(meta::Dump),
176    FinishImport(ImportEntryMsg),
177    ClearScope(ClearScope),
178}
179
180#[derive(Debug)]
181pub(crate) struct ClearScope {
182    pub scope: Scope,
183}
184
185impl InternalCommand {
186    pub fn parent_span(&self) -> tracing::Span {
187        match self {
188            Self::Dump(_) => tracing::Span::current(),
189            Self::ClearScope(_) => tracing::Span::current(),
190            Self::FinishImport(cmd) => cmd
191                .parent_span_opt()
192                .cloned()
193                .unwrap_or_else(tracing::Span::current),
194        }
195    }
196}
197
198/// Context needed by most tasks
199#[derive(Debug)]
200struct TaskContext {
201    // Store options such as paths and inline thresholds, in an Arc to cheaply share with tasks.
202    pub options: Arc<Options>,
203    // Metadata database, basically a mpsc sender with some extra functionality.
204    pub db: meta::Db,
205    // Handle to send internal commands
206    pub internal_cmd_tx: tokio::sync::mpsc::Sender<InternalCommand>,
207    /// Handle to protect files from deletion.
208    pub protect: ProtectHandle,
209}
210
211impl TaskContext {
212    pub async fn clear_scope(&self, scope: Scope) {
213        self.internal_cmd_tx
214            .send(ClearScope { scope }.into())
215            .await
216            .ok();
217    }
218}
219
220#[derive(Debug)]
221struct EmParams;
222
223impl entity_manager::Params for EmParams {
224    type EntityId = Hash;
225
226    type GlobalState = Arc<TaskContext>;
227
228    type EntityState = BaoFileHandle;
229
230    async fn on_shutdown(
231        state: entity_manager::ActiveEntityState<Self>,
232        cause: entity_manager::ShutdownCause,
233    ) {
234        trace!("persist {:?} due to {cause:?}", state.id);
235        state.persist().await;
236    }
237}
238
239#[derive(Debug)]
240struct Actor {
241    // Context that can be cheaply shared with tasks.
242    context: Arc<TaskContext>,
243    // Receiver for incoming user commands.
244    cmd_rx: tokio::sync::mpsc::Receiver<Command>,
245    // Receiver for incoming file store specific commands.
246    fs_cmd_rx: tokio::sync::mpsc::Receiver<InternalCommand>,
247    // Tasks for import and export operations.
248    tasks: JoinSet<()>,
249    // Entity manager that handles concurrency for entities.
250    handles: EntityManagerState<EmParams>,
251    // temp tags
252    temp_tags: TempTags,
253    // waiters for idle state.
254    idle_waiters: Vec<irpc::channel::oneshot::Sender<()>>,
255    // our private tokio runtime. It has to live somewhere.
256    _rt: RtWrapper,
257}
258
259type HashContext = ActiveEntityState<EmParams>;
260
261impl SyncEntityApi for HashContext {
262    /// Load the state from the database.
263    ///
264    /// If the state is Initial, this will start the load.
265    /// If it is Loading, it will wait until loading is done.
266    /// If it is any other state, it will be a noop.
267    async fn load(&self) {
268        enum Action {
269            Load,
270            Wait,
271            None,
272        }
273        let mut action = Action::None;
274        self.state.send_if_modified(|guard| match guard.deref() {
275            BaoFileStorage::Initial => {
276                *guard = BaoFileStorage::Loading;
277                action = Action::Load;
278                true
279            }
280            BaoFileStorage::Loading => {
281                action = Action::Wait;
282                false
283            }
284            _ => false,
285        });
286        match action {
287            Action::Load => {
288                let state = if self.id == Hash::EMPTY {
289                    BaoFileStorage::Complete(CompleteStorage {
290                        data: MemOrFile::Mem(Bytes::new()),
291                        outboard: MemOrFile::empty(),
292                    })
293                } else {
294                    // we must assign a new state even in the error case, otherwise
295                    // tasks waiting for loading would stall!
296                    match self.global.db.get(self.id).await {
297                        Ok(state) => match BaoFileStorage::open(state, self).await {
298                            Ok(handle) => handle,
299                            Err(_) => BaoFileStorage::Poisoned,
300                        },
301                        Err(_) => BaoFileStorage::Poisoned,
302                    }
303                };
304                self.state.send_replace(state);
305            }
306            Action::Wait => {
307                // we are in state loading already, so we just need to wait for the
308                // other task to complete loading.
309                while matches!(self.state.borrow().deref(), BaoFileStorage::Loading) {
310                    self.state.0.subscribe().changed().await.ok();
311                }
312            }
313            Action::None => {}
314        }
315    }
316
317    /// Write a batch and notify the db
318    async fn write_batch(&self, batch: &[BaoContentItem], bitfield: &Bitfield) -> io::Result<()> {
319        trace!("write_batch bitfield={:?} batch={}", bitfield, batch.len());
320        let mut res = Ok(None);
321        self.state.send_if_modified(|state| {
322            let Ok((state1, update)) = state.take().write_batch(batch, bitfield, self) else {
323                res = Err(io::Error::other("write batch failed"));
324                return false;
325            };
326            res = Ok(update);
327            *state = state1;
328            true
329        });
330        if let Some(update) = res? {
331            self.global.db.update(self.id, update).await?;
332        }
333        Ok(())
334    }
335
336    /// An AsyncSliceReader for the data file.
337    ///
338    /// Caution: this is a reader for the unvalidated data file. Reading this
339    /// can produce data that does not match the hash.
340    #[allow(refining_impl_trait_internal)]
341    fn data_reader(&self) -> DataReader {
342        DataReader(self.state.clone())
343    }
344
345    /// An AsyncSliceReader for the outboard file.
346    ///
347    /// The outboard file is used to validate the data file. It is not guaranteed
348    /// to be complete.
349    #[allow(refining_impl_trait_internal)]
350    fn outboard_reader(&self) -> OutboardReader {
351        OutboardReader(self.state.clone())
352    }
353
354    /// The most precise known total size of the data file.
355    fn current_size(&self) -> io::Result<u64> {
356        match self.state.borrow().deref() {
357            BaoFileStorage::Complete(mem) => Ok(mem.size()),
358            BaoFileStorage::PartialMem(mem) => Ok(mem.current_size()),
359            BaoFileStorage::Partial(file) => file.current_size(),
360            BaoFileStorage::Poisoned => Err(io::Error::other("poisoned storage")),
361            BaoFileStorage::Initial => Err(io::Error::other("initial")),
362            BaoFileStorage::Loading => Err(io::Error::other("loading")),
363            BaoFileStorage::NonExisting => Err(io::ErrorKind::NotFound.into()),
364        }
365    }
366
367    /// The most precise known total size of the data file.
368    fn bitfield(&self) -> io::Result<Bitfield> {
369        match self.state.borrow().deref() {
370            BaoFileStorage::Complete(mem) => Ok(mem.bitfield()),
371            BaoFileStorage::PartialMem(mem) => Ok(mem.bitfield().clone()),
372            BaoFileStorage::Partial(file) => Ok(file.bitfield().clone()),
373            BaoFileStorage::Poisoned => Err(io::Error::other("poisoned storage")),
374            BaoFileStorage::Initial => Err(io::Error::other("initial")),
375            BaoFileStorage::Loading => Err(io::Error::other("loading")),
376            BaoFileStorage::NonExisting => Err(io::ErrorKind::NotFound.into()),
377        }
378    }
379}
380
381impl HashContext {
382    /// The outboard for the file.
383    pub fn outboard(&self) -> io::Result<PreOrderOutboard<OutboardReader>> {
384        let tree = BaoTree::new(self.current_size()?, IROH_BLOCK_SIZE);
385        let outboard = self.outboard_reader();
386        Ok(PreOrderOutboard {
387            root: blake3::Hash::from(self.id),
388            tree,
389            data: outboard,
390        })
391    }
392
393    fn db(&self) -> &meta::Db {
394        &self.global.db
395    }
396
397    pub fn options(&self) -> &Arc<Options> {
398        &self.global.options
399    }
400
401    pub fn protect(&self, parts: impl IntoIterator<Item = BaoFilePart>) {
402        self.global.protect.protect(self.id, parts);
403    }
404
405    /// Update the entry state in the database, and wait for completion.
406    pub async fn update_await(&self, state: EntryState<Bytes>) -> io::Result<()> {
407        self.db().update_await(self.id, state).await?;
408        Ok(())
409    }
410
411    pub async fn get_entry_state(&self) -> io::Result<Option<EntryState<Bytes>>> {
412        let hash = self.id;
413        if hash == Hash::EMPTY {
414            return Ok(Some(EntryState::Complete {
415                data_location: DataLocation::Inline(Bytes::new()),
416                outboard_location: OutboardLocation::NotNeeded,
417            }));
418        };
419        self.db().get(hash).await
420    }
421
422    /// Update the entry state in the database, and wait for completion.
423    pub async fn set(&self, state: EntryState<Bytes>) -> io::Result<()> {
424        self.db().set(self.id, state).await
425    }
426}
427
428impl Actor {
429    fn db(&self) -> &meta::Db {
430        &self.context.db
431    }
432
433    fn context(&self) -> Arc<TaskContext> {
434        self.context.clone()
435    }
436
437    fn spawn(&mut self, fut: impl Future<Output = ()> + Send + 'static) {
438        let span = tracing::Span::current();
439        self.tasks.spawn(fut.instrument(span));
440    }
441
442    fn log_task_result(res: Result<(), JoinError>) {
443        match res {
444            Ok(_) => {}
445            Err(e) => {
446                error!("task failed: {e}");
447            }
448        }
449    }
450
451    async fn create_temp_tag(&mut self, cmd: CreateTempTagMsg) {
452        let CreateTempTagMsg { tx, inner, .. } = cmd;
453        let mut tt = self.temp_tags.create(inner.scope, inner.value);
454        if tx.is_rpc() {
455            tt.leak();
456        }
457        tx.send(tt).await.ok();
458    }
459
460    async fn handle_command(&mut self, cmd: Command) {
461        let span = cmd.parent_span();
462        let _entered = span.enter();
463        match cmd {
464            Command::SyncDb(cmd) => {
465                trace!("{cmd:?}");
466                self.db().send(cmd.into()).await.ok();
467            }
468            Command::WaitIdle(cmd) => {
469                trace!("{cmd:?}");
470                if self.tasks.is_empty() {
471                    // we are currently idle
472                    cmd.tx.send(()).await.ok();
473                } else {
474                    // wait for idle state
475                    self.idle_waiters.push(cmd.tx);
476                }
477            }
478            Command::Shutdown(cmd) => {
479                trace!("{cmd:?}");
480                self.db().send(cmd.into()).await.ok();
481            }
482            Command::CreateTag(cmd) => {
483                trace!("{cmd:?}");
484                self.db().send(cmd.into()).await.ok();
485            }
486            Command::SetTag(cmd) => {
487                trace!("{cmd:?}");
488                self.db().send(cmd.into()).await.ok();
489            }
490            Command::ListTags(cmd) => {
491                trace!("{cmd:?}");
492                self.db().send(cmd.into()).await.ok();
493            }
494            Command::DeleteTags(cmd) => {
495                trace!("{cmd:?}");
496                self.db().send(cmd.into()).await.ok();
497            }
498            Command::RenameTag(cmd) => {
499                trace!("{cmd:?}");
500                self.db().send(cmd.into()).await.ok();
501            }
502            Command::ClearProtected(cmd) => {
503                trace!("{cmd:?}");
504                self.db().send(cmd.into()).await.ok();
505            }
506            Command::BlobStatus(cmd) => {
507                trace!("{cmd:?}");
508                self.db().send(cmd.into()).await.ok();
509            }
510            Command::DeleteBlobs(cmd) => {
511                trace!("{cmd:?}");
512                self.db().send(cmd.into()).await.ok();
513            }
514            Command::ListBlobs(cmd) => {
515                trace!("{cmd:?}");
516                if let Ok(snapshot) = self.db().snapshot(cmd.span.clone()).await {
517                    self.spawn(list_blobs(snapshot, cmd));
518                }
519            }
520            Command::Batch(cmd) => {
521                trace!("{cmd:?}");
522                let (id, scope) = self.temp_tags.create_scope();
523                self.spawn(handle_batch(cmd, id, scope, self.context()));
524            }
525            Command::CreateTempTag(cmd) => {
526                trace!("{cmd:?}");
527                self.create_temp_tag(cmd).await;
528            }
529            Command::ListTempTags(cmd) => {
530                trace!("{cmd:?}");
531                let tts = self.temp_tags.list();
532                cmd.tx.send(tts).await.ok();
533            }
534            Command::ImportBytes(cmd) => {
535                trace!("{cmd:?}");
536                self.spawn(import_bytes(cmd, self.context()));
537            }
538            Command::ImportByteStream(cmd) => {
539                trace!("{cmd:?}");
540                self.spawn(import_byte_stream(cmd, self.context()));
541            }
542            Command::ImportPath(cmd) => {
543                trace!("{cmd:?}");
544                self.spawn(import_path(cmd, self.context()));
545            }
546            Command::ExportPath(cmd) => {
547                trace!("{cmd:?}");
548                cmd.spawn(&mut self.handles, &mut self.tasks).await;
549            }
550            Command::ExportBao(cmd) => {
551                trace!("{cmd:?}");
552                cmd.spawn(&mut self.handles, &mut self.tasks).await;
553            }
554            Command::ExportRanges(cmd) => {
555                trace!("{cmd:?}");
556                cmd.spawn(&mut self.handles, &mut self.tasks).await;
557            }
558            Command::ImportBao(cmd) => {
559                trace!("{cmd:?}");
560                cmd.spawn(&mut self.handles, &mut self.tasks).await;
561            }
562            Command::Observe(cmd) => {
563                trace!("{cmd:?}");
564                cmd.spawn(&mut self.handles, &mut self.tasks).await;
565            }
566        }
567    }
568
569    async fn handle_fs_command(&mut self, cmd: InternalCommand) {
570        let span = cmd.parent_span();
571        let _entered = span.enter();
572        match cmd {
573            InternalCommand::Dump(cmd) => {
574                trace!("{cmd:?}");
575                self.db().send(cmd.into()).await.ok();
576            }
577            InternalCommand::ClearScope(cmd) => {
578                trace!("{cmd:?}");
579                self.temp_tags.end_scope(cmd.scope);
580            }
581            InternalCommand::FinishImport(cmd) => {
582                trace!("{cmd:?}");
583                if cmd.hash == Hash::EMPTY {
584                    cmd.tx
585                        .send(AddProgressItem::Done(TempTag::leaking_empty(cmd.format)))
586                        .await
587                        .ok();
588                } else {
589                    let tt = self.temp_tags.create(
590                        cmd.scope,
591                        HashAndFormat {
592                            hash: cmd.hash,
593                            format: cmd.format,
594                        },
595                    );
596                    (tt, cmd).spawn(&mut self.handles, &mut self.tasks).await;
597                }
598            }
599        }
600    }
601
602    async fn run(mut self) {
603        loop {
604            tokio::select! {
605                task = self.handles.tick() => {
606                    if let Some(task) = task {
607                        self.spawn(task);
608                    }
609                }
610                cmd = self.cmd_rx.recv() => {
611                    let Some(cmd) = cmd else {
612                        break;
613                    };
614                    self.handle_command(cmd).await;
615                }
616                Some(cmd) = self.fs_cmd_rx.recv() => {
617                    self.handle_fs_command(cmd).await;
618                }
619                Some(res) = self.tasks.join_next(), if !self.tasks.is_empty() => {
620                    Self::log_task_result(res);
621                    if self.tasks.is_empty() {
622                        for tx in self.idle_waiters.drain(..) {
623                            tx.send(()).await.ok();
624                        }
625                    }
626                }
627            }
628        }
629        self.handles.shutdown().await;
630        while let Some(res) = self.tasks.join_next().await {
631            Self::log_task_result(res);
632        }
633    }
634
635    async fn new(
636        db_path: PathBuf,
637        rt: RtWrapper,
638        cmd_rx: tokio::sync::mpsc::Receiver<Command>,
639        fs_commands_rx: tokio::sync::mpsc::Receiver<InternalCommand>,
640        fs_commands_tx: tokio::sync::mpsc::Sender<InternalCommand>,
641        options: Arc<Options>,
642    ) -> Result<Self> {
643        trace!(
644            "creating data directory: {}",
645            options.path.data_path.display()
646        );
647        fs::create_dir_all(&options.path.data_path)?;
648        trace!(
649            "creating temp directory: {}",
650            options.path.temp_path.display()
651        );
652        fs::create_dir_all(&options.path.temp_path)?;
653        trace!(
654            "creating parent directory for db file{}",
655            db_path.parent().unwrap().display()
656        );
657        fs::create_dir_all(db_path.parent().unwrap())?;
658        let (db_send, db_recv) = tokio::sync::mpsc::channel(100);
659        let (protect, ds) = delete_set::pair(Arc::new(options.path.clone()));
660        let db_actor = meta::Actor::new(db_path, db_recv, ds, options.batch.clone())?;
661        let slot_context = Arc::new(TaskContext {
662            options: options.clone(),
663            db: meta::Db::new(db_send),
664            internal_cmd_tx: fs_commands_tx,
665            protect,
666        });
667        rt.spawn(db_actor.run());
668        Ok(Self {
669            context: slot_context.clone(),
670            cmd_rx,
671            fs_cmd_rx: fs_commands_rx,
672            tasks: JoinSet::new(),
673            handles: EntityManagerState::new(slot_context, 1024, 32, 32, 2),
674            temp_tags: Default::default(),
675            idle_waiters: Vec::new(),
676            _rt: rt,
677        })
678    }
679}
680
681trait HashSpecificCommand: HashSpecific + Send + 'static {
682    /// Handle the command on success by spawning a task into the per-hash context.
683    fn handle(self, ctx: HashContext) -> impl Future<Output = ()> + Send + 'static;
684
685    /// Opportunity to send an error if spawning fails due to the task being busy (inbox full)
686    /// or dead (e.g. panic in one of the running tasks).
687    fn on_error(self, arg: SpawnArg<EmParams>) -> impl Future<Output = ()> + Send + 'static;
688
689    async fn spawn(
690        self,
691        manager: &mut entity_manager::EntityManagerState<EmParams>,
692        tasks: &mut JoinSet<()>,
693    ) where
694        Self: Sized,
695    {
696        let span = tracing::Span::current();
697        let task = manager
698            .spawn(self.hash(), |arg| {
699                async move {
700                    match arg {
701                        SpawnArg::Active(state) => {
702                            self.handle(state).await;
703                        }
704                        SpawnArg::Busy => {
705                            self.on_error(arg).await;
706                        }
707                        SpawnArg::Dead => {
708                            self.on_error(arg).await;
709                        }
710                    }
711                }
712                .instrument(span)
713            })
714            .await;
715        if let Some(task) = task {
716            tasks.spawn(task);
717        }
718    }
719}
720
721impl HashSpecificCommand for ObserveMsg {
722    async fn handle(self, ctx: HashContext) {
723        ctx.observe(self).await
724    }
725    async fn on_error(self, _arg: SpawnArg<EmParams>) {}
726}
727impl HashSpecificCommand for ExportPathMsg {
728    async fn handle(self, ctx: HashContext) {
729        ctx.export_path(self).await
730    }
731    async fn on_error(self, arg: SpawnArg<EmParams>) {
732        let err = match arg {
733            SpawnArg::Busy => io::ErrorKind::ResourceBusy.into(),
734            SpawnArg::Dead => io::Error::other("entity is dead"),
735            _ => unreachable!(),
736        };
737        self.tx
738            .send(ExportProgressItem::Error(api::Error::from(err)))
739            .await
740            .ok();
741    }
742}
743impl HashSpecificCommand for ExportBaoMsg {
744    async fn handle(self, ctx: HashContext) {
745        ctx.export_bao(self).await
746    }
747    async fn on_error(self, arg: SpawnArg<EmParams>) {
748        let err = match arg {
749            SpawnArg::Busy => io::ErrorKind::ResourceBusy.into(),
750            SpawnArg::Dead => io::Error::other("entity is dead"),
751            _ => unreachable!(),
752        };
753        self.tx
754            .send(EncodedItem::Error(bao_tree::io::EncodeError::Io(err)))
755            .await
756            .ok();
757    }
758}
759impl HashSpecificCommand for ExportRangesMsg {
760    async fn handle(self, ctx: HashContext) {
761        ctx.export_ranges(self).await
762    }
763    async fn on_error(self, arg: SpawnArg<EmParams>) {
764        let err = match arg {
765            SpawnArg::Busy => io::ErrorKind::ResourceBusy.into(),
766            SpawnArg::Dead => io::Error::other("entity is dead"),
767            _ => unreachable!(),
768        };
769        self.tx
770            .send(ExportRangesItem::Error(api::Error::from(err)))
771            .await
772            .ok();
773    }
774}
775impl HashSpecificCommand for ImportBaoMsg {
776    async fn handle(self, ctx: HashContext) {
777        ctx.import_bao(self).await
778    }
779    async fn on_error(self, arg: SpawnArg<EmParams>) {
780        let err = match arg {
781            SpawnArg::Busy => io::ErrorKind::ResourceBusy.into(),
782            SpawnArg::Dead => io::Error::other("entity is dead"),
783            _ => unreachable!(),
784        };
785        self.tx.send(Err(api::Error::from(err))).await.ok();
786    }
787}
788impl HashSpecific for (TempTag, ImportEntryMsg) {
789    fn hash(&self) -> Hash {
790        self.1.hash()
791    }
792}
793impl HashSpecificCommand for (TempTag, ImportEntryMsg) {
794    async fn handle(self, ctx: HashContext) {
795        let (tt, cmd) = self;
796        ctx.finish_import(cmd, tt).await
797    }
798    async fn on_error(self, arg: SpawnArg<EmParams>) {
799        let err = match arg {
800            SpawnArg::Busy => io::ErrorKind::ResourceBusy.into(),
801            SpawnArg::Dead => io::Error::other("entity is dead"),
802            _ => unreachable!(),
803        };
804        self.1.tx.send(AddProgressItem::Error(err)).await.ok();
805    }
806}
807
808struct RtWrapper(Option<tokio::runtime::Runtime>);
809
810impl From<tokio::runtime::Runtime> for RtWrapper {
811    fn from(rt: tokio::runtime::Runtime) -> Self {
812        Self(Some(rt))
813    }
814}
815
816impl fmt::Debug for RtWrapper {
817    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
818        ValueOrPoisioned(self.0.as_ref()).fmt(f)
819    }
820}
821
822impl Deref for RtWrapper {
823    type Target = tokio::runtime::Runtime;
824
825    fn deref(&self) -> &Self::Target {
826        self.0.as_ref().unwrap()
827    }
828}
829
830impl Drop for RtWrapper {
831    fn drop(&mut self) {
832        if let Some(rt) = self.0.take() {
833            trace!("dropping tokio runtime");
834            tokio::task::block_in_place(|| {
835                drop(rt);
836            });
837            trace!("dropped tokio runtime");
838        }
839    }
840}
841
842async fn handle_batch(cmd: BatchMsg, id: Scope, scope: Arc<TempTagScope>, ctx: Arc<TaskContext>) {
843    if let Err(cause) = handle_batch_impl(cmd, id, &scope).await {
844        error!("batch failed: {cause}");
845    }
846    ctx.clear_scope(id).await;
847}
848
849async fn handle_batch_impl(cmd: BatchMsg, id: Scope, scope: &Arc<TempTagScope>) -> api::Result<()> {
850    let BatchMsg { tx, mut rx, .. } = cmd;
851    trace!("created scope {}", id);
852    tx.send(id).await.map_err(api::Error::other)?;
853    while let Some(msg) = rx.recv().await? {
854        match msg {
855            BatchResponse::Drop(msg) => scope.on_drop(&msg),
856            BatchResponse::Ping => {}
857        }
858    }
859    Ok(())
860}
861
862/// The minimal API you need to implement for an entity for a store to work.
863trait EntityApi {
864    /// Import from a stream of n0 bao encoded data.
865    async fn import_bao(&self, cmd: ImportBaoMsg);
866    /// Finish an import from a local file or memory.
867    async fn finish_import(&self, cmd: ImportEntryMsg, tt: TempTag);
868    /// Observe the bitfield of the entry.
869    async fn observe(&self, cmd: ObserveMsg);
870    /// Export byte ranges of the entry as data
871    async fn export_ranges(&self, cmd: ExportRangesMsg);
872    /// Export chunk ranges of the entry as a n0 bao encoded stream.
873    async fn export_bao(&self, cmd: ExportBaoMsg);
874    /// Export the entry to a local file.
875    async fn export_path(&self, cmd: ExportPathMsg);
876    /// Persist the entry at the end of its lifecycle.
877    async fn persist(&self);
878}
879
880/// A more opinionated API that can be used as a helper to save implementation
881/// effort when implementing the EntityApi trait.
882trait SyncEntityApi: EntityApi {
883    /// Load the entry state from the database. This must make sure that it is
884    /// not run concurrently, so if load is called multiple times, all but one
885    /// must wait. You can use a tokio::sync::OnceCell or similar to achieve this.
886    async fn load(&self);
887
888    /// Get a synchronous reader for the data file.
889    fn data_reader(&self) -> impl ReadBytesAt;
890
891    /// Get a synchronous reader for the outboard file.
892    fn outboard_reader(&self) -> impl ReadAt;
893
894    /// Get the best known size of the data file.
895    fn current_size(&self) -> io::Result<u64>;
896
897    /// Get the bitfield of the entry.
898    fn bitfield(&self) -> io::Result<Bitfield>;
899
900    /// Write a batch of content items to the entry.
901    async fn write_batch(&self, batch: &[BaoContentItem], bitfield: &Bitfield) -> io::Result<()>;
902}
903
904/// The high level entry point per entry.
905impl EntityApi for HashContext {
906    #[instrument(skip_all, fields(hash = %cmd.hash_short()))]
907    async fn import_bao(&self, cmd: ImportBaoMsg) {
908        trace!("{cmd:?}");
909        self.load().await;
910        let ImportBaoMsg {
911            inner: ImportBaoRequest { size, .. },
912            rx,
913            tx,
914            ..
915        } = cmd;
916        let res = import_bao_impl(self, size, rx).await;
917        trace!("{res:?}");
918        tx.send(res).await.ok();
919    }
920
921    #[instrument(skip_all, fields(hash = %cmd.hash_short()))]
922    async fn observe(&self, cmd: ObserveMsg) {
923        trace!("{cmd:?}");
924        self.load().await;
925        BaoFileStorageSubscriber::new(self.state.subscribe())
926            .forward(cmd.tx)
927            .await
928            .ok();
929    }
930
931    #[instrument(skip_all, fields(hash = %cmd.hash_short()))]
932    async fn export_ranges(&self, mut cmd: ExportRangesMsg) {
933        trace!("{cmd:?}");
934        self.load().await;
935        if let Err(cause) = export_ranges_impl(self, cmd.inner, &mut cmd.tx).await {
936            cmd.tx
937                .send(ExportRangesItem::Error(cause.into()))
938                .await
939                .ok();
940        }
941    }
942
943    #[instrument(skip_all, fields(hash = %cmd.hash_short()))]
944    async fn export_bao(&self, mut cmd: ExportBaoMsg) {
945        trace!("{cmd:?}");
946        self.load().await;
947        if let Err(cause) = export_bao_impl(self, cmd.inner, &mut cmd.tx).await {
948            // if the entry is in state NonExisting, this will be an io error with
949            // kind NotFound. So we must not wrap this somehow but pass it on directly.
950            cmd.tx
951                .send(bao_tree::io::EncodeError::Io(cause).into())
952                .await
953                .ok();
954        }
955    }
956
957    #[instrument(skip_all, fields(hash = %cmd.hash_short()))]
958    async fn export_path(&self, cmd: ExportPathMsg) {
959        trace!("{cmd:?}");
960        self.load().await;
961        let ExportPathMsg { inner, mut tx, .. } = cmd;
962        if let Err(cause) = export_path_impl(self, inner, &mut tx).await {
963            tx.send(cause.into()).await.ok();
964        }
965    }
966
967    #[instrument(skip_all, fields(hash = %cmd.hash_short()))]
968    async fn finish_import(&self, cmd: ImportEntryMsg, mut tt: TempTag) {
969        trace!("{cmd:?}");
970        self.load().await;
971        let res = match finish_import_impl(self, cmd.inner).await {
972            Ok(()) => {
973                // for a remote call, we can't have the on_drop callback, so we have to leak the temp tag
974                // it will be cleaned up when either the process exits or scope ends
975                if cmd.tx.is_rpc() {
976                    trace!("leaking temp tag {}", tt.hash_and_format());
977                    tt.leak();
978                }
979                AddProgressItem::Done(tt)
980            }
981            Err(cause) => AddProgressItem::Error(cause),
982        };
983        cmd.tx.send(res).await.ok();
984    }
985
986    #[instrument(skip_all, fields(hash = %self.id.fmt_short()))]
987    async fn persist(&self) {
988        self.state.send_if_modified(|guard| {
989            let hash = &self.id;
990            let BaoFileStorage::Partial(fs) = guard.take() else {
991                return false;
992            };
993            let path = self.global.options.path.bitfield_path(hash);
994            trace!("writing bitfield for hash {} to {}", hash, path.display());
995            if let Err(cause) = fs.sync_all(&path) {
996                error!(
997                    "failed to write bitfield for {} at {}: {:?}",
998                    hash,
999                    path.display(),
1000                    cause
1001                );
1002            }
1003            false
1004        });
1005    }
1006}
1007
1008async fn finish_import_impl(ctx: &HashContext, import_data: ImportEntry) -> io::Result<()> {
1009    if ctx.id == Hash::EMPTY {
1010        return Ok(()); // nothing to do for the empty hash
1011    }
1012    let ImportEntry {
1013        source,
1014        hash,
1015        outboard,
1016        ..
1017    } = import_data;
1018    let options = ctx.options();
1019    match &source {
1020        ImportSource::Memory(data) => {
1021            debug_assert!(options.is_inlined_data(data.len() as u64));
1022        }
1023        ImportSource::External(_, _, size) => {
1024            debug_assert!(!options.is_inlined_data(*size));
1025        }
1026        ImportSource::TempFile(_, _, size) => {
1027            debug_assert!(!options.is_inlined_data(*size));
1028        }
1029    }
1030    ctx.load().await;
1031    let handle = &ctx.state;
1032    // if I do have an existing handle, I have to possibly deal with observers.
1033    // if I don't have an existing handle, there are 2 cases:
1034    //   the entry exists in the db, but we don't have a handle
1035    //   the entry does not exist at all.
1036    // convert the import source to a data location and drop the open files
1037    ctx.protect([BaoFilePart::Data, BaoFilePart::Outboard]);
1038    let data_location = match source {
1039        ImportSource::Memory(data) => DataLocation::Inline(data),
1040        ImportSource::External(path, _file, size) => DataLocation::External(vec![path], size),
1041        ImportSource::TempFile(path, _file, size) => {
1042            // this will always work on any unix, but on windows there might be an issue if the target file is open!
1043            // possibly open with FILE_SHARE_DELETE on windows?
1044            let target = ctx.options().path.data_path(&hash);
1045            trace!(
1046                "moving temp file to owned data location: {} -> {}",
1047                path.display(),
1048                target.display()
1049            );
1050            if let Err(cause) = fs::rename(&path, &target) {
1051                error!(
1052                    "failed to move temp file {} to owned data location {}: {cause}",
1053                    path.display(),
1054                    target.display()
1055                );
1056            }
1057            DataLocation::Owned(size)
1058        }
1059    };
1060    let outboard_location = match outboard {
1061        MemOrFile::Mem(bytes) if bytes.is_empty() => OutboardLocation::NotNeeded,
1062        MemOrFile::Mem(bytes) => OutboardLocation::Inline(bytes),
1063        MemOrFile::File(path) => {
1064            // the same caveat as above applies here
1065            let target = ctx.options().path.outboard_path(&hash);
1066            trace!(
1067                "moving temp file to owned outboard location: {} -> {}",
1068                path.display(),
1069                target.display()
1070            );
1071            if let Err(cause) = fs::rename(&path, &target) {
1072                error!(
1073                    "failed to move temp file {} to owned outboard location {}: {cause}",
1074                    path.display(),
1075                    target.display()
1076                );
1077            }
1078            OutboardLocation::Owned
1079        }
1080    };
1081    let data = match &data_location {
1082        DataLocation::Inline(data) => MemOrFile::Mem(data.clone()),
1083        DataLocation::Owned(size) => {
1084            let path = ctx.options().path.data_path(&hash);
1085            let file = fs::File::open(&path)?;
1086            MemOrFile::File(FixedSize::new(file, *size))
1087        }
1088        DataLocation::External(paths, size) => {
1089            let Some(path) = paths.iter().next() else {
1090                return Err(io::Error::other("no external data path"));
1091            };
1092            let file = fs::File::open(path)?;
1093            MemOrFile::File(FixedSize::new(file, *size))
1094        }
1095    };
1096    let outboard = match &outboard_location {
1097        OutboardLocation::NotNeeded => MemOrFile::empty(),
1098        OutboardLocation::Inline(data) => MemOrFile::Mem(data.clone()),
1099        OutboardLocation::Owned => {
1100            let path = ctx.options().path.outboard_path(&hash);
1101            let file = fs::File::open(&path)?;
1102            MemOrFile::File(file)
1103        }
1104    };
1105    handle.complete(data, outboard);
1106    let state = EntryState::Complete {
1107        data_location,
1108        outboard_location,
1109    };
1110    ctx.update_await(state).await?;
1111    Ok(())
1112}
1113
1114fn chunk_range(leaf: &Leaf) -> ChunkRanges {
1115    let start = ChunkNum::chunks(leaf.offset);
1116    let end = ChunkNum::chunks(leaf.offset + leaf.data.len() as u64);
1117    (start..end).into()
1118}
1119
1120async fn import_bao_impl(
1121    ctx: &HashContext,
1122    size: NonZeroU64,
1123    mut rx: mpsc::Receiver<BaoContentItem>,
1124) -> api::Result<()> {
1125    trace!("importing bao: {} {} bytes", ctx.id.fmt_short(), size);
1126    let mut batch = Vec::<BaoContentItem>::new();
1127    let mut ranges = ChunkRanges::empty();
1128    while let Some(item) = rx.recv().await? {
1129        // if the batch is not empty, the last item is a leaf and the current item is a parent, write the batch
1130        if !batch.is_empty() && batch[batch.len() - 1].is_leaf() && item.is_parent() {
1131            let bitfield = Bitfield::new_unchecked(ranges, size.into());
1132            ctx.write_batch(&batch, &bitfield).await?;
1133            batch.clear();
1134            ranges = ChunkRanges::empty();
1135        }
1136        if let BaoContentItem::Leaf(leaf) = &item {
1137            let leaf_range = chunk_range(leaf);
1138            if is_validated(size, &leaf_range) && size.get() != leaf.offset + leaf.data.len() as u64
1139            {
1140                return Err(api::Error::io(io::ErrorKind::InvalidData, "invalid size"));
1141            }
1142            ranges |= leaf_range;
1143        }
1144        batch.push(item);
1145    }
1146    if !batch.is_empty() {
1147        let bitfield = Bitfield::new_unchecked(ranges, size.into());
1148        ctx.write_batch(&batch, &bitfield).await?;
1149    }
1150    Ok(())
1151}
1152
1153async fn export_ranges_impl(
1154    ctx: &HashContext,
1155    cmd: ExportRangesRequest,
1156    tx: &mut mpsc::Sender<ExportRangesItem>,
1157) -> io::Result<()> {
1158    let ExportRangesRequest { ranges, hash } = cmd;
1159    trace!(
1160        "exporting ranges: {hash} {ranges:?} size={}",
1161        ctx.current_size()?
1162    );
1163    let bitfield = ctx.bitfield()?;
1164    let data = ctx.data_reader();
1165    let size = bitfield.size();
1166    for range in ranges.iter() {
1167        let range = match range {
1168            RangeSetRange::Range(range) => size.min(*range.start)..size.min(*range.end),
1169            RangeSetRange::RangeFrom(range) => size.min(*range.start)..size,
1170        };
1171        let requested = ChunkRanges::bytes(range.start..range.end);
1172        if !bitfield.ranges.is_superset(&requested) {
1173            return Err(io::Error::other(format!(
1174                "missing range: {requested:?}, present: {bitfield:?}",
1175            )));
1176        }
1177        let bs = 1024;
1178        let mut offset = range.start;
1179        loop {
1180            let end: u64 = (offset + bs).min(range.end);
1181            let size = (end - offset) as usize;
1182            let res = data.read_bytes_at(offset, size);
1183            tx.send(ExportRangesItem::Data(Leaf { offset, data: res? }))
1184                .await?;
1185            offset = end;
1186            if offset >= range.end {
1187                break;
1188            }
1189        }
1190    }
1191    Ok(())
1192}
1193
1194async fn export_bao_impl(
1195    ctx: &HashContext,
1196    cmd: ExportBaoRequest,
1197    tx: &mut mpsc::Sender<EncodedItem>,
1198) -> io::Result<()> {
1199    let ExportBaoRequest { ranges, hash, .. } = cmd;
1200    let outboard = ctx.outboard()?;
1201    let size = outboard.tree.size();
1202    if size == 0 && cmd.hash != Hash::EMPTY {
1203        // we have no data whatsoever, so we stop here
1204        return Ok(());
1205    }
1206    trace!("exporting bao: {hash} {ranges:?} size={size}",);
1207    let data = ctx.data_reader();
1208    let tx = BaoTreeSender::new(tx);
1209    traverse_ranges_validated(data, outboard, &ranges, tx).await?;
1210    Ok(())
1211}
1212
1213async fn export_path_impl(
1214    ctx: &HashContext,
1215    cmd: ExportPathRequest,
1216    tx: &mut mpsc::Sender<ExportProgressItem>,
1217) -> api::Result<()> {
1218    let ExportPathRequest { mode, target, .. } = cmd;
1219    if !target.is_absolute() {
1220        return Err(api::Error::io(
1221            io::ErrorKind::InvalidInput,
1222            "path is not absolute",
1223        ));
1224    }
1225    if let Some(parent) = target.parent() {
1226        fs::create_dir_all(parent)?;
1227    }
1228    let state = ctx.get_entry_state().await?;
1229    let (data_location, outboard_location) = match state {
1230        Some(EntryState::Complete {
1231            data_location,
1232            outboard_location,
1233        }) => (data_location, outboard_location),
1234        Some(EntryState::Partial { .. }) => {
1235            return Err(api::Error::io(
1236                io::ErrorKind::InvalidInput,
1237                "cannot export partial entry",
1238            ));
1239        }
1240        None => {
1241            return Err(api::Error::io(io::ErrorKind::NotFound, "no entry found"));
1242        }
1243    };
1244    trace!("exporting {} to {}", cmd.hash.to_hex(), target.display());
1245    let (data, mut external) = match data_location {
1246        DataLocation::Inline(data) => (MemOrFile::Mem(data), vec![]),
1247        DataLocation::Owned(size) => (
1248            MemOrFile::File((ctx.options().path.data_path(&cmd.hash), size)),
1249            vec![],
1250        ),
1251        DataLocation::External(paths, size) => (
1252            MemOrFile::File((
1253                paths.first().cloned().ok_or_else(|| {
1254                    io::Error::new(io::ErrorKind::NotFound, "no external data path")
1255                })?,
1256                size,
1257            )),
1258            paths,
1259        ),
1260    };
1261    let size = match &data {
1262        MemOrFile::Mem(data) => data.len() as u64,
1263        MemOrFile::File((_, size)) => *size,
1264    };
1265    tx.send(ExportProgressItem::Size(size))
1266        .await
1267        .map_err(api::Error::other)?;
1268    match data {
1269        MemOrFile::Mem(data) => {
1270            let mut target = fs::File::create(&target)?;
1271            target.write_all(&data)?;
1272        }
1273        MemOrFile::File((source_path, size)) => match mode {
1274            ExportMode::Copy => {
1275                let res = reflink_or_copy_with_progress(&source_path, &target, size, tx).await?;
1276                trace!(
1277                    "exported {} to {}, {res:?}",
1278                    source_path.display(),
1279                    target.display()
1280                );
1281            }
1282            ExportMode::TryReference => {
1283                if !external.is_empty() {
1284                    // the file already exists externally, so we need to copy it.
1285                    // if the OS supports reflink, we might as well use that.
1286                    let res =
1287                        reflink_or_copy_with_progress(&source_path, &target, size, tx).await?;
1288                    trace!(
1289                        "exported {} also to {}, {res:?}",
1290                        source_path.display(),
1291                        target.display()
1292                    );
1293                    external.push(target);
1294                    external.sort();
1295                    external.dedup();
1296                    external.truncate(MAX_EXTERNAL_PATHS);
1297                } else {
1298                    // the file was previously owned, so we can just move it.
1299                    // if that fails with ERR_CROSS, we fall back to copy.
1300                    match std::fs::rename(&source_path, &target) {
1301                        Ok(()) => {}
1302                        Err(cause) => {
1303                            const ERR_CROSS: i32 = 18;
1304                            if cause.raw_os_error() == Some(ERR_CROSS) {
1305                                reflink_or_copy_with_progress(&source_path, &target, size, tx)
1306                                    .await?;
1307                            } else {
1308                                return Err(cause.into());
1309                            }
1310                        }
1311                    }
1312                    external.push(target);
1313                };
1314                // setting the new entry state will also take care of deleting the owned data file!
1315                ctx.set(EntryState::Complete {
1316                    data_location: DataLocation::External(external, size),
1317                    outboard_location,
1318                })
1319                .await?;
1320            }
1321        },
1322    }
1323    tx.send(ExportProgressItem::Done)
1324        .await
1325        .map_err(api::Error::other)?;
1326    Ok(())
1327}
1328
1329trait CopyProgress: RpcMessage {
1330    fn from_offset(offset: u64) -> Self;
1331}
1332
1333impl CopyProgress for ExportProgressItem {
1334    fn from_offset(offset: u64) -> Self {
1335        ExportProgressItem::CopyProgress(offset)
1336    }
1337}
1338
1339impl CopyProgress for AddProgressItem {
1340    fn from_offset(offset: u64) -> Self {
1341        AddProgressItem::CopyProgress(offset)
1342    }
1343}
1344
1345#[derive(Debug)]
1346enum CopyResult {
1347    Reflinked,
1348    Copied,
1349}
1350
1351async fn reflink_or_copy_with_progress(
1352    from: impl AsRef<Path>,
1353    to: impl AsRef<Path>,
1354    size: u64,
1355    tx: &mut mpsc::Sender<impl CopyProgress>,
1356) -> io::Result<CopyResult> {
1357    let from = from.as_ref();
1358    let to = to.as_ref();
1359    if reflink_copy::reflink(from, to).is_ok() {
1360        return Ok(CopyResult::Reflinked);
1361    }
1362    let source = fs::File::open(from)?;
1363    let mut target = fs::File::create(to)?;
1364    copy_with_progress(source, size, &mut target, tx).await?;
1365    Ok(CopyResult::Copied)
1366}
1367
1368async fn copy_with_progress<T: CopyProgress>(
1369    file: impl ReadAt,
1370    size: u64,
1371    target: &mut impl Write,
1372    tx: &mut mpsc::Sender<T>,
1373) -> io::Result<()> {
1374    let mut offset = 0;
1375    let mut buf = vec![0u8; 1024 * 1024];
1376    while offset < size {
1377        let remaining = buf.len().min((size - offset) as usize);
1378        let buf: &mut [u8] = &mut buf[..remaining];
1379        file.read_exact_at(offset, buf)?;
1380        target.write_all(buf)?;
1381        tx.try_send(T::from_offset(offset))
1382            .await
1383            .map_err(|_e| io::Error::other(""))?;
1384        yield_now().await;
1385        offset += buf.len() as u64;
1386    }
1387    Ok(())
1388}
1389
1390impl FsStore {
1391    /// Load or create a new store.
1392    pub async fn load(root: impl AsRef<Path>) -> Result<Self> {
1393        let path = root.as_ref();
1394        let db_path = path.join("blobs.db");
1395        let options = Options::new(path);
1396        Self::load_with_opts(db_path, options).await
1397    }
1398
1399    /// Load or create a new store with custom options, returning an additional sender for file store specific commands.
1400    pub async fn load_with_opts(db_path: PathBuf, options: Options) -> Result<FsStore> {
1401        static THREAD_NR: AtomicU64 = AtomicU64::new(0);
1402        let rt = tokio::runtime::Builder::new_multi_thread()
1403            .thread_name_fn(|| {
1404                format!(
1405                    "iroh-blob-store-{}",
1406                    THREAD_NR.fetch_add(1, Ordering::Relaxed)
1407                )
1408            })
1409            .enable_time()
1410            .build()?;
1411        let handle = rt.handle().clone();
1412        let (commands_tx, commands_rx) = tokio::sync::mpsc::channel(100);
1413        let (fs_commands_tx, fs_commands_rx) = tokio::sync::mpsc::channel(100);
1414        let gc_config = options.gc.clone();
1415        let actor = handle
1416            .spawn(Actor::new(
1417                db_path,
1418                rt.into(),
1419                commands_rx,
1420                fs_commands_rx,
1421                fs_commands_tx.clone(),
1422                Arc::new(options),
1423            ))
1424            .await
1425            .anyerr()??;
1426        handle.spawn(actor.run());
1427        let store = FsStore::new(commands_tx.into(), fs_commands_tx);
1428        if let Some(config) = gc_config {
1429            handle.spawn(run_gc(store.deref().clone(), config));
1430        }
1431        Ok(store)
1432    }
1433}
1434
1435/// A file based store.
1436///
1437/// A store can be created using [`load`](FsStore::load) or [`load_with_opts`](FsStore::load_with_opts).
1438/// Load will use the default options and create the required directories, while load_with_opts allows
1439/// you to customize the options and the location of the database. Both variants will create the database
1440/// if it does not exist, and load an existing database if one is found at the configured location.
1441///
1442/// In addition to implementing the [`Store`](`crate::api::Store`) API via [`Deref`](`std::ops::Deref`),
1443/// there are a few additional methods that are specific to file based stores, such as [`dump`](FsStore::dump).
1444#[derive(Debug, Clone)]
1445pub struct FsStore {
1446    sender: ApiClient,
1447    db: tokio::sync::mpsc::Sender<InternalCommand>,
1448}
1449
1450impl From<FsStore> for Store {
1451    fn from(value: FsStore) -> Self {
1452        Store::from_sender(value.sender)
1453    }
1454}
1455
1456impl Deref for FsStore {
1457    type Target = Store;
1458
1459    fn deref(&self) -> &Self::Target {
1460        Store::ref_from_sender(&self.sender)
1461    }
1462}
1463
1464impl AsRef<Store> for FsStore {
1465    fn as_ref(&self) -> &Store {
1466        self.deref()
1467    }
1468}
1469
1470impl FsStore {
1471    fn new(
1472        sender: irpc::LocalSender<proto::Request>,
1473        db: tokio::sync::mpsc::Sender<InternalCommand>,
1474    ) -> Self {
1475        Self {
1476            sender: sender.into(),
1477            db,
1478        }
1479    }
1480
1481    pub async fn dump(&self) -> Result<()> {
1482        let (tx, rx) = oneshot::channel();
1483        self.db
1484            .send(
1485                meta::Dump {
1486                    tx,
1487                    span: tracing::Span::current(),
1488                }
1489                .into(),
1490            )
1491            .await
1492            .anyerr()?;
1493        rx.await.anyerr()??;
1494        Ok(())
1495    }
1496}
1497
1498#[cfg(test)]
1499pub mod tests {
1500    use core::panic;
1501    use std::collections::{HashMap, HashSet};
1502
1503    use bao_tree::{io::round_up_to_chunks_groups, ChunkRanges};
1504    use n0_future::{stream, Stream, StreamExt};
1505    use testresult::TestResult;
1506    use walkdir::WalkDir;
1507
1508    use super::*;
1509    use crate::{
1510        api::blobs::Bitfield,
1511        store::{
1512            util::{read_checksummed, tests::create_n0_bao, SliceInfoExt, Tag},
1513            IROH_BLOCK_SIZE,
1514        },
1515    };
1516
1517    /// Interesting sizes for testing.
1518    pub const INTERESTING_SIZES: [usize; 8] = [
1519        0,               // annoying corner case - always present, handled by the api
1520        1,               // less than 1 chunk, data inline, outboard not needed
1521        1024,            // exactly 1 chunk, data inline, outboard not needed
1522        1024 * 16 - 1,   // less than 1 chunk group, data inline, outboard not needed
1523        1024 * 16,       // exactly 1 chunk group, data inline, outboard not needed
1524        1024 * 16 + 1,   // data file, outboard inline (just 1 hash pair)
1525        1024 * 1024,     // data file, outboard inline (many hash pairs)
1526        1024 * 1024 * 8, // data file, outboard file
1527    ];
1528
1529    pub fn round_up_request(size: u64, ranges: &ChunkRanges) -> ChunkRanges {
1530        let last_chunk = ChunkNum::chunks(size);
1531        let data_range = ChunkRanges::from(..last_chunk);
1532        let ranges = if !data_range.intersects(ranges) && !ranges.is_empty() {
1533            if last_chunk == 0 {
1534                ChunkRanges::all()
1535            } else {
1536                ChunkRanges::from(last_chunk - 1..)
1537            }
1538        } else {
1539            ranges.clone()
1540        };
1541        round_up_to_chunks_groups(ranges, IROH_BLOCK_SIZE)
1542    }
1543
1544    fn create_n0_bao_full(
1545        data: &[u8],
1546        ranges: &ChunkRanges,
1547    ) -> n0_error::Result<(Hash, ChunkRanges, Vec<u8>)> {
1548        let ranges = round_up_request(data.len() as u64, ranges);
1549        let (hash, encoded) = create_n0_bao(data, &ranges)?;
1550        Ok((hash, ranges, encoded))
1551    }
1552
1553    #[tokio::test]
1554    // #[traced_test]
1555    async fn test_observe() -> TestResult<()> {
1556        tracing_subscriber::fmt::try_init().ok();
1557        let testdir = tempfile::tempdir()?;
1558        let db_dir = testdir.path().join("db");
1559        let options = Options::new(&db_dir);
1560        let store = FsStore::load_with_opts(db_dir.join("blobs.db"), options).await?;
1561        let sizes = INTERESTING_SIZES;
1562        for size in sizes {
1563            let data = test_data(size);
1564            let ranges = ChunkRanges::all();
1565            let (hash, bao) = create_n0_bao(&data, &ranges)?;
1566            let obs = store.observe(hash);
1567            let task = tokio::spawn(async move {
1568                obs.await_completion().await?;
1569                api::Result::Ok(())
1570            });
1571            store.import_bao_bytes(hash, ranges, bao).await?;
1572            task.await??;
1573        }
1574        Ok(())
1575    }
1576
1577    /// Generate test data for size n.
1578    ///
1579    /// We don't really care about the content, since we assume blake3 works.
1580    /// The only thing it should not be is all zeros, since that is what you
1581    /// will get for a gap.
1582    pub fn test_data(n: usize) -> Bytes {
1583        let mut res = Vec::with_capacity(n);
1584        // Using uppercase A-Z (65-90), 26 possible characters
1585        for i in 0..n {
1586            // Change character every 1024 bytes
1587            let block_num = i / 1024;
1588            // Map to uppercase A-Z range (65-90)
1589            let ascii_val = 65 + (block_num % 26) as u8;
1590            res.push(ascii_val);
1591        }
1592        Bytes::from(res)
1593    }
1594
1595    // import data via import_bytes, check that we can observe it and that it is complete
1596    #[tokio::test]
1597    async fn test_import_byte_stream() -> TestResult<()> {
1598        tracing_subscriber::fmt::try_init().ok();
1599        let testdir = tempfile::tempdir()?;
1600        let db_dir = testdir.path().join("db");
1601        let store = FsStore::load(db_dir).await?;
1602        for size in INTERESTING_SIZES {
1603            let expected = test_data(size);
1604            let expected_hash = Hash::new(&expected);
1605            let stream = bytes_to_stream(expected.clone(), 1023);
1606            let obs = store.observe(expected_hash);
1607            let tt = store.add_stream(stream).await.temp_tag().await?;
1608            assert_eq!(expected_hash, tt.hash());
1609            // we must at some point see completion, otherwise the test will hang
1610            obs.await_completion().await?;
1611            let actual = store.get_bytes(expected_hash).await?;
1612            // check that the data is there
1613            assert_eq!(&expected, &actual);
1614        }
1615        Ok(())
1616    }
1617
1618    // import data via import_bytes, check that we can observe it and that it is complete
1619    #[tokio::test]
1620    async fn test_import_bytes_simple() -> TestResult<()> {
1621        tracing_subscriber::fmt::try_init().ok();
1622        let testdir = tempfile::tempdir()?;
1623        let db_dir = testdir.path().join("db");
1624        let store = FsStore::load(&db_dir).await?;
1625        let sizes = INTERESTING_SIZES;
1626        trace!("{}", Options::new(&db_dir).is_inlined_data(16385));
1627        for size in sizes {
1628            let expected = test_data(size);
1629            let expected_hash = Hash::new(&expected);
1630            let obs = store.observe(expected_hash);
1631            let tt = store.add_bytes(expected.clone()).await?;
1632            assert_eq!(expected_hash, tt.hash);
1633            // we must at some point see completion, otherwise the test will hang
1634            obs.await_completion().await?;
1635            let actual = store.get_bytes(expected_hash).await?;
1636            // check that the data is there
1637            assert_eq!(&expected, &actual);
1638        }
1639        store.shutdown().await?;
1640        dump_dir_full(db_dir)?;
1641        Ok(())
1642    }
1643
1644    // import data via import_bytes, check that we can observe it and that it is complete
1645    #[tokio::test]
1646    #[ignore = "flaky. I need a reliable way to keep the handle alive"]
1647    async fn test_roundtrip_bytes_small() -> TestResult<()> {
1648        tracing_subscriber::fmt::try_init().ok();
1649        let testdir = tempfile::tempdir()?;
1650        let db_dir = testdir.path().join("db");
1651        let store = FsStore::load(db_dir).await?;
1652        for size in INTERESTING_SIZES
1653            .into_iter()
1654            .filter(|x| *x != 0 && *x <= IROH_BLOCK_SIZE.bytes())
1655        {
1656            let expected = test_data(size);
1657            let expected_hash = Hash::new(&expected);
1658            let obs = store.observe(expected_hash);
1659            let tt = store.add_bytes(expected.clone()).await?;
1660            assert_eq!(expected_hash, tt.hash);
1661            let actual = store.get_bytes(expected_hash).await?;
1662            // check that the data is there
1663            assert_eq!(&expected, &actual);
1664            assert_eq!(
1665                &expected.addr(),
1666                &actual.addr(),
1667                "address mismatch for size {size}"
1668            );
1669            // we must at some point see completion, otherwise the test will hang
1670            // keep the handle alive by observing until the end, otherwise the handle
1671            // will change and the bytes won't be the same instance anymore
1672            obs.await_completion().await?;
1673        }
1674        store.shutdown().await?;
1675        Ok(())
1676    }
1677
1678    // import data via import_bytes, check that we can observe it and that it is complete
1679    #[tokio::test]
1680    async fn test_import_path() -> TestResult<()> {
1681        tracing_subscriber::fmt::try_init().ok();
1682        let testdir = tempfile::tempdir()?;
1683        let db_dir = testdir.path().join("db");
1684        let store = FsStore::load(db_dir).await?;
1685        for size in INTERESTING_SIZES {
1686            let expected = test_data(size);
1687            let expected_hash = Hash::new(&expected);
1688            let path = testdir.path().join(format!("in-{size}"));
1689            fs::write(&path, &expected)?;
1690            let obs = store.observe(expected_hash);
1691            let tt = store.add_path(&path).await?;
1692            assert_eq!(expected_hash, tt.hash);
1693            // we must at some point see completion, otherwise the test will hang
1694            obs.await_completion().await?;
1695            let actual = store.get_bytes(expected_hash).await?;
1696            // check that the data is there
1697            assert_eq!(&expected, &actual, "size={size}");
1698        }
1699        dump_dir_full(testdir.path())?;
1700        Ok(())
1701    }
1702
1703    // import data via import_bytes, check that we can observe it and that it is complete
1704    #[tokio::test]
1705    async fn test_export_path() -> TestResult<()> {
1706        tracing_subscriber::fmt::try_init().ok();
1707        let testdir = tempfile::tempdir()?;
1708        let db_dir = testdir.path().join("db");
1709        let store = FsStore::load(db_dir).await?;
1710        for size in INTERESTING_SIZES {
1711            let expected = test_data(size);
1712            let expected_hash = Hash::new(&expected);
1713            let tt = store.add_bytes(expected.clone()).await?;
1714            assert_eq!(expected_hash, tt.hash);
1715            let out_path = testdir.path().join(format!("out-{size}"));
1716            store.export(expected_hash, &out_path).await?;
1717            let actual = fs::read(&out_path)?;
1718            assert_eq!(expected, actual);
1719        }
1720        Ok(())
1721    }
1722
1723    #[tokio::test]
1724    async fn test_import_bao_ranges() -> TestResult<()> {
1725        tracing_subscriber::fmt::try_init().ok();
1726        let testdir = tempfile::tempdir()?;
1727        let db_dir = testdir.path().join("db");
1728        {
1729            let store = FsStore::load(&db_dir).await?;
1730            let data = test_data(100000);
1731            let ranges = ChunkRanges::chunks(16..32);
1732            let (hash, bao) = create_n0_bao(&data, &ranges)?;
1733            store
1734                .import_bao_bytes(hash, ranges.clone(), bao.clone())
1735                .await?;
1736            let bitfield = store.observe(hash).await?;
1737            assert_eq!(bitfield.ranges, ranges);
1738            assert_eq!(bitfield.size(), data.len() as u64);
1739            let export = store.export_bao(hash, ranges).bao_to_vec().await?;
1740            assert_eq!(export, bao);
1741        }
1742        Ok(())
1743    }
1744
1745    #[tokio::test]
1746    async fn test_import_bao_minimal() -> TestResult<()> {
1747        tracing_subscriber::fmt::try_init().ok();
1748        let testdir = tempfile::tempdir()?;
1749        let sizes = [1];
1750        let db_dir = testdir.path().join("db");
1751        {
1752            let store = FsStore::load(&db_dir).await?;
1753            for size in sizes {
1754                let data = vec![0u8; size];
1755                let (hash, encoded) = create_n0_bao(&data, &ChunkRanges::all())?;
1756                let data = Bytes::from(encoded);
1757                store
1758                    .import_bao_bytes(hash, ChunkRanges::all(), data)
1759                    .await?;
1760            }
1761            store.shutdown().await?;
1762        }
1763        Ok(())
1764    }
1765
1766    #[tokio::test]
1767    async fn test_import_bao_simple() -> TestResult<()> {
1768        tracing_subscriber::fmt::try_init().ok();
1769        let testdir = tempfile::tempdir()?;
1770        let sizes = [1048576];
1771        let db_dir = testdir.path().join("db");
1772        {
1773            let store = FsStore::load(&db_dir).await?;
1774            for size in sizes {
1775                let data = vec![0u8; size];
1776                let (hash, encoded) = create_n0_bao(&data, &ChunkRanges::all())?;
1777                let data = Bytes::from(encoded);
1778                trace!("importing size={}", size);
1779                store
1780                    .import_bao_bytes(hash, ChunkRanges::all(), data)
1781                    .await?;
1782            }
1783            store.shutdown().await?;
1784        }
1785        Ok(())
1786    }
1787
1788    #[tokio::test]
1789    async fn test_import_bao_persistence_full() -> TestResult<()> {
1790        tracing_subscriber::fmt::try_init().ok();
1791        let testdir = tempfile::tempdir()?;
1792        let sizes = INTERESTING_SIZES;
1793        let db_dir = testdir.path().join("db");
1794        {
1795            let store = FsStore::load(&db_dir).await?;
1796            for size in sizes {
1797                let data = vec![0u8; size];
1798                let (hash, encoded) = create_n0_bao(&data, &ChunkRanges::all())?;
1799                let data = Bytes::from(encoded);
1800                store
1801                    .import_bao_bytes(hash, ChunkRanges::all(), data)
1802                    .await?;
1803            }
1804            store.shutdown().await?;
1805        }
1806        {
1807            let store = FsStore::load(&db_dir).await?;
1808            for size in sizes {
1809                let expected = vec![0u8; size];
1810                let hash = Hash::new(&expected);
1811                let actual = store
1812                    .export_bao(hash, ChunkRanges::all())
1813                    .data_to_vec()
1814                    .await?;
1815                assert_eq!(&expected, &actual);
1816            }
1817            store.shutdown().await?;
1818        }
1819        Ok(())
1820    }
1821
1822    #[tokio::test]
1823    async fn test_import_bao_persistence_just_size() -> TestResult<()> {
1824        tracing_subscriber::fmt::try_init().ok();
1825        let testdir = tempfile::tempdir()?;
1826        let sizes = INTERESTING_SIZES;
1827        let db_dir = testdir.path().join("db");
1828        let just_size = ChunkRanges::last_chunk();
1829        {
1830            let store = FsStore::load(&db_dir).await?;
1831            for size in sizes {
1832                let data = test_data(size);
1833                let (hash, ranges, encoded) = create_n0_bao_full(&data, &just_size)?;
1834                let data = Bytes::from(encoded);
1835                if let Err(cause) = store.import_bao_bytes(hash, ranges, data).await {
1836                    panic!("failed to import size={size}: {cause}");
1837                }
1838            }
1839            store.dump().await?;
1840            store.shutdown().await?;
1841        }
1842        {
1843            let store = FsStore::load(&db_dir).await?;
1844            store.dump().await?;
1845            for size in sizes {
1846                let data = test_data(size);
1847                let (hash, ranges, expected) = create_n0_bao_full(&data, &just_size)?;
1848                let actual = match store.export_bao(hash, ranges).bao_to_vec().await {
1849                    Ok(actual) => actual,
1850                    Err(cause) => panic!("failed to export size={size}: {cause}"),
1851                };
1852                assert_eq!(&expected, &actual);
1853            }
1854            store.shutdown().await?;
1855        }
1856        dump_dir_full(testdir.path())?;
1857        Ok(())
1858    }
1859
1860    #[tokio::test]
1861    async fn test_import_bao_persistence_two_stages() -> TestResult<()> {
1862        tracing_subscriber::fmt::try_init().ok();
1863        let testdir = tempfile::tempdir()?;
1864        let sizes = INTERESTING_SIZES;
1865        let db_dir = testdir.path().join("db");
1866        let just_size = ChunkRanges::last_chunk();
1867        // stage 1, import just the last full chunk group to get a validated size
1868        {
1869            let store = FsStore::load(&db_dir).await?;
1870            for size in sizes {
1871                let data = test_data(size);
1872                let (hash, ranges, encoded) = create_n0_bao_full(&data, &just_size)?;
1873                let data = Bytes::from(encoded);
1874                if let Err(cause) = store.import_bao_bytes(hash, ranges, data).await {
1875                    panic!("failed to import size={size}: {cause}");
1876                }
1877            }
1878            store.dump().await?;
1879            store.shutdown().await?;
1880        }
1881        dump_dir_full(testdir.path())?;
1882        // stage 2, import the rest
1883        {
1884            let store = FsStore::load(&db_dir).await?;
1885            for size in sizes {
1886                let remaining = ChunkRanges::all() - round_up_request(size as u64, &just_size);
1887                if remaining.is_empty() {
1888                    continue;
1889                }
1890                let data = test_data(size);
1891                let (hash, ranges, encoded) = create_n0_bao_full(&data, &remaining)?;
1892                let data = Bytes::from(encoded);
1893                if let Err(cause) = store.import_bao_bytes(hash, ranges, data).await {
1894                    panic!("failed to import size={size}: {cause}");
1895                }
1896            }
1897            store.dump().await?;
1898            store.shutdown().await?;
1899        }
1900        // check if the data is complete
1901        {
1902            let store = FsStore::load(&db_dir).await?;
1903            store.dump().await?;
1904            for size in sizes {
1905                let data = test_data(size);
1906                let (hash, ranges, expected) = create_n0_bao_full(&data, &ChunkRanges::all())?;
1907                let actual = match store.export_bao(hash, ranges).bao_to_vec().await {
1908                    Ok(actual) => actual,
1909                    Err(cause) => panic!("failed to export size={size}: {cause}"),
1910                };
1911                assert_eq!(&expected, &actual);
1912            }
1913            store.dump().await?;
1914            store.shutdown().await?;
1915        }
1916        dump_dir_full(testdir.path())?;
1917        Ok(())
1918    }
1919
1920    fn just_size() -> ChunkRanges {
1921        ChunkRanges::last_chunk()
1922    }
1923
1924    #[tokio::test]
1925    async fn test_import_bao_persistence_observe() -> TestResult<()> {
1926        tracing_subscriber::fmt::try_init().ok();
1927        let testdir = tempfile::tempdir()?;
1928        let sizes = INTERESTING_SIZES;
1929        let db_dir = testdir.path().join("db");
1930        let just_size = just_size();
1931        // stage 1, import just the last full chunk group to get a validated size
1932        {
1933            let store = FsStore::load(&db_dir).await?;
1934            for size in sizes {
1935                let data = test_data(size);
1936                let (hash, ranges, encoded) = create_n0_bao_full(&data, &just_size)?;
1937                let data = Bytes::from(encoded);
1938                if let Err(cause) = store.import_bao_bytes(hash, ranges, data).await {
1939                    panic!("failed to import size={size}: {cause}");
1940                }
1941            }
1942            store.dump().await?;
1943            store.shutdown().await?;
1944        }
1945        dump_dir_full(testdir.path())?;
1946        // stage 2, import the rest
1947        {
1948            let store = FsStore::load(&db_dir).await?;
1949            for size in sizes {
1950                let expected_ranges = round_up_request(size as u64, &just_size);
1951                let data = test_data(size);
1952                let hash = Hash::new(&data);
1953                let bitfield = store.observe(hash).await?;
1954                assert_eq!(bitfield.ranges, expected_ranges);
1955            }
1956            store.dump().await?;
1957            store.shutdown().await?;
1958        }
1959        Ok(())
1960    }
1961
1962    #[tokio::test]
1963    async fn test_import_bao_persistence_recover() -> TestResult<()> {
1964        tracing_subscriber::fmt::try_init().ok();
1965        let testdir = tempfile::tempdir()?;
1966        let sizes = INTERESTING_SIZES;
1967        let db_dir = testdir.path().join("db");
1968        let options = Options::new(&db_dir);
1969        let just_size = just_size();
1970        // stage 1, import just the last full chunk group to get a validated size
1971        {
1972            let store = FsStore::load_with_opts(db_dir.join("blobs.db"), options.clone()).await?;
1973            for size in sizes {
1974                let data = test_data(size);
1975                let (hash, ranges, encoded) = create_n0_bao_full(&data, &just_size)?;
1976                let data = Bytes::from(encoded);
1977                if let Err(cause) = store.import_bao_bytes(hash, ranges, data).await {
1978                    panic!("failed to import size={size}: {cause}");
1979                }
1980            }
1981            store.dump().await?;
1982            store.shutdown().await?;
1983        }
1984        delete_rec(testdir.path(), "bitfield")?;
1985        dump_dir_full(testdir.path())?;
1986        // stage 2, import the rest
1987        {
1988            let store = FsStore::load_with_opts(db_dir.join("blobs.db"), options.clone()).await?;
1989            for size in sizes {
1990                let expected_ranges = round_up_request(size as u64, &just_size);
1991                let data = test_data(size);
1992                let hash = Hash::new(&data);
1993                let bitfield = store.observe(hash).await?;
1994                assert_eq!(bitfield.ranges, expected_ranges, "size={size}");
1995            }
1996            store.dump().await?;
1997            store.shutdown().await?;
1998        }
1999        Ok(())
2000    }
2001
2002    #[tokio::test]
2003    async fn test_import_bytes_persistence_full() -> TestResult<()> {
2004        tracing_subscriber::fmt::try_init().ok();
2005        let testdir = tempfile::tempdir()?;
2006        let sizes = INTERESTING_SIZES;
2007        let db_dir = testdir.path().join("db");
2008        {
2009            let store = FsStore::load(&db_dir).await?;
2010            let mut tts = Vec::new();
2011            for size in sizes {
2012                let data = test_data(size);
2013                let data = data;
2014                tts.push(store.add_bytes(data.clone()).await?);
2015            }
2016            store.dump().await?;
2017            store.shutdown().await?;
2018        }
2019        {
2020            let store = FsStore::load(&db_dir).await?;
2021            store.dump().await?;
2022            for size in sizes {
2023                let expected = test_data(size);
2024                let hash = Hash::new(&expected);
2025                let Ok(actual) = store
2026                    .export_bao(hash, ChunkRanges::all())
2027                    .data_to_vec()
2028                    .await
2029                else {
2030                    panic!("failed to export size={size}");
2031                };
2032                assert_eq!(&expected, &actual, "size={size}");
2033            }
2034            store.shutdown().await?;
2035        }
2036        Ok(())
2037    }
2038
2039    async fn test_batch(store: &Store) -> TestResult<()> {
2040        let batch = store.blobs().batch().await?;
2041        let tt1 = batch.temp_tag(Hash::new("foo")).await?;
2042        let tt2 = batch.add_slice("boo").await?;
2043        let tts = store
2044            .tags()
2045            .list_temp_tags()
2046            .await?
2047            .collect::<HashSet<_>>()
2048            .await;
2049        assert!(tts.contains(&tt1.hash_and_format()));
2050        assert!(tts.contains(&tt2.hash_and_format()));
2051        drop(batch);
2052        store.sync_db().await?;
2053        store.wait_idle().await?;
2054        let tts = store
2055            .tags()
2056            .list_temp_tags()
2057            .await?
2058            .collect::<HashSet<_>>()
2059            .await;
2060        // temp tag went out of scope, so it does not work anymore
2061        assert!(!tts.contains(&tt1.hash_and_format()));
2062        assert!(!tts.contains(&tt2.hash_and_format()));
2063        drop(tt1);
2064        drop(tt2);
2065        Ok(())
2066    }
2067
2068    #[tokio::test]
2069    async fn test_batch_fs() -> TestResult<()> {
2070        tracing_subscriber::fmt::try_init().ok();
2071        let testdir = tempfile::tempdir()?;
2072        let db_dir = testdir.path().join("db");
2073        let store = FsStore::load(db_dir).await?;
2074        test_batch(&store).await
2075    }
2076
2077    #[tokio::test]
2078    async fn smoke() -> TestResult<()> {
2079        tracing_subscriber::fmt::try_init().ok();
2080        let testdir = tempfile::tempdir()?;
2081        let db_dir = testdir.path().join("db");
2082        let store = FsStore::load(db_dir).await?;
2083        let haf = HashAndFormat::raw(Hash::from([0u8; 32]));
2084        store.tags().set(Tag::from("test"), haf).await?;
2085        store.tags().set(Tag::from("boo"), haf).await?;
2086        store.tags().set(Tag::from("bar"), haf).await?;
2087        let sizes = INTERESTING_SIZES;
2088        let mut hashes = Vec::new();
2089        let mut data_by_hash = HashMap::new();
2090        let mut bao_by_hash = HashMap::new();
2091        for size in sizes {
2092            let data = vec![0u8; size];
2093            let data = Bytes::from(data);
2094            let tt = store.add_bytes(data.clone()).temp_tag().await?;
2095            data_by_hash.insert(tt.hash(), data);
2096            hashes.push(tt);
2097        }
2098        store.sync_db().await?;
2099        for tt in &hashes {
2100            let hash = tt.hash();
2101            let path = testdir.path().join(format!("{hash}.txt"));
2102            store.export(hash, path).await?;
2103        }
2104        for tt in &hashes {
2105            let hash = tt.hash();
2106            let data = store
2107                .export_bao(hash, ChunkRanges::all())
2108                .data_to_vec()
2109                .await
2110                .unwrap();
2111            assert_eq!(data, data_by_hash[&hash].to_vec());
2112            let bao = store
2113                .export_bao(hash, ChunkRanges::all())
2114                .bao_to_vec()
2115                .await
2116                .unwrap();
2117            bao_by_hash.insert(hash, bao);
2118        }
2119        store.dump().await?;
2120
2121        for size in sizes {
2122            let data = test_data(size);
2123            let ranges = ChunkRanges::all();
2124            let (hash, bao) = create_n0_bao(&data, &ranges)?;
2125            store.import_bao_bytes(hash, ranges, bao).await?;
2126        }
2127
2128        for (_hash, _bao_tree) in bao_by_hash {
2129            // let mut reader = Cursor::new(bao_tree);
2130            // let size = reader.read_u64_le().await?;
2131            // let tree = BaoTree::new(size, IROH_BLOCK_SIZE);
2132            // let ranges = ChunkRanges::all();
2133            // let mut decoder = DecodeResponseIter::new(hash, tree, reader, &ranges);
2134            // while let Some(item) = decoder.next() {
2135            //     let item = item?;
2136            // }
2137            // store.import_bao_bytes(hash, ChunkRanges::all(), bao_tree.into()).await?;
2138        }
2139        Ok(())
2140    }
2141
2142    pub fn delete_rec(root_dir: impl AsRef<Path>, extension: &str) -> Result<(), std::io::Error> {
2143        // Remove leading dot if present, so we have just the extension
2144        let ext = extension.trim_start_matches('.').to_lowercase();
2145
2146        for entry in WalkDir::new(root_dir).into_iter().filter_map(|e| e.ok()) {
2147            let path = entry.path();
2148
2149            if path.is_file() {
2150                if let Some(file_ext) = path.extension() {
2151                    if file_ext.to_string_lossy().to_lowercase() == ext {
2152                        fs::remove_file(path)?;
2153                    }
2154                }
2155            }
2156        }
2157
2158        Ok(())
2159    }
2160
2161    pub fn dump_dir(path: impl AsRef<Path>) -> io::Result<()> {
2162        let mut entries: Vec<_> = WalkDir::new(&path)
2163            .into_iter()
2164            .filter_map(Result::ok) // Skip errors
2165            .collect();
2166
2167        // Sort by path (name at each depth)
2168        entries.sort_by(|a, b| a.path().cmp(b.path()));
2169
2170        for entry in entries {
2171            let depth = entry.depth();
2172            let indent = "  ".repeat(depth); // Two spaces per level
2173            let name = entry.file_name().to_string_lossy();
2174            let size = entry.metadata()?.len(); // Size in bytes
2175
2176            if entry.file_type().is_file() {
2177                println!("{indent}{name} ({size} bytes)");
2178            } else if entry.file_type().is_dir() {
2179                println!("{indent}{name}/");
2180            }
2181        }
2182        Ok(())
2183    }
2184
2185    pub fn dump_dir_full(path: impl AsRef<Path>) -> io::Result<()> {
2186        let mut entries: Vec<_> = WalkDir::new(&path)
2187            .into_iter()
2188            .filter_map(Result::ok) // Skip errors
2189            .collect();
2190
2191        // Sort by path (name at each depth)
2192        entries.sort_by(|a, b| a.path().cmp(b.path()));
2193
2194        for entry in entries {
2195            let depth = entry.depth();
2196            let indent = "  ".repeat(depth);
2197            let name = entry.file_name().to_string_lossy();
2198
2199            if entry.file_type().is_dir() {
2200                println!("{indent}{name}/");
2201            } else if entry.file_type().is_file() {
2202                let size = entry.metadata()?.len();
2203                println!("{indent}{name} ({size} bytes)");
2204
2205                // Dump depending on file type
2206                let path = entry.path();
2207                if name.ends_with(".data") {
2208                    print!("{indent}  ");
2209                    dump_file(path, 1024 * 16)?;
2210                } else if name.ends_with(".obao4") {
2211                    print!("{indent}  ");
2212                    dump_file(path, 64)?;
2213                } else if name.ends_with(".sizes4") {
2214                    print!("{indent}  ");
2215                    dump_file(path, 8)?;
2216                } else if name.ends_with(".bitfield") {
2217                    match read_checksummed::<Bitfield>(path) {
2218                        Ok(bitfield) => {
2219                            println!("{indent}  bitfield: {bitfield:?}");
2220                        }
2221                        Err(cause) => {
2222                            println!("{indent}  bitfield: error: {cause}");
2223                        }
2224                    }
2225                } else {
2226                    continue; // Skip content dump for other files
2227                };
2228            }
2229        }
2230        Ok(())
2231    }
2232
2233    pub fn dump_file<P: AsRef<Path>>(path: P, chunk_size: u64) -> io::Result<()> {
2234        let bits = file_bits(path, chunk_size)?;
2235        println!("{}", print_bitfield_ansi(bits));
2236        Ok(())
2237    }
2238
2239    pub fn file_bits(path: impl AsRef<Path>, chunk_size: u64) -> io::Result<Vec<bool>> {
2240        let file = fs::File::open(&path)?;
2241        let file_size = file.metadata()?.len();
2242        let mut buffer = vec![0u8; chunk_size as usize];
2243        let mut bits = Vec::new();
2244
2245        let mut offset = 0u64;
2246        while offset < file_size {
2247            let remaining = file_size - offset;
2248            let current_chunk_size = chunk_size.min(remaining);
2249
2250            let chunk = &mut buffer[..current_chunk_size as usize];
2251            file.read_exact_at(offset, chunk)?;
2252
2253            let has_non_zero = chunk.iter().any(|&byte| byte != 0);
2254            bits.push(has_non_zero);
2255
2256            offset += current_chunk_size;
2257        }
2258
2259        Ok(bits)
2260    }
2261
2262    #[allow(dead_code)]
2263    fn print_bitfield(bits: impl IntoIterator<Item = bool>) -> String {
2264        bits.into_iter()
2265            .map(|bit| if bit { '#' } else { '_' })
2266            .collect()
2267    }
2268
2269    fn print_bitfield_ansi(bits: impl IntoIterator<Item = bool>) -> String {
2270        let mut result = String::new();
2271        let mut iter = bits.into_iter();
2272
2273        while let Some(b1) = iter.next() {
2274            let b2 = iter.next();
2275
2276            // ANSI color codes
2277            let white_fg = "\x1b[97m"; // bright white foreground
2278            let reset = "\x1b[0m"; // reset all attributes
2279            let gray_bg = "\x1b[100m"; // bright black (gray) background
2280            let black_bg = "\x1b[40m"; // black background
2281
2282            let colored_char = match (b1, b2) {
2283                (true, Some(true)) => format!("{}{}{}", white_fg, '█', reset), // 11 - solid white on default background
2284                (true, Some(false)) => format!("{}{}{}{}", gray_bg, white_fg, '▌', reset), // 10 - left half white on gray background
2285                (false, Some(true)) => format!("{}{}{}{}", gray_bg, white_fg, '▐', reset), // 01 - right half white on gray background
2286                (false, Some(false)) => format!("{}{}{}{}", gray_bg, white_fg, ' ', reset), // 00 - space with gray background
2287                (true, None) => format!("{}{}{}{}", black_bg, white_fg, '▌', reset), // 1 (pad 0) - left half white on black background
2288                (false, None) => format!("{}{}{}{}", black_bg, white_fg, ' ', reset), // 0 (pad 0) - space with black background
2289            };
2290
2291            result.push_str(&colored_char);
2292        }
2293
2294        // Ensure we end with a reset code to prevent color bleeding
2295        result.push_str("\x1b[0m");
2296        result
2297    }
2298
2299    fn bytes_to_stream(
2300        bytes: Bytes,
2301        chunk_size: usize,
2302    ) -> impl Stream<Item = io::Result<Bytes>> + 'static {
2303        assert!(chunk_size > 0, "Chunk size must be greater than 0");
2304        stream::unfold((bytes, 0), move |(bytes, offset)| async move {
2305            if offset >= bytes.len() {
2306                None
2307            } else {
2308                let chunk_len = chunk_size.min(bytes.len() - offset);
2309                let chunk = bytes.slice(offset..offset + chunk_len);
2310                Some((Ok(chunk), (bytes, offset + chunk_len)))
2311            }
2312        })
2313    }
2314}