]> git.proxmox.com Git - rustc.git/blob - vendor/mdbook/src/utils/fs.rs
New upstream version 1.51.0+dfsg1
[rustc.git] / vendor / mdbook / src / utils / fs.rs
1 use crate::errors::*;
2 use std::convert::Into;
3 use std::fs::{self, File};
4 use std::io::Write;
5 use std::path::{Component, Path, PathBuf};
6
7 /// Naively replaces any path seperator with a forward-slash '/'
8 pub fn normalize_path(path: &str) -> String {
9 use std::path::is_separator;
10 path.chars()
11 .map(|ch| if is_separator(ch) { '/' } else { ch })
12 .collect::<String>()
13 }
14
15 /// Write the given data to a file, creating it first if necessary
16 pub fn write_file<P: AsRef<Path>>(build_dir: &Path, filename: P, content: &[u8]) -> Result<()> {
17 let path = build_dir.join(filename);
18
19 create_file(&path)?.write_all(content).map_err(Into::into)
20 }
21
22 /// Takes a path and returns a path containing just enough `../` to point to
23 /// the root of the given path.
24 ///
25 /// This is mostly interesting for a relative path to point back to the
26 /// directory from where the path starts.
27 ///
28 /// ```rust
29 /// # use std::path::Path;
30 /// # use mdbook::utils::fs::path_to_root;
31 /// let path = Path::new("some/relative/path");
32 /// assert_eq!(path_to_root(path), "../../");
33 /// ```
34 ///
35 /// **note:** it's not very fool-proof, if you find a situation where
36 /// it doesn't return the correct path.
37 /// Consider [submitting a new issue](https://github.com/rust-lang/mdBook/issues)
38 /// or a [pull-request](https://github.com/rust-lang/mdBook/pulls) to improve it.
39 pub fn path_to_root<P: Into<PathBuf>>(path: P) -> String {
40 debug!("path_to_root");
41 // Remove filename and add "../" for every directory
42
43 path.into()
44 .parent()
45 .expect("")
46 .components()
47 .fold(String::new(), |mut s, c| {
48 match c {
49 Component::Normal(_) => s.push_str("../"),
50 _ => {
51 debug!("Other path component... {:?}", c);
52 }
53 }
54 s
55 })
56 }
57
58 /// This function creates a file and returns it. But before creating the file
59 /// it checks every directory in the path to see if it exists,
60 /// and if it does not it will be created.
61 pub fn create_file(path: &Path) -> Result<File> {
62 debug!("Creating {}", path.display());
63
64 // Construct path
65 if let Some(p) = path.parent() {
66 trace!("Parent directory is: {:?}", p);
67
68 fs::create_dir_all(p)?;
69 }
70
71 File::create(path).map_err(Into::into)
72 }
73
74 /// Removes all the content of a directory but not the directory itself
75 pub fn remove_dir_content(dir: &Path) -> Result<()> {
76 for item in fs::read_dir(dir)? {
77 if let Ok(item) = item {
78 let item = item.path();
79 if item.is_dir() {
80 fs::remove_dir_all(item)?;
81 } else {
82 fs::remove_file(item)?;
83 }
84 }
85 }
86 Ok(())
87 }
88
89 /// Copies all files of a directory to another one except the files
90 /// with the extensions given in the `ext_blacklist` array
91 pub fn copy_files_except_ext(
92 from: &Path,
93 to: &Path,
94 recursive: bool,
95 avoid_dir: Option<&PathBuf>,
96 ext_blacklist: &[&str],
97 ) -> Result<()> {
98 debug!(
99 "Copying all files from {} to {} (blacklist: {:?}), avoiding {:?}",
100 from.display(),
101 to.display(),
102 ext_blacklist,
103 avoid_dir
104 );
105
106 // Check that from and to are different
107 if from == to {
108 return Ok(());
109 }
110
111 for entry in fs::read_dir(from)? {
112 let entry = entry?;
113 let metadata = entry
114 .path()
115 .metadata()
116 .with_context(|| format!("Failed to read {:?}", entry.path()))?;
117
118 // If the entry is a dir and the recursive option is enabled, call itself
119 if metadata.is_dir() && recursive {
120 if entry.path() == to.to_path_buf() {
121 continue;
122 }
123
124 if let Some(avoid) = avoid_dir {
125 if entry.path() == *avoid {
126 continue;
127 }
128 }
129
130 // check if output dir already exists
131 if !to.join(entry.file_name()).exists() {
132 fs::create_dir(&to.join(entry.file_name()))?;
133 }
134
135 copy_files_except_ext(
136 &from.join(entry.file_name()),
137 &to.join(entry.file_name()),
138 true,
139 avoid_dir,
140 ext_blacklist,
141 )?;
142 } else if metadata.is_file() {
143 // Check if it is in the blacklist
144 if let Some(ext) = entry.path().extension() {
145 if ext_blacklist.contains(&ext.to_str().unwrap()) {
146 continue;
147 }
148 }
149 debug!(
150 "creating path for file: {:?}",
151 &to.join(
152 entry
153 .path()
154 .file_name()
155 .expect("a file should have a file name...")
156 )
157 );
158
159 debug!(
160 "Copying {:?} to {:?}",
161 entry.path(),
162 &to.join(
163 entry
164 .path()
165 .file_name()
166 .expect("a file should have a file name...")
167 )
168 );
169 fs::copy(
170 entry.path(),
171 &to.join(
172 entry
173 .path()
174 .file_name()
175 .expect("a file should have a file name..."),
176 ),
177 )?;
178 }
179 }
180 Ok(())
181 }
182
183 pub fn get_404_output_file(input_404: &Option<String>) -> String {
184 input_404
185 .as_ref()
186 .unwrap_or(&"404.md".to_string())
187 .replace(".md", ".html")
188 }
189
190 #[cfg(test)]
191 mod tests {
192 use super::copy_files_except_ext;
193 use std::{fs, io::Result, path::Path};
194
195 #[cfg(target_os = "windows")]
196 fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> Result<()> {
197 std::os::windows::fs::symlink_file(src, dst)
198 }
199
200 #[cfg(not(target_os = "windows"))]
201 fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> Result<()> {
202 std::os::unix::fs::symlink(src, dst)
203 }
204
205 #[test]
206 fn copy_files_except_ext_test() {
207 let tmp = match tempfile::TempDir::new() {
208 Ok(t) => t,
209 Err(e) => panic!("Could not create a temp dir: {}", e),
210 };
211
212 // Create a couple of files
213 if let Err(err) = fs::File::create(&tmp.path().join("file.txt")) {
214 panic!("Could not create file.txt: {}", err);
215 }
216 if let Err(err) = fs::File::create(&tmp.path().join("file.md")) {
217 panic!("Could not create file.md: {}", err);
218 }
219 if let Err(err) = fs::File::create(&tmp.path().join("file.png")) {
220 panic!("Could not create file.png: {}", err);
221 }
222 if let Err(err) = fs::create_dir(&tmp.path().join("sub_dir")) {
223 panic!("Could not create sub_dir: {}", err);
224 }
225 if let Err(err) = fs::File::create(&tmp.path().join("sub_dir/file.png")) {
226 panic!("Could not create sub_dir/file.png: {}", err);
227 }
228 if let Err(err) = fs::create_dir(&tmp.path().join("sub_dir_exists")) {
229 panic!("Could not create sub_dir_exists: {}", err);
230 }
231 if let Err(err) = fs::File::create(&tmp.path().join("sub_dir_exists/file.txt")) {
232 panic!("Could not create sub_dir_exists/file.txt: {}", err);
233 }
234 if let Err(err) = symlink(
235 &tmp.path().join("file.png"),
236 &tmp.path().join("symlink.png"),
237 ) {
238 panic!("Could not symlink file.png: {}", err);
239 }
240
241 // Create output dir
242 if let Err(err) = fs::create_dir(&tmp.path().join("output")) {
243 panic!("Could not create output: {}", err);
244 }
245 if let Err(err) = fs::create_dir(&tmp.path().join("output/sub_dir_exists")) {
246 panic!("Could not create output/sub_dir_exists: {}", err);
247 }
248
249 if let Err(e) =
250 copy_files_except_ext(&tmp.path(), &tmp.path().join("output"), true, None, &["md"])
251 {
252 panic!("Error while executing the function:\n{:?}", e);
253 }
254
255 // Check if the correct files where created
256 if !(&tmp.path().join("output/file.txt")).exists() {
257 panic!("output/file.txt should exist")
258 }
259 if (&tmp.path().join("output/file.md")).exists() {
260 panic!("output/file.md should not exist")
261 }
262 if !(&tmp.path().join("output/file.png")).exists() {
263 panic!("output/file.png should exist")
264 }
265 if !(&tmp.path().join("output/sub_dir/file.png")).exists() {
266 panic!("output/sub_dir/file.png should exist")
267 }
268 if !(&tmp.path().join("output/sub_dir_exists/file.txt")).exists() {
269 panic!("output/sub_dir/file.png should exist")
270 }
271 if !(&tmp.path().join("output/symlink.png")).exists() {
272 panic!("output/symlink.png should exist")
273 }
274 }
275 }