@@ -5,24 +5,56 @@ use std::os::unix::fs::symlink as unix_symlink;
55#[ cfg( windows) ]
66use std:: os:: windows:: fs:: { symlink_dir, symlink_file} ;
77
8+ fn deref ( to_be_dereferenced : String ) -> String {
9+ let mut metadata = fs:: metadata ( & to_be_dereferenced) . unwrap ( ) ;
10+ let file = & to_be_dereferenced;
11+ let mut new_file = file. clone ( ) ;
12+ while metadata. is_symlink ( ) {
13+ new_file = String :: from ( fs:: read_link ( new_file. to_string ( ) ) . unwrap ( ) . into_os_string ( ) . into_string ( ) . unwrap ( ) ) ;
14+ metadata = fs:: metadata ( & new_file) . unwrap ( )
15+ }
16+
17+ new_file
18+ }
19+
820#[ cfg( windows) ]
9- pub fn symlink ( to_be_linked : String , destination : String ) -> std:: io:: Result < ( ) > {
10- let target_metadata = fs:: metadata ( & to_be_linked) ?;
11- if target_metadata. is_dir ( ) {
12- symlink_dir ( & to_be_linked, & destination)
21+ pub fn symlink ( to_be_linked : String , destination : String , dereference : bool ) -> std:: io:: Result < ( ) > {
22+ if !dereference {
23+ let target_metadata = fs:: metadata ( & to_be_linked) ?;
24+ if target_metadata. is_dir ( ) {
25+ symlink_dir ( & to_be_linked, & destination)
26+ } else {
27+ symlink_file ( & to_be_linked, & destination)
28+ } ?;
1329 } else {
14- symlink_file ( & to_be_linked, & destination)
15- } ?;
30+ let dereferenced = deref ( to_be_linked) ;
31+ let deref_metadata = fs:: metadata ( & to_be_linked) ?;
32+ if deref_metadata. is_dir ( ) {
33+ symlink_dir ( & dereferenced, & destination)
34+ } else {
35+ symlink_file ( & dereferenced, & destination)
36+ } ?;
37+ }
1638 Ok ( ( ) )
1739}
1840
1941#[ cfg( unix) ]
20- pub fn symlink ( to_be_linked : String , destination : String ) -> std:: io:: Result < ( ) > {
21- unix_symlink ( to_be_linked, destination) ?;
42+ pub fn symlink ( to_be_linked : String , destination : String , dereference : bool ) -> std:: io:: Result < ( ) > {
43+ if !dereference {
44+ unix_symlink ( to_be_linked, destination) ?;
45+ } else {
46+ let dereferenced = deref ( to_be_linked) ;
47+ unix_symlink ( dereferenced, destination) ?;
48+ }
2249 Ok ( ( ) )
2350}
2451
25- pub fn hard_link ( to_be_linked : String , destination : String ) -> std:: io:: Result < ( ) > {
26- fs:: hard_link ( to_be_linked, destination) ?;
52+ pub fn hard_link ( to_be_linked : String , destination : String , dereference : bool ) -> std:: io:: Result < ( ) > {
53+ if !dereference {
54+ fs:: hard_link ( to_be_linked, destination) ?;
55+ } else {
56+ let dereferenced = deref ( to_be_linked) ;
57+ fs:: hard_link ( dereferenced, destination) ?;
58+ }
2759 Ok ( ( ) )
2860}
0 commit comments