@@ -30,29 +30,55 @@ pub fn check_for_update() -> Result<Option<String>, String> {
3030}
3131
3232pub fn download_and_install ( tag : & str ) -> Result < ( ) , String > {
33+ let temp_dir = std:: env:: temp_dir ( ) ;
34+ let log_path = temp_dir. join ( "corescribe_update.log" ) ;
35+ let mut log = fs:: File :: create ( & log_path) . unwrap ( ) ;
36+
37+ macro_rules! log {
38+ ( $( $arg: tt) * ) => { {
39+ let line = format!( $( $arg) * ) ;
40+ use std:: io:: Write ;
41+ let _ = writeln!( log, "{}" , line) ;
42+ } } ;
43+ }
44+
3345 let url = format ! (
3446 "https://github.com/{}/releases/download/{}/corescribe.exe" ,
3547 REPO , tag
3648 ) ;
37- println ! ( "Downloading from: {}" , url) ;
49+ log ! ( "Downloading from: {}" , url) ;
3850
3951 let response = ureq:: get ( & url)
4052 . call ( )
41- . map_err ( |e| format ! ( "Failed to download: {}" , e) ) ?;
53+ . map_err ( |e| { log ! ( "Download failed: {}" , e) ; format ! ( "Failed to download: {}" , e) } ) ?;
54+
55+ log ! ( "Download response OK, status: {}" , response. status( ) ) ;
4256
43- let temp_dir = std:: env:: temp_dir ( ) ;
4457 let update_exe = temp_dir. join ( "corescribe_update.exe" ) ;
58+ log ! ( "Saving to: {}" , update_exe. display( ) ) ;
4559
46- let mut file =
47- fs :: File :: create ( & update_exe ) . map_err ( |e| format ! ( "Failed to create temp file: {}" , e) ) ?;
60+ let mut file = fs :: File :: create ( & update_exe )
61+ . map_err ( |e| { log ! ( "Create file failed: {}" , e ) ; format ! ( "Failed to create temp file: {}" , e) } ) ?;
4862
49- std:: io:: copy ( & mut response. into_reader ( ) , & mut file)
50- . map_err ( |e| format ! ( "Failed to write file: {}" , e) ) ?;
63+ let bytes = std:: io:: copy ( & mut response. into_reader ( ) , & mut file)
64+ . map_err ( |e| { log ! ( "Write failed: {}" , e ) ; format ! ( "Failed to write file: {}" , e) } ) ?;
5165
66+ log ! ( "Downloaded {} bytes" , bytes) ;
5267 drop ( file) ;
5368
54- let current_exe =
55- std:: env:: current_exe ( ) . map_err ( |e| format ! ( "Failed to get current exe path: {}" , e) ) ?;
69+ let file_size = fs:: metadata ( & update_exe) . map ( |m| m. len ( ) ) . unwrap_or ( 0 ) ;
70+ log ! ( "File size on disk: {} bytes" , file_size) ;
71+
72+ if file_size < 100_000 {
73+ let content = fs:: read_to_string ( & update_exe) . unwrap_or_default ( ) ;
74+ log ! ( "File too small, content: {}" , & content[ ..content. len( ) . min( 500 ) ] ) ;
75+ return Err ( format ! ( "Downloaded file is too small ({} bytes) - likely an error page" , file_size) ) ;
76+ }
77+
78+ let current_exe = std:: env:: current_exe ( )
79+ . map_err ( |e| { log ! ( "current_exe failed: {}" , e) ; format ! ( "Failed to get current exe: {}" , e) } ) ?;
80+
81+ log ! ( "Current exe: {}" , current_exe. display( ) ) ;
5682
5783 #[ cfg( target_os = "windows" ) ]
5884 {
@@ -67,14 +93,20 @@ pub fn download_and_install(tag: &str) -> Result<(), String> {
6793 ) ;
6894
6995 let bat_path = temp_dir. join ( "corescribe_update.bat" ) ;
96+ log ! ( "Writing bat to: {}" , bat_path. display( ) ) ;
97+ log ! ( "Bat content:\n {}" , bat) ;
98+
7099 fs:: write ( & bat_path, bat. as_bytes ( ) )
71- . map_err ( |e| format ! ( "Failed to write update script: {}" , e) ) ?;
100+ . map_err ( |e| { log ! ( "Write bat failed: {}" , e ) ; format ! ( "Failed to write update script: {}" , e) } ) ?;
72101
102+ log ! ( "Spawning bat script..." ) ;
73103 std:: process:: Command :: new ( "cmd" )
74104 . args ( & [ "/c" , & bat_path. to_string_lossy ( ) . to_string ( ) ] )
75105 . creation_flags ( CREATE_NO_WINDOW )
76106 . spawn ( )
77- . map_err ( |e| format ! ( "Failed to spawn updater: {}" , e) ) ?;
107+ . map_err ( |e| { log ! ( "Spawn failed: {}" , e) ; format ! ( "Failed to spawn updater: {}" , e) } ) ?;
108+
109+ log ! ( "Bat spawned, exiting app..." ) ;
78110 }
79111
80112 std:: process:: exit ( 0 ) ;
0 commit comments