@@ -94,6 +94,7 @@ impl RemoteRelease {
94
94
pub type OnBeforeExit = Arc < dyn Fn ( ) + Send + Sync + ' static > ;
95
95
96
96
pub struct UpdaterBuilder {
97
+ app_name : String ,
97
98
current_version : Version ,
98
99
config : Config ,
99
100
version_comparator : Option < Box < dyn Fn ( Version , RemoteRelease ) -> bool + Send + Sync > > ,
@@ -109,14 +110,17 @@ pub struct UpdaterBuilder {
109
110
}
110
111
111
112
impl UpdaterBuilder {
112
- pub fn new ( current_version : Version , config : crate :: Config ) -> Self {
113
+ /// It's prefered to use [`crate::UpdaterExt::updater_builder`] instead of
114
+ /// constructing a [`UpdaterBuilder`] with this function yourself
115
+ pub fn new ( app_name : String , current_version : Version , config : crate :: Config ) -> Self {
113
116
Self {
114
117
installer_args : config
115
118
. windows
116
119
. as_ref ( )
117
120
. map ( |w| w. installer_args . clone ( ) )
118
121
. unwrap_or_default ( ) ,
119
122
current_exe_args : Vec :: new ( ) ,
123
+ app_name,
120
124
current_version,
121
125
config,
122
126
version_comparator : None ,
@@ -239,6 +243,7 @@ impl UpdaterBuilder {
239
243
240
244
Ok ( Updater {
241
245
config : self . config ,
246
+ app_name : self . app_name ,
242
247
current_version : self . current_version ,
243
248
version_comparator : self . version_comparator ,
244
249
timeout : self . timeout ,
@@ -270,6 +275,7 @@ impl UpdaterBuilder {
270
275
271
276
pub struct Updater {
272
277
config : Config ,
278
+ app_name : String ,
273
279
current_version : Version ,
274
280
version_comparator : Option < Box < dyn Fn ( Version , RemoteRelease ) -> bool + Send + Sync > > ,
275
281
timeout : Option < Duration > ,
@@ -386,6 +392,7 @@ impl Updater {
386
392
Some ( Update {
387
393
config : self . config . clone ( ) ,
388
394
on_before_exit : self . on_before_exit . clone ( ) ,
395
+ app_name : self . app_name . clone ( ) ,
389
396
current_version : self . current_version . to_string ( ) ,
390
397
target : self . target . clone ( ) ,
391
398
extract_path : self . extract_path . clone ( ) ,
@@ -436,6 +443,9 @@ pub struct Update {
436
443
/// Extract path
437
444
#[ allow( unused) ]
438
445
extract_path : PathBuf ,
446
+ /// App name, used for creating named tempfiles on Windows
447
+ #[ allow( unused) ]
448
+ app_name : String ,
439
449
#[ allow( unused) ]
440
450
installer_args : Vec < OsString > ,
441
451
#[ allow( unused) ]
@@ -584,7 +594,7 @@ impl Update {
584
594
Win32 :: UI :: { Shell :: ShellExecuteW , WindowsAndMessaging :: SW_SHOW } ,
585
595
} ;
586
596
587
- let updater_type = Self :: extract ( bytes) ?;
597
+ let updater_type = self . extract ( bytes) ?;
588
598
589
599
let install_mode = self . config . install_mode ( ) ;
590
600
let current_args = & self . current_exe_args ( ) [ 1 ..] ;
@@ -663,24 +673,31 @@ impl Update {
663
673
. collect :: < Vec < _ > > ( )
664
674
}
665
675
666
- fn extract ( bytes : & [ u8 ] ) -> Result < WindowsUpdaterType > {
676
+ fn extract ( & self , bytes : & [ u8 ] ) -> Result < WindowsUpdaterType > {
667
677
#[ cfg( feature = "zip" ) ]
668
678
if infer:: archive:: is_zip ( bytes) {
669
- return Self :: extract_zip ( bytes) ;
679
+ return self . extract_zip ( bytes) ;
670
680
}
671
681
672
- Self :: extract_exe ( bytes)
682
+ self . extract_exe ( bytes)
683
+ }
684
+
685
+ fn make_temp_dir ( & self ) -> Result < PathBuf > {
686
+ Ok ( tempfile:: Builder :: new ( )
687
+ . prefix ( & format ! ( "{}-{}-updater-" , self . app_name, self . version) )
688
+ . tempdir ( ) ?
689
+ . into_path ( ) )
673
690
}
674
691
675
692
#[ cfg( feature = "zip" ) ]
676
- fn extract_zip ( bytes : & [ u8 ] ) -> Result < WindowsUpdaterType > {
677
- let tmp_dir = tempfile :: Builder :: new ( ) . tempdir ( ) ?. into_path ( ) ;
693
+ fn extract_zip ( & self , bytes : & [ u8 ] ) -> Result < WindowsUpdaterType > {
694
+ let temp_dir = self . make_temp_dir ( ) ?;
678
695
679
696
let archive = Cursor :: new ( bytes) ;
680
697
let mut extractor = zip:: ZipArchive :: new ( archive) ?;
681
- extractor. extract ( & tmp_dir ) ?;
698
+ extractor. extract ( & temp_dir ) ?;
682
699
683
- let paths = std:: fs:: read_dir ( & tmp_dir ) ?;
700
+ let paths = std:: fs:: read_dir ( & temp_dir ) ?;
684
701
for path in paths {
685
702
let path = path?. path ( ) ;
686
703
let ext = path. extension ( ) ;
@@ -694,22 +711,31 @@ impl Update {
694
711
Err ( crate :: Error :: BinaryNotFoundInArchive )
695
712
}
696
713
697
- fn extract_exe ( bytes : & [ u8 ] ) -> Result < WindowsUpdaterType > {
714
+ fn extract_exe ( & self , bytes : & [ u8 ] ) -> Result < WindowsUpdaterType > {
698
715
if infer:: app:: is_exe ( bytes) {
699
- let ( path, temp) = Self :: write_to_temp ( bytes, ".exe" ) ?;
716
+ let ( path, temp) = self . write_to_temp ( bytes, ".exe" ) ?;
700
717
Ok ( WindowsUpdaterType :: nsis ( path, temp) )
701
718
} else if infer:: archive:: is_msi ( bytes) {
702
- let ( path, temp) = Self :: write_to_temp ( bytes, ".msi" ) ?;
719
+ let ( path, temp) = self . write_to_temp ( bytes, ".msi" ) ?;
703
720
Ok ( WindowsUpdaterType :: msi ( path, temp) )
704
721
} else {
705
722
Err ( crate :: Error :: InvalidUpdaterFormat )
706
723
}
707
724
}
708
725
709
- fn write_to_temp ( bytes : & [ u8 ] , ext : & str ) -> Result < ( PathBuf , Option < tempfile:: TempPath > ) > {
726
+ fn write_to_temp (
727
+ & self ,
728
+ bytes : & [ u8 ] ,
729
+ ext : & str ,
730
+ ) -> Result < ( PathBuf , Option < tempfile:: TempPath > ) > {
710
731
use std:: io:: Write ;
711
732
712
- let mut temp_file = tempfile:: Builder :: new ( ) . suffix ( ext) . tempfile ( ) ?;
733
+ let temp_dir = self . make_temp_dir ( ) ?;
734
+ let mut temp_file = tempfile:: Builder :: new ( )
735
+ . prefix ( & format ! ( "{}-{}-installer" , self . app_name, self . version) )
736
+ . suffix ( ext)
737
+ . rand_bytes ( 0 )
738
+ . tempfile_in ( temp_dir) ?;
713
739
temp_file. write_all ( bytes) ?;
714
740
715
741
let temp = temp_file. into_temp_path ( ) ;
0 commit comments