Skip to main content

hashtree_fuse/
lib.rs

1use std::collections::{HashMap, HashSet};
2use std::hash::{Hash as StdHash, Hasher};
3use std::sync::atomic::{AtomicU64, Ordering};
4use std::sync::{Arc, Mutex, RwLock};
5
6use futures::executor::block_on;
7use hashtree_core::{
8    sha256, to_hex, Cid, HashTree, HashTreeConfig, HashTreeError, LinkType, Store,
9};
10use thiserror::Error;
11
12pub const ROOT_INODE: u64 = 1;
13// Virtual entry used only to wake Linux directory monitors after remote root updates.
14pub const DIRECTORY_REFRESH_SENTINEL_NAME: &str = "iris-drive-refresh";
15const WHOLE_FILE_HASH_META_KEY: &str = "whole_file_hash";
16
17#[derive(Debug, Error)]
18pub enum FsError {
19    #[error("root hash is not a directory")]
20    InvalidRoot,
21    #[error("entry not found")]
22    NotFound,
23    #[error("not a directory")]
24    NotDir,
25    #[error("is a directory")]
26    IsDir,
27    #[error("entry already exists")]
28    AlreadyExists,
29    #[error("directory not empty")]
30    NotEmpty,
31    #[error("invalid entry name")]
32    InvalidName,
33    #[error("tree error: {0}")]
34    Tree(String),
35    #[error("publish error: {0}")]
36    Publish(String),
37}
38
39impl From<HashTreeError> for FsError {
40    fn from(err: HashTreeError) -> Self {
41        FsError::Tree(err.to_string())
42    }
43}
44
45#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46pub enum EntryKind {
47    File,
48    Directory,
49}
50
51#[derive(Debug, Clone, PartialEq, Eq)]
52pub struct EntryAttr {
53    pub inode: u64,
54    pub size: u64,
55    pub kind: EntryKind,
56}
57
58#[derive(Debug, Clone, PartialEq, Eq)]
59pub struct DirEntry {
60    pub inode: u64,
61    pub name: String,
62    pub kind: EntryKind,
63}
64
65pub trait RootPublisher: Send + Sync {
66    fn publish(&self, cid: &Cid) -> Result<(), FsError>;
67}
68
69#[derive(Debug, Clone, Eq)]
70struct ChildKey {
71    parent: u64,
72    name: String,
73}
74
75impl PartialEq for ChildKey {
76    fn eq(&self, other: &Self) -> bool {
77        self.parent == other.parent && self.name == other.name
78    }
79}
80
81impl StdHash for ChildKey {
82    fn hash<H: Hasher>(&self, state: &mut H) {
83        self.parent.hash(state);
84        self.name.hash(state);
85    }
86}
87
88#[derive(Debug, Clone, PartialEq)]
89struct ResolvedEntry {
90    cid: Cid,
91    link_type: LinkType,
92    size: u64,
93    meta: Option<HashMap<String, serde_json::Value>>,
94}
95
96#[cfg(feature = "fuse")]
97#[derive(Debug, Clone, PartialEq, Eq)]
98enum FuseInvalidation {
99    Entry {
100        parent: u64,
101        name: String,
102    },
103    Delete {
104        parent: u64,
105        child: u64,
106        name: String,
107    },
108}
109
110pub struct HashtreeFuseInner<S: Store> {
111    tree: HashTree<S>,
112    root: RwLock<Cid>,
113    paths: RwLock<HashMap<u64, Vec<String>>>,
114    children: RwLock<HashMap<ChildKey, u64>>,
115    parents: RwLock<HashMap<u64, u64>>,
116    next_inode: AtomicU64,
117    publisher: Option<Arc<dyn RootPublisher>>,
118    refresh_unlinks: Mutex<HashSet<Vec<String>>>,
119    modify_lock: Mutex<()>,
120    #[cfg(feature = "fuse")]
121    notifier: Mutex<Option<fuser::Notifier>>,
122}
123
124pub struct HashtreeFuse<S: Store> {
125    inner: Arc<HashtreeFuseInner<S>>,
126}
127
128impl<S: Store> Clone for HashtreeFuse<S> {
129    fn clone(&self) -> Self {
130        Self {
131            inner: self.inner.clone(),
132        }
133    }
134}
135
136impl<S: Store> std::ops::Deref for HashtreeFuse<S> {
137    type Target = HashtreeFuseInner<S>;
138
139    fn deref(&self) -> &Self::Target {
140        &self.inner
141    }
142}
143
144impl<S: Store> HashtreeFuse<S> {
145    pub fn new(store: Arc<S>, root: Cid) -> Result<Self, FsError> {
146        Self::new_with_publisher(store, root, None)
147    }
148
149    pub fn new_with_publisher(
150        store: Arc<S>,
151        root: Cid,
152        publisher: Option<Arc<dyn RootPublisher>>,
153    ) -> Result<Self, FsError> {
154        let mut config = HashTreeConfig::new(store);
155        if root.key.is_none() {
156            config = config.public();
157        }
158        let tree = HashTree::new(config);
159
160        let is_dir = block_on(tree.get_directory_node(&root))?.is_some();
161        if !is_dir {
162            return Err(FsError::InvalidRoot);
163        }
164
165        let mut paths = HashMap::new();
166        paths.insert(ROOT_INODE, Vec::new());
167        let mut parents = HashMap::new();
168        parents.insert(ROOT_INODE, ROOT_INODE);
169
170        Ok(Self {
171            inner: Arc::new(HashtreeFuseInner {
172                tree,
173                root: RwLock::new(root),
174                paths: RwLock::new(paths),
175                children: RwLock::new(HashMap::new()),
176                parents: RwLock::new(parents),
177                next_inode: AtomicU64::new(ROOT_INODE + 1),
178                publisher,
179                refresh_unlinks: Mutex::new(HashSet::new()),
180                modify_lock: Mutex::new(()),
181                #[cfg(feature = "fuse")]
182                notifier: Mutex::new(None),
183            }),
184        })
185    }
186
187    pub fn current_root(&self) -> Cid {
188        self.root.read().unwrap().clone()
189    }
190
191    pub fn replace_root(&self, root: Cid) -> Result<(), FsError> {
192        let _guard = self.modify_lock.lock().unwrap();
193        self.replace_root_locked(root)
194    }
195
196    pub fn replace_root_if_current(
197        &self,
198        expected_current: &Cid,
199        root: Cid,
200    ) -> Result<bool, FsError> {
201        let _guard = self.modify_lock.lock().unwrap();
202        if self.current_root() != *expected_current {
203            return Ok(false);
204        }
205        self.replace_root_locked(root)?;
206        Ok(true)
207    }
208
209    fn replace_root_locked(&self, root: Cid) -> Result<(), FsError> {
210        let is_dir = block_on(self.tree.get_directory_node(&root))?.is_some();
211        if !is_dir {
212            return Err(FsError::InvalidRoot);
213        }
214
215        #[cfg(feature = "fuse")]
216        let invalidations = self.changed_known_entries_for_root(&root);
217        #[cfg(feature = "fuse")]
218        let paths_needing_new_inode = self.changed_known_paths_requiring_new_inode_for_root(&root);
219
220        *self.root.write().unwrap() = root;
221
222        self.retain_existing_paths_after_root_update()?;
223        #[cfg(feature = "fuse")]
224        self.drop_paths(paths_needing_new_inode);
225
226        #[cfg(feature = "fuse")]
227        self.notify_entries_invalidated(&invalidations);
228
229        Ok(())
230    }
231
232    #[cfg(feature = "fuse")]
233    pub fn removed_known_entry_paths_for_root(&self, root: &Cid) -> Vec<Vec<String>> {
234        self.changed_known_entries_for_root(root)
235            .into_iter()
236            .filter_map(|invalidation| match invalidation {
237                FuseInvalidation::Delete { parent, name, .. } => {
238                    let mut path = self.path_for_inode(parent).ok()?;
239                    path.push(name);
240                    Some(path)
241                }
242                _ => None,
243            })
244            .collect()
245    }
246
247    #[cfg(not(feature = "fuse"))]
248    pub fn removed_known_entry_paths_for_root(&self, _root: &Cid) -> Vec<Vec<String>> {
249        Vec::new()
250    }
251
252    pub fn begin_refresh_unlinks(&self, paths: impl IntoIterator<Item = Vec<String>>) {
253        self.refresh_unlinks.lock().unwrap().extend(paths);
254    }
255
256    pub fn clear_refresh_unlinks(&self) {
257        self.refresh_unlinks.lock().unwrap().clear();
258    }
259
260    #[cfg(feature = "fuse")]
261    fn set_notifier(&self, notifier: fuser::Notifier) {
262        *self.notifier.lock().unwrap() = Some(notifier);
263    }
264
265    #[cfg(feature = "fuse")]
266    fn clear_notifier(&self) {
267        *self.notifier.lock().unwrap() = None;
268    }
269
270    #[cfg(feature = "fuse")]
271    fn notify_entries_invalidated(&self, invalidations: &[FuseInvalidation]) {
272        let guard = self.notifier.lock().unwrap();
273        let Some(notifier) = guard.as_ref() else {
274            return;
275        };
276        for invalidation in invalidations {
277            match invalidation {
278                FuseInvalidation::Entry { parent, name } => {
279                    let _ = notifier.inval_entry(*parent, std::ffi::OsStr::new(name));
280                }
281                FuseInvalidation::Delete {
282                    parent,
283                    child,
284                    name,
285                } => {
286                    let _ = notifier.delete(*parent, *child, std::ffi::OsStr::new(name));
287                }
288            }
289        }
290    }
291
292    #[cfg(feature = "fuse")]
293    fn changed_known_entries_for_root(&self, new_root: &Cid) -> Vec<FuseInvalidation> {
294        let old_root = self.current_root();
295        let paths = self.paths.read().unwrap().clone();
296        let children = self.children.read().unwrap().clone();
297        let mut invalidations = Vec::new();
298
299        for (inode, path) in paths {
300            let old_entries = self.directory_entry_names_at_root(&old_root, &path);
301            let new_entries = self.directory_entry_names_at_root(new_root, &path);
302            if old_entries.is_none() && new_entries.is_none() {
303                continue;
304            }
305
306            let old_entries = old_entries.unwrap_or_default();
307            let new_entries = new_entries.unwrap_or_default();
308            for removed in old_entries.difference(&new_entries) {
309                invalidations.push(FuseInvalidation::Entry {
310                    parent: inode,
311                    name: removed.clone(),
312                });
313                if let Some(child) = children
314                    .get(&ChildKey {
315                        parent: inode,
316                        name: removed.clone(),
317                    })
318                    .copied()
319                {
320                    invalidations.push(FuseInvalidation::Delete {
321                        parent: inode,
322                        child,
323                        name: removed.clone(),
324                    });
325                }
326            }
327
328            for retained in old_entries.intersection(&new_entries) {
329                if self.entry_changed_between_roots(&old_root, new_root, &path, retained) {
330                    invalidations.push(FuseInvalidation::Entry {
331                        parent: inode,
332                        name: retained.clone(),
333                    });
334                }
335            }
336
337            for added in new_entries.difference(&old_entries) {
338                invalidations.push(FuseInvalidation::Entry {
339                    parent: inode,
340                    name: added.clone(),
341                });
342            }
343        }
344
345        invalidations
346    }
347
348    #[cfg(feature = "fuse")]
349    fn changed_known_paths_requiring_new_inode_for_root(&self, new_root: &Cid) -> Vec<Vec<String>> {
350        let old_root = self.current_root();
351        let paths: Vec<Vec<String>> = self.paths.read().unwrap().values().cloned().collect();
352
353        paths
354            .into_iter()
355            .filter(|path| {
356                !path.is_empty()
357                    && !Self::is_directory_refresh_sentinel_path(path)
358                    && self.entry_path_changed_between_roots(&old_root, new_root, path)
359                    && self.changed_entry_should_get_new_inode(&old_root, new_root, path)
360            })
361            .collect()
362    }
363
364    #[cfg(feature = "fuse")]
365    fn changed_entry_should_get_new_inode(
366        &self,
367        old_root: &Cid,
368        new_root: &Cid,
369        path: &[String],
370    ) -> bool {
371        match (
372            self.resolve_entry_at_root(old_root, path),
373            self.resolve_entry_at_root(new_root, path),
374        ) {
375            (Ok(old), Ok(new)) => {
376                !old.link_type.is_directory_like() || !new.link_type.is_directory_like()
377            }
378            _ => true,
379        }
380    }
381
382    #[cfg(feature = "fuse")]
383    fn entry_path_changed_between_roots(
384        &self,
385        old_root: &Cid,
386        new_root: &Cid,
387        path: &[String],
388    ) -> bool {
389        match (
390            self.resolve_entry_at_root(old_root, path),
391            self.resolve_entry_at_root(new_root, path),
392        ) {
393            (Ok(old), Ok(new)) => old != new,
394            (Err(_), Err(_)) => false,
395            _ => true,
396        }
397    }
398
399    #[cfg(feature = "fuse")]
400    fn entry_changed_between_roots(
401        &self,
402        old_root: &Cid,
403        new_root: &Cid,
404        parent_path: &[String],
405        name: &str,
406    ) -> bool {
407        let mut child_path = parent_path.to_vec();
408        child_path.push(name.to_string());
409        match (
410            self.resolve_entry_at_root(old_root, &child_path),
411            self.resolve_entry_at_root(new_root, &child_path),
412        ) {
413            (Ok(old), Ok(new)) => old != new,
414            (Err(_), Err(_)) => false,
415            _ => true,
416        }
417    }
418
419    pub fn lookup_child(&self, parent: u64, name: &str) -> Result<EntryAttr, FsError> {
420        if name.is_empty() {
421            return Err(FsError::NotFound);
422        }
423        if name == "." {
424            return self.get_attr(parent);
425        }
426        if name == ".." {
427            let parent_inode = self.parent_inode(parent);
428            return self.get_attr(parent_inode);
429        }
430        if Self::is_directory_refresh_sentinel(name) {
431            if self.get_attr(parent)?.kind != EntryKind::Directory {
432                return Err(FsError::NotDir);
433            }
434            let inode = self.get_or_create_child_inode(parent, name)?;
435            return Ok(Self::directory_refresh_sentinel_attr(inode));
436        }
437
438        let child_inode = self.get_or_create_child_inode(parent, name)?;
439        let path = self.path_for_inode(child_inode)?;
440
441        match self.resolve_entry(&path) {
442            Ok(entry) => self.entry_attr_from_resolved(child_inode, entry),
443            Err(FsError::NotFound) => {
444                self.drop_inode(child_inode);
445                Err(FsError::NotFound)
446            }
447            Err(err) => Err(err),
448        }
449    }
450
451    pub fn get_attr(&self, inode: u64) -> Result<EntryAttr, FsError> {
452        if inode == ROOT_INODE {
453            return Ok(EntryAttr {
454                inode,
455                size: 0,
456                kind: EntryKind::Directory,
457            });
458        }
459
460        let path = self.path_for_inode(inode)?;
461        if Self::is_directory_refresh_sentinel_path(&path) {
462            let parent_path = &path[..path.len() - 1];
463            self.resolve_dir_cid(parent_path)?;
464            return Ok(Self::directory_refresh_sentinel_attr(inode));
465        }
466        let entry = self.resolve_entry(&path)?;
467        self.entry_attr_from_resolved(inode, entry)
468    }
469
470    pub fn read_file(&self, inode: u64, offset: u64, size: u32) -> Result<Vec<u8>, FsError> {
471        let path = self.path_for_inode(inode)?;
472        if Self::is_directory_refresh_sentinel_path(&path) {
473            return Ok(Vec::new());
474        }
475        let entry = self.resolve_entry(&path)?;
476        if entry.link_type.is_directory_like() {
477            return Err(FsError::IsDir);
478        }
479
480        let file_size = self.entry_size(&entry)?;
481        if offset >= file_size {
482            return Ok(vec![]);
483        }
484        let read_len = (size as u64).min(file_size - offset);
485        if read_len == 0 {
486            return Ok(vec![]);
487        }
488
489        if entry.cid.key.is_some() {
490            let data = block_on(self.tree.get(&entry.cid, None))?.ok_or(FsError::NotFound)?;
491            let start = usize::try_from(offset).unwrap_or(usize::MAX);
492            if start >= data.len() {
493                return Ok(vec![]);
494            }
495            let end_u64 = offset.saturating_add(read_len);
496            let mut end = usize::try_from(end_u64).unwrap_or(data.len());
497            if end > data.len() {
498                end = data.len();
499            }
500            return Ok(data[start..end].to_vec());
501        }
502
503        let end = offset.saturating_add(read_len);
504        let data = block_on(
505            self.tree
506                .read_file_range(&entry.cid.hash, offset, Some(end)),
507        )?
508        .ok_or(FsError::NotFound)?;
509        Ok(data)
510    }
511
512    pub fn read_dir(&self, inode: u64) -> Result<Vec<DirEntry>, FsError> {
513        let path = self.path_for_inode(inode)?;
514        let dir_cid = self.resolve_dir_cid(&path)?;
515
516        let entries = block_on(self.tree.list_directory(&dir_cid))?;
517        let mut out = Vec::with_capacity(entries.len());
518
519        for entry in entries {
520            if Self::is_directory_refresh_sentinel(&entry.name) {
521                continue;
522            }
523            let child_inode = self.get_or_create_child_inode(inode, &entry.name)?;
524            out.push(DirEntry {
525                inode: child_inode,
526                name: entry.name,
527                kind: Self::kind_from_link(entry.link_type),
528            });
529        }
530
531        Ok(out)
532    }
533
534    pub fn create_file(&self, parent: u64, name: &str) -> Result<EntryAttr, FsError> {
535        self.ensure_valid_name(name)?;
536        let _guard = self.modify_lock.lock().unwrap();
537
538        let parent_path = self.path_for_inode(parent)?;
539        let mut child_path = parent_path.clone();
540        child_path.push(name.to_string());
541
542        if self.resolve_entry(&child_path).is_ok() {
543            return Err(FsError::AlreadyExists);
544        }
545
546        let (cid, size) = block_on(self.tree.put(&[]))?;
547        let link_type = self.link_type_for_size(size);
548        let meta = Self::file_entry_meta(&[]);
549        let new_root = block_on(self.tree.set_entry_with_meta(
550            &self.current_root(),
551            &self.path_refs(&parent_path),
552            name,
553            &cid,
554            size,
555            link_type,
556            Some(meta),
557        ))?;
558
559        self.apply_root_update(new_root)?;
560
561        let inode = self.insert_path(parent, name.to_string(), child_path);
562        Ok(EntryAttr {
563            inode,
564            size,
565            kind: EntryKind::File,
566        })
567    }
568
569    pub fn mkdir(&self, parent: u64, name: &str) -> Result<EntryAttr, FsError> {
570        self.ensure_valid_name(name)?;
571        let _guard = self.modify_lock.lock().unwrap();
572
573        let parent_path = self.path_for_inode(parent)?;
574        let mut child_path = parent_path.clone();
575        child_path.push(name.to_string());
576
577        if self.resolve_entry(&child_path).is_ok() {
578            return Err(FsError::AlreadyExists);
579        }
580
581        let dir_cid = block_on(self.tree.put_directory(Vec::new()))?;
582        let new_root = block_on(self.tree.set_entry(
583            &self.current_root(),
584            &self.path_refs(&parent_path),
585            name,
586            &dir_cid,
587            0,
588            LinkType::Dir,
589        ))?;
590
591        self.apply_root_update(new_root)?;
592
593        let inode = self.insert_path(parent, name.to_string(), child_path);
594        Ok(EntryAttr {
595            inode,
596            size: 0,
597            kind: EntryKind::Directory,
598        })
599    }
600
601    pub fn write_file(&self, inode: u64, offset: u64, data: &[u8]) -> Result<u32, FsError> {
602        let _guard = self.modify_lock.lock().unwrap();
603        let path = self.path_for_inode(inode)?;
604        let existing = self.read_file_full(&path)?;
605        let new_data = Self::apply_write(existing, offset, data);
606        self.update_file_at_path(&path, new_data)?;
607        Ok(data.len() as u32)
608    }
609
610    pub fn truncate_file(&self, inode: u64, size: u64) -> Result<(), FsError> {
611        let _guard = self.modify_lock.lock().unwrap();
612        let path = self.path_for_inode(inode)?;
613        let existing = self.read_file_full(&path)?;
614        let new_data = Self::apply_truncate(existing, size);
615        self.update_file_at_path(&path, new_data)?;
616        Ok(())
617    }
618
619    pub fn unlink(&self, parent: u64, name: &str) -> Result<(), FsError> {
620        if Self::is_directory_refresh_sentinel(name) {
621            return Ok(());
622        }
623        if self.take_refresh_unlink(parent, name)? {
624            return Ok(());
625        }
626        self.ensure_valid_name(name)?;
627        let _guard = self.modify_lock.lock().unwrap();
628
629        let parent_path = self.path_for_inode(parent)?;
630        let mut child_path = parent_path.clone();
631        child_path.push(name.to_string());
632        let entry = self.resolve_entry(&child_path)?;
633        if entry.link_type.is_directory_like() {
634            return Err(FsError::IsDir);
635        }
636
637        let new_root = block_on(self.tree.remove_entry(
638            &self.current_root(),
639            &self.path_refs(&parent_path),
640            name,
641        ))?;
642
643        self.apply_root_update(new_root)?;
644        self.remove_paths_prefix(&child_path);
645        self.children.write().unwrap().remove(&ChildKey {
646            parent,
647            name: name.to_string(),
648        });
649
650        Ok(())
651    }
652
653    pub fn rmdir(&self, parent: u64, name: &str) -> Result<(), FsError> {
654        if self.take_refresh_unlink(parent, name)? {
655            return Ok(());
656        }
657        self.ensure_valid_name(name)?;
658        let _guard = self.modify_lock.lock().unwrap();
659
660        let parent_path = self.path_for_inode(parent)?;
661        let mut child_path = parent_path.clone();
662        child_path.push(name.to_string());
663        let entry = self.resolve_entry(&child_path)?;
664        if !entry.link_type.is_directory_like() {
665            return Err(FsError::NotDir);
666        }
667
668        let dir_entries = block_on(self.tree.list_directory(&entry.cid))?;
669        if !dir_entries.is_empty() {
670            return Err(FsError::NotEmpty);
671        }
672
673        let new_root = block_on(self.tree.remove_entry(
674            &self.current_root(),
675            &self.path_refs(&parent_path),
676            name,
677        ))?;
678
679        self.apply_root_update(new_root)?;
680        self.remove_paths_prefix(&child_path);
681        self.children.write().unwrap().remove(&ChildKey {
682            parent,
683            name: name.to_string(),
684        });
685
686        Ok(())
687    }
688
689    pub fn rename(
690        &self,
691        parent: u64,
692        name: &str,
693        new_parent: u64,
694        new_name: &str,
695    ) -> Result<(), FsError> {
696        self.ensure_valid_name(name)?;
697        self.ensure_valid_name(new_name)?;
698        let _guard = self.modify_lock.lock().unwrap();
699
700        if parent == new_parent && name == new_name {
701            return Ok(());
702        }
703
704        let parent_path = self.path_for_inode(parent)?;
705        let new_parent_path = self.path_for_inode(new_parent)?;
706
707        let mut old_path = parent_path.clone();
708        old_path.push(name.to_string());
709        let entry = self.resolve_entry(&old_path)?;
710
711        let new_root = block_on(self.tree.set_entry_with_meta(
712            &self.current_root(),
713            &self.path_refs(&new_parent_path),
714            new_name,
715            &entry.cid,
716            entry.size,
717            entry.link_type,
718            entry.meta.clone(),
719        ))?;
720        let new_root = block_on(self.tree.remove_entry(
721            &new_root,
722            &self.path_refs(&parent_path),
723            name,
724        ))?;
725
726        self.apply_root_update(new_root)?;
727
728        let inode = self.get_or_create_child_inode(parent, name)?;
729        let mut new_path = new_parent_path.clone();
730        new_path.push(new_name.to_string());
731
732        self.children.write().unwrap().remove(&ChildKey {
733            parent,
734            name: name.to_string(),
735        });
736        self.children.write().unwrap().insert(
737            ChildKey {
738                parent: new_parent,
739                name: new_name.to_string(),
740            },
741            inode,
742        );
743
744        self.parents.write().unwrap().insert(inode, new_parent);
745        self.update_paths_prefix(&old_path, &new_path);
746
747        Ok(())
748    }
749
750    fn ensure_valid_name(&self, name: &str) -> Result<(), FsError> {
751        if name.is_empty() || name.contains('/') || Self::is_directory_refresh_sentinel(name) {
752            return Err(FsError::InvalidName);
753        }
754        Ok(())
755    }
756
757    fn is_directory_refresh_sentinel(name: &str) -> bool {
758        name == DIRECTORY_REFRESH_SENTINEL_NAME
759    }
760
761    fn is_directory_refresh_sentinel_path(path: &[String]) -> bool {
762        path.last()
763            .is_some_and(|name| Self::is_directory_refresh_sentinel(name))
764    }
765
766    fn directory_refresh_sentinel_attr(inode: u64) -> EntryAttr {
767        EntryAttr {
768            inode,
769            size: 0,
770            kind: EntryKind::File,
771        }
772    }
773
774    fn path_for_inode(&self, inode: u64) -> Result<Vec<String>, FsError> {
775        self.paths
776            .read()
777            .unwrap()
778            .get(&inode)
779            .cloned()
780            .ok_or(FsError::NotFound)
781    }
782
783    fn take_refresh_unlink(&self, parent: u64, name: &str) -> Result<bool, FsError> {
784        let parent_path = self.path_for_inode(parent)?;
785        let mut child_path = parent_path;
786        child_path.push(name.to_string());
787        Ok(self.refresh_unlinks.lock().unwrap().remove(&child_path))
788    }
789
790    fn resolve_entry(&self, path: &[String]) -> Result<ResolvedEntry, FsError> {
791        self.resolve_entry_at_root(&self.current_root(), path)
792    }
793
794    fn resolve_entry_at_root(&self, root: &Cid, path: &[String]) -> Result<ResolvedEntry, FsError> {
795        if path.is_empty() {
796            return Ok(ResolvedEntry {
797                cid: root.clone(),
798                link_type: LinkType::Dir,
799                size: 0,
800                meta: None,
801            });
802        }
803
804        let (parent_path, name) = path.split_at(path.len() - 1);
805        let parent_cid = self.resolve_dir_cid_at_root(root, parent_path)?;
806        let entries = block_on(self.tree.list_directory(&parent_cid))?;
807        let entry = entries
808            .into_iter()
809            .find(|e| e.name == name[0])
810            .ok_or(FsError::NotFound)?;
811
812        Ok(ResolvedEntry {
813            cid: Cid {
814                hash: entry.hash,
815                key: entry.key,
816            },
817            link_type: entry.link_type,
818            size: entry.size,
819            meta: entry.meta,
820        })
821    }
822
823    fn resolve_dir_cid(&self, path: &[String]) -> Result<Cid, FsError> {
824        self.resolve_dir_cid_at_root(&self.current_root(), path)
825    }
826
827    fn resolve_dir_cid_at_root(&self, root: &Cid, path: &[String]) -> Result<Cid, FsError> {
828        if path.is_empty() {
829            return Ok(root.clone());
830        }
831
832        let path_str = path.join("/");
833        let cid = block_on(self.tree.resolve(root, &path_str))?.ok_or(FsError::NotFound)?;
834
835        let is_dir = block_on(self.tree.is_dir(&cid))?;
836        if !is_dir {
837            return Err(FsError::NotDir);
838        }
839
840        Ok(cid)
841    }
842
843    #[cfg(feature = "fuse")]
844    fn directory_entry_names_at_root(
845        &self,
846        root: &Cid,
847        path: &[String],
848    ) -> Option<HashSet<String>> {
849        let dir_cid = self.resolve_dir_cid_at_root(root, path).ok()?;
850        let entries = block_on(self.tree.list_directory(&dir_cid)).ok()?;
851        Some(
852            entries
853                .into_iter()
854                .map(|entry| entry.name)
855                .filter(|name| !Self::is_directory_refresh_sentinel(name))
856                .collect(),
857        )
858    }
859
860    fn retain_existing_paths_after_root_update(&self) -> Result<(), FsError> {
861        let known: Vec<(u64, Vec<String>)> = self
862            .paths
863            .read()
864            .unwrap()
865            .iter()
866            .map(|(inode, path)| (*inode, path.clone()))
867            .collect();
868        let mut keep = HashSet::new();
869        keep.insert(ROOT_INODE);
870
871        for (inode, path) in known {
872            if inode == ROOT_INODE {
873                continue;
874            }
875            if Self::is_directory_refresh_sentinel_path(&path) {
876                if self.resolve_dir_cid(&path[..path.len() - 1]).is_ok() {
877                    keep.insert(inode);
878                }
879                continue;
880            }
881            match self.resolve_entry(&path) {
882                Ok(_) => {
883                    keep.insert(inode);
884                }
885                Err(FsError::NotFound) | Err(FsError::NotDir) => {}
886                Err(error) => return Err(error),
887            }
888        }
889
890        self.paths
891            .write()
892            .unwrap()
893            .retain(|inode, _| keep.contains(inode));
894        self.parents
895            .write()
896            .unwrap()
897            .retain(|inode, parent| keep.contains(inode) && keep.contains(parent));
898        self.children
899            .write()
900            .unwrap()
901            .retain(|key, inode| keep.contains(&key.parent) && keep.contains(inode));
902        Ok(())
903    }
904
905    fn entry_attr_from_resolved(
906        &self,
907        inode: u64,
908        entry: ResolvedEntry,
909    ) -> Result<EntryAttr, FsError> {
910        let kind = Self::kind_from_link(entry.link_type);
911        let size = if kind == EntryKind::Directory {
912            0
913        } else {
914            self.entry_size(&entry)?
915        };
916
917        Ok(EntryAttr { inode, size, kind })
918    }
919
920    fn entry_size(&self, entry: &ResolvedEntry) -> Result<u64, FsError> {
921        if entry.link_type.is_directory_like() {
922            return Ok(0);
923        }
924        if entry.size > 0 {
925            return Ok(entry.size);
926        }
927
928        let data = block_on(self.tree.get(&entry.cid, None))?.ok_or(FsError::NotFound)?;
929        Ok(data.len() as u64)
930    }
931
932    fn read_file_full(&self, path: &[String]) -> Result<Vec<u8>, FsError> {
933        let entry = self.resolve_entry(path)?;
934        if entry.link_type.is_directory_like() {
935            return Err(FsError::IsDir);
936        }
937        let data = block_on(self.tree.get(&entry.cid, None))?.ok_or(FsError::NotFound)?;
938        Ok(data)
939    }
940
941    fn update_file_at_path(&self, path: &[String], data: Vec<u8>) -> Result<(), FsError> {
942        let (parent_path, name) = path.split_at(path.len() - 1);
943        let (cid, size) = block_on(self.tree.put(&data))?;
944        let link_type = self.link_type_for_size(size);
945        let meta = Self::file_entry_meta(&data);
946
947        let new_root = block_on(self.tree.set_entry_with_meta(
948            &self.current_root(),
949            &self.path_refs(parent_path),
950            name[0].as_str(),
951            &cid,
952            size,
953            link_type,
954            Some(meta),
955        ))?;
956
957        self.apply_root_update(new_root)
958    }
959
960    fn file_entry_meta(data: &[u8]) -> HashMap<String, serde_json::Value> {
961        HashMap::from([(
962            WHOLE_FILE_HASH_META_KEY.to_string(),
963            serde_json::Value::String(to_hex(&sha256(data))),
964        )])
965    }
966
967    fn apply_root_update(&self, new_root: Cid) -> Result<(), FsError> {
968        if let Some(publisher) = &self.publisher {
969            publisher.publish(&new_root)?;
970        }
971        *self.root.write().unwrap() = new_root;
972        Ok(())
973    }
974
975    fn link_type_for_size(&self, size: u64) -> LinkType {
976        if size as usize > self.tree.chunk_size() {
977            LinkType::File
978        } else {
979            LinkType::Blob
980        }
981    }
982
983    fn get_or_create_child_inode(&self, parent: u64, name: &str) -> Result<u64, FsError> {
984        let key = ChildKey {
985            parent,
986            name: name.to_string(),
987        };
988        if let Some(inode) = self.children.read().unwrap().get(&key).copied() {
989            return Ok(inode);
990        }
991
992        let parent_path = self.path_for_inode(parent)?;
993        let mut child_path = parent_path.clone();
994        child_path.push(name.to_string());
995
996        if let Some(existing) = self.find_inode_by_path(&child_path) {
997            self.children.write().unwrap().insert(key, existing);
998            return Ok(existing);
999        }
1000
1001        Ok(self.insert_path(parent, name.to_string(), child_path))
1002    }
1003
1004    fn insert_path(&self, parent: u64, name: String, path: Vec<String>) -> u64 {
1005        let inode = self.next_inode.fetch_add(1, Ordering::Relaxed);
1006        self.paths.write().unwrap().insert(inode, path);
1007        self.parents.write().unwrap().insert(inode, parent);
1008        self.children
1009            .write()
1010            .unwrap()
1011            .insert(ChildKey { parent, name }, inode);
1012        inode
1013    }
1014
1015    fn find_inode_by_path(&self, path: &[String]) -> Option<u64> {
1016        self.paths
1017            .read()
1018            .unwrap()
1019            .iter()
1020            .find_map(|(inode, inode_path)| {
1021                if inode_path == path {
1022                    Some(*inode)
1023                } else {
1024                    None
1025                }
1026            })
1027    }
1028
1029    fn update_paths_prefix(&self, old_prefix: &[String], new_prefix: &[String]) {
1030        let mut paths = self.paths.write().unwrap();
1031        for path in paths.values_mut() {
1032            if Self::path_has_prefix(path, old_prefix) {
1033                let mut updated = new_prefix.to_vec();
1034                updated.extend_from_slice(&path[old_prefix.len()..]);
1035                *path = updated;
1036            }
1037        }
1038    }
1039
1040    fn remove_paths_prefix(&self, prefix: &[String]) {
1041        let mut to_remove = Vec::new();
1042        {
1043            let paths = self.paths.read().unwrap();
1044            for (inode, path) in paths.iter() {
1045                if *inode == ROOT_INODE {
1046                    continue;
1047                }
1048                if Self::path_has_prefix(path, prefix) {
1049                    to_remove.push(*inode);
1050                }
1051            }
1052        }
1053
1054        if to_remove.is_empty() {
1055            return;
1056        }
1057
1058        let remove_set: std::collections::HashSet<u64> = to_remove.into_iter().collect();
1059        self.paths
1060            .write()
1061            .unwrap()
1062            .retain(|inode, _| !remove_set.contains(inode));
1063        self.parents
1064            .write()
1065            .unwrap()
1066            .retain(|inode, _| !remove_set.contains(inode));
1067        self.children
1068            .write()
1069            .unwrap()
1070            .retain(|_, inode| !remove_set.contains(inode));
1071    }
1072
1073    #[cfg(feature = "fuse")]
1074    fn drop_paths(&self, paths: impl IntoIterator<Item = Vec<String>>) {
1075        for path in paths {
1076            if let Some(inode) = self.find_inode_by_path(&path) {
1077                self.drop_inode(inode);
1078            }
1079        }
1080    }
1081
1082    fn drop_inode(&self, inode: u64) {
1083        if inode == ROOT_INODE {
1084            return;
1085        }
1086        let mut paths = self.paths.write().unwrap();
1087        let removed_path = paths.remove(&inode);
1088        drop(paths);
1089        self.parents.write().unwrap().remove(&inode);
1090        if let Some(path) = removed_path {
1091            if let Some((name, parent_path)) = path.split_last() {
1092                if let Some(parent_inode) = self.find_inode_by_path(parent_path) {
1093                    self.children.write().unwrap().remove(&ChildKey {
1094                        parent: parent_inode,
1095                        name: name.to_string(),
1096                    });
1097                }
1098            }
1099        }
1100    }
1101
1102    fn parent_inode(&self, inode: u64) -> u64 {
1103        self.parents
1104            .read()
1105            .unwrap()
1106            .get(&inode)
1107            .copied()
1108            .unwrap_or(ROOT_INODE)
1109    }
1110
1111    fn kind_from_link(link_type: LinkType) -> EntryKind {
1112        match link_type {
1113            LinkType::Dir | LinkType::Fanout => EntryKind::Directory,
1114            LinkType::Blob | LinkType::File => EntryKind::File,
1115        }
1116    }
1117
1118    fn apply_write(mut existing: Vec<u8>, offset: u64, data: &[u8]) -> Vec<u8> {
1119        let offset_usize = offset as usize;
1120        if existing.len() < offset_usize {
1121            existing.resize(offset_usize, 0);
1122        }
1123        if existing.len() < offset_usize + data.len() {
1124            existing.resize(offset_usize + data.len(), 0);
1125        }
1126        existing[offset_usize..offset_usize + data.len()].copy_from_slice(data);
1127        existing
1128    }
1129
1130    fn apply_truncate(mut existing: Vec<u8>, size: u64) -> Vec<u8> {
1131        let size = size as usize;
1132        if existing.len() > size {
1133            existing.truncate(size);
1134        } else if existing.len() < size {
1135            existing.resize(size, 0);
1136        }
1137        existing
1138    }
1139
1140    fn path_refs<'a>(&self, path: &'a [String]) -> Vec<&'a str> {
1141        path.iter().map(|p| p.as_str()).collect()
1142    }
1143
1144    fn path_has_prefix(path: &[String], prefix: &[String]) -> bool {
1145        if prefix.len() > path.len() {
1146            return false;
1147        }
1148        path.iter().zip(prefix.iter()).all(|(a, b)| a == b)
1149    }
1150}
1151
1152#[cfg(feature = "fuse")]
1153mod fuse_impl {
1154    use super::*;
1155    use fuser::{
1156        FileAttr, FileType, Filesystem, MountOption, ReplyAttr, ReplyCreate, ReplyData,
1157        ReplyDirectory, ReplyEmpty, ReplyEntry, ReplyStatfs, ReplyWrite, Request,
1158    };
1159    use std::ffi::CString;
1160    use std::ffi::OsStr;
1161    use std::path::Path;
1162    use std::time::{Duration, SystemTime};
1163
1164    const TTL: Duration = Duration::ZERO;
1165    const FALLBACK_BLOCK_SIZE: u32 = 4096;
1166    const FALLBACK_TOTAL_BYTES: u64 = 1 << 40;
1167    const FALLBACK_FREE_BYTES: u64 = 1 << 39;
1168    const FALLBACK_TOTAL_FILES: u64 = 1_000_000;
1169    const FALLBACK_FREE_FILES: u64 = 900_000;
1170
1171    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
1172    pub(crate) struct FsStats {
1173        pub(crate) blocks: u64,
1174        pub(crate) bfree: u64,
1175        pub(crate) bavail: u64,
1176        pub(crate) files: u64,
1177        pub(crate) ffree: u64,
1178        pub(crate) bsize: u32,
1179        pub(crate) namelen: u32,
1180        pub(crate) frsize: u32,
1181    }
1182
1183    impl FsError {
1184        fn errno(&self) -> i32 {
1185            match self {
1186                FsError::InvalidRoot | FsError::InvalidName => libc::EINVAL,
1187                FsError::NotFound => libc::ENOENT,
1188                FsError::NotDir => libc::ENOTDIR,
1189                FsError::IsDir => libc::EISDIR,
1190                FsError::AlreadyExists => libc::EEXIST,
1191                FsError::NotEmpty => libc::ENOTEMPTY,
1192                FsError::Tree(_) | FsError::Publish(_) => libc::EIO,
1193            }
1194        }
1195    }
1196
1197    pub(crate) fn fallback_fs_stats() -> FsStats {
1198        let blocks = FALLBACK_TOTAL_BYTES / u64::from(FALLBACK_BLOCK_SIZE);
1199        let free_blocks = FALLBACK_FREE_BYTES / u64::from(FALLBACK_BLOCK_SIZE);
1200        FsStats {
1201            blocks,
1202            bfree: free_blocks,
1203            bavail: free_blocks,
1204            files: FALLBACK_TOTAL_FILES,
1205            ffree: FALLBACK_FREE_FILES,
1206            bsize: FALLBACK_BLOCK_SIZE,
1207            namelen: 255,
1208            frsize: FALLBACK_BLOCK_SIZE,
1209        }
1210    }
1211
1212    fn host_fs_stats(path: &Path) -> Option<FsStats> {
1213        let path = CString::new(path.as_os_str().as_encoded_bytes()).ok()?;
1214        let mut stat = std::mem::MaybeUninit::<libc::statfs>::uninit();
1215        let result = unsafe { libc::statfs(path.as_ptr(), stat.as_mut_ptr()) };
1216        if result != 0 {
1217            return None;
1218        }
1219
1220        let stat = unsafe { stat.assume_init() };
1221        let bsize = u32::try_from(stat.f_bsize).ok()?;
1222        Some(FsStats {
1223            blocks: stat.f_blocks,
1224            bfree: stat.f_bfree,
1225            bavail: stat.f_bavail,
1226            files: stat.f_files,
1227            ffree: stat.f_ffree,
1228            bsize,
1229            namelen: 255,
1230            frsize: bsize,
1231        })
1232    }
1233
1234    pub(crate) fn current_fs_stats() -> FsStats {
1235        host_fs_stats(Path::new("/")).unwrap_or_else(fallback_fs_stats)
1236    }
1237
1238    impl<S: Store + Send + Sync + 'static> HashtreeFuse<S> {
1239        pub fn mount(
1240            self,
1241            mountpoint: impl AsRef<Path>,
1242            options: &[MountOption],
1243        ) -> std::io::Result<()> {
1244            let mut session = fuser::Session::new(self.clone(), mountpoint.as_ref(), options)?;
1245            self.set_notifier(session.notifier());
1246            let result = session.run();
1247            self.clear_notifier();
1248            result
1249        }
1250
1251        fn file_attr(&self, attr: &EntryAttr) -> FileAttr {
1252            let (kind, perm, nlink) = match attr.kind {
1253                EntryKind::Directory => (FileType::Directory, 0o755, 2),
1254                EntryKind::File => (FileType::RegularFile, 0o644, 1),
1255            };
1256            let uid = unsafe { libc::geteuid() };
1257            let gid = unsafe { libc::getegid() };
1258            let blocks = attr.size.div_ceil(512);
1259
1260            FileAttr {
1261                ino: attr.inode,
1262                size: attr.size,
1263                blocks,
1264                atime: SystemTime::UNIX_EPOCH,
1265                mtime: SystemTime::UNIX_EPOCH,
1266                ctime: SystemTime::UNIX_EPOCH,
1267                crtime: SystemTime::UNIX_EPOCH,
1268                kind,
1269                perm,
1270                nlink,
1271                uid,
1272                gid,
1273                rdev: 0,
1274                blksize: 512,
1275                flags: 0,
1276            }
1277        }
1278
1279        fn file_type(kind: EntryKind) -> FileType {
1280            match kind {
1281                EntryKind::Directory => FileType::Directory,
1282                EntryKind::File => FileType::RegularFile,
1283            }
1284        }
1285    }
1286
1287    impl<S: Store + Send + Sync + 'static> Filesystem for HashtreeFuse<S> {
1288        fn lookup(&mut self, _req: &Request<'_>, parent: u64, name: &OsStr, reply: ReplyEntry) {
1289            let name = match name.to_str() {
1290                Some(value) => value,
1291                None => {
1292                    reply.error(libc::ENOENT);
1293                    return;
1294                }
1295            };
1296
1297            match self.lookup_child(parent, name) {
1298                Ok(attr) => reply.entry(&TTL, &self.file_attr(&attr), 0),
1299                Err(err) => reply.error(err.errno()),
1300            }
1301        }
1302
1303        fn getattr(&mut self, _req: &Request<'_>, ino: u64, reply: ReplyAttr) {
1304            match self.get_attr(ino) {
1305                Ok(attr) => reply.attr(&TTL, &self.file_attr(&attr)),
1306                Err(err) => reply.error(err.errno()),
1307            }
1308        }
1309
1310        fn open(&mut self, _req: &Request<'_>, _ino: u64, _flags: i32, reply: fuser::ReplyOpen) {
1311            reply.opened(0, 0);
1312        }
1313
1314        fn read(
1315            &mut self,
1316            _req: &Request<'_>,
1317            ino: u64,
1318            _fh: u64,
1319            offset: i64,
1320            size: u32,
1321            _flags: i32,
1322            _lock_owner: Option<u64>,
1323            reply: ReplyData,
1324        ) {
1325            let offset = if offset < 0 { 0 } else { offset as u64 };
1326            match self.read_file(ino, offset, size) {
1327                Ok(data) => reply.data(&data),
1328                Err(err) => reply.error(err.errno()),
1329            }
1330        }
1331
1332        fn write(
1333            &mut self,
1334            _req: &Request<'_>,
1335            ino: u64,
1336            _fh: u64,
1337            offset: i64,
1338            data: &[u8],
1339            _write_flags: u32,
1340            _flags: i32,
1341            _lock_owner: Option<u64>,
1342            reply: ReplyWrite,
1343        ) {
1344            let offset = if offset < 0 { 0 } else { offset as u64 };
1345            match self.write_file(ino, offset, data) {
1346                Ok(written) => reply.written(written),
1347                Err(err) => reply.error(err.errno()),
1348            }
1349        }
1350
1351        fn create(
1352            &mut self,
1353            _req: &Request<'_>,
1354            parent: u64,
1355            name: &OsStr,
1356            _mode: u32,
1357            _umask: u32,
1358            _flags: i32,
1359            reply: ReplyCreate,
1360        ) {
1361            let name = match name.to_str() {
1362                Some(value) => value,
1363                None => {
1364                    reply.error(libc::EINVAL);
1365                    return;
1366                }
1367            };
1368
1369            match self.create_file(parent, name) {
1370                Ok(attr) => reply.created(&TTL, &self.file_attr(&attr), 0, 0, 0),
1371                Err(err) => reply.error(err.errno()),
1372            }
1373        }
1374
1375        fn mkdir(
1376            &mut self,
1377            _req: &Request<'_>,
1378            parent: u64,
1379            name: &OsStr,
1380            _mode: u32,
1381            _umask: u32,
1382            reply: ReplyEntry,
1383        ) {
1384            let name = match name.to_str() {
1385                Some(value) => value,
1386                None => {
1387                    reply.error(libc::EINVAL);
1388                    return;
1389                }
1390            };
1391
1392            match HashtreeFuse::mkdir(self, parent, name) {
1393                Ok(attr) => reply.entry(&TTL, &self.file_attr(&attr), 0),
1394                Err(err) => reply.error(err.errno()),
1395            }
1396        }
1397
1398        fn unlink(&mut self, _req: &Request<'_>, parent: u64, name: &OsStr, reply: ReplyEmpty) {
1399            let name = match name.to_str() {
1400                Some(value) => value,
1401                None => {
1402                    reply.error(libc::EINVAL);
1403                    return;
1404                }
1405            };
1406
1407            match HashtreeFuse::unlink(self, parent, name) {
1408                Ok(()) => reply.ok(),
1409                Err(err) => reply.error(err.errno()),
1410            }
1411        }
1412
1413        fn rmdir(&mut self, _req: &Request<'_>, parent: u64, name: &OsStr, reply: ReplyEmpty) {
1414            let name = match name.to_str() {
1415                Some(value) => value,
1416                None => {
1417                    reply.error(libc::EINVAL);
1418                    return;
1419                }
1420            };
1421
1422            match HashtreeFuse::rmdir(self, parent, name) {
1423                Ok(()) => reply.ok(),
1424                Err(err) => reply.error(err.errno()),
1425            }
1426        }
1427
1428        fn rename(
1429            &mut self,
1430            _req: &Request<'_>,
1431            parent: u64,
1432            name: &OsStr,
1433            newparent: u64,
1434            newname: &OsStr,
1435            _flags: u32,
1436            reply: ReplyEmpty,
1437        ) {
1438            let name = match name.to_str() {
1439                Some(value) => value,
1440                None => {
1441                    reply.error(libc::EINVAL);
1442                    return;
1443                }
1444            };
1445            let newname = match newname.to_str() {
1446                Some(value) => value,
1447                None => {
1448                    reply.error(libc::EINVAL);
1449                    return;
1450                }
1451            };
1452
1453            match HashtreeFuse::rename(self, parent, name, newparent, newname) {
1454                Ok(()) => reply.ok(),
1455                Err(err) => reply.error(err.errno()),
1456            }
1457        }
1458
1459        fn setattr(
1460            &mut self,
1461            _req: &Request<'_>,
1462            ino: u64,
1463            _mode: Option<u32>,
1464            _uid: Option<u32>,
1465            _gid: Option<u32>,
1466            size: Option<u64>,
1467            _atime: Option<fuser::TimeOrNow>,
1468            _mtime: Option<fuser::TimeOrNow>,
1469            _ctime: Option<SystemTime>,
1470            _fh: Option<u64>,
1471            _crtime: Option<SystemTime>,
1472            _chgtime: Option<SystemTime>,
1473            _bkuptime: Option<SystemTime>,
1474            _flags: Option<u32>,
1475            reply: ReplyAttr,
1476        ) {
1477            if let Some(size) = size {
1478                match self.truncate_file(ino, size) {
1479                    Ok(()) => {
1480                        if let Ok(attr) = self.get_attr(ino) {
1481                            reply.attr(&TTL, &self.file_attr(&attr));
1482                        } else {
1483                            reply.error(libc::EIO);
1484                        }
1485                    }
1486                    Err(err) => reply.error(err.errno()),
1487                }
1488            } else {
1489                match self.get_attr(ino) {
1490                    Ok(attr) => reply.attr(&TTL, &self.file_attr(&attr)),
1491                    Err(err) => reply.error(err.errno()),
1492                }
1493            }
1494        }
1495
1496        fn readdir(
1497            &mut self,
1498            _req: &Request<'_>,
1499            ino: u64,
1500            _fh: u64,
1501            offset: i64,
1502            mut reply: ReplyDirectory,
1503        ) {
1504            let mut entries = Vec::new();
1505            entries.push((ino, EntryKind::Directory, ".".to_string()));
1506            let parent = self.parent_inode(ino);
1507            entries.push((parent, EntryKind::Directory, "..".to_string()));
1508
1509            match self.read_dir(ino) {
1510                Ok(children) => {
1511                    for entry in children {
1512                        entries.push((entry.inode, entry.kind, entry.name));
1513                    }
1514                }
1515                Err(err) => {
1516                    reply.error(err.errno());
1517                    return;
1518                }
1519            }
1520
1521            let start = if offset < 0 { 0 } else { offset as usize };
1522            for (index, (inode, kind, name)) in entries.into_iter().enumerate().skip(start) {
1523                let next_offset = (index + 1) as i64;
1524                let full = reply.add(inode, next_offset, Self::file_type(kind), name);
1525                if full {
1526                    break;
1527                }
1528            }
1529
1530            reply.ok();
1531        }
1532
1533        fn statfs(&mut self, _req: &Request<'_>, _ino: u64, reply: ReplyStatfs) {
1534            let stats = current_fs_stats();
1535            reply.statfs(
1536                stats.blocks,
1537                stats.bfree,
1538                stats.bavail,
1539                stats.files,
1540                stats.ffree,
1541                stats.bsize,
1542                stats.namelen,
1543                stats.frsize,
1544            );
1545        }
1546    }
1547}
1548
1549#[cfg(test)]
1550mod tests {
1551    use super::*;
1552    use hashtree_core::store::MemoryStore;
1553
1554    #[cfg(feature = "fuse")]
1555    use super::fuse_impl::{current_fs_stats, fallback_fs_stats};
1556
1557    struct RecordingPublisher {
1558        updates: Mutex<Vec<Cid>>,
1559    }
1560
1561    impl RecordingPublisher {
1562        fn new() -> Self {
1563            Self {
1564                updates: Mutex::new(Vec::new()),
1565            }
1566        }
1567
1568        fn updates(&self) -> Vec<Cid> {
1569            self.updates.lock().unwrap().clone()
1570        }
1571    }
1572
1573    impl RootPublisher for RecordingPublisher {
1574        fn publish(&self, cid: &Cid) -> Result<(), FsError> {
1575            self.updates.lock().unwrap().push(cid.clone());
1576            Ok(())
1577        }
1578    }
1579
1580    async fn empty_root(store: Arc<MemoryStore>) -> Cid {
1581        let tree = HashTree::new(HashTreeConfig::new(store.clone()));
1582        tree.put_directory(Vec::new()).await.unwrap()
1583    }
1584
1585    #[tokio::test]
1586    async fn test_create_write_read_file() {
1587        let store = Arc::new(MemoryStore::new());
1588        let root = empty_root(store.clone()).await;
1589        let fs = HashtreeFuse::new(store.clone(), root).unwrap();
1590
1591        let attr = fs.create_file(ROOT_INODE, "hello.txt").unwrap();
1592        assert_eq!(attr.kind, EntryKind::File);
1593
1594        fs.write_file(attr.inode, 0, b"hello").unwrap();
1595        let read = fs.read_file(attr.inode, 0, 5).unwrap();
1596        assert_eq!(read, b"hello");
1597
1598        let tree = HashTree::new(HashTreeConfig::new(store));
1599        let entries = tree.list_directory(&fs.current_root()).await.unwrap();
1600        let file = entries
1601            .iter()
1602            .find(|entry| entry.name == "hello.txt")
1603            .unwrap();
1604        assert_eq!(
1605            file.meta
1606                .as_ref()
1607                .and_then(|meta| meta.get("whole_file_hash"))
1608                .and_then(serde_json::Value::as_str),
1609            Some("2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
1610        );
1611    }
1612
1613    #[tokio::test]
1614    async fn test_mkdir_and_rename() {
1615        let store = Arc::new(MemoryStore::new());
1616        let root = empty_root(store.clone()).await;
1617        let fs = HashtreeFuse::new(store.clone(), root).unwrap();
1618
1619        let dir = fs.mkdir(ROOT_INODE, "docs").unwrap();
1620        let file = fs.create_file(dir.inode, "draft.txt").unwrap();
1621        fs.write_file(file.inode, 0, b"data").unwrap();
1622
1623        fs.rename(dir.inode, "draft.txt", dir.inode, "final.txt")
1624            .unwrap();
1625        let entries = fs.read_dir(dir.inode).unwrap();
1626        let names: Vec<String> = entries.into_iter().map(|e| e.name).collect();
1627        assert!(names.contains(&"final.txt".to_string()));
1628        assert!(!names.contains(&"draft.txt".to_string()));
1629
1630        let tree = HashTree::new(HashTreeConfig::new(store));
1631        let docs = tree
1632            .resolve(&fs.current_root(), "docs")
1633            .await
1634            .unwrap()
1635            .unwrap();
1636        let entries = tree.list_directory(&docs).await.unwrap();
1637        let file = entries
1638            .iter()
1639            .find(|entry| entry.name == "final.txt")
1640            .unwrap();
1641        let expected_hash = to_hex(&sha256(b"data"));
1642        assert_eq!(
1643            file.meta
1644                .as_ref()
1645                .and_then(|meta| meta.get("whole_file_hash"))
1646                .and_then(serde_json::Value::as_str),
1647            Some(expected_hash.as_str())
1648        );
1649    }
1650
1651    #[tokio::test]
1652    async fn test_truncate_file() {
1653        let store = Arc::new(MemoryStore::new());
1654        let root = empty_root(store.clone()).await;
1655        let fs = HashtreeFuse::new(store, root).unwrap();
1656
1657        let file = fs.create_file(ROOT_INODE, "file.bin").unwrap();
1658        fs.write_file(file.inode, 0, b"abcdef").unwrap();
1659        fs.truncate_file(file.inode, 3).unwrap();
1660        let read = fs.read_file(file.inode, 0, 10).unwrap();
1661        assert_eq!(read, b"abc");
1662    }
1663
1664    #[tokio::test]
1665    async fn test_directory_refresh_sentinel_is_virtual() {
1666        let store = Arc::new(MemoryStore::new());
1667        let root = empty_root(store.clone()).await;
1668        let fs = HashtreeFuse::new(store, root.clone()).unwrap();
1669
1670        let entries = fs.read_dir(ROOT_INODE).unwrap();
1671        assert!(!entries
1672            .iter()
1673            .any(|entry| entry.name == DIRECTORY_REFRESH_SENTINEL_NAME));
1674        let sentinel = fs
1675            .lookup_child(ROOT_INODE, DIRECTORY_REFRESH_SENTINEL_NAME)
1676            .unwrap();
1677        assert_eq!(sentinel.kind, EntryKind::File);
1678        assert!(fs.read_file(sentinel.inode, 0, 10).unwrap().is_empty());
1679        assert!(fs
1680            .create_file(ROOT_INODE, DIRECTORY_REFRESH_SENTINEL_NAME)
1681            .is_err());
1682        fs.unlink(ROOT_INODE, DIRECTORY_REFRESH_SENTINEL_NAME)
1683            .unwrap();
1684        assert_eq!(fs.current_root(), root);
1685    }
1686
1687    #[test]
1688    fn test_directory_refresh_sentinel_is_not_hidden() {
1689        assert!(!DIRECTORY_REFRESH_SENTINEL_NAME.starts_with('.'));
1690    }
1691
1692    #[tokio::test]
1693    async fn test_publisher_invoked() {
1694        let store = Arc::new(MemoryStore::new());
1695        let root = empty_root(store.clone()).await;
1696        let publisher = Arc::new(RecordingPublisher::new());
1697        let fs = HashtreeFuse::new_with_publisher(store, root, Some(publisher.clone())).unwrap();
1698
1699        let file = fs.create_file(ROOT_INODE, "note.txt").unwrap();
1700        fs.write_file(file.inode, 0, b"note").unwrap();
1701
1702        let updates = publisher.updates();
1703        assert!(!updates.is_empty());
1704        assert_eq!(updates.last().unwrap(), &fs.current_root());
1705    }
1706
1707    #[tokio::test]
1708    async fn test_replace_root_refreshes_visible_tree_without_publishing() {
1709        let store = Arc::new(MemoryStore::new());
1710        let tree = HashTree::new(HashTreeConfig::new(store.clone()));
1711        let root = empty_root(store.clone()).await;
1712        let publisher = Arc::new(RecordingPublisher::new());
1713        let fs = HashtreeFuse::new_with_publisher(store, root, Some(publisher.clone())).unwrap();
1714
1715        let old_file = fs.create_file(ROOT_INODE, "old.txt").unwrap();
1716        fs.write_file(old_file.inode, 0, b"old").unwrap();
1717        assert!(!publisher.updates().is_empty());
1718        publisher.updates.lock().unwrap().clear();
1719
1720        let (new_blob, new_size) = tree.put(b"new").await.unwrap();
1721        let new_root = tree
1722            .put_directory(vec![hashtree_core::DirEntry::from_cid(
1723                "new.txt", &new_blob,
1724            )
1725            .with_size(new_size)
1726            .with_link_type(LinkType::Blob)])
1727            .await
1728            .unwrap();
1729
1730        let old_lookup = fs.lookup_child(ROOT_INODE, "old.txt").unwrap();
1731        fs.replace_root(new_root.clone()).unwrap();
1732
1733        assert_eq!(fs.current_root(), new_root);
1734        assert!(fs.lookup_child(ROOT_INODE, "old.txt").is_err());
1735        let new_lookup = fs.lookup_child(ROOT_INODE, "new.txt").unwrap();
1736        assert_ne!(old_lookup.inode, new_lookup.inode);
1737        assert_eq!(fs.read_file(new_lookup.inode, 0, 3).unwrap(), b"new");
1738        assert!(publisher.updates().is_empty());
1739    }
1740
1741    #[tokio::test]
1742    async fn test_replace_root_if_current_preserves_dirty_root() {
1743        let store = Arc::new(MemoryStore::new());
1744        let tree = HashTree::new(HashTreeConfig::new(store.clone()));
1745        let root = empty_root(store.clone()).await;
1746        let fs = HashtreeFuse::new(store, root.clone()).unwrap();
1747
1748        let local = fs.create_file(ROOT_INODE, "local.txt").unwrap();
1749        fs.write_file(local.inode, 0, b"local").unwrap();
1750        let dirty_root = fs.current_root();
1751
1752        let (remote_blob, remote_size) = tree.put(b"remote").await.unwrap();
1753        let remote_root = tree
1754            .put_directory(vec![hashtree_core::DirEntry::from_cid(
1755                "remote.txt",
1756                &remote_blob,
1757            )
1758            .with_size(remote_size)
1759            .with_link_type(LinkType::Blob)])
1760            .await
1761            .unwrap();
1762
1763        let replaced = fs
1764            .replace_root_if_current(&root, remote_root.clone())
1765            .unwrap();
1766
1767        assert!(!replaced);
1768        assert_eq!(fs.current_root(), dirty_root);
1769        assert!(fs.lookup_child(ROOT_INODE, "local.txt").is_ok());
1770        assert!(fs.lookup_child(ROOT_INODE, "remote.txt").is_err());
1771
1772        let replaced = fs
1773            .replace_root_if_current(&dirty_root, remote_root.clone())
1774            .unwrap();
1775        assert!(replaced);
1776        assert_eq!(fs.current_root(), remote_root);
1777    }
1778
1779    #[cfg(feature = "fuse")]
1780    #[tokio::test]
1781    async fn test_replace_root_invalidates_removed_entries_without_inode_notification() {
1782        let store = Arc::new(MemoryStore::new());
1783        let root = empty_root(store.clone()).await;
1784        let fs = HashtreeFuse::new(store.clone(), root).unwrap();
1785
1786        let old_file = fs.create_file(ROOT_INODE, "old.txt").unwrap();
1787        fs.write_file(old_file.inode, 0, b"old").unwrap();
1788        let new_root = empty_root(store).await;
1789
1790        let invalidations = fs.changed_known_entries_for_root(&new_root);
1791
1792        assert!(invalidations.contains(&FuseInvalidation::Entry {
1793            parent: ROOT_INODE,
1794            name: "old.txt".to_string(),
1795        }));
1796        assert_eq!(invalidations.len(), 2);
1797    }
1798
1799    #[cfg(feature = "fuse")]
1800    #[tokio::test]
1801    async fn test_replace_root_emits_delete_invalidation_for_removed_entries() {
1802        let store = Arc::new(MemoryStore::new());
1803        let root = empty_root(store.clone()).await;
1804        let fs = HashtreeFuse::new(store.clone(), root).unwrap();
1805
1806        let old_file = fs.create_file(ROOT_INODE, "old.txt").unwrap();
1807        fs.write_file(old_file.inode, 0, b"old").unwrap();
1808        let new_root = empty_root(store).await;
1809
1810        let invalidations = fs.changed_known_entries_for_root(&new_root);
1811
1812        assert!(invalidations.contains(&FuseInvalidation::Delete {
1813            parent: ROOT_INODE,
1814            child: old_file.inode,
1815            name: "old.txt".to_string(),
1816        }));
1817        assert_eq!(
1818            fs.removed_known_entry_paths_for_root(&new_root),
1819            vec![vec!["old.txt".to_string()]]
1820        );
1821        let delete_index = invalidations
1822            .iter()
1823            .position(|invalidation| {
1824                matches!(invalidation, FuseInvalidation::Delete { name, .. } if name == "old.txt")
1825            })
1826            .unwrap();
1827        let entry_index = invalidations
1828            .iter()
1829            .position(|invalidation| {
1830                matches!(invalidation, FuseInvalidation::Entry { name, .. } if name == "old.txt")
1831            })
1832            .unwrap();
1833        assert!(entry_index < delete_index);
1834    }
1835
1836    #[cfg(feature = "fuse")]
1837    #[tokio::test]
1838    async fn test_replace_root_invalidates_changed_known_entry_by_name_and_new_inode() {
1839        let store = Arc::new(MemoryStore::new());
1840        let tree = HashTree::new(HashTreeConfig::new(store.clone()));
1841        let (old_blob, old_size) = tree.put(b"old").await.unwrap();
1842        let root = tree
1843            .put_directory(vec![hashtree_core::DirEntry::from_cid(
1844                "note.txt", &old_blob,
1845            )
1846            .with_size(old_size)
1847            .with_link_type(LinkType::Blob)])
1848            .await
1849            .unwrap();
1850        let fs = HashtreeFuse::new(store, root).unwrap();
1851        let old_file = fs.lookup_child(ROOT_INODE, "note.txt").unwrap();
1852
1853        let (new_blob, new_size) = tree.put(b"new").await.unwrap();
1854        let new_root = tree
1855            .put_directory(vec![hashtree_core::DirEntry::from_cid(
1856                "note.txt", &new_blob,
1857            )
1858            .with_size(new_size)
1859            .with_link_type(LinkType::Blob)])
1860            .await
1861            .unwrap();
1862
1863        let invalidations = fs.changed_known_entries_for_root(&new_root);
1864
1865        assert!(invalidations.contains(&FuseInvalidation::Entry {
1866            parent: ROOT_INODE,
1867            name: "note.txt".to_string(),
1868        }));
1869        assert_eq!(invalidations.len(), 1);
1870
1871        fs.replace_root(new_root).unwrap();
1872        let new_file = fs.lookup_child(ROOT_INODE, "note.txt").unwrap();
1873        assert_ne!(old_file.inode, new_file.inode);
1874        assert_eq!(fs.read_file(new_file.inode, 0, 3).unwrap(), b"new");
1875    }
1876
1877    #[cfg(feature = "fuse")]
1878    #[tokio::test]
1879    async fn test_replace_root_invalidates_removed_name_without_known_child_inode() {
1880        let store = Arc::new(MemoryStore::new());
1881        let tree = HashTree::new(HashTreeConfig::new(store.clone()));
1882        let (old_blob, old_size) = tree.put(b"old").await.unwrap();
1883        let root = tree
1884            .put_directory(vec![hashtree_core::DirEntry::from_cid(
1885                "old.txt", &old_blob,
1886            )
1887            .with_size(old_size)
1888            .with_link_type(LinkType::Blob)])
1889            .await
1890            .unwrap();
1891        let fs = HashtreeFuse::new(store.clone(), root).unwrap();
1892        let new_root = empty_root(store).await;
1893
1894        let invalidations = fs.changed_known_entries_for_root(&new_root);
1895
1896        assert!(invalidations.contains(&FuseInvalidation::Entry {
1897            parent: ROOT_INODE,
1898            name: "old.txt".to_string(),
1899        }));
1900    }
1901
1902    #[tokio::test]
1903    async fn test_refresh_unlink_does_not_publish_or_change_root() {
1904        let store = Arc::new(MemoryStore::new());
1905        let root = empty_root(store.clone()).await;
1906        let publisher = Arc::new(RecordingPublisher::new());
1907        let fs = HashtreeFuse::new_with_publisher(store, root, Some(publisher.clone())).unwrap();
1908
1909        let old_file = fs.create_file(ROOT_INODE, "old.txt").unwrap();
1910        fs.write_file(old_file.inode, 0, b"old").unwrap();
1911        let old_root = fs.current_root();
1912        publisher.updates.lock().unwrap().clear();
1913
1914        fs.begin_refresh_unlinks(vec![vec!["old.txt".to_string()]]);
1915        fs.unlink(ROOT_INODE, "old.txt").unwrap();
1916
1917        assert_eq!(fs.current_root(), old_root);
1918        assert!(publisher.updates().is_empty());
1919        assert!(fs.lookup_child(ROOT_INODE, "old.txt").is_ok());
1920    }
1921
1922    #[cfg(feature = "fuse")]
1923    #[tokio::test]
1924    async fn test_replace_root_invalidates_added_entries_by_name() {
1925        let store = Arc::new(MemoryStore::new());
1926        let tree = HashTree::new(HashTreeConfig::new(store.clone()));
1927        let root = empty_root(store.clone()).await;
1928        let fs = HashtreeFuse::new(store, root).unwrap();
1929
1930        let (new_blob, new_size) = tree.put(b"new").await.unwrap();
1931        let new_root = tree
1932            .put_directory(vec![hashtree_core::DirEntry::from_cid(
1933                "new.txt", &new_blob,
1934            )
1935            .with_size(new_size)
1936            .with_link_type(LinkType::Blob)])
1937            .await
1938            .unwrap();
1939
1940        let invalidations = fs.changed_known_entries_for_root(&new_root);
1941
1942        assert!(invalidations.contains(&FuseInvalidation::Entry {
1943            parent: ROOT_INODE,
1944            name: "new.txt".to_string(),
1945        }));
1946    }
1947
1948    #[tokio::test]
1949    async fn test_replace_root_preserves_existing_directory_inodes() {
1950        let store = Arc::new(MemoryStore::new());
1951        let tree = HashTree::new(HashTreeConfig::new(store.clone()));
1952        let root = empty_root(store.clone()).await;
1953        let fs = HashtreeFuse::new(store, root).unwrap();
1954
1955        let docs = fs.mkdir(ROOT_INODE, "docs").unwrap();
1956        let old_file = fs.create_file(docs.inode, "old.txt").unwrap();
1957        fs.write_file(old_file.inode, 0, b"old").unwrap();
1958        let old_entries = fs.read_dir(docs.inode).unwrap();
1959        assert!(old_entries.iter().any(|entry| entry.name == "old.txt"));
1960
1961        let (new_blob, new_size) = tree.put(b"new").await.unwrap();
1962        let new_docs = tree
1963            .put_directory(vec![hashtree_core::DirEntry::from_cid(
1964                "new.txt", &new_blob,
1965            )
1966            .with_size(new_size)
1967            .with_link_type(LinkType::Blob)])
1968            .await
1969            .unwrap();
1970        let new_root = tree
1971            .put_directory(vec![
1972                hashtree_core::DirEntry::from_cid("docs", &new_docs).with_link_type(LinkType::Dir)
1973            ])
1974            .await
1975            .unwrap();
1976
1977        fs.replace_root(new_root).unwrap();
1978
1979        assert_eq!(
1980            fs.lookup_child(ROOT_INODE, "docs").unwrap().inode,
1981            docs.inode
1982        );
1983        assert_eq!(fs.get_attr(docs.inode).unwrap().kind, EntryKind::Directory);
1984        let new_entries = fs.read_dir(docs.inode).unwrap();
1985        let names: Vec<String> = new_entries.into_iter().map(|entry| entry.name).collect();
1986        assert!(names.contains(&"new.txt".to_string()));
1987        assert!(!names.contains(&"old.txt".to_string()));
1988    }
1989
1990    #[tokio::test]
1991    async fn test_fanout_directory_entry_behaves_as_directory() {
1992        let store = Arc::new(MemoryStore::new());
1993        let tree = HashTree::new(HashTreeConfig::new(store.clone()).with_max_links(2));
1994        let mut fanout_entries = Vec::new();
1995        for index in 0..5 {
1996            let body = format!("file-{index}");
1997            let (cid, size) = tree.put(body.as_bytes()).await.unwrap();
1998            fanout_entries.push(
1999                hashtree_core::DirEntry::from_cid(format!("file-{index}.txt"), &cid)
2000                    .with_size(size)
2001                    .with_link_type(LinkType::Blob),
2002            );
2003        }
2004        let fanout_dir = tree.put_directory(fanout_entries).await.unwrap();
2005        let root = tree
2006            .put_directory(vec![hashtree_core::DirEntry::from_cid("big", &fanout_dir)
2007                .with_link_type(LinkType::Fanout)])
2008            .await
2009            .unwrap();
2010        let fs = HashtreeFuse::new(store, root).unwrap();
2011
2012        let big = fs.lookup_child(ROOT_INODE, "big").unwrap();
2013        assert_eq!(big.kind, EntryKind::Directory);
2014        assert!(matches!(fs.read_file(big.inode, 0, 8), Err(FsError::IsDir)));
2015        assert!(matches!(fs.unlink(ROOT_INODE, "big"), Err(FsError::IsDir)));
2016        assert!(matches!(
2017            fs.rmdir(ROOT_INODE, "big"),
2018            Err(FsError::NotEmpty)
2019        ));
2020
2021        let names: Vec<String> = fs
2022            .read_dir(big.inode)
2023            .unwrap()
2024            .into_iter()
2025            .map(|entry| entry.name)
2026            .collect();
2027        assert!(names.contains(&"file-3.txt".to_string()));
2028
2029        let file = fs.lookup_child(big.inode, "file-3.txt").unwrap();
2030        assert_eq!(file.kind, EntryKind::File);
2031        assert_eq!(fs.read_file(file.inode, 0, 6).unwrap(), b"file-3");
2032    }
2033
2034    #[cfg(feature = "fuse")]
2035    #[test]
2036    fn test_fallback_fs_stats_reports_free_space() {
2037        let stats = fallback_fs_stats();
2038        assert!(stats.blocks > 0);
2039        assert!(stats.bfree > 0);
2040        assert!(stats.bavail > 0);
2041        assert!(stats.bsize > 0);
2042        assert_eq!(stats.bfree, stats.bavail);
2043    }
2044
2045    #[cfg(feature = "fuse")]
2046    #[test]
2047    fn test_current_fs_stats_reports_free_space() {
2048        let stats = current_fs_stats();
2049        assert!(stats.blocks > 0);
2050        assert!(stats.bfree > 0);
2051        assert!(stats.bavail > 0);
2052        assert!(stats.bsize > 0);
2053    }
2054}