@@ -8,12 +8,14 @@ use db::{connect_db, add_package_into_database, add_build_into_database, add_pat
8
8
use cargo:: core:: Package ;
9
9
use cargo:: util:: CargoResultExt ;
10
10
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} ;
13
13
use postgres:: Connection ;
14
14
use rustc_serialize:: json:: { Json , ToJson } ;
15
15
use error:: Result ;
16
+ use systemstat:: { Platform , Filesystem , System } ;
16
17
18
+ const MAX_DISK_USAGE : f32 = 80.0 ;
17
19
18
20
/// List of targets supported by docs.rs
19
21
const TARGETS : [ & ' static str ; 6 ] = [
@@ -237,6 +239,20 @@ impl DocBuilder {
237
239
. join ( "doc" ) ;
238
240
let _ = remove_dir_all ( crate_doc_path) ;
239
241
}
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
+
240
256
Ok ( ( ) )
241
257
}
242
258
@@ -466,6 +482,24 @@ fn crates<F>(path: PathBuf, mut func: F) -> Result<()>
466
482
crates_from_path ( & path, & mut func)
467
483
}
468
484
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
+
469
503
470
504
#[ cfg( test) ]
471
505
mod test {
0 commit comments