Skip to content

Commit a313440

Browse files
authored
Merge pull request #390 from Mark-Simulacrum/clear-target-dir
Clear target dir
2 parents 48eed8a + 442b5c5 commit a313440

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

Cargo.lock

+31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ rusoto_core = "0.40"
3636
rusoto_credential = "0.40"
3737
futures = "0.1"
3838
tokio = "0.1"
39+
systemstat = "0.1.4"
3940

4041
# iron dependencies
4142
iron = "0.5"

src/db/file.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub fn get_path(conn: &Connection, path: &str) -> Option<Blob> {
7272

7373
let res = match res {
7474
Ok(r) => r,
75-
Err(err) => {
75+
Err(_) => {
7676
return None;
7777
}
7878
};

src/docbuilder/chroot_builder.rs

+36-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ use db::{connect_db, add_package_into_database, add_build_into_database, add_pat
88
use cargo::core::Package;
99
use cargo::util::CargoResultExt;
1010
use std::process::Command;
11-
use std::path::PathBuf;
12-
use std::fs::remove_dir_all;
11+
use std::path::{Path, PathBuf};
12+
use std::fs::{self, remove_dir_all};
1313
use postgres::Connection;
1414
use rustc_serialize::json::{Json, ToJson};
1515
use error::Result;
16+
use systemstat::{Platform, Filesystem, System};
1617

18+
const MAX_DISK_USAGE: f32 = 80.0;
1719

1820
/// List of targets supported by docs.rs
1921
const TARGETS: [&'static str; 6] = [
@@ -237,6 +239,20 @@ impl DocBuilder {
237239
.join("doc");
238240
let _ = remove_dir_all(crate_doc_path);
239241
}
242+
243+
let crate_build_path = PathBuf::from(&self.options.chroot_path)
244+
.join("home")
245+
.join(&self.options.chroot_user)
246+
.join("cratesfyi");
247+
let fs = mount_for_path(&crate_build_path);
248+
let disk_usage = 100.0 - 100.0 * (fs.free.as_usize() as f32 / fs.total.as_usize() as f32);
249+
if disk_usage >= MAX_DISK_USAGE {
250+
info!("Cleaning target directory, disk usage {:.2} exceeded {:.2}",
251+
disk_usage, MAX_DISK_USAGE);
252+
let _ = remove_dir_all(&crate_build_path);
253+
let _ = fs::create_dir_all(&crate_build_path);
254+
}
255+
240256
Ok(())
241257
}
242258

@@ -466,6 +482,24 @@ fn crates<F>(path: PathBuf, mut func: F) -> Result<()>
466482
crates_from_path(&path, &mut func)
467483
}
468484

485+
fn mount_for_path(path: &Path) -> Filesystem {
486+
let system = System::new();
487+
488+
let mut found = None;
489+
let mut found_pos = std::usize::MAX;
490+
for mount in system.mounts().expect("get all mounts").into_iter() {
491+
let mount_path = Path::new(&mount.fs_mounted_on);
492+
for (i, ancestor) in path.ancestors().enumerate() {
493+
if ancestor == mount_path && i < found_pos {
494+
found_pos = i;
495+
found = Some(mount);
496+
break;
497+
}
498+
}
499+
}
500+
found.expect("on a disk mount")
501+
}
502+
469503

470504
#[cfg(test)]
471505
mod test {

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ extern crate rusoto_core;
3535
extern crate rusoto_credential;
3636
extern crate futures;
3737
extern crate tokio;
38+
extern crate systemstat;
3839

3940
pub use self::docbuilder::DocBuilder;
4041
pub use self::docbuilder::ChrootBuilderResult;

0 commit comments

Comments
 (0)