hashtree-cli 0.2.39

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
#![cfg(feature = "fuse")]

mod common;

use std::path::{Path, PathBuf};
use std::process::{Child, Command, Output, Stdio};
use std::time::{Duration, Instant};

use common::htree_bin;
use hashtree_cli::HashtreeStore;
use hashtree_core::{from_hex, nhash_encode};
use serde_json::Value;
use tempfile::TempDir;

const EMPTY_MOUNTS_OUTPUT: &str = "No active hashtree mounts found.";

struct ChildGuard(Option<Child>);

impl ChildGuard {
    fn spawn(command: &mut Command) -> Self {
        let child = command.spawn().expect("spawn mount command");
        Self(Some(child))
    }

    fn child_mut(&mut self) -> &mut Child {
        self.0.as_mut().expect("child process available")
    }

    fn kill(&mut self) {
        if let Some(child) = self.0.as_mut() {
            let _ = child.kill();
        }
    }

    fn wait_with_output(&mut self) -> Output {
        self.0
            .take()
            .expect("child process available")
            .wait_with_output()
            .expect("wait for child output")
    }
}

impl Drop for ChildGuard {
    fn drop(&mut self) {
        if let Some(child) = self.0.as_mut() {
            let _ = child.kill();
            let _ = child.wait();
        }
    }
}

#[test]
fn mounts_command_reports_empty_state() {
    let tmp = TempDir::new().expect("temp dir");
    let config_dir = tmp.path().join("config");
    let data_dir = tmp.path().join("data");
    std::fs::create_dir_all(&config_dir).expect("create config dir");
    std::fs::create_dir_all(&data_dir).expect("create data dir");

    let output = run_htree_command(&config_dir, &data_dir, ["mounts"]);
    assert_success(&output, "htree mounts");

    let stdout = String::from_utf8(output.stdout).expect("stdout utf-8");
    assert!(
        stdout.contains(EMPTY_MOUNTS_OUTPUT),
        "expected empty mounts output.\nstdout:\n{stdout}"
    );
}

#[test]
fn mount_smoke_lists_active_mount_and_unmounts_cleanly() {
    if let Some(reason) = fuse_smoke_skip_reason() {
        eprintln!("skipping fuse smoke test: {reason}");
        return;
    }

    let tmp = TempDir::new().expect("temp dir");
    let config_dir = tmp.path().join("config");
    let data_dir = tmp.path().join("data");
    let source_dir = tmp.path().join("source");
    let mountpoint = tmp.path().join("mount");
    std::fs::create_dir_all(&config_dir).expect("create config dir");
    std::fs::create_dir_all(&data_dir).expect("create data dir");
    std::fs::create_dir_all(source_dir.join("nested")).expect("create source dir");
    std::fs::create_dir_all(&mountpoint).expect("create mountpoint");
    std::fs::write(
        source_dir.join("nested").join("hello.txt"),
        "hello through fuse",
    )
    .expect("write source file");

    let store = HashtreeStore::new(&data_dir).expect("create store");
    let root_hex = store.upload_dir(&source_dir).expect("upload source dir");
    let nhash =
        nhash_encode(&from_hex(&root_hex).expect("decode root hash")).expect("encode nhash");

    let mut mount_command = Command::new(htree_bin());
    mount_command
        .arg("--data-dir")
        .arg(&data_dir)
        .arg("mount")
        .arg(&nhash)
        .arg(&mountpoint);
    #[cfg(target_os = "macos")]
    mount_command.arg("--allow-other");
    mount_command
        .env("HTREE_CONFIG_DIR", &config_dir)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());

    let mut child = ChildGuard::spawn(&mut mount_command);
    wait_for_mounted_file(
        &mut child,
        &config_dir,
        &data_dir,
        &mountpoint,
        mountpoint.join("nested").join("hello.txt"),
        "hello through fuse",
    );

    let mounts_output = run_htree_command(&config_dir, &data_dir, ["mounts"]);
    assert_success(&mounts_output, "htree mounts while mounted");
    let mounts_stdout = String::from_utf8(mounts_output.stdout).expect("stdout utf-8");
    assert!(
        mounts_stdout.contains(&mountpoint.display().to_string()),
        "expected mounts output to include mountpoint.\nstdout:\n{mounts_stdout}"
    );
    assert!(
        mounts_stdout.contains(&nhash),
        "expected mounts output to include target/root.\nstdout:\n{mounts_stdout}"
    );

    let mounts_json_output = run_htree_command(&config_dir, &data_dir, ["mounts", "--json"]);
    assert_success(&mounts_json_output, "htree mounts --json while mounted");
    let mounts_json: Value =
        serde_json::from_slice(&mounts_json_output.stdout).expect("parse mounts json");
    let mounts = mounts_json.as_array().expect("mounts array");
    assert_eq!(mounts.len(), 1, "expected one active mount in json");
    let mount = mounts[0].as_object().expect("mount entry object");
    assert_eq!(
        mount.get("mountpoint").and_then(Value::as_str),
        Some(mountpoint.to_string_lossy().as_ref())
    );
    assert_eq!(
        mount.get("target").and_then(Value::as_str),
        Some(nhash.as_str())
    );
    assert_eq!(
        mount.get("mounted_cid").and_then(Value::as_str),
        Some(nhash.as_str())
    );
    assert_eq!(
        mount.get("visibility").and_then(Value::as_str),
        Some("public")
    );
    assert_eq!(
        mount.get("pid").and_then(Value::as_u64),
        Some(child.child_mut().id().into())
    );
    assert_eq!(mount.get("published_key"), Some(&Value::Null));
    assert_eq!(
        mount.get("allow_other").and_then(Value::as_bool),
        Some(cfg!(target_os = "macos"))
    );

    unmount(&mountpoint);

    let mount_exit = child.wait_with_output();
    assert_success(&mount_exit, "mounted htree process");

    let post_unmount = run_htree_command(&config_dir, &data_dir, ["mounts"]);
    assert_success(&post_unmount, "htree mounts after unmount");
    let post_unmount_stdout = String::from_utf8(post_unmount.stdout).expect("stdout utf-8");
    assert!(
        post_unmount_stdout.contains(EMPTY_MOUNTS_OUTPUT),
        "expected mounts output to be empty after unmount.\nstdout:\n{post_unmount_stdout}"
    );
}

