11use std:: path:: { Path , PathBuf } ;
22
3- use zsync_rs:: ZsyncAssembly ;
3+ use zsync_rs:: { ControlFile , ZsyncAssembly } ;
44
55use crate :: appimage:: AppImage ;
66use crate :: error:: { Error , Result } ;
@@ -60,14 +60,27 @@ impl Updater {
6060 self
6161 }
6262
63- pub fn check_for_update ( & self ) -> Result < bool > {
63+ fn fetch_control_file ( & self ) -> Result < ( ControlFile , String ) > {
6464 let zsync_url = self . update_info . zsync_url ( ) ?;
6565 let http = zsync_rs:: HttpClient :: new ( ) ;
66- let _control = http
66+ let control = http
6767 . fetch_control_file ( & zsync_url)
6868 . map_err ( |e| Error :: Zsync ( format ! ( "Failed to fetch control file: {}" , e) ) ) ?;
69+ Ok ( ( control, zsync_url) )
70+ }
6971
70- let output_path = self . output_path ( ) ;
72+ fn resolve_output_path ( & self , control : & ControlFile ) -> Result < PathBuf > {
73+ let filename = control
74+ . filename
75+ . as_ref ( )
76+ . ok_or_else ( || Error :: Zsync ( "Control file has no filename" . into ( ) ) ) ?;
77+ Ok ( self . output_dir . join ( filename) )
78+ }
79+
80+ pub fn check_for_update ( & self ) -> Result < bool > {
81+ let ( control, _zsync_url) = self . fetch_control_file ( ) ?;
82+
83+ let output_path = self . resolve_output_path ( & control) ?;
7184 if output_path. exists ( ) && !self . overwrite {
7285 return Err ( Error :: AppImage ( format ! (
7386 "Output file already exists: {}" ,
@@ -79,8 +92,8 @@ impl Updater {
7992 }
8093
8194 pub fn perform_update ( & self ) -> Result < PathBuf > {
82- let zsync_url = self . update_info . zsync_url ( ) ?;
83- let output_path = self . output_path ( ) ;
95+ let ( control , zsync_url) = self . fetch_control_file ( ) ?;
96+ let output_path = self . resolve_output_path ( & control ) ? ;
8497
8598 if output_path. exists ( ) && !self . overwrite {
8699 return Err ( Error :: AppImage ( format ! (
@@ -90,34 +103,30 @@ impl Updater {
90103 }
91104
92105 let assembly = ZsyncAssembly :: from_url ( & zsync_url, & output_path)
93- . map_err ( |e| Error :: AppImage ( format ! ( "Failed to initialize zsync: {}" , e) ) ) ?;
106+ . map_err ( |e| Error :: Zsync ( format ! ( "Failed to initialize zsync: {}" , e) ) ) ?;
94107
95108 let mut assembly = assembly;
96109
97110 assembly
98111 . submit_source_file ( self . appimage . path ( ) )
99- . map_err ( |e| Error :: AppImage ( format ! ( "Failed to submit source file: {}" , e) ) ) ?;
112+ . map_err ( |e| Error :: Zsync ( format ! ( "Failed to submit source file: {}" , e) ) ) ?;
100113
101114 assembly
102115 . submit_self_referential ( )
103- . map_err ( |e| Error :: AppImage ( format ! ( "Self-referential scan failed: {}" , e) ) ) ?;
116+ . map_err ( |e| Error :: Zsync ( format ! ( "Self-referential scan failed: {}" , e) ) ) ?;
104117
105118 assembly
106119 . download_missing_blocks ( )
107- . map_err ( |e| Error :: AppImage ( format ! ( "Failed to download blocks: {}" , e) ) ) ?;
120+ . map_err ( |e| Error :: Zsync ( format ! ( "Failed to download blocks: {}" , e) ) ) ?;
108121
109122 assembly
110123 . complete ( )
111- . map_err ( |e| Error :: AppImage ( format ! ( "Failed to complete assembly: {}" , e) ) ) ?;
124+ . map_err ( |e| Error :: Zsync ( format ! ( "Failed to complete assembly: {}" , e) ) ) ?;
112125
113126 Ok ( output_path)
114127 }
115128
116129 pub fn progress ( & self ) -> Option < ( u64 , u64 ) > {
117130 None
118131 }
119-
120- fn output_path ( & self ) -> PathBuf {
121- self . appimage . path ( ) . to_path_buf ( )
122- }
123132}
0 commit comments