1
- use std:: fs ;
1
+ use std:: io ;
2
2
use std:: path:: Path ;
3
3
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
+
4
69
/// A wrapper around [`std::fs::remove_file`] which includes the file path in the panic message.
5
70
#[ track_caller]
6
71
pub fn remove_file < P : AsRef < Path > > ( path : P ) {
7
- fs:: remove_file ( path. as_ref ( ) )
72
+ std :: fs:: remove_file ( path. as_ref ( ) )
8
73
. expect ( & format ! ( "the file in path \" {}\" could not be removed" , path. as_ref( ) . display( ) ) ) ;
9
74
}
10
75
11
76
/// A wrapper around [`std::fs::copy`] which includes the file path in the panic message.
12
77
#[ track_caller]
13
78
pub 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 ! (
15
80
"the file \" {}\" could not be copied over to \" {}\" " ,
16
81
from. as_ref( ) . display( ) ,
17
82
to. as_ref( ) . display( ) ,
@@ -21,37 +86,37 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) {
21
86
/// A wrapper around [`std::fs::File::create`] which includes the file path in the panic message.
22
87
#[ track_caller]
23
88
pub fn create_file < P : AsRef < Path > > ( path : P ) {
24
- fs:: File :: create ( path. as_ref ( ) )
89
+ std :: fs:: File :: create ( path. as_ref ( ) )
25
90
. expect ( & format ! ( "the file in path \" {}\" could not be created" , path. as_ref( ) . display( ) ) ) ;
26
91
}
27
92
28
93
/// A wrapper around [`std::fs::read`] which includes the file path in the panic message.
29
94
#[ track_caller]
30
95
pub fn read < P : AsRef < Path > > ( path : P ) -> Vec < u8 > {
31
- fs:: read ( path. as_ref ( ) )
96
+ std :: fs:: read ( path. as_ref ( ) )
32
97
. expect ( & format ! ( "the file in path \" {}\" could not be read" , path. as_ref( ) . display( ) ) )
33
98
}
34
99
35
100
/// A wrapper around [`std::fs::read_to_string`] which includes the file path in the panic message.
36
101
#[ track_caller]
37
102
pub 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 ! (
39
104
"the file in path \" {}\" could not be read into a String" ,
40
105
path. as_ref( ) . display( )
41
106
) )
42
107
}
43
108
44
109
/// A wrapper around [`std::fs::read_dir`] which includes the file path in the panic message.
45
110
#[ 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 ( ) )
48
113
. expect ( & format ! ( "the directory in path \" {}\" could not be read" , path. as_ref( ) . display( ) ) )
49
114
}
50
115
51
116
/// A wrapper around [`std::fs::write`] which includes the file path in the panic message.
52
117
#[ track_caller]
53
118
pub 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 ! (
55
120
"the file in path \" {}\" could not be written to" ,
56
121
path. as_ref( ) . display( )
57
122
) ) ;
@@ -60,7 +125,7 @@ pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) {
60
125
/// A wrapper around [`std::fs::remove_dir_all`] which includes the file path in the panic message.
61
126
#[ track_caller]
62
127
pub 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 ! (
64
129
"the directory in path \" {}\" could not be removed alongside all its contents" ,
65
130
path. as_ref( ) . display( ) ,
66
131
) ) ;
@@ -69,7 +134,7 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) {
69
134
/// A wrapper around [`std::fs::create_dir`] which includes the file path in the panic message.
70
135
#[ track_caller]
71
136
pub 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 ! (
73
138
"the directory in path \" {}\" could not be created" ,
74
139
path. as_ref( ) . display( )
75
140
) ) ;
@@ -78,16 +143,16 @@ pub fn create_dir<P: AsRef<Path>>(path: P) {
78
143
/// A wrapper around [`std::fs::create_dir_all`] which includes the file path in the panic message.
79
144
#[ track_caller]
80
145
pub 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 ! (
82
147
"the directory (and all its parents) in path \" {}\" could not be created" ,
83
148
path. as_ref( ) . display( )
84
149
) ) ;
85
150
}
86
151
87
152
/// A wrapper around [`std::fs::metadata`] which includes the file path in the panic message.
88
153
#[ 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 ! (
91
156
"the file's metadata in path \" {}\" could not be read" ,
92
157
path. as_ref( ) . display( )
93
158
) )
@@ -96,7 +161,7 @@ pub fn metadata<P: AsRef<Path>>(path: P) -> fs::Metadata {
96
161
/// A wrapper around [`std::fs::rename`] which includes the file path in the panic message.
97
162
#[ track_caller]
98
163
pub 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 ! (
100
165
"the file \" {}\" could not be moved over to \" {}\" " ,
101
166
from. as_ref( ) . display( ) ,
102
167
to. as_ref( ) . display( ) ,
@@ -105,8 +170,8 @@ pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) {
105
170
106
171
/// A wrapper around [`std::fs::set_permissions`] which includes the file path in the panic message.
107
172
#[ 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 ! (
110
175
"the file's permissions in path \" {}\" could not be changed" ,
111
176
path. as_ref( ) . display( )
112
177
) ) ;
0 commit comments