diff --git a/Cargo.lock b/Cargo.lock index d15a8e4947..e90eb74431 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1413,6 +1413,7 @@ dependencies = [ "once_cell", "percent-encoding", "pgp", + "port_check", "pretty_env_logger", "proptest", "qrcodegen", @@ -4538,6 +4539,12 @@ dependencies = [ "universal-hash", ] +[[package]] +name = "port_check" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6519412c9e0d4be579b9f0618364d19cb434b324fc6ddb1b27b1e682c7105ed" + [[package]] name = "portable-atomic" version = "0.3.15" diff --git a/Cargo.toml b/Cargo.toml index 37d72eea12..2e3e0a332e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ license = "MPL-2.0" rust-version = "1.56" [profile.dev] -#debug = 0 +debug = 0 panic = 'abort' opt-level = 1 @@ -83,7 +83,8 @@ async_zip = { git = "https://github.com/dignifiedquire/rs-async-zip", branch = " iroh-share = { git = "https://github.com/n0-computer/iroh", branch = "main" } iroh-resolver = { git = "https://github.com/n0-computer/iroh", branch = "main", default-features = false } tempfile = "3" -multibase = "0.9.1" +multibase = "0.9" +port_check = "0.1.5" [dev-dependencies] ansi_term = "0.12.0" diff --git a/examples/repl/cmdline.rs b/examples/repl/cmdline.rs index 9e32d70463..d7bc49005d 100644 --- a/examples/repl/cmdline.rs +++ b/examples/repl/cmdline.rs @@ -475,10 +475,12 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu Some(arg2.to_string()), ) .await?; - println!("Exported to {}.", dir.to_string_lossy()); + println!("Exported to {}.", dir.display()); } "send-backup" => { - let dir = dirs::home_dir().unwrap_or_default(); + let tdir = tempfile::TempDir::new()?; + let dir = tdir.path(); + println!("Storing backup in: {} ", dir.display()); let transfer = send_backup(&context, dir.as_ref(), Some(arg1.to_string())).await?; let ticket = transfer.ticket(); let ticket_bytes = ticket.as_bytes(); diff --git a/foo-shm b/foo-shm new file mode 100644 index 0000000000..85e14c8a0d Binary files /dev/null and b/foo-shm differ diff --git a/foo-wal b/foo-wal new file mode 100644 index 0000000000..4454e4f4ea Binary files /dev/null and b/foo-wal differ diff --git a/src/imex.rs b/src/imex.rs index 96727807ef..c666660fbf 100644 --- a/src/imex.rs +++ b/src/imex.rs @@ -5,7 +5,7 @@ use std::ffi::OsStr; use std::path::{Path, PathBuf}; use ::pgp::types::KeyTrait; -use anyhow::{bail, ensure, format_err, Context as _, Result}; +use anyhow::{anyhow, bail, ensure, format_err, Context as _, Result}; use futures::{StreamExt, TryStreamExt}; use futures_lite::FutureExt; use rand::{thread_rng, Rng}; @@ -107,6 +107,10 @@ pub async fn imex( res } +/// Receives a backup, as sent from `send_backup`. +/// +/// Only one receive-progress can run at the same time. +/// To cancel a receive-progress, drop the future returned by this function. pub async fn receive_backup( context: &Context, ticket_bytes: Vec, @@ -135,7 +139,7 @@ pub async fn receive_backup( res } -pub async fn receive_backup_inner( +async fn receive_backup_inner( context: &Context, ticket_bytes: Vec, passphrase: String, @@ -153,11 +157,12 @@ pub async fn receive_backup_inner( let recv_dir = tempfile::tempdir().unwrap(); let recv_db = recv_dir.path().join("db"); - let port = 9991; + let port = port_check::free_local_port() + .ok_or_else(|| anyhow!("failed to find free local port to bind to"))?; let receiver = Receiver::new(port, &recv_db) .await - .context("failed to create sender")?; + .context("failed to create receiver")?; let mut receiver_transfer = receiver .transfer_from_ticket(&ticket) .await @@ -200,9 +205,10 @@ pub async fn receive_backup_inner( let name = link.name.unwrap_or_default(); let path = out.join(&name); - let mut file = tokio::fs::File::create(&path) + let file = tokio::fs::File::create(&path) .await .with_context(|| format!("create file: {}", path.display()))?; + let mut file = tokio::io::BufWriter::new(file); let mut content = file_content.pretty()?; tokio::io::copy(&mut content, &mut file) .await @@ -232,6 +238,10 @@ pub async fn receive_backup_inner( Ok(()) } +/// Send a backup. +/// +/// Only one send-progress can run at the same time. +/// To cancel a send-progress, drop the future returned by this function. pub async fn send_backup( context: &Context, path: &Path, @@ -832,7 +842,8 @@ async fn export_backup_iroh( match res { Ok(dir_builder) => { - let port = 9990; + let port = port_check::free_local_port() + .ok_or_else(|| anyhow!("failed to find free local port to bind to"))?; let sender = iroh_share::Sender::new(port, &sender_db_path).await?; let transfer = sender.transfer_from_dir_builder(dir_builder).await?;