@@ -5,18 +5,21 @@ extern crate curl;
55extern crate failure;
66extern crate dirs;
77extern crate flate2;
8+ extern crate fs2;
89extern crate hex;
910extern crate is_executable;
1011extern crate siphasher;
1112extern crate tar;
1213extern crate zip;
1314
1415use failure:: { Error , ResultExt } ;
16+ use fs2:: FileExt ;
1517use siphasher:: sip:: SipHasher13 ;
1618use std:: collections:: HashSet ;
1719use std:: env;
1820use std:: ffi;
1921use std:: fs;
22+ use std:: fs:: File ;
2023use std:: hash:: { Hash , Hasher } ;
2124use std:: io;
2225use std:: path:: { Path , PathBuf } ;
@@ -48,6 +51,9 @@ impl Cache {
4851 Some ( home. join ( & cache_name) )
4952 } )
5053 . ok_or_else ( || format_err ! ( "couldn't find your home directory, is $HOME not set?" ) ) ?;
54+ if !destination. exists ( ) {
55+ fs:: create_dir_all ( & destination) ?;
56+ }
5157 Ok ( Cache :: at ( & destination) )
5258 }
5359
@@ -87,6 +93,9 @@ impl Cache {
8793
8894 let destination = self . destination . join ( & dirname) ;
8995
96+ let flock = File :: create ( self . destination . join ( & format ! ( ".{}.lock" , dirname) ) ) ?;
97+ flock. lock_exclusive ( ) ?;
98+
9099 if destination. exists ( ) {
91100 return Ok ( Some ( Download { root : destination } ) ) ;
92101 }
@@ -119,6 +128,8 @@ impl Cache {
119128 // Now that everything is ready move this over to our destination and
120129 // we're good to go.
121130 fs:: rename ( & temp, & destination) ?;
131+
132+ flock. unlock ( ) ?;
122133 Ok ( Some ( Download { root : destination } ) )
123134 }
124135
0 commit comments