@@ -133,6 +133,22 @@ impl<'a> Transaction<'a> {
133133 Ok ( ( ) )
134134 }
135135
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+
136152 pub fn temp ( & self ) -> & ' a temp:: Cfg {
137153 self . temp_cfg
138154 }
@@ -195,8 +211,12 @@ impl<'a> ChangedItem<'a> {
195211 }
196212 Ok ( ( ) )
197213 }
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 (
215+ prefix : & InstallPrefix ,
216+ component : & str ,
217+ relpath : & PathBuf ,
218+ ) -> Result < PathBuf > {
219+ let abs_path = prefix. abs_path ( relpath) ;
200220 if utils:: path_exists ( & abs_path) {
201221 Err ( ErrorKind :: ComponentConflict {
202222 name : component. to_owned ( ) ,
@@ -207,53 +227,34 @@ impl<'a> ChangedItem<'a> {
207227 if let Some ( p) = abs_path. parent ( ) {
208228 utils:: ensure_dir_exists ( "component" , p, & |_| ( ) ) ?;
209229 }
210- let file = File :: create ( & abs_path)
211- . chain_err ( || format ! ( "error creating file '{}'" , abs_path. display( ) ) ) ?;
212-
213- Ok ( ( ChangedItem :: AddedFile ( relpath) , file) )
230+ Ok ( abs_path)
214231 }
215232 }
233+ fn add_file ( prefix : & InstallPrefix , component : & str , relpath : PathBuf ) -> Result < ( Self , File ) > {
234+ let abs_path = ChangedItem :: dest_abs_path ( prefix, component, & relpath) ?;
235+ let file = File :: create ( & abs_path)
236+ . chain_err ( || format ! ( "error creating file '{}'" , abs_path. display( ) ) ) ?;
237+ Ok ( ( ChangedItem :: AddedFile ( relpath) , file) )
238+ }
216239 fn copy_file (
217240 prefix : & InstallPrefix ,
218241 component : & str ,
219242 relpath : PathBuf ,
220243 src : & Path ,
221244 ) -> 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- }
245+ let abs_path = ChangedItem :: dest_abs_path ( prefix, component, & relpath) ?;
246+ utils:: copy_file ( src, & abs_path) ?;
247+ Ok ( ChangedItem :: AddedFile ( relpath) )
236248 }
237249 fn copy_dir (
238250 prefix : & InstallPrefix ,
239251 component : & str ,
240252 relpath : PathBuf ,
241253 src : & Path ,
242254 ) -> 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- }
255+ let abs_path = ChangedItem :: dest_abs_path ( prefix, component, & relpath) ?;
256+ utils:: copy_dir ( src, & abs_path, & |_| ( ) ) ?;
257+ Ok ( ChangedItem :: AddedDir ( relpath) )
257258 }
258259 fn remove_file (
259260 prefix : & InstallPrefix ,
@@ -311,4 +312,24 @@ impl<'a> ChangedItem<'a> {
311312 Ok ( ChangedItem :: ModifiedFile ( relpath, None ) )
312313 }
313314 }
315+ fn move_file (
316+ prefix : & InstallPrefix ,
317+ component : & str ,
318+ relpath : PathBuf ,
319+ src : & Path ,
320+ ) -> Result < Self > {
321+ let abs_path = ChangedItem :: dest_abs_path ( prefix, component, & relpath) ?;
322+ utils:: rename_file ( "component" , src, & abs_path) ?;
323+ Ok ( ChangedItem :: AddedFile ( relpath) )
324+ }
325+ fn move_dir (
326+ prefix : & InstallPrefix ,
327+ component : & str ,
328+ relpath : PathBuf ,
329+ src : & Path ,
330+ ) -> Result < Self > {
331+ let abs_path = ChangedItem :: dest_abs_path ( prefix, component, & relpath) ?;
332+ utils:: rename_dir ( "component" , src, & abs_path) ?;
333+ Ok ( ChangedItem :: AddedDir ( relpath) )
334+ }
314335}
0 commit comments