agentvfs 0.1.6

Virtual filesystem CLI backed by embedded databases for AI agents
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//! Filesystem operations.

use std::path::PathBuf;
use std::sync::Arc;

use crate::error::{Result, VfsError};
use crate::fs::entry::{DirEntry, FileEntry, FileType};
use crate::fs::path;
use crate::storage::{VaultBackend};

/// Virtual filesystem operations.
pub struct FileSystem {
    backend: Arc<VaultBackend>,
}

impl FileSystem {
    /// Create a new filesystem with the given storage backend.
    pub fn new(backend: Arc<VaultBackend>) -> Self {
        Self { backend }
    }

    /// Get the storage backend.
    pub fn backend(&self) -> &Arc<VaultBackend> {
        &self.backend
    }

    /// Resolve a path to a file entry.
    pub fn get_entry(&self, vpath: &str) -> Result<FileEntry> {
        let normalized = path::normalize(vpath)?;
        self.backend.get_entry_by_path(&normalized)
    }

    /// List contents of a directory.
    pub fn list_dir(&self, vpath: &str) -> Result<Vec<DirEntry>> {
        let normalized = path::normalize(vpath)?;
        let entry = self.backend.get_entry_by_path(&normalized)?;

        if !entry.is_dir() {
            return Err(VfsError::NotADirectory(PathBuf::from(normalized)));
        }

        let children = self.backend.list_children(&normalized)?;
        Ok(children.iter().map(DirEntry::from).collect())
    }

    /// Read file contents.
    pub fn read_file(&self, vpath: &str) -> Result<Vec<u8>> {
        let normalized = path::normalize(vpath)?;
        let entry = self.backend.get_entry_by_path(&normalized)?;

        if !entry.is_file() {
            return Err(VfsError::NotAFile(PathBuf::from(normalized)));
        }

        let hash = entry
            .content_hash
            .ok_or_else(|| VfsError::Internal("file has no content hash".to_string()))?;

        self.backend.read_content(&hash)
    }

    /// Write file contents.
    ///
    /// Creates the file if it doesn't exist, or updates it if it does.
    /// Automatically creates a version snapshot on every write.
    pub fn write_file(&self, vpath: &str, content: &[u8]) -> Result<()> {
        let normalized = path::normalize(vpath)?;
        let (parent_path, name) = path::split(&normalized)?;
        let parent_path =
            parent_path.ok_or_else(|| VfsError::InvalidPath("cannot write to root".to_string()))?;

        if name.is_empty() {
            return Err(VfsError::InvalidPath("cannot write to root".to_string()));
        }

        // Ensure parent exists and is a directory
        let parent = self.backend.get_entry_by_path(&parent_path)?;
        if !parent.is_dir() {
            return Err(VfsError::NotADirectory(PathBuf::from(&parent_path)));
        }

        // Use backend-specific atomic path when available
        match self.backend.as_ref() {
            VaultBackend::Sqlite(backend) => {
                backend.write_file_atomic(parent.id, &name, content, &normalized)?;
                Ok(())
            }
            #[cfg(feature = "sled-backend")]
            VaultBackend::Sled(backend) => {
                let hash = backend.write_content(content)?;
                let size = content.len() as u64;
                if let Some(file_id) = backend.get_file_id(parent.id, &name)? {
                    let current = backend.get_entry_by_id(file_id)?.ok_or_else(|| {
                        VfsError::Internal(format!("file entry not found: id={file_id}"))
                    })?;
                    if let Some(current_hash) = current.content_hash {
                        backend.create_version(file_id, current_hash, current.size)?;
                    }
                    backend.update_file(file_id, hash, size)?;
                } else {
                    let file_id = backend.create_file(parent.id, &name, hash, size)?;
                    backend.create_version(file_id, hash, size)?;
                }
                // Sled manages its own search index
                Ok(())
            }
            #[cfg(feature = "lmdb-backend")]
            VaultBackend::Lmdb(backend) => {
                let hash = backend.write_content(content)?;
                let size = content.len() as u64;
                if let Some(file_id) = backend.get_file_id(parent.id, &name)? {
                    let current = backend.get_entry_by_id(file_id)?.ok_or_else(|| {
                        VfsError::Internal(format!("file entry not found: id={file_id}"))
                    })?;
                    if let Some(current_hash) = current.content_hash {
                        backend.create_version(file_id, current_hash, current.size)?;
                    }
                    backend.update_file(file_id, hash, size)?;
                } else {
                    let file_id = backend.create_file(parent.id, &name, hash, size)?;
                    backend.create_version(file_id, hash, size)?;
                }
                // LMDB manages its own search index
                Ok(())
            }
        }
    }

