1 //! Rustdoc's FileSystem abstraction module.
3 //! On Windows this indirects IO into threads to work around performance issues
4 //! with Defender (and other similar virus scanners that do blocking operations).
5 //! On other platforms this is a thin shim to fs.
7 //! Only calls needed to permit this workaround have been abstracted: thus
8 //! fs::read is still done directly via the fs module; if in future rustdoc
9 //! needs to read-after-write from a file, then it would be added to this
18 use std::sync::mpsc::{channel, Receiver, Sender};
20 macro_rules! try_err {
21 ($e:expr, $file:expr) => {{
24 Err(e) => return Err(E::new(e, $file)),
30 fn new<P: AsRef<Path>>(e: io::Error, path: P) -> Self;
33 pub struct ErrorStorage {
34 sender: Option<Sender<Option<String>>>,
35 receiver: Receiver<Option<String>>,
39 pub fn new() -> ErrorStorage {
40 let (sender, receiver) = channel();
47 /// Prints all stored errors. Returns the number of printed errors.
48 pub fn write_errors(&mut self, diag: &errors::Handler) -> usize {
50 // In order to drop the sender part of the channel.
53 for msg in self.receiver.iter() {
54 if let Some(ref error) = msg {
55 diag.struct_err(&error).emit();
65 errors: Arc<ErrorStorage>,
69 pub fn new(errors: &Arc<ErrorStorage>) -> DocFS {
72 errors: Arc::clone(errors),
76 pub fn set_sync_only(&mut self, sync_only: bool) {
77 self.sync_only = sync_only;
80 pub fn create_dir_all<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
81 // For now, dir creation isn't a huge time consideration, do it
82 // synchronously, which avoids needing ordering between write() actions
83 // and directory creation.
84 fs::create_dir_all(path)
87 pub fn write<P, C, E>(&self, path: P, contents: C) -> Result<(), E>
93 if !self.sync_only && cfg!(windows) {
94 // A possible future enhancement after more detailed profiling would
95 // be to create the file sync so errors are reported eagerly.
96 let contents = contents.as_ref().to_vec();
97 let path = path.as_ref().to_path_buf();
98 let sender = self.errors.sender.clone().unwrap();
99 rayon::spawn(move || {
100 match fs::write(&path, &contents) {
103 .expect(&format!("failed to send error on \"{}\"", path.display()));
106 sender.send(Some(format!("\"{}\": {}", path.display(), e)))
107 .expect(&format!("failed to send non-error on \"{}\"", path.display()));
113 Ok(try_err!(fs::write(&path, contents), path))