hashtree-cli 0.2.62

Hashtree daemon and CLI - content-addressed storage with P2P sync
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;

use anyhow::{bail, Context, Result};
use async_trait::async_trait;
use hashtree_cli::config::ensure_keys_string;
use hashtree_cli::{
    Config, FetchConfig, Fetcher, HashtreeStore, NostrKeys, NostrResolverConfig, NostrRootResolver,
};
use hashtree_core::{Hash, HashTree, HashTreeConfig, Store, StoreError};
use hashtree_updater::{
    install, AssetKind, DownloadEvent, DownloadOptions, HashtreeUpdater, InstallTarget,
    UpdateAsset, UpdateCheckOptions, UpdateRef, UpdateTarget,
};

/// `Store` adapter that backs reads with a `Fetcher` so unknown chunks are
/// pulled from Blossom/WebRTC on demand. Writes pass straight through to the
/// underlying `HashtreeStore`.
struct FetchingStore {
    store: Arc<HashtreeStore>,
    fetcher: Arc<Fetcher>,
}

impl FetchingStore {
    fn new(store: Arc<HashtreeStore>, fetcher: Arc<Fetcher>) -> Self {
        Self { store, fetcher }
    }
}

#[async_trait]
impl Store for FetchingStore {
    async fn put(&self, hash: Hash, data: Vec<u8>) -> Result<bool, StoreError> {
        self.store.store_arc().put(hash, data).await
    }

    async fn put_many(&self, items: Vec<(Hash, Vec<u8>)>) -> Result<usize, StoreError> {
        self.store.store_arc().put_many(items).await
    }

    async fn get(&self, hash: &Hash) -> Result<Option<Vec<u8>>, StoreError> {
        if let Some(data) = self.store.store_arc().get(hash).await? {
            return Ok(Some(data));
        }
        match self
            .fetcher
            .fetch_chunk_with_store(&self.store, None, hash)
            .await
        {
            Ok(data) => Ok(Some(data)),
            Err(_) => Ok(None),
        }
    }

    async fn has(&self, hash: &Hash) -> Result<bool, StoreError> {
        self.store.store_arc().has(hash).await
    }

    async fn delete(&self, hash: &Hash) -> Result<bool, StoreError> {
        self.store.store_arc().delete(hash).await
    }
}

async fn build_updater(
    data_dir: &Path,
) -> Result<HashtreeUpdater<NostrRootResolver, FetchingStore>> {
    let store = Arc::new(HashtreeStore::new(data_dir)?);
    let fetcher = Arc::new(Fetcher::new(FetchConfig::default()));
    let fetching_store = Arc::new(FetchingStore::new(store, fetcher));
    let tree = HashTree::new(HashTreeConfig::new(fetching_store));

    let config = Config::load()?;
    let (nsec_str, _) = ensure_keys_string()?;
    let keys = NostrKeys::parse(&nsec_str).context("Failed to parse nsec")?;
    let resolver_config = NostrResolverConfig {
        relays: config.nostr.relays.clone(),
        resolve_timeout: Duration::from_secs(10),
        secret_key: Some(keys),
    };
    let resolver = NostrRootResolver::new(resolver_config)
        .await
        .context("Failed to create Nostr resolver")?;
    Ok(HashtreeUpdater::new(resolver, tree))
}

fn build_check_options(
    reference: &str,
    current_version: String,
    target: Option<String>,
    manifest_path: String,
) -> Result<UpdateCheckOptions> {
    let reference = UpdateRef::parse(reference)?;
    let target = target
        .map(UpdateTarget::new)
        .unwrap_or_else(UpdateTarget::current);
    Ok(UpdateCheckOptions {
        reference,
        current_version,
        target,
        manifest_path,
        ..UpdateCheckOptions::default()
    })
}

/// Default destination for plain binaries / binary-archives when the user
/// doesn't pass --to. Picks `~/.local/bin/<asset binary name>`.
fn default_install_path(asset: &UpdateAsset) -> Result<PathBuf> {
    let home = std::env::var_os("HOME").context("HOME is not set; pass --to explicitly")?;
    let bin_dir = PathBuf::from(home).join(".local/bin");
    let entry_name = asset
        .executable
        .as_deref()
        .map(|s| s.rsplit('/').next().unwrap_or(s))
        .unwrap_or_else(|| {
            // Strip common archive extensions to derive a sensible binary
            // name from the asset name itself.
            let n = asset.name.as_str();
            let n = n.strip_suffix(".tar.gz").unwrap_or(n);
            let n = n.strip_suffix(".tgz").unwrap_or(n);
            let n = n.strip_suffix(".zip").unwrap_or(n);
            n
        });
    Ok(bin_dir.join(entry_name))
}