    /// Create a directory.
    pub fn create_dir(&self, vpath: &str) -> Result<()> {
        let normalized = path::normalize(vpath)?;
        let (parent_path, name) = path::split(&normalized)?;
        let parent_path =
            parent_path.ok_or_else(|| VfsError::InvalidPath("cannot create root".to_string()))?;

        if name.is_empty() {
            return Err(VfsError::InvalidPath("cannot create root".to_string()));
        }

        // Ensure parent exists and is a directory
        let parent = self.backend.get_entry_by_path(&parent_path)?;
        if !parent.is_dir() {
            return Err(VfsError::NotADirectory(PathBuf::from(&parent_path)));
        }

        // Use backend-specific atomic path when available
        match self.backend.as_ref() {
            VaultBackend::Sqlite(backend) => {
                backend.create_directory_atomic(parent.id, &name, &normalized)?;
                Ok(())
            }
            #[cfg(feature = "sled-backend")]
            VaultBackend::Sled(backend) => {
                if backend.name_exists(parent.id, &name)? {
                    return Err(VfsError::AlreadyExists(PathBuf::from(&normalized)));
                }
                backend.create_directory(parent.id, &name)?;
                Ok(())
            }
            #[cfg(feature = "lmdb-backend")]
            VaultBackend::Lmdb(backend) => {
                if backend.name_exists(parent.id, &name)? {
                    return Err(VfsError::AlreadyExists(PathBuf::from(&normalized)));
                }
                backend.create_directory(parent.id, &name)?;
                Ok(())
            }
        }
    }

    /// Create a directory and all parent directories.
    pub fn create_dir_all(&self, vpath: &str) -> Result<()> {
        let components = path::components(vpath)?;
        let mut current_path = String::new();

        for component in components {
            current_path = if current_path.is_empty() {
                format!("/{}", component)
            } else {
                format!("{}/{}", current_path, component)
            };

            // Try to create, ignore if exists
            match self.create_dir(&current_path) {
                Ok(()) => {}
                Err(VfsError::AlreadyExists(_)) => {}
                Err(e) => return Err(e),
            }
        }

        Ok(())
    }

    /// Remove a file or directory.
    ///
    /// If recursive is true, removes directory contents recursively.
    pub fn remove(&self, vpath: &str, recursive: bool) -> Result<()> {
        let normalized = path::normalize(vpath)?;

        if path::is_root(&normalized) {
            return Err(VfsError::InvalidPath("cannot remove root".to_string()));
        }

        let entry = self.backend.get_entry_by_path(&normalized)?;

        if entry.is_dir() && !recursive {
            // Check if directory has contents
            if self.backend.has_children(entry.id)? {
                return Err(VfsError::NotEmpty(PathBuf::from(&normalized)));
            }
        }

        self.remove_index_subtree(&normalized)?;

        // Use backend-specific atomic path when available
        match self.backend.as_ref() {
            VaultBackend::Sqlite(backend) => {
                backend.delete_entry_atomic(entry.id, &normalized)?;
            }
            #[cfg(feature = "sled-backend")]
            VaultBackend::Sled(backend) => {
                backend.delete_entry(entry.id, recursive)?;
            }
            #[cfg(feature = "lmdb-backend")]
            VaultBackend::Lmdb(backend) => {
                backend.delete_entry(entry.id, recursive)?;
            }
        }

        Ok(())
    }

    /// Copy a file or directory.
    pub fn copy(&self, src: &str, dst: &str) -> Result<()> {
        let src_normalized = path::normalize(src)?;
        let dst_normalized = path::normalize(dst)?;

        let src_entry = self.backend.get_entry_by_path(&src_normalized)?;

        if src_entry.is_file() {
            self.copy_file(&src_entry, &dst_normalized)
        } else {
            self.copy_dir(&src_normalized, &dst_normalized)
        }
    }

    /// Copy a single file.
    fn copy_file(&self, src_entry: &FileEntry, dst_path: &str) -> Result<()> {
        let (parent_path, mut name) = path::split(dst_path)?;
        let mut parent_path =
            parent_path.ok_or_else(|| VfsError::InvalidPath("cannot copy to root".to_string()))?;

        // Check if destination is a directory
        if let Ok(dst_entry) = self.backend.get_entry_by_path(dst_path) {
            if dst_entry.is_dir() {
                // Copy into directory with same name
                parent_path = dst_path.to_string();
                name = src_entry.name.clone();
            } else {
                return Err(VfsError::AlreadyExists(PathBuf::from(dst_path)));
            }
        }

        let parent = self.backend.get_entry_by_path(&parent_path)?;
        if !parent.is_dir() {
            return Err(VfsError::NotADirectory(PathBuf::from(&parent_path)));
        }

        let new_path = path::join(&parent_path, &name)?;

        match self.backend.as_ref() {
            VaultBackend::Sqlite(backend) => {
                backend.copy_file_atomic(src_entry, parent.id, &name, &new_path)?;
            }
            #[cfg(any(feature = "sled-backend", feature = "lmdb-backend"))]
            _ => {
                self.backend
                    .copy_file(src_entry, parent.id, &name, &new_path)?;
            }
        }

        self.backend.sync_file_index(&new_path)?;

        Ok(())
    }

