@@ -9,11 +9,19 @@ pub fn create_symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) {
9
9
if link. as_ref ( ) . exists ( ) {
10
10
std:: fs:: remove_dir ( link. as_ref ( ) ) . unwrap ( ) ;
11
11
}
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
+ }
17
25
}
18
26
19
27
/// 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>) {
41
49
let ty = entry. file_type ( ) ?;
42
50
if ty. is_dir ( ) {
43
51
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 ( ) ) ) ?;
44
54
} else {
45
55
std:: fs:: copy ( entry. path ( ) , dst. join ( entry. file_name ( ) ) ) ?;
46
56
}
@@ -59,6 +69,12 @@ pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
59
69
}
60
70
}
61
71
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
+
62
78
/// Helper for reading entries in a given directory.
63
79
pub fn read_dir_entries < P : AsRef < Path > , F : FnMut ( & Path ) > ( dir : P , mut callback : F ) {
64
80
for entry in read_dir ( dir) {
@@ -83,28 +99,6 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) {
83
99
) ) ;
84
100
}
85
101
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
-
108
102
/// A wrapper around [`std::fs::File::create`] which includes the file path in the panic message.
109
103
#[ track_caller]
110
104
pub fn create_file < P : AsRef < Path > > ( path : P ) {
0 commit comments