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
//! The read-only port to one directory on a disk.
//!
//! It is a trait of its own so the rules in `types` stay free of `std::fs`: one implementation reads
//! the real disk and another, in tests, reads memory, and no rule can tell them apart.
//!
//! # Responsibilities
//!
//! - [`FileTree`] is a directory root plus three reads below it: what an entry is, which names a
//! directory holds, and a file's bytes. Every path it takes is relative to the root.
//! - [`FileTree::read_text`] reads a file's bytes as UTF-8 text, so every reader of a text file
//! words "not valid UTF-8" the same way, through the conversion that `types::tree` also offers
//! a reader that already holds a file's bytes.
//!
//! # Non-responsibilities
//!
//! It never writes. [`FileTree::kind`] reports a symlink as the link itself, and an implementation
//! over a real disk refuses to read through one: [`FileTree::read_dir`] and
//! [`FileTree::read_file`] fail when the path, or a directory above it below the root, is a
//! symlink when the read begins. What cannot be guaranteed is a swap made between that check and
//! the read. Applying the dot rule, refusing entries and hashing belong to the logic built on
//! top.
use OsString;
use io;
use Path;
use crateEntryKind;
/// A read-only directory tree rooted at one directory.
///
/// An empty `rel` names the root itself. Failures are the platform's own [`io::Error`], so a
/// caller reports them with the platform's wording.
///
/// # Examples
///
/// ```
/// use std::path::Path;
///
/// use agentstacks::fs::StdFileTree;
/// use agentstacks::port::FileTree;
///
/// let pack = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/packs/valid-hooks");
/// let tree = StdFileTree::new(pack);
/// assert_eq!(tree.root(), Path::new(pack));
///
/// let mut names = tree.read_dir(Path::new(""))?;
/// names.sort();
/// assert_eq!(names, ["hooks", "pack.toml", "skills"]);
/// assert!(tree.read_text(Path::new("pack.toml"))?.starts_with("[pack]"));
/// assert!(!tree.read_file(Path::new("skills/a/SKILL.md"))?.is_empty());
///
/// // A path that could leave the root is refused, and never read.
/// assert!(tree.read_dir(Path::new("..")).is_err());
/// # Ok::<(), std::io::Error>(())
/// ```