33//! prefix, represented by a `Components` instance.
44
55use std:: collections:: { HashMap , HashSet } ;
6- use std:: fmt;
76use std:: io:: { self , ErrorKind as IOErrorKind , Read } ;
87use std:: mem;
8+ use std:: ops:: Deref ;
99use std:: path:: { Path , PathBuf } ;
1010use std:: sync:: OnceLock ;
1111
@@ -17,6 +17,7 @@ use crate::diskio::{CompletedIo, Executor, FileBuffer, IO_CHUNK_SIZE, Item, Kind
1717use crate :: dist:: component:: components:: { ComponentPart , ComponentPartKind , Components } ;
1818use crate :: dist:: component:: transaction:: Transaction ;
1919use crate :: dist:: download:: DownloadCfg ;
20+ use crate :: dist:: manifest:: CompressionKind ;
2021use crate :: dist:: temp;
2122use crate :: errors:: RustupError ;
2223use crate :: utils;
@@ -26,18 +27,6 @@ use crate::utils::units::Size;
2627pub ( crate ) const INSTALLER_VERSION : & str = "3" ;
2728pub ( crate ) const VERSION_FILE : & str = "rust-installer-version" ;
2829
29- pub trait Package : fmt:: Debug {
30- fn contains ( & self , component : & str , short_name : Option < & str > ) -> bool ;
31- fn install < ' a > (
32- & self ,
33- target : & Components ,
34- component : & str ,
35- short_name : Option < & str > ,
36- tx : Transaction < ' a > ,
37- ) -> Result < Transaction < ' a > > ;
38- fn components ( & self ) -> Vec < String > ;
39- }
40-
4130#[ derive( Debug ) ]
4231pub struct DirectoryPackage {
4332 path : PathBuf ,
@@ -69,16 +58,17 @@ fn validate_installer_version(path: &Path) -> Result<()> {
6958 }
7059}
7160
72- impl Package for DirectoryPackage {
73- fn contains ( & self , component : & str , short_name : Option < & str > ) -> bool {
61+ impl DirectoryPackage {
62+ pub fn contains ( & self , component : & str , short_name : Option < & str > ) -> bool {
7463 self . components . contains ( component)
7564 || if let Some ( n) = short_name {
7665 self . components . contains ( n)
7766 } else {
7867 false
7968 }
8069 }
81- fn install < ' a > (
70+
71+ pub fn install < ' a > (
8272 & self ,
8373 target : & Components ,
8474 name : & str ,
@@ -129,7 +119,7 @@ impl Package for DirectoryPackage {
129119 Ok ( tx)
130120 }
131121
132- fn components ( & self ) -> Vec < String > {
122+ pub ( crate ) fn components ( & self ) -> Vec < String > {
133123 self . components . iter ( ) . cloned ( ) . collect ( )
134124 }
135125}
@@ -139,7 +129,19 @@ impl Package for DirectoryPackage {
139129pub ( crate ) struct TarPackage ( DirectoryPackage , temp:: Dir ) ;
140130
141131impl TarPackage {
142- pub ( crate ) fn new < R : Read > ( stream : R , dl_cfg : & DownloadCfg < ' _ > ) -> Result < Self > {
132+ pub ( crate ) fn compressed < R : Read > (
133+ stream : R ,
134+ kind : CompressionKind ,
135+ dl_cfg : & DownloadCfg < ' _ > ,
136+ ) -> Result < Self > {
137+ match kind {
138+ CompressionKind :: GZip => Self :: new ( flate2:: read:: GzDecoder :: new ( stream) , dl_cfg) ,
139+ CompressionKind :: ZStd => Self :: new ( zstd:: stream:: read:: Decoder :: new ( stream) ?, dl_cfg) ,
140+ CompressionKind :: XZ => Self :: new ( xz2:: read:: XzDecoder :: new ( stream) , dl_cfg) ,
141+ }
142+ }
143+
144+ fn new < R : Read > ( stream : R , dl_cfg : & DownloadCfg < ' _ > ) -> Result < Self > {
143145 let temp_dir = dl_cfg. tmp_cx . new_directory ( ) ?;
144146 let mut archive = tar:: Archive :: new ( stream) ;
145147 // The rust-installer packages unpack to a directory called
@@ -155,6 +157,14 @@ impl TarPackage {
155157 }
156158}
157159
160+ impl Deref for TarPackage {
161+ type Target = DirectoryPackage ;
162+
163+ fn deref ( & self ) -> & Self :: Target {
164+ & self . 0
165+ }
166+ }
167+
158168// Probably this should live in diskio but ¯\_(ツ)_/¯
159169fn unpack_ram (
160170 io_chunk_size : usize ,
@@ -525,105 +535,3 @@ fn unpack_without_first_dir<R: Read>(
525535
526536 Ok ( ( ) )
527537}
528-
529- impl Package for TarPackage {
530- fn contains ( & self , component : & str , short_name : Option < & str > ) -> bool {
531- self . 0 . contains ( component, short_name)
532- }
533- fn install < ' b > (
534- & self ,
535- target : & Components ,
536- component : & str ,
537- short_name : Option < & str > ,
538- tx : Transaction < ' b > ,
539- ) -> Result < Transaction < ' b > > {
540- self . 0 . install ( target, component, short_name, tx)
541- }
542- fn components ( & self ) -> Vec < String > {
543- self . 0 . components ( )
544- }
545- }
546-
547- #[ derive( Debug ) ]
548- pub ( crate ) struct TarGzPackage ( TarPackage ) ;
549-
550- impl TarGzPackage {
551- pub ( crate ) fn new < R : Read > ( stream : R , dl_cfg : & DownloadCfg < ' _ > ) -> Result < Self > {
552- let stream = flate2:: read:: GzDecoder :: new ( stream) ;
553- Ok ( TarGzPackage ( TarPackage :: new ( stream, dl_cfg) ?) )
554- }
555- }
556-
557- impl Package for TarGzPackage {
558- fn contains ( & self , component : & str , short_name : Option < & str > ) -> bool {
559- self . 0 . contains ( component, short_name)
560- }
561- fn install < ' b > (
562- & self ,
563- target : & Components ,
564- component : & str ,
565- short_name : Option < & str > ,
566- tx : Transaction < ' b > ,
567- ) -> Result < Transaction < ' b > > {
568- self . 0 . install ( target, component, short_name, tx)
569- }
570- fn components ( & self ) -> Vec < String > {
571- self . 0 . components ( )
572- }
573- }
574-
575- #[ derive( Debug ) ]
576- pub ( crate ) struct TarXzPackage ( TarPackage ) ;
577-
578- impl TarXzPackage {
579- pub ( crate ) fn new < R : Read > ( stream : R , dl_cfg : & DownloadCfg < ' _ > ) -> Result < Self > {
580- let stream = xz2:: read:: XzDecoder :: new ( stream) ;
581- Ok ( TarXzPackage ( TarPackage :: new ( stream, dl_cfg) ?) )
582- }
583- }
584-
585- impl Package for TarXzPackage {
586- fn contains ( & self , component : & str , short_name : Option < & str > ) -> bool {
587- self . 0 . contains ( component, short_name)
588- }
589- fn install < ' b > (
590- & self ,
591- target : & Components ,
592- component : & str ,
593- short_name : Option < & str > ,
594- tx : Transaction < ' b > ,
595- ) -> Result < Transaction < ' b > > {
596- self . 0 . install ( target, component, short_name, tx)
597- }
598- fn components ( & self ) -> Vec < String > {
599- self . 0 . components ( )
600- }
601- }
602-
603- #[ derive( Debug ) ]
604- pub ( crate ) struct TarZStdPackage ( TarPackage ) ;
605-
606- impl TarZStdPackage {
607- pub ( crate ) fn new < R : Read > ( stream : R , dl_cfg : & DownloadCfg < ' _ > ) -> Result < Self > {
608- let stream = zstd:: stream:: read:: Decoder :: new ( stream) ?;
609- Ok ( TarZStdPackage ( TarPackage :: new ( stream, dl_cfg) ?) )
610- }
611- }
612-
613- impl Package for TarZStdPackage {
614- fn contains ( & self , component : & str , short_name : Option < & str > ) -> bool {
615- self . 0 . contains ( component, short_name)
616- }
617- fn install < ' b > (
618- & self ,
619- target : & Components ,
620- component : & str ,
621- short_name : Option < & str > ,
622- tx : Transaction < ' b > ,
623- ) -> Result < Transaction < ' b > > {
624- self . 0 . install ( target, component, short_name, tx)
625- }
626- fn components ( & self ) -> Vec < String > {
627- self . 0 . components ( )
628- }
629- }
0 commit comments