    /// Copy a directory recursively.
    fn copy_dir(&self, src_path: &str, dst_path: &str) -> Result<()> {
        let src_entry = self.backend.get_entry_by_path(src_path)?;
        let (parent_path, mut name) = path::split(dst_path)?;
        let mut parent_path =
            parent_path.ok_or_else(|| VfsError::InvalidPath("cannot copy to root".to_string()))?;

        // Check if destination is a directory
        if let Ok(dst_entry) = self.backend.get_entry_by_path(dst_path) {
            if dst_entry.is_dir() {
                // Copy into directory
                parent_path = dst_path.to_string();
                name = src_entry.name.clone();
            } else {
                return Err(VfsError::AlreadyExists(PathBuf::from(dst_path)));
            }
        }

        let parent = self.backend.get_entry_by_path(&parent_path)?;
        if !parent.is_dir() {
            return Err(VfsError::NotADirectory(PathBuf::from(&parent_path)));
        }

        let new_dir_path = path::join(&parent_path, &name)?;
        if path_is_same_or_descendant(src_path, &new_dir_path) {
            return Err(VfsError::InvalidPath(format!(
                "cannot copy '{}' into itself or its descendant '{}'",
                src_path, new_dir_path
            )));
        }

        // Create new directory
        self.backend
            .create_directory(parent.id, &name, &new_dir_path)?;
        let new_dir = self.backend.get_entry_by_path(&new_dir_path)?;

        // Copy children
        let children = self.backend.list_children(src_path)?;
        for child in children {
            let child_src_path = path::join(src_path, &child.name)?;
            let child_dst_path = path::join(&new_dir_path, &child.name)?;

            if child.is_dir() {
                self.copy_dir(&child_src_path, &child_dst_path)?;
            } else {
                match self.backend.as_ref() {
                    VaultBackend::Sqlite(backend) => {
                        backend.copy_file_atomic(&child, new_dir.id, &child.name, &child_dst_path)?;
                    }
                    #[cfg(any(feature = "sled-backend", feature = "lmdb-backend"))]
                    _ => {
                        self.backend
                            .copy_file(&child, new_dir.id, &child.name, &child_dst_path)?;
                    }
                }
                self.backend.sync_file_index(&child_dst_path)?;
            }
        }

        Ok(())
    }

    /// Move/rename a file or directory.
    pub fn move_entry(&self, src: &str, dst: &str) -> Result<()> {
        let src_normalized = path::normalize(src)?;
        let dst_normalized = path::normalize(dst)?;

        if path::is_root(&src_normalized) {
            return Err(VfsError::InvalidPath("cannot move root".to_string()));
        }

        let src_entry = self.backend.get_entry_by_path(&src_normalized)?;

        let (parent_path, mut name) = path::split(&dst_normalized)?;
        let mut parent_path =
            parent_path.ok_or_else(|| VfsError::InvalidPath("cannot move to root".to_string()))?;

        // Check if destination is a directory (move into it)
        if let Ok(dst_entry) = self.backend.get_entry_by_path(&dst_normalized) {
            if dst_entry.is_dir() {
                parent_path = dst_normalized.clone();
                name = src_entry.name.clone();
            } else {
                return Err(VfsError::AlreadyExists(PathBuf::from(&dst_normalized)));
            }
        }

        let parent = self.backend.get_entry_by_path(&parent_path)?;
        if !parent.is_dir() {
            return Err(VfsError::NotADirectory(PathBuf::from(&parent_path)));
        }

        // Check if destination name already exists
        if self.backend.name_exists(parent.id, &name)? {
            return Err(VfsError::AlreadyExists(PathBuf::from(path::join(
                &parent_path,
                &name,
            )?)));
        }

        let new_path = path::join(&parent_path, &name)?;
        if src_entry.is_dir() && path_is_same_or_descendant(&src_normalized, &new_path) {
            return Err(VfsError::InvalidPath(format!(
                "cannot move '{}' into itself or its descendant '{}'",
                src_normalized, new_path
            )));
        }

        // Use backend-specific atomic path when available
        match self.backend.as_ref() {
            VaultBackend::Sqlite(backend) => {
                backend.move_entry_atomic(
                    src_entry.id,
                    parent.id,
                    &name,
                    &src_normalized,
                    &new_path,
                )?;
            }
            #[cfg(any(feature = "sled-backend", feature = "lmdb-backend"))]
            _ => {
                self.backend
                    .move_entry(src_entry.id, parent.id, &name, &src_normalized, &new_path)?;

                if src_entry.is_dir() {
                    self.backend.rebuild_child_paths(src_entry.id, &new_path)?;
                }
            }
        }

        self.sync_index_subtree(&new_path)?;

        Ok(())
    }