fn run_htree_command<const N: usize>(
    config_dir: &Path,
    data_dir: &Path,
    args: [&str; N],
) -> Output {
    Command::new(htree_bin())
        .arg("--data-dir")
        .arg(data_dir)
        .args(args)
        .env("HTREE_CONFIG_DIR", config_dir)
        .output()
        .expect("run htree command")
}

fn assert_success(output: &Output, description: &str) {
    assert!(
        output.status.success(),
        "{description} failed.\nstdout:\n{}\nstderr:\n{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

fn wait_for_mounted_file(
    child: &mut ChildGuard,
    config_dir: &Path,
    data_dir: &Path,
    mountpoint: &Path,
    path: PathBuf,
    expected: &str,
) {
    let deadline = Instant::now() + Duration::from_secs(60);
    loop {
        if let Some(status) = child.child_mut().try_wait().expect("poll child") {
            panic!("mount process exited early: {status}");
        }
        if !mountpoint_is_active(mountpoint) {
            if Instant::now() >= deadline {
                let registry_output = run_htree_command(config_dir, data_dir, ["mounts", "--json"]);
                child.kill();
                let mount_output = child.wait_with_output();
                panic!(
                    "timed out waiting for active mount {}\nmountinfo:\n{}\nroot snapshot:\n{}\nregistry stdout:\n{}\nregistry stderr:\n{}\nmount stdout:\n{}\nmount stderr:\n{}",
                    mountpoint.display(),
                    current_mountinfo(),
                    snapshot_dir(mountpoint),
                    String::from_utf8_lossy(&registry_output.stdout),
                    String::from_utf8_lossy(&registry_output.stderr),
                    String::from_utf8_lossy(&mount_output.stdout),
                    String::from_utf8_lossy(&mount_output.stderr)
                );
            }
            std::thread::sleep(Duration::from_millis(100));
            continue;
        }
        if let Ok(contents) = std::fs::read_to_string(&path) {
            if contents == expected {
                return;
            }
        }
        if Instant::now() >= deadline {
            let registry_output = run_htree_command(config_dir, data_dir, ["mounts", "--json"]);
            child.kill();
            let mount_output = child.wait_with_output();
            panic!(
                "timed out waiting for mounted file {}\nmountinfo:\n{}\nroot snapshot:\n{}\nparent snapshot:\n{}\nregistry stdout:\n{}\nregistry stderr:\n{}\nmount stdout:\n{}\nmount stderr:\n{}",
                path.display(),
                current_mountinfo(),
                snapshot_dir(mountpoint),
                snapshot_dir(path.parent().unwrap_or(mountpoint)),
                String::from_utf8_lossy(&registry_output.stdout),
                String::from_utf8_lossy(&registry_output.stderr),
                String::from_utf8_lossy(&mount_output.stdout),
                String::from_utf8_lossy(&mount_output.stderr)
            );
        }
        std::thread::sleep(Duration::from_millis(100));
    }
}

fn unmount(mountpoint: &Path) {
    #[cfg(target_os = "linux")]
    let commands: &[&str] = &["fusermount3", "fusermount", "umount"];
    #[cfg(not(target_os = "linux"))]
    let commands: &[&str] = &["umount"];

    for command in commands {
        let status = Command::new(command).arg(mountpoint).status();
        match status {
            Ok(status) if status.success() => return,
            Ok(_) | Err(_) => continue,
        }
    }

    panic!("failed to unmount {}", mountpoint.display());
}

fn fuse_smoke_skip_reason() -> Option<String> {
    if std::env::var_os("HTREE_REQUIRE_FUSE_SMOKE").is_some() {
        return None;
    }

    #[cfg(not(target_os = "linux"))]
    {
        return Some(
            "automatic FUSE smoke coverage only runs on Linux; set HTREE_REQUIRE_FUSE_SMOKE=1 to force a local run"
                .to_string(),
        );
    }

    #[cfg(target_os = "linux")]
    {
        if !Path::new("/dev/fuse").exists() {
            return Some("/dev/fuse is not available".to_string());
        }

        return None;
    }
}

fn snapshot_dir(path: &Path) -> String {
    match std::fs::read_dir(path) {
        Ok(entries) => {
            let mut names = entries
                .filter_map(|entry| entry.ok())
                .map(|entry| entry.file_name().to_string_lossy().into_owned())
                .collect::<Vec<_>>();
            names.sort();
            if names.is_empty() {
                "(empty)".to_string()
            } else {
                names.join("\n")
            }
        }
        Err(error) => format!("read_dir failed: {error}"),
    }
}

#[cfg(target_os = "linux")]
fn mountpoint_is_active(mountpoint: &Path) -> bool {
    let Ok(mountinfo) = std::fs::read_to_string("/proc/self/mountinfo") else {
        return false;
    };
    mountinfo_lists_mount(&mountinfo, mountpoint)
}

#[cfg(not(target_os = "linux"))]
fn mountpoint_is_active(_mountpoint: &Path) -> bool {
    true
}

#[cfg(target_os = "linux")]
fn current_mountinfo() -> String {
    std::fs::read_to_string("/proc/self/mountinfo")
        .unwrap_or_else(|error| format!("failed to read /proc/self/mountinfo: {error}"))
}

#[cfg(not(target_os = "linux"))]
fn current_mountinfo() -> String {
    "mountinfo unavailable on this platform".to_string()
}

fn mountinfo_lists_mount(mountinfo: &str, mountpoint: &Path) -> bool {
    let expected = mountpoint.to_string_lossy();
    mountinfo.lines().any(|line| {
        let Some(fields) = line.split(" - ").next() else {
            return false;
        };
        let mut parts = fields.split(' ');
        let _mount_id = parts.next();
        let _parent_id = parts.next();
        let _major_minor = parts.next();
        let _root = parts.next();
        let Some(mount_point) = parts.next() else {
            return false;
        };
        decode_mountinfo_path(mount_point) == expected
    })
}

fn decode_mountinfo_path(value: &str) -> String {
    let bytes = value.as_bytes();
    let mut decoded = Vec::with_capacity(bytes.len());
    let mut index = 0;
    while index < bytes.len() {
        if bytes[index] == b'\\' && index + 3 < bytes.len() {
            let maybe_octal = &value[index + 1..index + 4];
            if maybe_octal
                .bytes()
                .all(|byte| (b'0'..=b'7').contains(&byte))
            {
                let octal = u8::from_str_radix(maybe_octal, 8).expect("valid octal escape");
                decoded.push(octal);
                index += 4;
                continue;
            }
        }
        decoded.push(bytes[index]);
        index += 1;
    }

    String::from_utf8(decoded).expect("mountinfo path is valid utf-8")
}

#[test]
fn mountinfo_parser_matches_escaped_mountpoint_paths() {
    let mountinfo = "\
35 26 0:31 / /tmp/hashtree\\040mount rw,nosuid,nodev - fuse.hashtree hashtree rw,user_id=1000,group_id=1000\n\
";

    assert!(mountinfo_lists_mount(
        mountinfo,
        Path::new("/tmp/hashtree mount")
    ));
}

#[test]
fn mountinfo_parser_ignores_other_mountpoints() {
    let mountinfo = "\
35 26 0:31 / /tmp/other rw,nosuid,nodev - fuse.hashtree hashtree rw,user_id=1000,group_id=1000\n\
";

    assert!(!mountinfo_lists_mount(
        mountinfo,
        Path::new("/tmp/hashtree mount")
    ));
}