Repository navigation
Expand file tree
/
Copy pathfs.rs
More file actions
226 lines (205 loc) · 7.81 KB
/
Copy pathfs.rs
File metadata and controls
226 lines (205 loc) · 7.81 KB
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
//! Facilities for reading sources from the filesystem.
use crate::error::SourceError;
use core::fmt::Write;
use core::ops::Range;
use core::str::Utf8Error;
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::Mutex;
pub trait Loader {
/// Search for an include. `relative_to` specifies one directory to check first.
/// The result is the path found and its contents, or `None` if no matching file was found.
fn find(&self, relative_to: &Path, included_path: &Path) -> Option<(&Path, &[u8])>;
/// Read and cache a file. If successful, the return value is a reference to
/// the path and the data (both owned by `self`). Otherwise `None` is returned.
fn read(&self, path: PathBuf) -> Option<(&Path, &[u8])>;
// TODO: This should return a custom error type holding the path. Use SourceError?
fn find_utf8(
&self,
relative_to: &Path,
included_path: &Path,
) -> Result<Option<(&Path, &str)>, Utf8Error> {
Ok(match self.find(relative_to, included_path) {
Some((path, bytes)) => Some((path, core::str::from_utf8(bytes)?)),
None => None,
})
}
// TODO: This should return a custom error type holding the path. Use SourceError?
fn read_utf8(&self, path: PathBuf) -> Result<Option<(&Path, &str)>, Utf8Error> {
Ok(match self.read(path) {
Some((path, bytes)) => Some((path, core::str::from_utf8(bytes)?)),
None => None,
})
}
/// Files successfully read.
fn positive_deps(&self) -> Vec<PathBuf>;
/// Directories checked for files not found within them.
/// (When any of these change, compilation results could change.)
fn negative_deps(&self) -> Vec<PathBuf>;
/// Produce a representation of the files accessed, in the format of a ninja depfile
/// or the output of `cpp -MD` or `makedepend`.
fn write_depfile(&self, goal: &str) -> String {
let files = self.positive_deps();
let dirs = self.negative_deps();
let mut out = String::new();
let escape = |s: &str| s.replace(' ', "\\ ");
_ = write!(out, "{}:", escape(goal));
for f in files {
// Arguably we should exclude "<stdin>", but dtc does not.
_ = write!(out, " {}", escape(&f.to_string_lossy()));
}
// TODO: Make this optional? Unclear if we want the rigor.
for d in dirs {
_ = write!(out, " {}", escape(&d.to_string_lossy()));
}
_ = writeln!(out);
out
}
/// Given a memory range within a buffer cached by this loader, report the path of the
/// corresponding file.
fn path_of_buffer(&self, mem: Range<*const u8>) -> Option<PathBuf>;
/// Annotate an error with the path of a source file owned by this buffer.
fn annotate_error(&self, err: SourceError) -> SourceError {
if err.path().is_some() {
return err;
}
let path = self
.path_of_buffer(err.buffer())
.unwrap_or("<unknown>".into());
err.with_path(&path)
}
}
/// A do-nothing implementation for tests.
pub struct DummyLoader;
#[allow(unused_variables)]
impl Loader for DummyLoader {
fn find(&self, relative_to: &Path, included_path: &Path) -> Option<(&Path, &[u8])> {
None
}
fn read(&self, path: PathBuf) -> Option<(&Path, &[u8])> {
None
}
fn positive_deps(&self) -> Vec<PathBuf> {
vec![]
}
fn negative_deps(&self) -> Vec<PathBuf> {
vec![]
}
fn path_of_buffer(&self, mem: Range<*const u8>) -> Option<PathBuf> {
None
}
}
/// A stateful helper for loading source files from a list of include directories to be searched.
pub struct LocalFileLoader {
/// directories to search
search_path: Vec<PathBuf>,
/// cache of previously loaded files
file_contents: Mutex<HashMap<PathBuf, Option<Vec<u8>>>>,
/// existing parent directories of files observed not to exist
parents_of_missing: Mutex<HashSet<PathBuf>>,
}
impl LocalFileLoader {
/// Magic pathname which will be loaded by reading from stdin.
pub const STDIN: &str = "<stdin>";
pub fn new(search_path: Vec<PathBuf>) -> Self {
Self {
search_path,
file_contents: Default::default(),
parents_of_missing: Default::default(),
}
}
fn track_parent_of_missing(&self, mut path: &Path) {
let mut parents_of_missing = self.parents_of_missing.lock().unwrap();
while let Some(parent) = path.parent() {
path = parent;
if path.as_os_str().is_empty() {
// final ancestor of a relative path is ""; translate to "."
parents_of_missing.insert(PathBuf::from("."));
}
if parents_of_missing.contains(path) {
return;
}
if path.exists() {
parents_of_missing.insert(path.into());
return;
}
}
}
}
impl Loader for LocalFileLoader {
fn find(&self, relative_to: &Path, included_path: &Path) -> Option<(&Path, &[u8])> {
let search_path = self.search_path.iter().map(PathBuf::as_ref);
for dir in core::iter::once(relative_to).chain(search_path) {
let path = dir.join(included_path);
if let Some(result) = self.read(path) {
return Some(result);
}
}
None
}
fn read<'a>(&'a self, path: PathBuf) -> Option<(&'a Path, &'a [u8])> {
let mut file_contents = self.file_contents.lock().unwrap();
let entry = file_contents.entry(path);
// SAFETY: We never erase items from the map.
// The `entry.key()` PathBuf is either in the map, or will be moved into it.
// PathBuf is "StableDeref": moving it does not move the heap buffer.
// The PathBuf's contents are never modified and thus will never reallocate.
let key: &'a Path = unsafe { core::mem::transmute(entry.key().as_path()) };
match entry.or_insert_with(|| {
let result = if key == Path::new(Self::STDIN) {
use std::io::Read;
let mut bytes = vec![];
std::io::stdin().read_to_end(&mut bytes).map(|_| bytes).ok()
} else {
std::fs::read(key).ok()
};
if result.is_none() {
self.track_parent_of_missing(key);
}
result
}) {
None => None,
Some(value) => {
// SAFETY: We never erase items from the map.
// Hashtable resizes may move the Vec, but Vec is "StableDeref":
// moving it does not move the heap buffer.
// The Vec's contents are never modified and thus will never reallocate.
let value: &'a [u8] = unsafe { core::mem::transmute(value.as_slice()) };
Some((key, value))
}
}
}
fn positive_deps(&self) -> Vec<PathBuf> {
let mut deps: Vec<_> = self
.file_contents
.lock()
.unwrap()
.iter()
.filter(|(_, bytes)| bytes.is_some())
.map(|(path, _)| path.clone())
.collect();
deps.sort_unstable();
deps
}
fn negative_deps(&self) -> Vec<PathBuf> {
let mut dirs: Vec<_> = self
.parents_of_missing
.lock()
.unwrap()
.iter()
.cloned()
.collect();
dirs.sort_unstable();
dirs
}
fn path_of_buffer(&self, mem: Range<*const u8>) -> Option<PathBuf> {
for (path, bytes) in self.file_contents.lock().unwrap().iter() {
if let Some(bytes) = bytes {
if bytes.as_ptr_range() == mem {
return Some(path.clone());
}
}
}
None
}
}