    fn remove_index_subtree(&self, vpath: &str) -> Result<()> {
        let entry = self.backend.get_entry_by_path(vpath)?;
        if entry.is_file() {
            self.backend.remove_from_index(vpath)?;
            return Ok(());
        }

        for child in self.backend.list_children(vpath)? {
            let child_path = path::join(vpath, &child.name)?;
            self.remove_index_subtree(&child_path)?;
        }

        Ok(())
    }

    fn sync_index_subtree(&self, vpath: &str) -> Result<()> {
        let entry = self.backend.get_entry_by_path(vpath)?;
        if entry.is_file() {
            self.backend.sync_file_index(vpath)?;
            return Ok(());
        }

        for child in self.backend.list_children(vpath)? {
            let child_path = path::join(vpath, &child.name)?;
            self.sync_index_subtree(&child_path)?;
        }

        Ok(())
    }

    /// Check if a path exists.
    pub fn exists(&self, vpath: &str) -> Result<bool> {
        match self.get_entry(vpath) {
            Ok(_) => Ok(true),
            Err(VfsError::NotFound(_)) => Ok(false),
            Err(e) => Err(e),
        }
    }

    /// Get a tree structure of a directory.
    pub fn tree(&self, vpath: &str, max_depth: Option<usize>) -> Result<TreeNode> {
        let normalized = path::normalize(vpath)?;
        let entry = self.backend.get_entry_by_path(&normalized)?;

        self.build_tree(&entry, &normalized, 0, max_depth)
    }

    fn build_tree(
        &self,
        entry: &FileEntry,
        vpath: &str,
        depth: usize,
        max_depth: Option<usize>,
    ) -> Result<TreeNode> {
        let children = if entry.is_dir() && max_depth.map_or(true, |m| depth < m) {
            let entries = self.backend.list_children(vpath)?;
            let mut children = Vec::new();
            for child in entries {
                let child_path = path::join(vpath, &child.name)?;
                children.push(self.build_tree(&child, &child_path, depth + 1, max_depth)?);
            }
            children
        } else {
            Vec::new()
        };

        Ok(TreeNode {
            name: entry.name.clone(),
            file_type: entry.file_type,
            size: entry.size,
            children,
        })
    }
}

/// Tree node for directory structure.
#[derive(Debug, Clone)]
pub struct TreeNode {
    pub name: String,
    pub file_type: FileType,
    pub size: u64,
    pub children: Vec<TreeNode>,
}

impl TreeNode {
    /// Format tree as text.
    pub fn format(&self, prefix: &str, is_last: bool, is_root: bool) -> String {
        let mut result = String::new();

        if is_root {
            result.push_str(&self.name);
            if self.name.is_empty() {
                result.push('/');
            }
            result.push('\n');
        } else {
            let connector = if is_last { "└── " } else { "├── " };
            result.push_str(prefix);
            result.push_str(connector);
            result.push_str(&self.name);
            if self.file_type.is_dir() {
                result.push('/');
            }
            result.push('\n');
        }

        let child_prefix = if is_root {
            String::new()
        } else if is_last {
            format!("{}    ", prefix)
        } else {
            format!("{}", prefix)
        };

        for (i, child) in self.children.iter().enumerate() {
            let is_last_child = i == self.children.len() - 1;
            result.push_str(&child.format(&child_prefix, is_last_child, false));
        }

        result
    }
}

fn path_is_same_or_descendant(root: &str, candidate: &str) -> bool {
    candidate == root
        || candidate
            .strip_prefix(root)
            .is_some_and(|suffix| suffix.starts_with('/'))
}

#[cfg(test)]
mod tests {
    use super::FileSystem;
    use crate::storage::{BackendType, VaultBackend};
    use std::sync::Arc;
    use tempfile::tempdir;

    #[test]
    fn binary_rewrite_removes_stale_search_results() {
        let dir = tempdir().unwrap();
        let db_path = dir.path().join("test.avfs");
        let backend = Arc::new(VaultBackend::open(&db_path, BackendType::Sqlite).unwrap());
        let fs = FileSystem::new(backend.clone());

        fs.create_dir("/docs").unwrap();
        fs.write_file("/docs/file.txt", b"hello world").unwrap();
        assert_eq!(backend.search_content("hello", 10).unwrap().len(), 1);

        fs.write_file("/docs/file.txt", &[0xff, 0xfe, 0xfd])
            .unwrap();
        assert!(backend.search_content("hello", 10).unwrap().is_empty());
    }
}