#[allow(clippy::too_many_arguments)]
pub(crate) async fn run_install(
    data_dir: &Path,
    reference: String,
    to: Option<PathBuf>,
    check_only: bool,
    download_only: bool,
    current_version: String,
    target: Option<String>,
    manifest_path: String,
    kind: Option<String>,
    executable: bool,
    archive_entry: Option<String>,
    only_if_newer: bool,
) -> Result<()> {
    let updater = build_updater(data_dir).await?;
    let options = build_check_options(&reference, current_version.clone(), target, manifest_path)?;
    let check = updater.check(options).await?;

    let mut asset: UpdateAsset = check
        .asset
        .clone()
        .context("no asset matched the platform")?;
    if let Some(kind_override) = kind {
        if AssetKind::parse(&kind_override).is_none() {
            bail!("unknown asset kind: {kind_override}");
        }
        asset.kind = Some(kind_override);
    }
    if let Some(entry) = archive_entry {
        asset.executable = Some(entry);
    }

    if check_only {
        println!("Version:    {}", check.manifest.effective_version());
        println!("Current:    {}", current_version);
        println!("Newer:      {}", check.update_available);
        if let Some(notes) = check.manifest.notes.as_deref() {
            println!("Notes:      {}", notes);
        }
        println!("Asset:      {} ({})", asset.name, asset.path);
        println!("Kind:       {}", asset.asset_kind().as_str());
        if let Some(exe) = asset.executable.as_deref() {
            println!("Entry:      {}", exe);
        }
        return Ok(());
    }

    if only_if_newer && !check.update_available {
        println!(
            "Already up to date (manifest version {} not newer)",
            check.manifest.effective_version()
        );
        return Ok(());
    }

    let downloaded = updater
        .download(&check, DownloadOptions::default(), Some(progress_logger()))
        .await?;

    if download_only {
        let out_path = to.unwrap_or_else(|| PathBuf::from(&asset.name));
        if let Some(parent) = out_path.parent() {
            if !parent.as_os_str().is_empty() {
                std::fs::create_dir_all(parent)?;
            }
        }
        std::fs::write(&out_path, &downloaded.bytes)?;
        println!(
            "Wrote {} bytes to {}",
            downloaded.bytes.len(),
            out_path.display()
        );
        return Ok(());
    }

    let dest = match to {
        Some(p) => p,
        None => default_install_path(&asset)?,
    };
    let target = InstallTarget::new(&dest).executable(executable);
    install(&asset, &downloaded.bytes, &target)?;
    println!(
        "Installed {} ({}) → {}",
        check.manifest.effective_version(),
        asset.asset_kind().as_str(),
        dest.display()
    );
    Ok(())
}

use serde::{Deserialize, Serialize};

/// Cached result of the most recent background check. Written by the
/// detached `__bg_check` child, read on every startup to print the
/// pending notification before the new command runs.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
struct CachedUpdateResult {
    /// Unix seconds when the check completed.
    checked_at: u64,
    /// htree version the check was run as.
    checked_as_version: String,
    /// Latest published htree version, if newer than `checked_as_version`.
    available_version: Option<String>,
    /// Whether the bg flow already installed the update (auto_install).
    installed: bool,
}

fn cached_result_path(data_dir: &Path) -> PathBuf {
    data_dir.join("last-update-result.json")
}

fn sentinel_path(data_dir: &Path) -> PathBuf {
    data_dir.join("last-update-check")
}

