Revision: 201 http://rigsofrods.svn.sourceforge.net/rigsofrods/?rev=201&view=rev Author: rorthomas Date: 2009-04-21 00:12:18 +0000 (Tue, 21 Apr 2009)
Log Message: ----------- updated wsync Modified Paths: -------------- trunk/build/wsync/include/wsync.h trunk/build/wsync/source/main.cpp trunk/build/wsync/source/wsync.cpp Modified: trunk/build/wsync/include/wsync.h =================================================================== --- trunk/build/wsync/include/wsync.h 2009-04-21 00:11:45 UTC (rev 200) +++ trunk/build/wsync/include/wsync.h 2009-04-21 00:12:18 UTC (rev 201) @@ -50,13 +50,13 @@ WSync(); ~WSync(); // main functions - int sync(boost::filesystem::path localDir, std::string server, std::string remoteDir, bool useMirror=true); + int sync(boost::filesystem::path localDir, std::string server, std::string remoteDir, bool useMirror=true, bool deleteOk=false); int createIndex(boost::filesystem::path localDir, int mode); // useful util functions int downloadFile(boost::filesystem::path localFile, std::string server, std::string remoteDir, bool displayProgress=false); std::string generateFileHash(boost::filesystem::path file); - int getTempFilename(boost::filesystem::path &tempfile); + static int getTempFilename(boost::filesystem::path &tempfile); int downloadConfigFile(std::string server, std::string remoteDir, std::vector< std::vector< std::string > > &list); protected: Modified: trunk/build/wsync/source/main.cpp =================================================================== --- trunk/build/wsync/source/main.cpp 2009-04-21 00:11:45 UTC (rev 200) +++ trunk/build/wsync/source/main.cpp 2009-04-21 00:12:18 UTC (rev 201) @@ -39,22 +39,114 @@ else printf("index created: %s\n", (local_path / INDEXFILENAME).string().c_str()); - } else if(argc == 5 && !strcmp(argv[1], "sync")) + } else if(argc == 5 && (!strcmp(argv[1], "sync") || !strcmp(argv[1], "sync2"))) { + printf("syncing...\n"); string local_path = string(argv[2]); string remote_server = string(argv[3]); string remote_path = string(argv[4]); WSync *w = new WSync(); - w->sync(local_path, remote_server, remote_path); - } else if((argc == 2 && !strcmp(argv[1], "sync")) || (argc == 1) ) + bool deleteOK = exists(path("RoR.exe")); + if(!deleteOK) printf("RoR.exe not detected, wont delete any files in here!\n"); + w->sync(local_path, remote_server, remote_path, true, deleteOK); + +#ifdef WIN32 + path exe_path = system_complete("update.exe"); + PROCESS_INFORMATION pi; + STARTUPINFO si; + DWORD dwCode = 0; + memset(&si, 0, sizeof(STARTUPINFO)); + memset(&pi, 0, sizeof(PROCESS_INFORMATION)); + si.cb = sizeof(STARTUPINFO); + si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; + si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); + si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); + si.hStdError = GetStdHandle(STD_ERROR_HANDLE); + si.lpTitle = "sync"; + si.wShowWindow = SW_SHOW; + + char path[2048]; + sprintf(path, "%s postsync", exe_path.file_string().c_str()); + + //printf("cmdline2: '%s'\n", path); + CreateProcess(NULL, path, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi); + exit(0); + } else if(argc == 2 && !strcmp(argv[1], "postsync")) { + Sleep(100); + path local_path = boost::filesystem::current_path(); + path exe_path = system_complete(path(argv[0])); + path exe_temp = exe_path; + exe_temp.replace_extension(".temp.exe"); + if(exists(exe_temp)) + remove(exe_temp); + // wait for key press + printf("Press any key to continue...\n"); + _getch(); + exit(0); + } else if(argc == 1) + { + printf("duplicating...\n"); + path local_path = boost::filesystem::current_path(); + path exe_path = system_complete(path(argv[0])); + path exe_temp = exe_path; + exe_temp.replace_extension(".temp.exe"); + + //printf("this exe path: %s\n", exe_path.string().c_str()); + //printf("temporary exe path: %s\n", exe_temp.string().c_str()); + //printf("path to update: %s\n", local_path.string().c_str()); + + + // 3. copy self to temp file + try + { + if(exists(exe_temp)) + remove(exe_temp); + if(exists(exe_temp)) + { + printf("error removing tempfile!\n"); + return 1; + } + boost::filesystem::copy_file(exe_path, exe_temp); + } catch(...) + { + printf("error duplicating self!\n"); + printf("this step is required to possibly update the updater\n"); + printf("ensure that you run this application with administrator\n"); + printf("privileges to be able to create files in the current directory.\n"); + return 1; + } + + PROCESS_INFORMATION pi; + STARTUPINFO si; + DWORD dwCode = 0; + memset(&si, 0, sizeof(STARTUPINFO)); + memset(&pi, 0, sizeof(PROCESS_INFORMATION)); + si.cb = sizeof(STARTUPINFO); + si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; + si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); + si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); + si.hStdError = GetStdHandle(STD_ERROR_HANDLE); + si.lpTitle = "sync"; + si.wShowWindow = SW_SHOW; + + + char path[2048]; + sprintf(path, "%s sync2 \"%s\" wsync.rigsofrods.com /", exe_temp.file_string().c_str(), local_path.file_string().c_str()); + + //printf("cmdline1: '%s'\n", path); + CreateProcess(NULL, path, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi); + exit(0); + } else if((argc == 2 && !strcmp(argv[1], "sync"))) + { // shortcut string local_path = "."; string remote_server = "wsync.rigsofrods.com"; string remote_path = "/"; WSync *w = new WSync(); - w->sync(local_path, remote_server, remote_path); -#ifdef WIN32 + bool deleteOK = exists(path("RoR.exe")); + if(!deleteOK) printf("RoR.exe not detected, wont delete any files in here!\n"); + w->sync(local_path, remote_server, remote_path, true, deleteOK); if(argc == 1) { // wait for key press @@ -62,6 +154,7 @@ _getch(); } #endif + #if 0 } else if(argc == 2 && !strcmp(argv[1], "test")) { Modified: trunk/build/wsync/source/wsync.cpp =================================================================== --- trunk/build/wsync/source/wsync.cpp 2009-04-21 00:11:45 UTC (rev 200) +++ trunk/build/wsync/source/wsync.cpp 2009-04-21 00:12:18 UTC (rev 201) @@ -5,9 +5,9 @@ #include "tokenize.h" #include <ctime> -#ifdef _WIN32 -# include <windows.h> -#else +#ifdef WIN32 +#include <windows.h> +#include <conio.h> // for getch #endif using namespace boost::asio; @@ -91,7 +91,7 @@ return ""; } -int WSync::sync(boost::filesystem::path localDir, string server, string remoteDir, bool useMirror) +int WSync::sync(boost::filesystem::path localDir, string server, string remoteDir, bool useMirror, bool deleteOk) { // download remote currrent file first path remoteFileIndex; @@ -149,11 +149,13 @@ //first, detect deleted or changed files for(it = hashMapLocal.begin(); it != hashMapLocal.end(); it++) { + if(it->first == string("/update.temp.exe")) + continue; //if(hashMapRemote[it->first] == it->second) //printf("same: %s %s==%s\n", it->first.c_str(), hashMapRemote[it->first].c_str(), it->second.c_str()); if(hashMapRemote.find(it->first) == hashMapRemote.end()) deletedFiles.push_back(Fileentry(it->first, it->second.filesize)); - else if(hashMapRemote[it->first].hash != it->second.hash && it->first != "/update.exe") + else if(hashMapRemote[it->first].hash != it->second.hash) changedFiles.push_back(Fileentry(it->first, hashMapRemote[it->first].filesize)); } // second, detect new files @@ -164,6 +166,7 @@ } // done comparing + std::vector<Fileentry>::iterator itf; int changeCounter = 0, changeMax = changedFiles.size() + newFiles.size() + deletedFiles.size(); int filesToDownload = newFiles.size() + changedFiles.size(); @@ -173,6 +176,18 @@ for(itf=changedFiles.begin(); itf!=changedFiles.end(); itf++) predDownloadSize += (int)itf->filesize; + // security check in order not to delete the entire harddrive + if(deletedFiles.size() > 1000) + { + printf("are you sure you have placed the application in the correct directory?\n"); + printf("It will delete over 1000 files! Aborting ...\n"); +#ifdef WIN32 + printf("Press any key to continue...\n"); + _getch(); + exit(0); +#endif + } + if(changeMax) { string server_use = server, dir_use = remoteDir; @@ -213,7 +228,7 @@ path localfile = localDir / itf->filename; string url = "/" + dir_use + "/" + itf->filename; int stat = downloadFile(localfile, server_use, url, true); - if(stat == -404 && retrycount == 0) + if(stat == -404 && retrycount < 2) { // fallback to main server! printf("falling back to main server.\n"); @@ -228,13 +243,26 @@ } else { string checkHash = generateFileHash(localfile); - if(findHashInHashmap(hashMapRemote, itf->filename) == checkHash) + string hash_remote = findHashInHashmap(hashMapRemote, itf->filename); + if(hash_remote == checkHash) { printf(" OK \n"); } else { printf(" FAILED \n"); + //printf(" hash is: '%s'\n", checkHash.c_str()); + //printf(" hash should be: '%s'\n", hash_remote.c_str()); remove(localfile); + if(retrycount < 2) + { + // fallback to main server! + printf(" hash wrong, falling back to main server.\n"); + printf(" probably the mirror is not in sync yet\n"); + server_use = server; + dir_use = remoteDir; + retrycount++; + goto retry; + } } } } @@ -252,7 +280,7 @@ path localfile = localDir / itf->filename; string url = "/" + dir_use + "/" + itf->filename; int stat = downloadFile(localfile, server_use, url, true); - if(stat == -404 && retrycount == 0) + if(stat == -404 && retrycount < 2) { // fallback to main server! printf("falling back to main server.\n"); @@ -267,17 +295,33 @@ } else { string checkHash = generateFileHash(localfile); - if(findHashInHashmap(hashMapRemote, itf->filename) == checkHash) + string hash_remote = findHashInHashmap(hashMapRemote, itf->filename); + if(hash_remote == checkHash) + { printf(" OK \n"); - else + } else + { printf(" FAILED \n"); + //printf(" hash is: '%s'\n", checkHash.c_str()); + //printf(" hash should be: '%s'\n", hash_remote.c_str()); + remove(localfile); + if(retrycount < 2) + { + // fallback to main server! + printf(" hash wrong, falling back to main server.\n"); + printf(" probably the mirror is not in sync yet\n"); + server_use = server; + dir_use = remoteDir; + retrycount++; + goto retry2; + } + } } } } - if(deletedFiles.size() && !(modeNumber & WMO_NODELETE)) + if(deleteOk && deletedFiles.size() && !(modeNumber & WMO_NODELETE)) { - progressOutputShort(float(changeCounter)/float(changeMax)); for(itf=deletedFiles.begin();itf!=deletedFiles.end();itf++, changeCounter++) { progressOutputShort(float(changeCounter)/float(changeMax)); @@ -293,12 +337,15 @@ printf("unable to delete file: %s\n", localfile.string().c_str()); } } + } else if (!deleteOk && deletedFiles.size()) + { + printf("wont delete any files, since safety conditions are not met.\n"); } + printf("sync complete, downloaded %s\n", formatFilesize(downloadSize).c_str()); } else printf("sync complete (already up to date), downloaded %s\n", formatFilesize(downloadSize).c_str()); - //remove temp files again remove(remoteFileIndex); return 0; @@ -308,7 +355,8 @@ string WSync::generateFileHash(boost::filesystem::path file) { CSHA1 sha1; - sha1.HashFile(const_cast<char*>(file.string().c_str())); + bool res = sha1.HashFile(const_cast<char*>(file.string().c_str())); + if(!res) return string(""); sha1.Final(); char resultHash[256] = ""; sha1.ReportHash(resultHash, CSHA1::REPORT_HEX_SHORT); @@ -343,6 +391,9 @@ if (found != string::npos) continue; + found = respath.find(".temp."); + if (found != string::npos) + continue; string resultHash = generateFileHash(it->c_str()); @@ -615,7 +666,12 @@ //printf("filesize: %d bytes\n", reported_filesize); // open local file late -> prevent creating emoty files - myfile.open(localFile.string().c_str(), ios::out | ios::binary); + myfile.open(localFile.string().c_str(), ios::out | ios::binary); + if(!myfile.is_open()) + { + printf("error opening file: %s\n", localFile.string().c_str()); + return 2; + } // Write whatever content we already have to output. if (response.size() > 0) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Stay on top of everything new and different, both inside and around Java (TM) technology - register by April 22, and save $200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco. 300 plus technical and hands-on sessions. Register today. Use priority code J9JMT32. http://p.sf.net/sfu/p _______________________________________________ Rigsofrods-devel mailing list Rigsofrods-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rigsofrods-devel