@@ -9,6 +9,7 @@ use std::path::{Path, PathBuf};
99use std:: rc:: Rc ;
1010use anyhow:: { bail, Context , Result } ;
1111use clap:: Subcommand ;
12+ use glob:: glob;
1213use rustii:: archive:: u8;
1314
1415#[ derive( Subcommand ) ]
@@ -30,8 +31,41 @@ pub enum Commands {
3031 }
3132}
3233
33- pub fn pack_u8_archive ( _input : & str , _output : & str ) -> Result < ( ) > {
34- todo ! ( ) ;
34+ fn pack_dir_recursive ( dir : & Rc < RefCell < u8:: U8Directory > > , in_path : PathBuf ) -> Result < ( ) > {
35+ let mut files = Vec :: new ( ) ;
36+ let mut dirs = Vec :: new ( ) ;
37+ for entry in glob ( & format ! ( "{}/*" , in_path. display( ) ) ) ?. flatten ( ) {
38+ match fs:: metadata ( & entry) {
39+ Ok ( meta) if meta. is_file ( ) => files. push ( entry) ,
40+ Ok ( meta) if meta. is_dir ( ) => dirs. push ( entry) ,
41+ _ => { } // Anything that isn't a normal file/directory just gets ignored.
42+ }
43+ }
44+ for file in files {
45+ let node = u8:: U8File :: new ( file. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . to_owned ( ) , fs:: read ( file) ?) ;
46+ u8:: U8Directory :: add_file ( dir, node) ;
47+ }
48+ for child_dir in dirs {
49+ let node = u8:: U8Directory :: new ( child_dir. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . to_owned ( ) ) ;
50+ u8:: U8Directory :: add_dir ( dir, node) ;
51+ let dir = u8:: U8Directory :: get_child_dir ( dir, child_dir. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ) . unwrap ( ) ;
52+ pack_dir_recursive ( & dir, child_dir) ?;
53+ }
54+ Ok ( ( ) )
55+ }
56+
57+ pub fn pack_u8_archive ( input : & str , output : & str ) -> Result < ( ) > {
58+ let in_path = Path :: new ( input) ;
59+ if !in_path. exists ( ) {
60+ bail ! ( "Source directory \" {}\" could not be found." , in_path. display( ) ) ;
61+ }
62+ let out_path = PathBuf :: from ( output) ;
63+ let node_tree = u8:: U8Directory :: new ( String :: new ( ) ) ;
64+ pack_dir_recursive ( & node_tree, in_path. to_path_buf ( ) ) . with_context ( || "A U8 archive could not be packed." ) ?;
65+ let u8_archive = u8:: U8Archive :: from_tree ( & node_tree) . with_context ( || "An unknown error occurred while creating a U8 archive from the data." ) ?;
66+ fs:: write ( & out_path, & u8_archive. to_bytes ( ) ?) . with_context ( || format ! ( "Could not open output file \" {}\" for writing." , out_path. display( ) ) ) ?;
67+ println ! ( "Successfully packed directory \" {}\" into U8 archive \" {}\" !" , in_path. display( ) , out_path. display( ) ) ;
68+ Ok ( ( ) )
3569}
3670
3771fn unpack_dir_recursive ( dir : & Rc < RefCell < u8:: U8Directory > > , out_path : PathBuf ) -> Result < ( ) > {
@@ -51,7 +85,7 @@ fn unpack_dir_recursive(dir: &Rc<RefCell<u8::U8Directory>>, out_path: PathBuf) -
5185pub fn unpack_u8_archive ( input : & str , output : & str ) -> Result < ( ) > {
5286 let in_path = Path :: new ( input) ;
5387 if !in_path. exists ( ) {
54- bail ! ( "Source U8 archive \" {}\" could not be found." , input ) ;
88+ bail ! ( "Source U8 archive \" {}\" could not be found." , in_path . display ( ) ) ;
5589 }
5690 let out_path = PathBuf :: from ( output) ;
5791 if out_path. exists ( ) {
0 commit comments