/// Read the cached result and print an inline notification if there's a
/// newer version than the running binary. Strictly bounded: the file
/// read happens on a worker thread with a 50 ms timeout so a hung
/// filesystem (NFS, FUSE, busy spinning disk) can't stall command
/// startup. Worst case the notification skips this round.
pub(crate) fn print_cached_update_notification(data_dir: &Path) {
    let path = cached_result_path(data_dir);
    let contents = match read_to_string_with_timeout(path, std::time::Duration::from_millis(50)) {
        Some(s) => s,
        None => return,
    };
    let Ok(cached): std::result::Result<CachedUpdateResult, _> = serde_json::from_str(&contents)
    else {
        return;
    };
    let Some(version) = cached.available_version.as_deref() else {
        return;
    };
    let current = env!("CARGO_PKG_VERSION");
    // Don't notify if the user already upgraded since the cache was written
    // (eg they ran `htree update` manually) or if the cached check was for
    // an older version of htree.
    if version == current {
        return;
    }
    if cached.installed {
        eprintln!(
            "htree was self-updated to {version} in the background — restart htree to use it",
        );
    } else {
        eprintln!(
            "htree update available: {version} (you're on {current}) — run `{}` to install",
            upgrade_command_hint(),
        );
    }
}

/// Read a file's contents on a worker thread, returning `None` if the
/// read doesn't complete within `timeout`. The worker is orphaned on
/// timeout — fine, the OS reaps it on process exit and the FS layer
/// will eventually unblock or the kernel will tear down the syscall.
fn read_to_string_with_timeout(path: PathBuf, timeout: std::time::Duration) -> Option<String> {
    let (tx, rx) = std::sync::mpsc::channel();
    std::thread::spawn(move || {
        let _ = tx.send(std::fs::read_to_string(&path).ok());
    });
    rx.recv_timeout(timeout).ok().flatten()
}

/// Spawn a detached child process to run the actual check. Survives the
/// parent's exit (important for short commands like `htree user` whose
/// tokio runtime tears down before a network call would complete).
/// Throttled by mtime on the sentinel file.
pub(crate) fn spawn_detached_bg_check(data_dir: &Path) {
    let config = match Config::load() {
        Ok(c) => c,
        Err(_) => return,
    };
    if !config.updater.auto_check {
        return;
    }

    let interval = std::time::Duration::from_secs(
        u64::from(config.updater.check_interval_hours).saturating_mul(3600),
    );
    let sentinel = sentinel_path(data_dir);
    if let Ok(meta) = std::fs::metadata(&sentinel) {
        if let Ok(modified) = meta.modified() {
            if modified.elapsed().map(|e| e < interval).unwrap_or(false) {
                return;
            }
        }
    }
    // Touch the sentinel up-front so concurrent invocations don't all race
    // the same network request. Race window is small but the worst case is
    // two simultaneous checks once per interval — no correctness issue.
    if let Some(parent) = sentinel.parent() {
        let _ = std::fs::create_dir_all(parent);
    }
    let _ = std::fs::OpenOptions::new()
        .create(true)
        .write(true)
        .truncate(false)
        .open(&sentinel)
        .and_then(|f| f.set_modified(std::time::SystemTime::now()));

    let exe = match std::env::current_exe() {
        Ok(p) => p,
        Err(_) => return,
    };
    // Detached: stdio piped to /dev/null, parent doesn't await. The OS
    // reaps the orphaned child via init/launchd. Failure to spawn is fine
    // — we just won't get a notification this round.
    use std::process::Stdio;
    let _ = std::process::Command::new(exe)
        .arg("__bg_check")
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn();
}

/// Body of the detached `htree __bg_check` invocation. Runs the check
/// (and optional install when auto_install is set), writes the result to
/// the cache file, exits.
pub(crate) async fn run_bg_check(data_dir: &Path) -> Result<()> {
    let config = Config::load().unwrap_or_default();
    let updater = build_updater(data_dir).await?;
    let options = build_check_options(
        super::args::HTREE_SELF_REFERENCE,
        env!("CARGO_PKG_VERSION").to_string(),
        None,
        "release.json".to_string(),
    )?;
    let mut cached = CachedUpdateResult {
        checked_at: now_unix(),
        checked_as_version: env!("CARGO_PKG_VERSION").to_string(),
        ..Default::default()
    };

    let check = match updater.check(options).await {
        Ok(c) => c,
        // Quiet for "no release yet" cases — write empty result so the
        // notifier stays silent.
        Err(hashtree_updater::UpdateError::ReleaseNotFound(_))
        | Err(hashtree_updater::UpdateError::ManifestNotFound(_)) => {
            write_cached_result(data_dir, &cached);
            return Ok(());
        }
        Err(e) => return Err(e.into()),
    };
    if !check.update_available {
        write_cached_result(data_dir, &cached);
        return Ok(());
    }
    let version = check.manifest.effective_version();
    cached.available_version = Some(version.clone());

    if config.updater.auto_install {
        let exe = std::env::current_exe()?;
        let mut asset = check
            .asset
            .clone()
            .context("no asset matched the platform")?;
        if asset.executable.is_none() {
            asset.executable = Some("htree".to_string());
        }
        let downloaded = updater
            .download(&check, DownloadOptions::default(), None)
            .await?;
        let target = InstallTarget::new(&exe).executable(true);
        if install(&asset, &downloaded.bytes, &target).is_ok() {
            cached.installed = true;
        }
    }

    write_cached_result(data_dir, &cached);
    Ok(())
}

