diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /src/librustdoc/docfs.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/librustdoc/docfs.rs')
-rw-r--r-- | src/librustdoc/docfs.rs | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/librustdoc/docfs.rs b/src/librustdoc/docfs.rs new file mode 100644 index 000000000..be066bdaf --- /dev/null +++ b/src/librustdoc/docfs.rs @@ -0,0 +1,78 @@ +//! Rustdoc's FileSystem abstraction module. +//! +//! On Windows this indirects IO into threads to work around performance issues +//! with Defender (and other similar virus scanners that do blocking operations). +//! On other platforms this is a thin shim to fs. +//! +//! Only calls needed to permit this workaround have been abstracted: thus +//! fs::read is still done directly via the fs module; if in future rustdoc +//! needs to read-after-write from a file, then it would be added to this +//! abstraction. + +use std::fs; +use std::io; +use std::path::{Path, PathBuf}; +use std::string::ToString; +use std::sync::mpsc::Sender; + +pub(crate) trait PathError { + fn new<S, P: AsRef<Path>>(e: S, path: P) -> Self + where + S: ToString + Sized; +} + +pub(crate) struct DocFS { + sync_only: bool, + errors: Option<Sender<String>>, +} + +impl DocFS { + pub(crate) fn new(errors: Sender<String>) -> DocFS { + DocFS { sync_only: false, errors: Some(errors) } + } + + pub(crate) fn set_sync_only(&mut self, sync_only: bool) { + self.sync_only = sync_only; + } + + pub(crate) fn close(&mut self) { + self.errors = None; + } + + pub(crate) fn create_dir_all<P: AsRef<Path>>(&self, path: P) -> io::Result<()> { + // For now, dir creation isn't a huge time consideration, do it + // synchronously, which avoids needing ordering between write() actions + // and directory creation. + fs::create_dir_all(path) + } + + pub(crate) fn write<E>( + &self, + path: PathBuf, + contents: impl 'static + Send + AsRef<[u8]>, + ) -> Result<(), E> + where + E: PathError, + { + #[cfg(windows)] + if !self.sync_only { + // A possible future enhancement after more detailed profiling would + // be to create the file sync so errors are reported eagerly. + let sender = self.errors.clone().expect("can't write after closing"); + rayon::spawn(move || { + fs::write(&path, contents).unwrap_or_else(|e| { + sender.send(format!("\"{}\": {}", path.display(), e)).unwrap_or_else(|_| { + panic!("failed to send error on \"{}\"", path.display()) + }) + }); + }); + } else { + fs::write(&path, contents).map_err(|e| E::new(e, path))?; + } + + #[cfg(not(windows))] + fs::write(&path, contents).map_err(|e| E::new(e, path))?; + + Ok(()) + } +} |