@@ -133,6 +133,22 @@ impl<'a> Transaction<'a> {
133
133
Ok ( ( ) )
134
134
}
135
135
136
+ /// Move a file to a relative path of the install prefix.
137
+ pub fn move_file ( & mut self , component : & str , relpath : PathBuf , src : & Path ) -> Result < ( ) > {
138
+ assert ! ( relpath. is_relative( ) ) ;
139
+ let item = ChangedItem :: move_file ( & self . prefix , component, relpath, src) ?;
140
+ self . change ( item) ;
141
+ Ok ( ( ) )
142
+ }
143
+
144
+ /// Recursively move a directory to a relative path of the install prefix.
145
+ pub fn move_dir ( & mut self , component : & str , relpath : PathBuf , src : & Path ) -> Result < ( ) > {
146
+ assert ! ( relpath. is_relative( ) ) ;
147
+ let item = ChangedItem :: move_dir ( & self . prefix , component, relpath, src) ?;
148
+ self . change ( item) ;
149
+ Ok ( ( ) )
150
+ }
151
+
136
152
pub fn temp ( & self ) -> & ' a temp:: Cfg {
137
153
self . temp_cfg
138
154
}
@@ -195,8 +211,8 @@ impl<'a> ChangedItem<'a> {
195
211
}
196
212
Ok ( ( ) )
197
213
}
198
- fn add_file ( prefix : & InstallPrefix , component : & str , relpath : PathBuf ) -> Result < ( Self , File ) > {
199
- let abs_path = prefix. abs_path ( & relpath) ;
214
+ fn dest_abs_path ( prefix : & InstallPrefix , component : & str , relpath : & PathBuf ) -> Result < PathBuf > {
215
+ let abs_path = prefix. abs_path ( relpath) ;
200
216
if utils:: path_exists ( & abs_path) {
201
217
Err ( ErrorKind :: ComponentConflict {
202
218
name : component. to_owned ( ) ,
@@ -207,53 +223,34 @@ impl<'a> ChangedItem<'a> {
207
223
if let Some ( p) = abs_path. parent ( ) {
208
224
utils:: ensure_dir_exists ( "component" , p, & |_| ( ) ) ?;
209
225
}
210
- let file = File :: create ( & abs_path)
211
- . chain_err ( || format ! ( "error creating file '{}'" , abs_path. display( ) ) ) ?;
212
-
213
- Ok ( ( ChangedItem :: AddedFile ( relpath) , file) )
226
+ Ok ( abs_path)
214
227
}
215
228
}
229
+ fn add_file ( prefix : & InstallPrefix , component : & str , relpath : PathBuf ) -> Result < ( Self , File ) > {
230
+ let abs_path = ChangedItem :: dest_abs_path ( prefix, component, & relpath) ?;
231
+ let file = File :: create ( & abs_path)
232
+ . chain_err ( || format ! ( "error creating file '{}'" , abs_path. display( ) ) ) ?;
233
+ Ok ( ( ChangedItem :: AddedFile ( relpath) , file) )
234
+ }
216
235
fn copy_file (
217
236
prefix : & InstallPrefix ,
218
237
component : & str ,
219
238
relpath : PathBuf ,
220
239
src : & Path ,
221
240
) -> Result < Self > {
222
- let abs_path = prefix. abs_path ( & relpath) ;
223
- if utils:: path_exists ( & abs_path) {
224
- Err ( ErrorKind :: ComponentConflict {
225
- name : component. to_owned ( ) ,
226
- path : relpath. clone ( ) ,
227
- }
228
- . into ( ) )
229
- } else {
230
- if let Some ( p) = abs_path. parent ( ) {
231
- utils:: ensure_dir_exists ( "component" , p, & |_| ( ) ) ?;
232
- }
233
- utils:: copy_file ( src, & abs_path) ?;
234
- Ok ( ChangedItem :: AddedFile ( relpath) )
235
- }
241
+ let abs_path = ChangedItem :: dest_abs_path ( prefix, component, & relpath) ?;
242
+ utils:: copy_file ( src, & abs_path) ?;
243
+ Ok ( ChangedItem :: AddedFile ( relpath) )
236
244
}
237
245
fn copy_dir (
238
246
prefix : & InstallPrefix ,
239
247
component : & str ,
240
248
relpath : PathBuf ,
241
249
src : & Path ,
242
250
) -> Result < Self > {
243
- let abs_path = prefix. abs_path ( & relpath) ;
244
- if utils:: path_exists ( & abs_path) {
245
- Err ( ErrorKind :: ComponentConflict {
246
- name : component. to_owned ( ) ,
247
- path : relpath. clone ( ) ,
248
- }
249
- . into ( ) )
250
- } else {
251
- if let Some ( p) = abs_path. parent ( ) {
252
- utils:: ensure_dir_exists ( "component" , p, & |_| ( ) ) ?;
253
- }
254
- utils:: copy_dir ( src, & abs_path, & |_| ( ) ) ?;
255
- Ok ( ChangedItem :: AddedDir ( relpath) )
256
- }
251
+ let abs_path = ChangedItem :: dest_abs_path ( prefix, component, & relpath) ?;
252
+ utils:: copy_dir ( src, & abs_path, & |_| ( ) ) ?;
253
+ Ok ( ChangedItem :: AddedDir ( relpath) )
257
254
}
258
255
fn remove_file (
259
256
prefix : & InstallPrefix ,
@@ -311,4 +308,25 @@ impl<'a> ChangedItem<'a> {
311
308
Ok ( ChangedItem :: ModifiedFile ( relpath, None ) )
312
309
}
313
310
}
311
+ fn move_file (
312
+ prefix : & InstallPrefix ,
313
+ component : & str ,
314
+ relpath : PathBuf ,
315
+ src : & Path ,
316
+ ) -> Result < Self > {
317
+ let abs_path = ChangedItem :: dest_abs_path ( prefix, component, & relpath) ?;
318
+ utils:: rename_file ( "component" , src, & abs_path) ?;
319
+ Ok ( ChangedItem :: AddedFile ( relpath) )
320
+ }
321
+ fn move_dir (
322
+ prefix : & InstallPrefix ,
323
+ component : & str ,
324
+ relpath : PathBuf ,
325
+ src : & Path ,
326
+ ) -> Result < Self > {
327
+ let abs_path = ChangedItem :: dest_abs_path ( prefix, component, & relpath) ?;
328
+ utils:: rename_dir ( "component" , src, & abs_path) ?;
329
+ Ok ( ChangedItem :: AddedDir ( relpath) )
330
+ }
331
+
314
332
}
0 commit comments