medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 14764db0ff7f340e843d60cec667d1d2a752c320
parent 6a46c9ea561babebe40083af64f25f8ec2282d3d
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sun Nov 20 13:51:58 +0200

Create the right directory structure while updating

Diffstat:
launcher/main.cc | 46+++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 41 insertions(+), 5 deletions(-)
diff --git a/launcher/main.cc b/launcher/main.cc @@ -3,6 +3,7 @@ #include <string> #include <vector> #include <map> +#include <set> #include "../intrinsics.h" #include "../log.h" @@ -141,6 +142,27 @@ static const char * file_get_contents_or_empty( const char * path, size_t * out_ return contents; } +static void recursive_mkdir( + std::vector< std::string > * directories_to_remove, + const std::string & path +) { + size_t start = 0; + while( true ) { + size_t end = path.find( "/", start ); + if( end == std::string::npos ) { + return; + } + + const std::string subdir = path.substr( 0, end ); + mkdir( subdir.c_str(), 0755 ); + start = end + 1; + + if( directories_to_remove != NULL ) { + directories_to_remove->push_back( subdir ); + } + } +} + #define HOST "f.mikejsavage.co.uk" int main() { #if PLATFORM_WINDOWS @@ -188,7 +210,9 @@ int main() { parse_manifest( local_manifest, local_manifest_str ); parse_manifest( remote_manifest, remote_manifest_str.c_str() ); - std::vector< std::string > files_to_update, files_to_remove; + std::vector< std::string > files_to_update; + std::vector< std::string > files_to_remove; + std::vector< std::string > directories_to_remove; u64 update_size = 0; // look for files that have changed and files that should be removed @@ -212,7 +236,6 @@ int main() { } } - mkdir( "update", 0755 ); for( size_t i = 0; i < files_to_update.size(); i++ ) { const ManifestEntry & entry = remote_manifest[ files_to_update[ i ] ]; const std::string update_path = "update/" + files_to_update[ i ]; @@ -244,18 +267,31 @@ int main() { FATAL( "checksums don't match" ); } - // TODO: create more directories if we need to - FILE * f = fopen( ( "update/" + files_to_update[ i ] ).c_str(), "wb" ); + recursive_mkdir( &directories_to_remove, update_path ); + + FILE * f = fopen( update_path.c_str(), "wb" ); + if( f == NULL ) { + FATAL( "couldn't open %s for writing", update_path.c_str() ); + } fwrite( file_contents.c_str(), 1, file_contents.size(), f ); fclose( f ); } for( size_t i = 0; i < files_to_update.size(); i++ ) { const std::string update_path = "update/" + files_to_update[ i ]; + recursive_mkdir( NULL, files_to_update[ i ].c_str() ); rename( update_path.c_str(), files_to_update[ i ].c_str() ); } - rmdir( "update" ); + std::set< std::string > already_removed; + for( size_t i = 0; i < directories_to_remove.size(); i++ ) { + size_t idx = directories_to_remove.size() - i - 1; + const std::string & dir = directories_to_remove[ idx ]; + if( already_removed.find( dir ) == already_removed.end() ) { + rmdir( dir.c_str() ); + already_removed.insert( dir ); + } + } for( size_t i = 0; i < files_to_remove.size(); i++ ) { printf( "removing: %s\n", files_to_remove[ i ].c_str() );