11use crate :: core:: shell:: Verbosity ;
2- use crate :: core:: { GitReference , Workspace } ;
2+ use crate :: core:: { GitReference , Package , Workspace } ;
33use crate :: ops;
44use crate :: sources:: path:: PathSource ;
55use crate :: sources:: CRATES_IO_REGISTRY ;
@@ -9,6 +9,7 @@ use cargo_util::{paths, Sha256};
99use serde:: Serialize ;
1010use std:: collections:: HashSet ;
1111use std:: collections:: { BTreeMap , BTreeSet , HashMap } ;
12+ use std:: ffi:: OsStr ;
1213use std:: fs:: { self , File , OpenOptions } ;
1314use std:: io:: { Read , Write } ;
1415use std:: path:: { Path , PathBuf } ;
@@ -225,7 +226,7 @@ fn sync(
225226 let pathsource = PathSource :: new ( src, id. source_id ( ) , config) ;
226227 let paths = pathsource. list_files ( pkg) ?;
227228 let mut map = BTreeMap :: new ( ) ;
228- cp_sources ( src, & paths, & dst, & mut map, & mut tmp_buf)
229+ cp_sources ( pkg , src, & paths, & dst, & mut map, & mut tmp_buf)
229230 . with_context ( || format ! ( "failed to copy over vendored sources for: {}" , id) ) ?;
230231
231232 // Finally, emit the metadata about this package
@@ -315,6 +316,7 @@ fn sync(
315316}
316317
317318fn cp_sources (
319+ pkg : & Package ,
318320 src : & Path ,
319321 paths : & [ PathBuf ] ,
320322 dst : & Path ,
@@ -354,35 +356,54 @@ fn cp_sources(
354356 . fold ( dst. to_owned ( ) , |acc, component| acc. join ( & component) ) ;
355357
356358 paths:: create_dir_all ( dst. parent ( ) . unwrap ( ) ) ?;
359+ let mut dst_opts = OpenOptions :: new ( ) ;
360+ dst_opts. write ( true ) . create ( true ) . truncate ( true ) ;
361+ let cksum = if dst. file_name ( ) == Some ( OsStr :: new ( "Cargo.toml" ) )
362+ && pkg. package_id ( ) . source_id ( ) . is_git ( )
363+ {
364+ let contents = toml:: to_string_pretty ( pkg. manifest ( ) . original ( ) ) ?;
365+ copy_and_checksum (
366+ & dst,
367+ & mut dst_opts,
368+ & mut contents. as_bytes ( ) ,
369+ "Generated Cargo.toml" ,
370+ tmp_buf,
371+ ) ?
372+ } else {
373+ let mut src = File :: open ( & p) . with_context ( || format ! ( "failed to open {:?}" , & p) ) ?;
374+ #[ cfg( unix) ]
375+ {
376+ use std:: os:: unix:: fs:: { MetadataExt , OpenOptionsExt } ;
377+ let src_metadata = src
378+ . metadata ( )
379+ . with_context ( || format ! ( "failed to stat {:?}" , p) ) ?;
380+ dst_opts. mode ( src_metadata. mode ( ) ) ;
381+ }
382+ copy_and_checksum ( & dst, & mut dst_opts, & mut src, p. to_str ( ) . unwrap ( ) , tmp_buf) ?
383+ } ;
357384
358- let cksum = copy_and_checksum ( p, & dst, tmp_buf) ?;
359385 cksums. insert ( relative. to_str ( ) . unwrap ( ) . replace ( "\\ " , "/" ) , cksum) ;
360386 }
361387 Ok ( ( ) )
362388}
363389
364- fn copy_and_checksum ( src_path : & Path , dst_path : & Path , buf : & mut [ u8 ] ) -> CargoResult < String > {
365- let mut src = File :: open ( src_path) . with_context ( || format ! ( "failed to open {:?}" , src_path) ) ?;
366- let mut dst_opts = OpenOptions :: new ( ) ;
367- dst_opts. write ( true ) . create ( true ) . truncate ( true ) ;
368- #[ cfg( unix) ]
369- {
370- use std:: os:: unix:: fs:: { MetadataExt , OpenOptionsExt } ;
371- let src_metadata = src
372- . metadata ( )
373- . with_context ( || format ! ( "failed to stat {:?}" , src_path) ) ?;
374- dst_opts. mode ( src_metadata. mode ( ) ) ;
375- }
390+ fn copy_and_checksum < T : Read > (
391+ dst_path : & Path ,
392+ dst_opts : & mut OpenOptions ,
393+ contents : & mut T ,
394+ contents_path : & str ,
395+ buf : & mut [ u8 ] ,
396+ ) -> CargoResult < String > {
376397 let mut dst = dst_opts
377398 . open ( dst_path)
378399 . with_context ( || format ! ( "failed to create {:?}" , dst_path) ) ?;
379400 // Not going to bother setting mode on pre-existing files, since there
380401 // shouldn't be any under normal conditions.
381402 let mut cksum = Sha256 :: new ( ) ;
382403 loop {
383- let n = src
404+ let n = contents
384405 . read ( buf)
385- . with_context ( || format ! ( "failed to read from {:?}" , src_path ) ) ?;
406+ . with_context ( || format ! ( "failed to read from {:?}" , contents_path ) ) ?;
386407 if n == 0 {
387408 break Ok ( cksum. finish_hex ( ) ) ;
388409 }
0 commit comments