@@ -196,6 +196,7 @@ mod file {
196
196
use gix:: filter:: plumbing:: driver:: apply:: Delay ;
197
197
use gix:: object:: tree:: EntryKind ;
198
198
use gix:: prelude:: ObjectIdExt ;
199
+ use gix:: tempfile:: create_dir:: Retries ;
199
200
use std:: path:: { Path , PathBuf } ;
200
201
201
202
pub enum RestoreMode {
@@ -240,29 +241,29 @@ mod file {
240
241
let file_path = path_check. verified_path_allow_nonexisting ( rela_path) ?;
241
242
match state. kind {
242
243
EntryKind :: Blob | EntryKind :: BlobExecutable => {
243
- let mut dest_lock_file = locked_resource_at ( wt_root, & file_path , state. kind ) ?;
244
+ let mut tempfile = tempfile_in_root_with_permissions_at ( wt_root, state. kind ) ?;
244
245
let obj_in_git = state. id . attach ( repo) . object ( ) ?;
245
246
let mut stream =
246
247
pipeline. convert_to_worktree ( & obj_in_git. data , rela_path, Delay :: Forbid ) ?;
247
- std:: io:: copy ( & mut stream, & mut dest_lock_file) ?;
248
-
249
- let ( file_path, maybe_file) = match dest_lock_file. commit ( ) {
248
+ std:: io:: copy ( & mut stream, & mut tempfile) ?;
249
+ gix:: tempfile:: create_dir:: all (
250
+ file_path. parent ( ) . context ( "encountered strange filepath" ) ?,
251
+ Retries :: default ( ) ,
252
+ ) ?;
253
+ let file = match tempfile. persist ( & file_path) {
250
254
Ok ( res) => res,
251
255
Err ( err) => {
252
256
if err. error . kind ( ) == std:: io:: ErrorKind :: IsADirectory {
253
257
// It's OK to remove everything that's in the way.
254
258
// Alternatives to this is to let it be handled by the stack.
255
- std:: fs:: remove_dir_all ( err . instance . resource_path ( ) ) ?;
256
- err. instance . commit ( ) ?
259
+ std:: fs:: remove_dir_all ( & file_path ) ?;
260
+ err. file . persist ( file_path ) ?
257
261
} else {
258
262
return Err ( err. into ( ) ) ;
259
263
}
260
264
}
261
265
} ;
262
- update_index ( match maybe_file {
263
- None => gix:: index:: fs:: Metadata :: from_path_no_follow ( & file_path) ?,
264
- Some ( file) => gix:: index:: fs:: Metadata :: from_file ( & file) ?,
265
- } ) ?;
266
+ update_index ( gix:: index:: fs:: Metadata :: from_file ( & file) ?) ?;
266
267
}
267
268
EntryKind :: Link => {
268
269
let link_path = file_path;
@@ -525,34 +526,19 @@ mod file {
525
526
}
526
527
}
527
528
528
- #[ cfg( unix) ]
529
- fn locked_resource_at (
529
+ fn tempfile_in_root_with_permissions_at (
530
530
root : PathBuf ,
531
- path : & Path ,
532
531
kind : EntryKind ,
533
- ) -> anyhow:: Result < gix:: lock:: File > {
534
- use std:: os:: unix:: fs:: PermissionsExt ;
535
- Ok (
536
- gix:: lock:: File :: acquire_to_update_resource_with_permissions (
537
- path,
538
- gix:: lock:: acquire:: Fail :: Immediately ,
539
- Some ( root) ,
540
- || std:: fs:: Permissions :: from_mode ( kind as u32 ) ,
541
- ) ?,
542
- )
543
- }
532
+ ) -> anyhow:: Result < tempfile:: NamedTempFile > {
533
+ #[ cfg_attr( not( unix) , allow( unused_mut) ) ]
534
+ let mut builder = tempfile:: Builder :: new ( ) ;
544
535
545
- #[ cfg( windows) ]
546
- fn locked_resource_at (
547
- root : PathBuf ,
548
- path : & Path ,
549
- _kind : EntryKind ,
550
- ) -> anyhow:: Result < gix:: lock:: File > {
551
- Ok ( gix:: lock:: File :: acquire_to_update_resource (
552
- path,
553
- gix:: lock:: acquire:: Fail :: Immediately ,
554
- Some ( root) ,
555
- ) ?)
536
+ #[ cfg( unix) ]
537
+ {
538
+ use std:: os:: unix:: fs:: PermissionsExt ;
539
+ builder. permissions ( std:: fs:: Permissions :: from_mode ( kind as u32 ) ) ;
540
+ }
541
+ Ok ( builder. tempfile_in ( root) ?)
556
542
}
557
543
}
558
544
0 commit comments