1
1
use crate :: core:: shell:: Verbosity ;
2
- use crate :: core:: { GitReference , Workspace } ;
2
+ use crate :: core:: { GitReference , Package , Workspace } ;
3
3
use crate :: ops;
4
4
use crate :: sources:: path:: PathSource ;
5
5
use crate :: sources:: CRATES_IO_REGISTRY ;
@@ -9,6 +9,7 @@ use cargo_util::{paths, Sha256};
9
9
use serde:: Serialize ;
10
10
use std:: collections:: HashSet ;
11
11
use std:: collections:: { BTreeMap , BTreeSet , HashMap } ;
12
+ use std:: ffi:: OsStr ;
12
13
use std:: fs:: { self , File , OpenOptions } ;
13
14
use std:: io:: { Read , Write } ;
14
15
use std:: path:: { Path , PathBuf } ;
@@ -225,7 +226,7 @@ fn sync(
225
226
let pathsource = PathSource :: new ( src, id. source_id ( ) , config) ;
226
227
let paths = pathsource. list_files ( pkg) ?;
227
228
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)
229
230
. with_context ( || format ! ( "failed to copy over vendored sources for: {}" , id) ) ?;
230
231
231
232
// Finally, emit the metadata about this package
@@ -313,6 +314,7 @@ fn sync(
313
314
}
314
315
315
316
fn cp_sources (
317
+ pkg : & Package ,
316
318
src : & Path ,
317
319
paths : & [ PathBuf ] ,
318
320
dst : & Path ,
@@ -352,35 +354,52 @@ fn cp_sources(
352
354
. fold ( dst. to_owned ( ) , |acc, component| acc. join ( & component) ) ;
353
355
354
356
paths:: create_dir_all ( dst. parent ( ) . unwrap ( ) ) ?;
357
+ let mut dst_opts = OpenOptions :: new ( ) ;
358
+ dst_opts. write ( true ) . create ( true ) . truncate ( true ) ;
359
+ let cksum = if dst. file_name ( ) == Some ( OsStr :: new ( "Cargo.toml" ) ) {
360
+ let contents = toml:: to_string_pretty ( pkg. manifest ( ) . original ( ) ) ?;
361
+ copy_and_checksum (
362
+ & dst,
363
+ & mut dst_opts,
364
+ & mut contents. as_bytes ( ) ,
365
+ "Generated Cargo.toml" ,
366
+ tmp_buf,
367
+ ) ?
368
+ } else {
369
+ let mut src = File :: open ( & p) . with_context ( || format ! ( "failed to open {:?}" , & p) ) ?;
370
+ #[ cfg( unix) ]
371
+ {
372
+ use std:: os:: unix:: fs:: { MetadataExt , OpenOptionsExt } ;
373
+ let src_metadata = src
374
+ . metadata ( )
375
+ . with_context ( || format ! ( "failed to stat {:?}" , p) ) ?;
376
+ dst_opts. mode ( src_metadata. mode ( ) ) ;
377
+ }
378
+ copy_and_checksum ( & dst, & mut dst_opts, & mut src, p. to_str ( ) . unwrap ( ) , tmp_buf) ?
379
+ } ;
355
380
356
- let cksum = copy_and_checksum ( p, & dst, tmp_buf) ?;
357
381
cksums. insert ( relative. to_str ( ) . unwrap ( ) . replace ( "\\ " , "/" ) , cksum) ;
358
382
}
359
383
Ok ( ( ) )
360
384
}
361
385
362
- fn copy_and_checksum ( src_path : & Path , dst_path : & Path , buf : & mut [ u8 ] ) -> CargoResult < String > {
363
- let mut src = File :: open ( src_path) . with_context ( || format ! ( "failed to open {:?}" , src_path) ) ?;
364
- let mut dst_opts = OpenOptions :: new ( ) ;
365
- dst_opts. write ( true ) . create ( true ) . truncate ( true ) ;
366
- #[ cfg( unix) ]
367
- {
368
- use std:: os:: unix:: fs:: { MetadataExt , OpenOptionsExt } ;
369
- let src_metadata = src
370
- . metadata ( )
371
- . with_context ( || format ! ( "failed to stat {:?}" , src_path) ) ?;
372
- dst_opts. mode ( src_metadata. mode ( ) ) ;
373
- }
386
+ fn copy_and_checksum < T : Read > (
387
+ dst_path : & Path ,
388
+ dst_opts : & mut OpenOptions ,
389
+ contents : & mut T ,
390
+ contents_path : & str ,
391
+ buf : & mut [ u8 ] ,
392
+ ) -> CargoResult < String > {
374
393
let mut dst = dst_opts
375
394
. open ( dst_path)
376
395
. with_context ( || format ! ( "failed to create {:?}" , dst_path) ) ?;
377
396
// Not going to bother setting mode on pre-existing files, since there
378
397
// shouldn't be any under normal conditions.
379
398
let mut cksum = Sha256 :: new ( ) ;
380
399
loop {
381
- let n = src
400
+ let n = contents
382
401
. read ( buf)
383
- . with_context ( || format ! ( "failed to read from {:?}" , src_path ) ) ?;
402
+ . with_context ( || format ! ( "failed to read from {:?}" , contents_path ) ) ?;
384
403
if n == 0 {
385
404
break Ok ( cksum. finish_hex ( ) ) ;
386
405
}
0 commit comments