]>
git.proxmox.com Git - rustc.git/blob - src/vendor/mdbook/src/utils/fs.rs
1 use std
::path
::{Path, Component}
;
3 use std
::io
::{self, Read}
;
4 use std
::fs
::{self, File}
;
6 /// Takes a path to a file and try to read the file into a String
8 pub fn file_to_string(path
: &Path
) -> Result
<String
, Box
<Error
>> {
9 let mut file
= match File
::open(path
) {
12 debug
!("[*]: Failed to open {:?}", path
);
13 return Err(Box
::new(e
));
17 let mut content
= String
::new();
19 if let Err(e
) = file
.read_to_string(&mut content
) {
20 debug
!("[*]: Failed to read {:?}", path
);
21 return Err(Box
::new(e
));
27 /// Takes a path and returns a path containing just enough `../` to point to
28 /// the root of the given path.
30 /// This is mostly interesting for a relative path to point back to the
31 /// directory from where the path starts.
34 /// let mut path = Path::new("some/relative/path");
36 /// println!("{}", path_to_root(&path));
45 /// **note:** it's not very fool-proof, if you find a situation where
46 /// it doesn't return the correct path.
47 /// Consider [submitting a new issue](https://github.com/azerupi/mdBook/issues)
48 /// or a [pull-request](https://github.com/azerupi/mdBook/pulls) to improve it.
50 pub fn path_to_root(path
: &Path
) -> String
{
51 debug
!("[fn]: path_to_root");
52 // Remove filename and add "../" for every directory
58 .fold(String
::new(), |mut s
, c
| {
60 Component
::Normal(_
) => s
.push_str("../"),
62 debug
!("[*]: Other path component... {:?}", c
);
71 /// This function creates a file and returns it. But before creating the file
72 /// it checks every directory in the path to see if it exists,
73 /// and if it does not it will be created.
75 pub fn create_file(path
: &Path
) -> io
::Result
<File
> {
76 debug
!("[fn]: create_file");
79 if let Some(p
) = path
.parent() {
80 debug
!("Parent directory is: {:?}", p
);
82 fs
::create_dir_all(p
)?
;
85 debug
!("[*]: Create file: {:?}", path
);
89 /// Removes all the content of a directory but not the directory itself
91 pub fn remove_dir_content(dir
: &Path
) -> Result
<(), Box
<Error
>> {
92 for item
in fs
::read_dir(dir
)?
{
93 if let Ok(item
) = item
{
94 let item
= item
.path();
96 fs
::remove_dir_all(item
)?
;
98 fs
::remove_file(item
)?
;
107 /// Copies all files of a directory to another one except the files
108 /// with the extensions given in the `ext_blacklist` array
110 pub fn copy_files_except_ext(from
: &Path
, to
: &Path
, recursive
: bool
, ext_blacklist
: &[&str])
111 -> Result
<(), Box
<Error
>> {
112 debug
!("[fn] copy_files_except_ext");
113 // Check that from and to are different
118 for entry
in fs
::read_dir(from
)?
{
120 debug
!("[*] {:?}", entry
.path());
121 let metadata
= entry
.metadata()?
;
123 // If the entry is a dir and the recursive option is enabled, call itself
124 if metadata
.is_dir() && recursive
{
125 if entry
.path() == to
.to_path_buf() {
128 debug
!("[*] is dir");
130 // check if output dir already exists
131 if !to
.join(entry
.file_name()).exists() {
132 fs
::create_dir(&to
.join(entry
.file_name()))?
;
135 copy_files_except_ext(&from
.join(entry
.file_name()), &to
.join(entry
.file_name()), true, ext_blacklist
)?
;
136 } else if metadata
.is_file() {
138 // Check if it is in the blacklist
139 if let Some(ext
) = entry
.path().extension() {
140 if ext_blacklist
.contains(&ext
.to_str().unwrap()) {
144 debug
!("[*] creating path for file: {:?}",
148 .expect("a file should have a file name...")));
150 info
!("[*] Copying file: {:?}\n to {:?}",
155 .expect("a file should have a file name...")));
156 fs
::copy(entry
.path(),
160 .expect("a file should have a file name...")))?
;
167 // ------------------------------------------------------------------------------------------------
168 // ------------------------------------------------------------------------------------------------
174 extern crate tempdir
;
176 use super::copy_files_except_ext
;
180 fn copy_files_except_ext_test() {
181 let tmp
= match tempdir
::TempDir
::new("") {
183 Err(_
) => panic
!("Could not create a temp dir"),
186 // Create a couple of files
187 if let Err(_
) = fs
::File
::create(&tmp
.path().join("file.txt")) {
188 panic
!("Could not create file.txt")
190 if let Err(_
) = fs
::File
::create(&tmp
.path().join("file.md")) {
191 panic
!("Could not create file.md")
193 if let Err(_
) = fs
::File
::create(&tmp
.path().join("file.png")) {
194 panic
!("Could not create file.png")
196 if let Err(_
) = fs
::create_dir(&tmp
.path().join("sub_dir")) {
197 panic
!("Could not create sub_dir")
199 if let Err(_
) = fs
::File
::create(&tmp
.path().join("sub_dir/file.png")) {
200 panic
!("Could not create sub_dir/file.png")
202 if let Err(_
) = fs
::create_dir(&tmp
.path().join("sub_dir_exists")) {
203 panic
!("Could not create sub_dir_exists")
205 if let Err(_
) = fs
::File
::create(&tmp
.path().join("sub_dir_exists/file.txt")) {
206 panic
!("Could not create sub_dir_exists/file.txt")
210 if let Err(_
) = fs
::create_dir(&tmp
.path().join("output")) {
211 panic
!("Could not create output")
213 if let Err(_
) = fs
::create_dir(&tmp
.path().join("output/sub_dir_exists")) {
214 panic
!("Could not create output/sub_dir_exists")
217 match copy_files_except_ext(&tmp
.path(), &tmp
.path().join("output"), true, &["md"]) {
218 Err(e
) => panic
!("Error while executing the function:\n{:?}", e
),
222 // Check if the correct files where created
223 if !(&tmp
.path().join("output/file.txt")).exists() {
224 panic
!("output/file.txt should exist")
226 if (&tmp
.path().join("output/file.md")).exists() {
227 panic
!("output/file.md should not exist")
229 if !(&tmp
.path().join("output/file.png")).exists() {
230 panic
!("output/file.png should exist")
232 if !(&tmp
.path().join("output/sub_dir/file.png")).exists() {
233 panic
!("output/sub_dir/file.png should exist")
235 if !(&tmp
.path().join("output/sub_dir_exists/file.txt")).exists() {
236 panic
!("output/sub_dir/file.png should exist")