@@ -9,11 +9,19 @@ pub fn create_symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) {
99 if link. as_ref ( ) . exists ( ) {
1010 std:: fs:: remove_dir ( link. as_ref ( ) ) . unwrap ( ) ;
1111 }
12- std:: os:: windows:: fs:: symlink_file ( original. as_ref ( ) , link. as_ref ( ) ) . expect ( & format ! (
13- "failed to create symlink {:?} for {:?}" ,
14- link. as_ref( ) . display( ) ,
15- original. as_ref( ) . display( ) ,
16- ) ) ;
12+ if original. as_ref ( ) . is_file ( ) {
13+ std:: os:: windows:: fs:: symlink_file ( original. as_ref ( ) , link. as_ref ( ) ) . expect ( & format ! (
14+ "failed to create symlink {:?} for {:?}" ,
15+ link. as_ref( ) . display( ) ,
16+ original. as_ref( ) . display( ) ,
17+ ) ) ;
18+ } else {
19+ std:: os:: windows:: fs:: symlink_dir ( original. as_ref ( ) , link. as_ref ( ) ) . expect ( & format ! (
20+ "failed to create symlink {:?} for {:?}" ,
21+ link. as_ref( ) . display( ) ,
22+ original. as_ref( ) . display( ) ,
23+ ) ) ;
24+ }
1725}
1826
1927/// Creates a new symlink to a path on the filesystem, adjusting for Windows or Unix.
@@ -41,6 +49,8 @@ pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
4149 let ty = entry. file_type ( ) ?;
4250 if ty. is_dir ( ) {
4351 copy_dir_all_inner ( entry. path ( ) , dst. join ( entry. file_name ( ) ) ) ?;
52+ } else if ty. is_symlink ( ) {
53+ copy_symlink ( entry. path ( ) , dst. join ( entry. file_name ( ) ) ) ?;
4454 } else {
4555 std:: fs:: copy ( entry. path ( ) , dst. join ( entry. file_name ( ) ) ) ?;
4656 }
@@ -59,6 +69,12 @@ pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
5969 }
6070}
6171
72+ fn copy_symlink < P : AsRef < Path > , Q : AsRef < Path > > ( from : P , to : Q ) -> io:: Result < ( ) > {
73+ let target_path = std:: fs:: read_link ( from) . unwrap ( ) ;
74+ create_symlink ( target_path, to) ;
75+ Ok ( ( ) )
76+ }
77+
6278/// Helper for reading entries in a given directory.
6379pub fn read_dir_entries < P : AsRef < Path > , F : FnMut ( & Path ) > ( dir : P , mut callback : F ) {
6480 for entry in read_dir ( dir) {
@@ -83,28 +99,6 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) {
8399 ) ) ;
84100}
85101
86- #[ track_caller]
87- /// An extension of [`std::fs::copy`] which can copy a directory recursively.
88- pub fn copy_dir_all < P : AsRef < Path > , Q : AsRef < Path > > ( from : P , to : Q ) {
89- create_dir_all ( & to) ;
90- for entry in read_dir ( from) {
91- let entry = entry. unwrap ( ) ;
92- let ty = entry. file_type ( ) . unwrap ( ) ;
93- if ty. is_dir ( ) {
94- copy_dir_all ( entry. path ( ) , to. as_ref ( ) . join ( entry. file_name ( ) ) ) ;
95- } else if ty. is_symlink ( ) {
96- copy_symlink ( entry. path ( ) , to. as_ref ( ) . join ( entry. file_name ( ) ) ) ;
97- } else {
98- copy ( entry. path ( ) , to. as_ref ( ) . join ( entry. file_name ( ) ) ) ;
99- }
100- }
101- }
102-
103- fn copy_symlink < P : AsRef < Path > , Q : AsRef < Path > > ( from : P , to : Q ) {
104- let target_path = fs:: read_link ( from) . unwrap ( ) ;
105- std:: os:: unix:: fs:: symlink ( target_path, to) . unwrap ( ) ;
106- }
107-
108102/// A wrapper around [`std::fs::File::create`] which includes the file path in the panic message.
109103#[ track_caller]
110104pub fn create_file < P : AsRef < Path > > ( path : P ) {
0 commit comments