1- use std:: fs ;
1+ use std:: io ;
22use std:: path:: Path ;
33
4+ // FIXME(jieyouxu): modify create_symlink to panic on windows.
5+
6+ /// Creates a new symlink to a path on the filesystem, adjusting for Windows or Unix.
7+ #[ cfg( target_family = "windows" ) ]
8+ pub fn create_symlink < P : AsRef < Path > , Q : AsRef < Path > > ( original : P , link : Q ) {
9+ if link. as_ref ( ) . exists ( ) {
10+ std:: fs:: remove_dir ( link. as_ref ( ) ) . unwrap ( ) ;
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+ ) ) ;
17+ }
18+
19+ /// Creates a new symlink to a path on the filesystem, adjusting for Windows or Unix.
20+ #[ cfg( target_family = "unix" ) ]
21+ pub fn create_symlink < P : AsRef < Path > , Q : AsRef < Path > > ( original : P , link : Q ) {
22+ if link. as_ref ( ) . exists ( ) {
23+ std:: fs:: remove_dir ( link. as_ref ( ) ) . unwrap ( ) ;
24+ }
25+ std:: os:: unix:: fs:: symlink ( original. as_ref ( ) , link. as_ref ( ) ) . expect ( & format ! (
26+ "failed to create symlink {:?} for {:?}" ,
27+ link. as_ref( ) . display( ) ,
28+ original. as_ref( ) . display( ) ,
29+ ) ) ;
30+ }
31+
32+ /// Copy a directory into another.
33+ pub fn copy_dir_all ( src : impl AsRef < Path > , dst : impl AsRef < Path > ) {
34+ fn copy_dir_all_inner ( src : impl AsRef < Path > , dst : impl AsRef < Path > ) -> io:: Result < ( ) > {
35+ let dst = dst. as_ref ( ) ;
36+ if !dst. is_dir ( ) {
37+ std:: fs:: create_dir_all ( & dst) ?;
38+ }
39+ for entry in std:: fs:: read_dir ( src) ? {
40+ let entry = entry?;
41+ let ty = entry. file_type ( ) ?;
42+ if ty. is_dir ( ) {
43+ copy_dir_all_inner ( entry. path ( ) , dst. join ( entry. file_name ( ) ) ) ?;
44+ } else {
45+ std:: fs:: copy ( entry. path ( ) , dst. join ( entry. file_name ( ) ) ) ?;
46+ }
47+ }
48+ Ok ( ( ) )
49+ }
50+
51+ if let Err ( e) = copy_dir_all_inner ( & src, & dst) {
52+ // Trying to give more context about what exactly caused the failure
53+ panic ! (
54+ "failed to copy `{}` to `{}`: {:?}" ,
55+ src. as_ref( ) . display( ) ,
56+ dst. as_ref( ) . display( ) ,
57+ e
58+ ) ;
59+ }
60+ }
61+
62+ /// Helper for reading entries in a given directory.
63+ pub fn read_dir_entries < P : AsRef < Path > , F : FnMut ( & Path ) > ( dir : P , mut callback : F ) {
64+ for entry in read_dir ( dir) {
65+ callback ( & entry. unwrap ( ) . path ( ) ) ;
66+ }
67+ }
68+
469/// A wrapper around [`std::fs::remove_file`] which includes the file path in the panic message.
570#[ track_caller]
671pub fn remove_file < P : AsRef < Path > > ( path : P ) {
7- fs:: remove_file ( path. as_ref ( ) )
72+ std :: fs:: remove_file ( path. as_ref ( ) )
873 . expect ( & format ! ( "the file in path \" {}\" could not be removed" , path. as_ref( ) . display( ) ) ) ;
974}
1075
1176/// A wrapper around [`std::fs::copy`] which includes the file path in the panic message.
1277#[ track_caller]
1378pub fn copy < P : AsRef < Path > , Q : AsRef < Path > > ( from : P , to : Q ) {
14- fs:: copy ( from. as_ref ( ) , to. as_ref ( ) ) . expect ( & format ! (
79+ std :: fs:: copy ( from. as_ref ( ) , to. as_ref ( ) ) . expect ( & format ! (
1580 "the file \" {}\" could not be copied over to \" {}\" " ,
1681 from. as_ref( ) . display( ) ,
1782 to. as_ref( ) . display( ) ,
@@ -21,37 +86,37 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) {
2186/// A wrapper around [`std::fs::File::create`] which includes the file path in the panic message.
2287#[ track_caller]
2388pub fn create_file < P : AsRef < Path > > ( path : P ) {
24- fs:: File :: create ( path. as_ref ( ) )
89+ std :: fs:: File :: create ( path. as_ref ( ) )
2590 . expect ( & format ! ( "the file in path \" {}\" could not be created" , path. as_ref( ) . display( ) ) ) ;
2691}
2792
2893/// A wrapper around [`std::fs::read`] which includes the file path in the panic message.
2994#[ track_caller]
3095pub fn read < P : AsRef < Path > > ( path : P ) -> Vec < u8 > {
31- fs:: read ( path. as_ref ( ) )
96+ std :: fs:: read ( path. as_ref ( ) )
3297 . expect ( & format ! ( "the file in path \" {}\" could not be read" , path. as_ref( ) . display( ) ) )
3398}
3499
35100/// A wrapper around [`std::fs::read_to_string`] which includes the file path in the panic message.
36101#[ track_caller]
37102pub fn read_to_string < P : AsRef < Path > > ( path : P ) -> String {
38- fs:: read_to_string ( path. as_ref ( ) ) . expect ( & format ! (
103+ std :: fs:: read_to_string ( path. as_ref ( ) ) . expect ( & format ! (
39104 "the file in path \" {}\" could not be read into a String" ,
40105 path. as_ref( ) . display( )
41106 ) )
42107}
43108
44109/// A wrapper around [`std::fs::read_dir`] which includes the file path in the panic message.
45110#[ track_caller]
46- pub fn read_dir < P : AsRef < Path > > ( path : P ) -> fs:: ReadDir {
47- fs:: read_dir ( path. as_ref ( ) )
111+ pub fn read_dir < P : AsRef < Path > > ( path : P ) -> std :: fs:: ReadDir {
112+ std :: fs:: read_dir ( path. as_ref ( ) )
48113 . expect ( & format ! ( "the directory in path \" {}\" could not be read" , path. as_ref( ) . display( ) ) )
49114}
50115
51116/// A wrapper around [`std::fs::write`] which includes the file path in the panic message.
52117#[ track_caller]
53118pub fn write < P : AsRef < Path > , C : AsRef < [ u8 ] > > ( path : P , contents : C ) {
54- fs:: write ( path. as_ref ( ) , contents. as_ref ( ) ) . expect ( & format ! (
119+ std :: fs:: write ( path. as_ref ( ) , contents. as_ref ( ) ) . expect ( & format ! (
55120 "the file in path \" {}\" could not be written to" ,
56121 path. as_ref( ) . display( )
57122 ) ) ;
@@ -60,7 +125,7 @@ pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) {
60125/// A wrapper around [`std::fs::remove_dir_all`] which includes the file path in the panic message.
61126#[ track_caller]
62127pub fn remove_dir_all < P : AsRef < Path > > ( path : P ) {
63- fs:: remove_dir_all ( path. as_ref ( ) ) . expect ( & format ! (
128+ std :: fs:: remove_dir_all ( path. as_ref ( ) ) . expect ( & format ! (
64129 "the directory in path \" {}\" could not be removed alongside all its contents" ,
65130 path. as_ref( ) . display( ) ,
66131 ) ) ;
@@ -69,7 +134,7 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) {
69134/// A wrapper around [`std::fs::create_dir`] which includes the file path in the panic message.
70135#[ track_caller]
71136pub fn create_dir < P : AsRef < Path > > ( path : P ) {
72- fs:: create_dir ( path. as_ref ( ) ) . expect ( & format ! (
137+ std :: fs:: create_dir ( path. as_ref ( ) ) . expect ( & format ! (
73138 "the directory in path \" {}\" could not be created" ,
74139 path. as_ref( ) . display( )
75140 ) ) ;
@@ -78,16 +143,16 @@ pub fn create_dir<P: AsRef<Path>>(path: P) {
78143/// A wrapper around [`std::fs::create_dir_all`] which includes the file path in the panic message.
79144#[ track_caller]
80145pub fn create_dir_all < P : AsRef < Path > > ( path : P ) {
81- fs:: create_dir_all ( path. as_ref ( ) ) . expect ( & format ! (
146+ std :: fs:: create_dir_all ( path. as_ref ( ) ) . expect ( & format ! (
82147 "the directory (and all its parents) in path \" {}\" could not be created" ,
83148 path. as_ref( ) . display( )
84149 ) ) ;
85150}
86151
87152/// A wrapper around [`std::fs::metadata`] which includes the file path in the panic message.
88153#[ track_caller]
89- pub fn metadata < P : AsRef < Path > > ( path : P ) -> fs:: Metadata {
90- fs:: metadata ( path. as_ref ( ) ) . expect ( & format ! (
154+ pub fn metadata < P : AsRef < Path > > ( path : P ) -> std :: fs:: Metadata {
155+ std :: fs:: metadata ( path. as_ref ( ) ) . expect ( & format ! (
91156 "the file's metadata in path \" {}\" could not be read" ,
92157 path. as_ref( ) . display( )
93158 ) )
@@ -96,7 +161,7 @@ pub fn metadata<P: AsRef<Path>>(path: P) -> fs::Metadata {
96161/// A wrapper around [`std::fs::rename`] which includes the file path in the panic message.
97162#[ track_caller]
98163pub fn rename < P : AsRef < Path > , Q : AsRef < Path > > ( from : P , to : Q ) {
99- fs:: rename ( from. as_ref ( ) , to. as_ref ( ) ) . expect ( & format ! (
164+ std :: fs:: rename ( from. as_ref ( ) , to. as_ref ( ) ) . expect ( & format ! (
100165 "the file \" {}\" could not be moved over to \" {}\" " ,
101166 from. as_ref( ) . display( ) ,
102167 to. as_ref( ) . display( ) ,
@@ -105,8 +170,8 @@ pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) {
105170
106171/// A wrapper around [`std::fs::set_permissions`] which includes the file path in the panic message.
107172#[ track_caller]
108- pub fn set_permissions < P : AsRef < Path > > ( path : P , perm : fs:: Permissions ) {
109- fs:: set_permissions ( path. as_ref ( ) , perm) . expect ( & format ! (
173+ pub fn set_permissions < P : AsRef < Path > > ( path : P , perm : std :: fs:: Permissions ) {
174+ std :: fs:: set_permissions ( path. as_ref ( ) , perm) . expect ( & format ! (
110175 "the file's permissions in path \" {}\" could not be changed" ,
111176 path. as_ref( ) . display( )
112177 ) ) ;
0 commit comments