@@ -8,12 +8,14 @@ use db::{connect_db, add_package_into_database, add_build_into_database, add_pat
88use cargo:: core:: Package ;
99use cargo:: util:: CargoResultExt ;
1010use 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} ;
1313use postgres:: Connection ;
1414use rustc_serialize:: json:: { Json , ToJson } ;
1515use 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
1921const 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) ]
471505mod test {
0 commit comments