fn write_cached_result(data_dir: &Path, cached: &CachedUpdateResult) {
    let path = cached_result_path(data_dir);
    if let Some(parent) = path.parent() {
        let _ = std::fs::create_dir_all(parent);
    }
    if let Ok(json) = serde_json::to_string(cached) {
        let _ = std::fs::write(&path, json);
    }
}

fn now_unix() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

/// How the running htree binary was installed — used to print the right
/// upgrade hint when self-updating clashes with a package manager.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum InstallSource {
    Cargo,
    Brew,
    Other,
}

fn detect_install_source() -> InstallSource {
    let Ok(exe) = std::env::current_exe() else {
        return InstallSource::Other;
    };
    let s = exe.to_string_lossy();
    if s.contains("/.cargo/bin/") {
        InstallSource::Cargo
    } else if s.contains("/Cellar/") || s.contains("/homebrew/") {
        InstallSource::Brew
    } else {
        InstallSource::Other
    }
}

/// The right one-liner to suggest in the update notification, picked
/// from the running binary's install source so the user can copy-paste it.
fn upgrade_command_hint() -> &'static str {
    match detect_install_source() {
        InstallSource::Cargo => "cargo install hashtree-cli --force",
        InstallSource::Brew => "brew upgrade htree",
        InstallSource::Other => "htree update",
    }
}

/// Self-update: install a fresh htree binary over the running one.
///
/// Refuses when the binary lives under a known package-manager path
/// (cargo, brew) so the package manager's metadata stays in sync. Pass
/// `force = true` to bypass and replace the binary directly anyway.
pub(crate) async fn run_self_update(data_dir: &Path, check_only: bool, force: bool) -> Result<()> {
    let current_exe = std::env::current_exe()?;
    if !check_only && !force {
        match detect_install_source() {
            InstallSource::Cargo => bail!(
                "htree was installed via cargo at {}.\n\
                 Run `cargo install hashtree-cli --force` to upgrade and keep cargo metadata in sync,\n\
                 or `htree update --force` to replace the binary directly anyway.",
                current_exe.display(),
            ),
            InstallSource::Brew => bail!(
                "htree was installed via brew at {}.\n\
                 Run `brew upgrade htree` to upgrade and keep brew metadata in sync,\n\
                 or `htree update --force` to replace the binary directly anyway.",
                current_exe.display(),
            ),
            InstallSource::Other => {}
        }
    }
    run_install(
        data_dir,
        super::args::HTREE_SELF_REFERENCE.to_string(),
        Some(current_exe),
        check_only,
        false,
        env!("CARGO_PKG_VERSION").to_string(),
        None,
        "release.json".to_string(),
        None,
        true,
        Some("htree".to_string()),
        true,
    )
    .await
}

fn progress_logger() -> hashtree_updater::DownloadCallback {
    Arc::new(|event| match event {
        DownloadEvent::Started { content_length } => {
            if let Some(total) = content_length {
                eprintln!("Downloading {} bytes...", total);
            } else {
                eprintln!("Downloading...");
            }
        }
        DownloadEvent::Progress {
            chunk_len: _,
            downloaded,
        } => {
            eprint!("\r  {} bytes", downloaded);
            let _ = std::io::Write::flush(&mut std::io::stderr());
        }
        DownloadEvent::Finished { total } => {
            eprintln!("\rdone ({} bytes)         ", total);
        }
    })
}