Skip to content

Commit 909ea9a

Browse files
authored
fix(database): Handle finding beetle data directory (#960)
This fixes very obscure errors when a beetle (formerly named iroh) data directory is found. - Error messages are improved to indicate what is failing. Making use of anyhow's error chaining. - The paths.bin file is used as a marker to decide if a database is present or needs to be created. The end result is that this happily creates the new iroh database at the same location of a previous beetle store, leaving the beetle files in place.
1 parent f21ccf8 commit 909ea9a

3 files changed

Lines changed: 37 additions & 12 deletions

File tree

src/main.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use tracing_subscriber::{prelude::*, EnvFilter};
2727
mod main_util;
2828
use iroh::tokio_util::SeekOptimized;
2929

30+
use iroh::provider::FNAME_PATHS;
3031
use iroh::{get, provider, Hash, Keypair, PeerId};
3132
use main_util::Blake3Cid;
3233

@@ -555,10 +556,16 @@ async fn main_impl() -> Result<()> {
555556
rpc_port,
556557
} => {
557558
let iroh_data_root = iroh_data_root()?;
559+
let marker = iroh_data_root.join(FNAME_PATHS);
558560
let db = {
559-
if iroh_data_root.is_dir() {
561+
if iroh_data_root.is_dir() && marker.exists() {
560562
// try to load db
561-
Database::load(&iroh_data_root).await?
563+
Database::load(&iroh_data_root).await.with_context(|| {
564+
format!(
565+
"Failed to load iroh database from {}",
566+
iroh_data_root.display()
567+
)
568+
})?
562569
} else {
563570
// directory does not exist, create an empty db
564571
Database::default()

src/provider/database.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ use std::{
1616
};
1717
use tokio::sync::mpsc;
1818

19+
/// File name of directory inside `IROH_DATA_DIR` where outboards are stored.
20+
const FNAME_OUTBOARDS: &str = "outboards";
21+
22+
/// File name of directory inside `IROH_DATA_DIR` where collections are stored.
23+
const FNAME_COLLECTIONS: &str = "collections";
24+
25+
/// File name inside `IROH_DATA_DIR` where paths to data are stored.
26+
pub const FNAME_PATHS: &str = "paths.bin";
27+
1928
/// Database containing content-addressed data (blobs or collections).
2029
#[derive(Debug, Clone, Default)]
2130
pub struct Database(Arc<RwLock<HashMap<Hash, BlobOrCollection>>>);
@@ -66,9 +75,9 @@ struct DataPaths {
6675
impl DataPaths {
6776
fn new(data_dir: PathBuf) -> Self {
6877
Self {
69-
outboards_dir: data_dir.join("outboards"),
70-
collections_dir: data_dir.join("collections"),
71-
paths_file: data_dir.join("paths.bin"),
78+
outboards_dir: data_dir.join(FNAME_OUTBOARDS),
79+
collections_dir: data_dir.join(FNAME_COLLECTIONS),
80+
paths_file: data_dir.join(FNAME_PATHS),
7281
data_dir,
7382
}
7483
}
@@ -97,7 +106,8 @@ impl Snapshot<io::Error> {
97106
paths_file,
98107
..
99108
} = DataPaths::new(data_dir.as_ref().to_path_buf());
100-
let paths = fs::read(paths_file)?;
109+
let paths = fs::read(&paths_file)
110+
.with_context(|| format!("Failed reading {}", paths_file.display()))?;
101111
let paths = postcard::from_bytes::<Vec<(Hash, u64, Option<PathBuf>)>>(&paths)?;
102112
let hashes = paths
103113
.iter()
@@ -107,7 +117,13 @@ impl Snapshot<io::Error> {
107117
let path = outboards_dir.join(format_hash(&hash));
108118
fs::read(path).map(|x| (hash, Bytes::from(x)))
109119
});
110-
let collections = fs::read_dir(collections_dir)?
120+
let collections = fs::read_dir(&collections_dir)
121+
.with_context(|| {
122+
format!(
123+
"Failed reading collections directory {}",
124+
collections_dir.display()
125+
)
126+
})?
111127
.map(move |entry| {
112128
let entry = entry?;
113129
let path = entry.path();
@@ -237,18 +253,20 @@ impl Database {
237253
}
238254

239255
/// Load a database from disk.
240-
pub(crate) fn from_snapshot<E: Into<io::Error>>(snapshot: Snapshot<E>) -> io::Result<Self> {
256+
pub(crate) fn from_snapshot<E: Into<io::Error>>(snapshot: Snapshot<E>) -> Result<Self> {
241257
let Snapshot {
242258
outboards,
243259
collections,
244260
paths,
245261
} = snapshot;
246262
let outboards = outboards
247263
.collect::<result::Result<HashMap<_, _>, E>>()
248-
.map_err(Into::into)?;
264+
.map_err(Into::into)
265+
.context("Failed reading outboards")?;
249266
let collections = collections
250267
.collect::<result::Result<HashMap<_, _>, E>>()
251-
.map_err(Into::into)?;
268+
.map_err(Into::into)
269+
.context("Failed reading collections")?;
252270
let mut db = HashMap::new();
253271
for (hash, size, path) in paths {
254272
if let (Some(path), Some(outboard)) = (path, outboards.get(&hash)) {

src/provider/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ mod database;
5757
mod ticket;
5858

5959
pub use database::Database;
60-
#[cfg(cli)]
61-
pub use database::Snapshot;
60+
#[cfg(feature = "cli")]
61+
pub use database::FNAME_PATHS;
6262
pub use ticket::Ticket;
6363

6464
const MAX_CONNECTIONS: u32 = 1024;

0 commit comments

Comments
 (0)