|
1 | | -use crate::{docbuilder::RustwideBuilder, utils::report_error, BuildQueue}; |
| 1 | +use crate::{docbuilder::RustwideBuilder, utils::report_error, BuildQueue, Config}; |
2 | 2 | use anyhow::{Context, Error}; |
3 | 3 | use std::panic::{catch_unwind, AssertUnwindSafe}; |
4 | 4 | use std::sync::Arc; |
5 | 5 | use std::time::Duration; |
6 | | -use std::{fs, io, thread}; |
| 6 | +use std::{fs, io, path::Path, thread}; |
7 | 7 | use tracing::{debug, error, warn}; |
8 | 8 |
|
9 | | -pub(crate) const TEMPDIR_PREFIX: &str = "docsrs-docs"; |
10 | | - |
11 | 9 | pub fn queue_builder( |
12 | 10 | mut builder: RustwideBuilder, |
13 | 11 | build_queue: Arc<BuildQueue>, |
| 12 | + config: Arc<Config>, |
14 | 13 | ) -> Result<(), Error> { |
15 | 14 | loop { |
16 | | - if let Err(e) = remove_tempdirs() { |
17 | | - report_error(&anyhow::anyhow!(e).context("failed to remove temporary directories")); |
| 15 | + if let Err(e) = remove_tempdirs(&config.temp_dir) { |
| 16 | + report_error(&anyhow::anyhow!(e).context(format!( |
| 17 | + "failed to clean temporary directory {:?}", |
| 18 | + &config.temp_dir |
| 19 | + ))); |
18 | 20 | } |
19 | 21 |
|
20 | 22 | // check lock file |
@@ -58,57 +60,8 @@ pub fn queue_builder( |
58 | 60 | /// Sometimes, when the server hits a hard crash or a build thread panics, |
59 | 61 | /// rustwide_builder won't actually remove the temporary directories it creates. |
60 | 62 | /// Remove them now to avoid running out of disk space. |
61 | | -fn remove_tempdirs() -> Result<(), io::Error> { |
62 | | - // NOTE: hardcodes that `tempfile::tempdir()` uses `std::env::temp_dir`. |
63 | | - for entry in std::fs::read_dir(std::env::temp_dir())? { |
64 | | - let entry = entry?; |
65 | | - if !entry.metadata()?.is_dir() { |
66 | | - continue; |
67 | | - } |
68 | | - |
69 | | - if let Some(dir_name) = entry.path().file_name() { |
70 | | - if dir_name.to_string_lossy().starts_with(TEMPDIR_PREFIX) { |
71 | | - fs::remove_dir_all(entry.path())?; |
72 | | - } |
73 | | - } |
74 | | - } |
75 | | - |
| 63 | +fn remove_tempdirs<P: AsRef<Path>>(path: P) -> Result<(), io::Error> { |
| 64 | + fs::remove_dir_all(&path)?; |
| 65 | + fs::create_dir_all(&path)?; |
76 | 66 | Ok(()) |
77 | 67 | } |
78 | | - |
79 | | -#[cfg(test)] |
80 | | -mod tests { |
81 | | - use super::*; |
82 | | - |
83 | | - #[test] |
84 | | - fn remove_existing_tempdirs() { |
85 | | - let file_with_prefix = tempfile::Builder::new() |
86 | | - .prefix(TEMPDIR_PREFIX) |
87 | | - .tempfile() |
88 | | - .unwrap(); |
89 | | - |
90 | | - let dir_with_prefix = tempfile::Builder::new() |
91 | | - .prefix(TEMPDIR_PREFIX) |
92 | | - .tempdir() |
93 | | - .unwrap(); |
94 | | - |
95 | | - let file_inside = dir_with_prefix.path().join("some_file_name"); |
96 | | - fs::File::create(&file_inside).unwrap(); |
97 | | - |
98 | | - let other_file = tempfile::Builder::new().tempfile().unwrap(); |
99 | | - |
100 | | - let other_dir = tempfile::Builder::new().tempdir().unwrap(); |
101 | | - |
102 | | - assert!(dir_with_prefix.path().exists()); |
103 | | - |
104 | | - remove_tempdirs().unwrap(); |
105 | | - |
106 | | - assert!(!dir_with_prefix.path().exists()); |
107 | | - assert!(!file_inside.exists()); |
108 | | - |
109 | | - // all these still exist |
110 | | - assert!(file_with_prefix.path().exists()); |
111 | | - assert!(other_file.path().exists()); |
112 | | - assert!(other_dir.path().exists()); |
113 | | - } |
114 | | -} |
0 commit comments