diff options
Diffstat (limited to 'cpp/src/IcePatch2')
-rw-r--r-- | cpp/src/IcePatch2/Calc.cpp | 308 | ||||
-rw-r--r-- | cpp/src/IcePatch2/Client.cpp | 264 | ||||
-rwxr-xr-x | cpp/src/IcePatch2/ClientUtil.cpp | 1144 | ||||
-rw-r--r-- | cpp/src/IcePatch2/FileServerI.cpp | 58 | ||||
-rw-r--r-- | cpp/src/IcePatch2/FileServerI.h | 2 | ||||
-rw-r--r-- | cpp/src/IcePatch2/OS.cpp | 4 | ||||
-rw-r--r-- | cpp/src/IcePatch2/Server.cpp | 116 | ||||
-rw-r--r-- | cpp/src/IcePatch2/Util.cpp | 1518 |
8 files changed, 1707 insertions, 1707 deletions
diff --git a/cpp/src/IcePatch2/Calc.cpp b/cpp/src/IcePatch2/Calc.cpp index 33b76fdee44..01947ac9d75 100644 --- a/cpp/src/IcePatch2/Calc.cpp +++ b/cpp/src/IcePatch2/Calc.cpp @@ -25,7 +25,7 @@ struct FileInfoPathLess: public binary_function<const FileInfo&, const FileInfo& bool operator()(const FileInfo& lhs, const FileInfo& rhs) { - return lhs.path < rhs.path; + return lhs.path < rhs.path; } }; @@ -34,20 +34,20 @@ struct IFileInfoPathEqual: public binary_function<const FileInfo&, const FileInf bool operator()(const FileInfo& lhs, const FileInfo& rhs) { - if(lhs.path.size() != rhs.path.size()) - { - return false; - } - - for(string::size_type i = 0; i < lhs.path.size(); ++i) - { - if(::tolower(lhs.path[i]) != ::tolower(rhs.path[i])) - { - return false; - } - } - - return true; + if(lhs.path.size() != rhs.path.size()) + { + return false; + } + + for(string::size_type i = 0; i < lhs.path.size(); ++i) + { + if(::tolower(lhs.path[i]) != ::tolower(rhs.path[i])) + { + return false; + } + } + + return true; } }; @@ -56,18 +56,18 @@ struct IFileInfoPathLess: public binary_function<const FileInfo&, const FileInfo bool operator()(const FileInfo& lhs, const FileInfo& rhs) { - for(string::size_type i = 0; i < lhs.path.size() && i < rhs.path.size(); ++i) - { - if(::tolower(lhs.path[i]) < ::tolower(rhs.path[i])) - { - return true; - } - else if(::tolower(lhs.path[i]) > ::tolower(rhs.path[i])) - { - return false; - } - } - return lhs.path.size() < rhs.path.size(); + for(string::size_type i = 0; i < lhs.path.size() && i < rhs.path.size(); ++i) + { + if(::tolower(lhs.path[i]) < ::tolower(rhs.path[i])) + { + return true; + } + else if(::tolower(lhs.path[i]) > ::tolower(rhs.path[i])) + { + return false; + } + } + return lhs.path.size() < rhs.path.size(); } }; @@ -78,22 +78,22 @@ public: virtual bool remove(const string& path) { - cout << "removing: " << path << endl; - return true; + cout << "removing: " << path << endl; + return true; } virtual bool checksum(const string& path) { - cout << "checksum: " << path << endl; - return true; + cout << "checksum: " << path << endl; + return true; } virtual bool compress(const string& path) { - cout << "compress: " << path << endl; - return true; + cout << "compress: " << path << endl; + return true; } }; @@ -132,32 +132,32 @@ main(int argc, char* argv[]) vector<string> args; try { - args = opts.parse(argc, (const char**)argv); + args = opts.parse(argc, (const char**)argv); } catch(const IceUtil::BadOptException& e) { cerr << e.reason << endl; - usage(argv[0]); - return EXIT_FAILURE; + usage(argv[0]); + return EXIT_FAILURE; } if(opts.isSet("help")) { - usage(argv[0]); - return EXIT_SUCCESS; + usage(argv[0]); + return EXIT_SUCCESS; } if(opts.isSet("version")) { - cout << ICE_STRING_VERSION << endl; - return EXIT_SUCCESS; + cout << ICE_STRING_VERSION << endl; + return EXIT_SUCCESS; } bool doCompress = opts.isSet("compress"); bool dontCompress = opts.isSet("no-compress"); if(doCompress && dontCompress) { cerr << argv[0] << ": only one of -z and -Z are mutually exclusive" << endl; - usage(argv[0]); - return EXIT_FAILURE; + usage(argv[0]); + return EXIT_FAILURE; } if(doCompress) { @@ -185,120 +185,120 @@ main(int argc, char* argv[]) try { - StringSeq::iterator p; - string absDataDir = dataDir; + StringSeq::iterator p; + string absDataDir = dataDir; - string cwd; - if(OS::getcwd(cwd) != 0) - { - throw "cannot get the current directory:\n" + lastError(); - } - - if(!isAbsolute(absDataDir)) - { - absDataDir = simplify(cwd + '/' + absDataDir); - } - - for(p = fileSeq.begin(); p != fileSeq.end(); ++p) - { - if(!isAbsolute(*p)) - { - *p = cwd + '/' + *p; - } - } - - // - // We must call simplify() here: under Cygwin, any path starting with - // a double slash simply doesn't work. But, if dataDir is "/", we end - // up with paths that start with "//" unless we call simplify(). - // - string absDataDirWithSlash = simplify(absDataDir + '/'); - - for(p = fileSeq.begin(); p != fileSeq.end(); ++p) - { - if(p->compare(0, absDataDirWithSlash.size(), absDataDirWithSlash) != 0) - { - throw "`" + *p + "' is not a path in `" + dataDir + "'"; - } - - p->erase(0, absDataDirWithSlash.size()); - } + string cwd; + if(OS::getcwd(cwd) != 0) + { + throw "cannot get the current directory:\n" + lastError(); + } + + if(!isAbsolute(absDataDir)) + { + absDataDir = simplify(cwd + '/' + absDataDir); + } + + for(p = fileSeq.begin(); p != fileSeq.end(); ++p) + { + if(!isAbsolute(*p)) + { + *p = cwd + '/' + *p; + } + } + + // + // We must call simplify() here: under Cygwin, any path starting with + // a double slash simply doesn't work. But, if dataDir is "/", we end + // up with paths that start with "//" unless we call simplify(). + // + string absDataDirWithSlash = simplify(absDataDir + '/'); + + for(p = fileSeq.begin(); p != fileSeq.end(); ++p) + { + if(p->compare(0, absDataDirWithSlash.size(), absDataDirWithSlash) != 0) + { + throw "`" + *p + "' is not a path in `" + dataDir + "'"; + } + + p->erase(0, absDataDirWithSlash.size()); + } - FileInfoSeq infoSeq; - - if(fileSeq.empty()) - { - CalcCB calcCB; - if(!getFileInfoSeq(absDataDir, compress, verbose ? &calcCB : 0, infoSeq)) - { - return EXIT_FAILURE; - } - } - else - { - loadFileInfoSeq(absDataDir, infoSeq); - - for(p = fileSeq.begin(); p != fileSeq.end(); ++p) - { - FileInfoSeq partialInfoSeq; - - CalcCB calcCB; - if(!getFileInfoSeqSubDir(absDataDir, *p, compress, verbose ? &calcCB : 0, partialInfoSeq)) - { - return EXIT_FAILURE; - } - - FileInfoSeq newInfoSeq; - newInfoSeq.reserve(infoSeq.size()); - - set_difference(infoSeq.begin(), - infoSeq.end(), - partialInfoSeq.begin(), - partialInfoSeq.end(), - back_inserter(newInfoSeq), - FileInfoPathLess()); - - infoSeq.swap(newInfoSeq); - - newInfoSeq.clear(); - newInfoSeq.reserve(infoSeq.size() + partialInfoSeq.size()); - - set_union(infoSeq.begin(), - infoSeq.end(), - partialInfoSeq.begin(), - partialInfoSeq.end(), - back_inserter(newInfoSeq), - FileInfoPathLess()); - - infoSeq.swap(newInfoSeq); - } - } - - if(caseInsensitive) - { - FileInfoSeq newInfoSeq = infoSeq; - sort(newInfoSeq.begin(), newInfoSeq.end(), IFileInfoPathLess()); - - string ex; - FileInfoSeq::iterator p = newInfoSeq.begin(); - while((p = adjacent_find(p, newInfoSeq.end(), IFileInfoPathEqual())) != newInfoSeq.end()) - { - do - { - ex += '\n' + dataDir + '/' + p->path; - ++p; - } - while(p < newInfoSeq.end() && IFileInfoPathEqual()(*(p - 1), *p)); - } - - if(!ex.empty()) - { - ex = "duplicate files:" + ex; - throw ex; - } - } - - saveFileInfoSeq(absDataDir, infoSeq); + FileInfoSeq infoSeq; + + if(fileSeq.empty()) + { + CalcCB calcCB; + if(!getFileInfoSeq(absDataDir, compress, verbose ? &calcCB : 0, infoSeq)) + { + return EXIT_FAILURE; + } + } + else + { + loadFileInfoSeq(absDataDir, infoSeq); + + for(p = fileSeq.begin(); p != fileSeq.end(); ++p) + { + FileInfoSeq partialInfoSeq; + + CalcCB calcCB; + if(!getFileInfoSeqSubDir(absDataDir, *p, compress, verbose ? &calcCB : 0, partialInfoSeq)) + { + return EXIT_FAILURE; + } + + FileInfoSeq newInfoSeq; + newInfoSeq.reserve(infoSeq.size()); + + set_difference(infoSeq.begin(), + infoSeq.end(), + partialInfoSeq.begin(), + partialInfoSeq.end(), + back_inserter(newInfoSeq), + FileInfoPathLess()); + + infoSeq.swap(newInfoSeq); + + newInfoSeq.clear(); + newInfoSeq.reserve(infoSeq.size() + partialInfoSeq.size()); + + set_union(infoSeq.begin(), + infoSeq.end(), + partialInfoSeq.begin(), + partialInfoSeq.end(), + back_inserter(newInfoSeq), + FileInfoPathLess()); + + infoSeq.swap(newInfoSeq); + } + } + + if(caseInsensitive) + { + FileInfoSeq newInfoSeq = infoSeq; + sort(newInfoSeq.begin(), newInfoSeq.end(), IFileInfoPathLess()); + + string ex; + FileInfoSeq::iterator p = newInfoSeq.begin(); + while((p = adjacent_find(p, newInfoSeq.end(), IFileInfoPathEqual())) != newInfoSeq.end()) + { + do + { + ex += '\n' + dataDir + '/' + p->path; + ++p; + } + while(p < newInfoSeq.end() && IFileInfoPathEqual()(*(p - 1), *p)); + } + + if(!ex.empty()) + { + ex = "duplicate files:" + ex; + throw ex; + } + } + + saveFileInfoSeq(absDataDir, infoSeq); } catch(const string& ex) { diff --git a/cpp/src/IcePatch2/Client.cpp b/cpp/src/IcePatch2/Client.cpp index 50cbbd474b1..5bdbf0c0e10 100644 --- a/cpp/src/IcePatch2/Client.cpp +++ b/cpp/src/IcePatch2/Client.cpp @@ -28,137 +28,137 @@ class TextPatcherFeedback : public PatcherFeedback public: TextPatcherFeedback() : - _pressAnyKeyMessage(false) + _pressAnyKeyMessage(false) { #ifndef _WIN32 - tcgetattr(0, &_savedTerm); - _savedFlags = fcntl(0, F_GETFL); - _block = true; + tcgetattr(0, &_savedTerm); + _savedFlags = fcntl(0, F_GETFL); + _block = true; #endif } virtual ~TextPatcherFeedback() { #ifndef _WIN32 - tcsetattr(0, TCSANOW, &_savedTerm); - fcntl(0, F_SETFL, _savedFlags); + tcsetattr(0, TCSANOW, &_savedTerm); + fcntl(0, F_SETFL, _savedFlags); #endif } virtual bool noFileSummary(const string& reason) { - cout << "Cannot load file summary:\n" << reason << endl; - string answer; - do - { - cout << "Do a thorough patch? (yes/no)" << endl; - cin >> answer; - transform(answer.begin(), answer.end(), answer.begin(), ::tolower); - if(answer == "no") - { - return false; - } - } - while(answer != "yes"); - return true; + cout << "Cannot load file summary:\n" << reason << endl; + string answer; + do + { + cout << "Do a thorough patch? (yes/no)" << endl; + cin >> answer; + transform(answer.begin(), answer.end(), answer.begin(), ::tolower); + if(answer == "no") + { + return false; + } + } + while(answer != "yes"); + return true; } virtual bool checksumStart() { - if(!_pressAnyKeyMessage) - { - cout << "[Press any key to abort]" << endl; - _pressAnyKeyMessage = true; - } + if(!_pressAnyKeyMessage) + { + cout << "[Press any key to abort]" << endl; + _pressAnyKeyMessage = true; + } - return !keyPressed(); + return !keyPressed(); } virtual bool checksumProgress(const string& path) { - cout << "Calculating checksum for " << getBasename(path) << endl; - return !keyPressed(); + cout << "Calculating checksum for " << getBasename(path) << endl; + return !keyPressed(); } virtual bool checksumEnd() { - return !keyPressed(); + return !keyPressed(); } virtual bool fileListStart() { - if(!_pressAnyKeyMessage) - { - cout << "[Press any key to abort]" << endl; - _pressAnyKeyMessage = true; - } - - _lastProgress = "0%"; - cout << "Getting list of files to patch: " << _lastProgress << flush; - return !keyPressed(); + if(!_pressAnyKeyMessage) + { + cout << "[Press any key to abort]" << endl; + _pressAnyKeyMessage = true; + } + + _lastProgress = "0%"; + cout << "Getting list of files to patch: " << _lastProgress << flush; + return !keyPressed(); } virtual bool fileListProgress(Int percent) { - for(unsigned int i = 0; i < _lastProgress.size(); ++i) - { - cout << '\b'; - } - ostringstream s; - s << percent << '%'; - _lastProgress = s.str(); - cout << _lastProgress << flush; - return !keyPressed(); + for(unsigned int i = 0; i < _lastProgress.size(); ++i) + { + cout << '\b'; + } + ostringstream s; + s << percent << '%'; + _lastProgress = s.str(); + cout << _lastProgress << flush; + return !keyPressed(); } virtual bool fileListEnd() { - cout << endl; - return !keyPressed(); + cout << endl; + return !keyPressed(); } virtual bool patchStart(const string& path, Long size, Long totalProgress, Long totalSize) { - if(!_pressAnyKeyMessage) - { - cout << "[Press any key to abort]" << endl; - _pressAnyKeyMessage = true; - } - - ostringstream s; - s << "0/" << size << " (" << totalProgress << '/' << totalSize << ')'; - _lastProgress = s.str(); - cout << getBasename(path) << ' ' << _lastProgress << flush; - return !keyPressed(); + if(!_pressAnyKeyMessage) + { + cout << "[Press any key to abort]" << endl; + _pressAnyKeyMessage = true; + } + + ostringstream s; + s << "0/" << size << " (" << totalProgress << '/' << totalSize << ')'; + _lastProgress = s.str(); + cout << getBasename(path) << ' ' << _lastProgress << flush; + return !keyPressed(); } virtual bool patchProgress(Long progress, Long size, Long totalProgress, Long totalSize) { - for(unsigned int i = 0; i < _lastProgress.size(); ++i) - { - cout << '\b'; - } - ostringstream s; - s << progress << '/' << size << " (" << totalProgress << '/' << totalSize << ')'; - _lastProgress = s.str(); - cout << _lastProgress << flush; - return !keyPressed(); + for(unsigned int i = 0; i < _lastProgress.size(); ++i) + { + cout << '\b'; + } + ostringstream s; + s << progress << '/' << size << " (" << totalProgress << '/' << totalSize << ')'; + _lastProgress = s.str(); + cout << _lastProgress << flush; + return !keyPressed(); } virtual bool patchEnd() { - cout << endl; - return !keyPressed(); + cout << endl; + return !keyPressed(); } private: @@ -168,17 +168,17 @@ private: bool keyPressed() { - bool pressed = false; + bool pressed = false; #ifdef __BCPLUSPLUS__ - while(kbhit()) + while(kbhit()) #else - while(_kbhit()) + while(_kbhit()) #endif - { - pressed = true; - _getch(); - } - return pressed; + { + pressed = true; + _getch(); + } + return pressed; } #else @@ -186,29 +186,29 @@ private: bool keyPressed() { - if(_block) - { - termios term; - memcpy(&term, &_savedTerm, sizeof(termios)); - term.c_lflag &= ~(ECHO | ICANON); - term.c_cc[VTIME] = 0; - term.c_cc[VMIN] = 1; - tcsetattr(0, TCSANOW, &term); - - int flags = _savedFlags; - flags |= O_NONBLOCK; - fcntl(0, F_SETFL, flags); - - _block = false; - } - - bool pressed = false; - char c; - while(read(0, &c, 1) > 0) - { - pressed = true; - } - return pressed; + if(_block) + { + termios term; + memcpy(&term, &_savedTerm, sizeof(termios)); + term.c_lflag &= ~(ECHO | ICANON); + term.c_cc[VTIME] = 0; + term.c_cc[VMIN] = 1; + tcsetattr(0, TCSANOW, &term); + + int flags = _savedFlags; + flags |= O_NONBLOCK; + fcntl(0, F_SETFL, flags); + + _block = false; + } + + bool pressed = false; + char c; + while(read(0, &c, 1) > 0) + { + pressed = true; + } + return pressed; } termios _savedTerm; @@ -245,59 +245,59 @@ Client::run(int argc, char* argv[]) vector<string> args; try { - args = opts.parse(argc, (const char**)argv); + args = opts.parse(argc, (const char**)argv); } catch(const IceUtil::BadOptException& e) { cerr << e.reason << endl; - usage(argv[0]); - return EXIT_FAILURE; + usage(argv[0]); + return EXIT_FAILURE; } if(opts.isSet("help")) { - usage(argv[0]); - return EXIT_SUCCESS; + usage(argv[0]); + return EXIT_SUCCESS; } if(opts.isSet("version")) { - cout << ICE_STRING_VERSION << endl; - return EXIT_SUCCESS; + cout << ICE_STRING_VERSION << endl; + return EXIT_SUCCESS; } if(opts.isSet("thorough")) { - properties->setProperty("IcePatch2.Thorough", "1"); + properties->setProperty("IcePatch2.Thorough", "1"); } if(args.size() > 1) { - cerr << argv[0] << ": too many arguments" << endl; - usage(argv[0]); - return EXIT_FAILURE; + cerr << argv[0] << ": too many arguments" << endl; + usage(argv[0]); + return EXIT_FAILURE; } if(args.size() == 1) { - properties->setProperty("IcePatch2.Directory", simplify(args[0])); + properties->setProperty("IcePatch2.Directory", simplify(args[0])); } bool aborted = false; try { - PatcherFeedbackPtr feedback = new TextPatcherFeedback; - PatcherPtr patcher = new Patcher(communicator(), feedback); + PatcherFeedbackPtr feedback = new TextPatcherFeedback; + PatcherPtr patcher = new Patcher(communicator(), feedback); - aborted = !patcher->prepare(); + aborted = !patcher->prepare(); - if(!aborted) - { - aborted = !patcher->patch(""); - } + if(!aborted) + { + aborted = !patcher->patch(""); + } - if(!aborted) - { - patcher->finish(); - } + if(!aborted) + { + patcher->finish(); + } } catch(const string& ex) { @@ -307,12 +307,12 @@ Client::run(int argc, char* argv[]) if(aborted) { - cout << "\n[Aborted]" << endl; - return EXIT_FAILURE; + cout << "\n[Aborted]" << endl; + return EXIT_FAILURE; } else { - return EXIT_SUCCESS; + return EXIT_SUCCESS; } } @@ -320,10 +320,10 @@ void Client::usage(const string& appName) { string options = - "Options:\n" - "-h, --help Show this message.\n" - "-v, --version Display the Ice version.\n" - "-t, --thorough Recalculate all checksums."; + "Options:\n" + "-h, --help Show this message.\n" + "-v, --version Display the Ice version.\n" + "-t, --thorough Recalculate all checksums."; cerr << "Usage: " << appName << " [options] [DIR]" << endl; cerr << options << endl; diff --git a/cpp/src/IcePatch2/ClientUtil.cpp b/cpp/src/IcePatch2/ClientUtil.cpp index fd986e90d29..64b3f472bed 100755 --- a/cpp/src/IcePatch2/ClientUtil.cpp +++ b/cpp/src/IcePatch2/ClientUtil.cpp @@ -30,107 +30,107 @@ class Decompressor : public IceUtil::Thread, public IceUtil::Monitor<IceUtil::Mu public: Decompressor(const string& dataDir) : - _dataDir(dataDir), - _destroy(false) + _dataDir(dataDir), + _destroy(false) { } virtual ~Decompressor() { - assert(_destroy); + assert(_destroy); } void destroy() { - IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); - _destroy = true; - notify(); + IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); + _destroy = true; + notify(); } void add(const FileInfo& info) { - IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); - if(!_exception.empty()) - { - throw _exception; - } - _files.push_back(info); - notify(); + IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); + if(!_exception.empty()) + { + throw _exception; + } + _files.push_back(info); + notify(); } void exception() const { - IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); - if(!_exception.empty()) - { - throw _exception; - } + IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); + if(!_exception.empty()) + { + throw _exception; + } } void log(FILE* fp) { - IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); + IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); - for(FileInfoSeq::const_iterator p = _filesDone.begin(); p != _filesDone.end(); ++p) - { - if(fputc('+', fp) == EOF || !writeFileInfo(fp, *p)) - { - throw "error writing log file:\n" + lastError(); - } - } + for(FileInfoSeq::const_iterator p = _filesDone.begin(); p != _filesDone.end(); ++p) + { + if(fputc('+', fp) == EOF || !writeFileInfo(fp, *p)) + { + throw "error writing log file:\n" + lastError(); + } + } - _filesDone.clear(); + _filesDone.clear(); } virtual void run() { - FileInfo info; - - while(true) - { - { - IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); - - if(!info.path.empty()) - { - _filesDone.push_back(info); - } - - while(!_destroy && _files.empty()) - { - wait(); - } - - if(!_files.empty()) - { - info = _files.front(); - _files.pop_front(); - } - else - { - return; - } - } - - try - { - decompressFile(_dataDir + '/' + info.path); - setFileFlags(_dataDir + '/' + info.path, info); - remove(_dataDir + '/' + info.path + ".bz2"); - } - catch(const string& ex) - { - IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); - _destroy = true; - _exception = ex; - return; - } - } + FileInfo info; + + while(true) + { + { + IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); + + if(!info.path.empty()) + { + _filesDone.push_back(info); + } + + while(!_destroy && _files.empty()) + { + wait(); + } + + if(!_files.empty()) + { + info = _files.front(); + _files.pop_front(); + } + else + { + return; + } + } + + try + { + decompressFile(_dataDir + '/' + info.path); + setFileFlags(_dataDir + '/' + info.path, info); + remove(_dataDir + '/' + info.path + ".bz2"); + } + catch(const string& ex) + { + IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); + _destroy = true; + _exception = ex; + return; + } + } } private: @@ -158,7 +158,7 @@ IcePatch2::Patcher::Patcher(const CommunicatorPtr& communicator, const PatcherFe const char* endpointsProperty = "Ice.OA.IcePatch2.Endpoints"; string endpoints = properties->getPropertyWithDefault(endpointsProperty, - properties->getProperty("IcePatch2.Endpoints")); + properties->getProperty("IcePatch2.Endpoints")); if(endpoints.empty()) { throw string("property `") + endpointsProperty + "' is not set"; @@ -172,18 +172,18 @@ IcePatch2::Patcher::Patcher(const CommunicatorPtr& communicator, const PatcherFe FileServerPrx server = FileServerPrx::checkedCast(serverBase); if(!server) { - throw "proxy `" + communicator->identityToString(id) + ':' + endpoints + "' is not a file server."; + throw "proxy `" + communicator->identityToString(id) + ':' + endpoints + "' is not a file server."; } init(server); } IcePatch2::Patcher::Patcher(const FileServerPrx& server, - const PatcherFeedbackPtr& feedback, - const string& dataDir, - bool thorough, - Ice::Int chunkSize, - Ice::Int remove) : + const PatcherFeedbackPtr& feedback, + const string& dataDir, + bool thorough, + Ice::Int chunkSize, + Ice::Int remove) : _feedback(feedback), _dataDir(simplify(dataDir)), _thorough(thorough), @@ -202,26 +202,26 @@ class PatcherGetFileInfoSeqCB : public GetFileInfoSeqCB public: PatcherGetFileInfoSeqCB(const PatcherFeedbackPtr& feedback) : - _feedback(feedback) + _feedback(feedback) { } virtual bool remove(const string&) { - return true; + return true; } virtual bool checksum(const string& path) { - return _feedback->checksumProgress(path); + return _feedback->checksumProgress(path); } virtual bool compress(const string&) { - assert(false); // Nothing must get compressed when we are patching. - return true; + assert(false); // Nothing must get compressed when we are patching. + return true; } private: @@ -234,50 +234,50 @@ class AMIGetFileInfoSeq : public AMI_FileServer_getFileInfoSeq, public IceUtil:: public: AMIGetFileInfoSeq() : - _done(false) + _done(false) { } FileInfoSeq getFileInfoSeq() { - IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); + IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); - while(!_done) - { - wait(); - } + while(!_done) + { + wait(); + } - _done = false; + _done = false; - if(_exception.get()) - { - auto_ptr<Exception> ex = _exception; - _fileInfoSeq.clear(); - ex->ice_throw(); - } + if(_exception.get()) + { + auto_ptr<Exception> ex = _exception; + _fileInfoSeq.clear(); + ex->ice_throw(); + } - FileInfoSeq fileInfoSeq; - fileInfoSeq.swap(_fileInfoSeq); - return fileInfoSeq; + FileInfoSeq fileInfoSeq; + fileInfoSeq.swap(_fileInfoSeq); + return fileInfoSeq; } virtual void ice_response(const FileInfoSeq& fileInfoSeq) { - IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); - _fileInfoSeq = fileInfoSeq; - _done = true; - notify(); + IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); + _fileInfoSeq = fileInfoSeq; + _done = true; + notify(); } virtual void ice_exception(const Exception& ex) { - IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); - _exception.reset(ex.ice_clone()); - _done = true; - notify(); + IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); + _exception.reset(ex.ice_clone()); + _done = true; + notify(); } private: @@ -298,39 +298,39 @@ IcePatch2::Patcher::prepare() if(!thorough) { - try - { - loadFileInfoSeq(_dataDir, _localFiles); - } - catch(const string& ex) - { - thorough = _feedback->noFileSummary(ex); - if(!thorough) - { - return false; - } - } + try + { + loadFileInfoSeq(_dataDir, _localFiles); + } + catch(const string& ex) + { + thorough = _feedback->noFileSummary(ex); + if(!thorough) + { + return false; + } + } } if(thorough) { - if(!_feedback->checksumStart()) - { - return false; - } + if(!_feedback->checksumStart()) + { + return false; + } - PatcherGetFileInfoSeqCB cb(_feedback); - if(!getFileInfoSeq(_dataDir, 0, &cb, _localFiles)) - { - return false; - } + PatcherGetFileInfoSeqCB cb(_feedback); + if(!getFileInfoSeq(_dataDir, 0, &cb, _localFiles)) + { + return false; + } - if(!_feedback->checksumEnd()) - { - return false; - } + if(!_feedback->checksumEnd()) + { + return false; + } - saveFileInfoSeq(_dataDir, _localFiles); + saveFileInfoSeq(_dataDir, _localFiles); } FileTree0 tree0; @@ -338,124 +338,124 @@ IcePatch2::Patcher::prepare() if(tree0.checksum != _serverCompress->getChecksum()) { - if(!_feedback->fileListStart()) - { - return false; - } - - ByteSeqSeq checksumSeq = _serverCompress->getChecksumSeq(); - if(checksumSeq.size() != 256) - { - throw string("server returned illegal value"); - } - - AMIGetFileInfoSeqPtr curCB; - AMIGetFileInfoSeqPtr nxtCB; - - for(int node0 = 0; node0 < 256; ++node0) - { - if(tree0.nodes[node0].checksum != checksumSeq[node0]) - { - if(!curCB) - { - assert(!nxtCB); - curCB = new AMIGetFileInfoSeq; - nxtCB = new AMIGetFileInfoSeq; - _serverCompress->getFileInfoSeq_async(curCB, node0); - } - else - { - assert(nxtCB); - swap(nxtCB, curCB); - } - - int node0Nxt = node0; - - do - { - ++node0Nxt; - } - while(node0Nxt < 256 && tree0.nodes[node0Nxt].checksum == checksumSeq[node0Nxt]); - - if(node0Nxt < 256) - { - _serverCompress->getFileInfoSeq_async(nxtCB, node0Nxt); - } - - FileInfoSeq files = curCB->getFileInfoSeq(); - - sort(files.begin(), files.end(), FileInfoLess()); - files.erase(unique(files.begin(), files.end(), FileInfoEqual()), files.end()); - - // - // Compute the set of files which were removed. - // - set_difference(tree0.nodes[node0].files.begin(), - tree0.nodes[node0].files.end(), - files.begin(), - files.end(), - back_inserter(_removeFiles), - FileInfoWithoutFlagsLess()); // NOTE: We ignore the flags here. - - // - // Compute the set of files which were updated (either the file contents, flags or both). - // - FileInfoSeq updatedFiles; - updatedFiles.reserve(files.size()); - - set_difference(files.begin(), - files.end(), - tree0.nodes[node0].files.begin(), - tree0.nodes[node0].files.end(), - back_inserter(updatedFiles), - FileInfoLess()); - - // - // Compute the set of files whose contents was updated. - // - FileInfoSeq contentsUpdatedFiles; - contentsUpdatedFiles.reserve(files.size()); - - set_difference(files.begin(), - files.end(), - tree0.nodes[node0].files.begin(), - tree0.nodes[node0].files.end(), - back_inserter(contentsUpdatedFiles), - FileInfoWithoutFlagsLess()); // NOTE: We ignore the flags here. - copy(contentsUpdatedFiles.begin(), contentsUpdatedFiles.end(), back_inserter(_updateFiles)); - - // - // Compute the set of files whose flags were updated. - // - set_difference(updatedFiles.begin(), - updatedFiles.end(), - contentsUpdatedFiles.begin(), - contentsUpdatedFiles.end(), - back_inserter(_updateFlags), - FileInfoLess()); - } - - if(!_feedback->fileListProgress((node0 + 1) * 100 / 256)) - { - return false; - } - } - - if(!_feedback->fileListEnd()) - { - return false; - } + if(!_feedback->fileListStart()) + { + return false; + } + + ByteSeqSeq checksumSeq = _serverCompress->getChecksumSeq(); + if(checksumSeq.size() != 256) + { + throw string("server returned illegal value"); + } + + AMIGetFileInfoSeqPtr curCB; + AMIGetFileInfoSeqPtr nxtCB; + + for(int node0 = 0; node0 < 256; ++node0) + { + if(tree0.nodes[node0].checksum != checksumSeq[node0]) + { + if(!curCB) + { + assert(!nxtCB); + curCB = new AMIGetFileInfoSeq; + nxtCB = new AMIGetFileInfoSeq; + _serverCompress->getFileInfoSeq_async(curCB, node0); + } + else + { + assert(nxtCB); + swap(nxtCB, curCB); + } + + int node0Nxt = node0; + + do + { + ++node0Nxt; + } + while(node0Nxt < 256 && tree0.nodes[node0Nxt].checksum == checksumSeq[node0Nxt]); + + if(node0Nxt < 256) + { + _serverCompress->getFileInfoSeq_async(nxtCB, node0Nxt); + } + + FileInfoSeq files = curCB->getFileInfoSeq(); + + sort(files.begin(), files.end(), FileInfoLess()); + files.erase(unique(files.begin(), files.end(), FileInfoEqual()), files.end()); + + // + // Compute the set of files which were removed. + // + set_difference(tree0.nodes[node0].files.begin(), + tree0.nodes[node0].files.end(), + files.begin(), + files.end(), + back_inserter(_removeFiles), + FileInfoWithoutFlagsLess()); // NOTE: We ignore the flags here. + + // + // Compute the set of files which were updated (either the file contents, flags or both). + // + FileInfoSeq updatedFiles; + updatedFiles.reserve(files.size()); + + set_difference(files.begin(), + files.end(), + tree0.nodes[node0].files.begin(), + tree0.nodes[node0].files.end(), + back_inserter(updatedFiles), + FileInfoLess()); + + // + // Compute the set of files whose contents was updated. + // + FileInfoSeq contentsUpdatedFiles; + contentsUpdatedFiles.reserve(files.size()); + + set_difference(files.begin(), + files.end(), + tree0.nodes[node0].files.begin(), + tree0.nodes[node0].files.end(), + back_inserter(contentsUpdatedFiles), + FileInfoWithoutFlagsLess()); // NOTE: We ignore the flags here. + copy(contentsUpdatedFiles.begin(), contentsUpdatedFiles.end(), back_inserter(_updateFiles)); + + // + // Compute the set of files whose flags were updated. + // + set_difference(updatedFiles.begin(), + updatedFiles.end(), + contentsUpdatedFiles.begin(), + contentsUpdatedFiles.end(), + back_inserter(_updateFlags), + FileInfoLess()); + } + + if(!_feedback->fileListProgress((node0 + 1) * 100 / 256)) + { + return false; + } + } + + if(!_feedback->fileListEnd()) + { + return false; + } } sort(_removeFiles.begin(), _removeFiles.end(), FileInfoLess()); sort(_updateFiles.begin(), _updateFiles.end(), FileInfoLess()); sort(_updateFlags.begin(), _updateFlags.end(), FileInfoLess()); - + string pathLog = simplify(_dataDir + '/' + logFile); _log = OS::fopen(pathLog, "w"); if(!_log) { - throw "cannot open `" + pathLog + "' for writing:\n" + lastError(); + throw "cannot open `" + pathLog + "' for writing:\n" + lastError(); } return true; @@ -468,102 +468,102 @@ IcePatch2::Patcher::patch(const string& d) if(dir.empty() || dir == ".") { - if(!_removeFiles.empty()) - { - if(!removeFiles(_removeFiles)) - { - return false; - } - } - - if(!_updateFiles.empty()) - { - if(!updateFiles(_updateFiles)) - { - return false; - } - } - - if(!_updateFlags.empty()) - { - if(!updateFlags(_updateFlags)) - { - return false; - } - } - - return true; + if(!_removeFiles.empty()) + { + if(!removeFiles(_removeFiles)) + { + return false; + } + } + + if(!_updateFiles.empty()) + { + if(!updateFiles(_updateFiles)) + { + return false; + } + } + + if(!_updateFlags.empty()) + { + if(!updateFlags(_updateFlags)) + { + return false; + } + } + + return true; } else { - string dirWithSlash = simplify(dir + '/'); - - FileInfoSeq::const_iterator p; - - FileInfoSeq remove; - for(p = _removeFiles.begin(); p != _removeFiles.end(); ++p) - { - if(p->path == dir) - { - remove.push_back(*p); - } - else if(p->path.compare(0, dirWithSlash.size(), dirWithSlash) == 0) - { - remove.push_back(*p); - } - } - - FileInfoSeq update; - for(p = _updateFiles.begin(); p != _updateFiles.end(); ++p) - { - if(p->path == dir) - { - update.push_back(*p); - } - else if(p->path.compare(0, dirWithSlash.size(), dirWithSlash) == 0) - { - update.push_back(*p); - } - } - - FileInfoSeq updateFlag; - for(p = _updateFlags.begin(); p != _updateFlags.end(); ++p) - { - if(p->path == dir) - { - updateFlag.push_back(*p); - } - else if(p->path.compare(0, dirWithSlash.size(), dirWithSlash) == 0) - { - updateFlag.push_back(*p); - } - } - - if(!remove.empty()) - { - if(!removeFiles(remove)) - { - return false; - } - } - - if(!update.empty()) - { - if(!updateFiles(update)) - { - return false; - } - } - - if(!updateFlag.empty()) - { - if(!updateFlags(updateFlag)) - { - return false; - } - } - - return true; + string dirWithSlash = simplify(dir + '/'); + + FileInfoSeq::const_iterator p; + + FileInfoSeq remove; + for(p = _removeFiles.begin(); p != _removeFiles.end(); ++p) + { + if(p->path == dir) + { + remove.push_back(*p); + } + else if(p->path.compare(0, dirWithSlash.size(), dirWithSlash) == 0) + { + remove.push_back(*p); + } + } + + FileInfoSeq update; + for(p = _updateFiles.begin(); p != _updateFiles.end(); ++p) + { + if(p->path == dir) + { + update.push_back(*p); + } + else if(p->path.compare(0, dirWithSlash.size(), dirWithSlash) == 0) + { + update.push_back(*p); + } + } + + FileInfoSeq updateFlag; + for(p = _updateFlags.begin(); p != _updateFlags.end(); ++p) + { + if(p->path == dir) + { + updateFlag.push_back(*p); + } + else if(p->path.compare(0, dirWithSlash.size(), dirWithSlash) == 0) + { + updateFlag.push_back(*p); + } + } + + if(!remove.empty()) + { + if(!removeFiles(remove)) + { + return false; + } + } + + if(!update.empty()) + { + if(!updateFiles(update)) + { + return false; + } + } + + if(!updateFlag.empty()) + { + if(!updateFlags(updateFlag)) + { + return false; + } + } + + return true; } } @@ -572,8 +572,8 @@ IcePatch2::Patcher::finish() { if(_log != 0) { - fclose(_log); - _log = 0; + fclose(_log); + _log = 0; } saveFileInfoSeq(_dataDir, _localFiles); @@ -584,7 +584,7 @@ IcePatch2::Patcher::init(const FileServerPrx& server) { if(_dataDir.empty()) { - throw string("no data directory specified"); + throw string("no data directory specified"); } // @@ -595,7 +595,7 @@ IcePatch2::Patcher::init(const FileServerPrx& server) server->ice_getCommunicator()->getProperties()->getPropertyAsIntWithDefault("Ice.MessageSizeMax", 1024); if(_chunkSize < 1) { - const_cast<Int&>(_chunkSize) = 1; + const_cast<Int&>(_chunkSize) = 1; } else if(_chunkSize > sizeMax) { @@ -607,19 +607,19 @@ IcePatch2::Patcher::init(const FileServerPrx& server) } else { - const_cast<Int&>(_chunkSize) *= 1024; + const_cast<Int&>(_chunkSize) *= 1024; } if(!isAbsolute(_dataDir)) { - string cwd; - if(OS::getcwd(cwd) != 0) - { - throw "cannot get the current directory:\n" + lastError(); - } - const_cast<string&>(_dataDir) = simplify(cwd + '/' + _dataDir); + string cwd; + if(OS::getcwd(cwd) != 0) + { + throw "cannot get the current directory:\n" + lastError(); + } + const_cast<string&>(_dataDir) = simplify(cwd + '/' + _dataDir); } - + const_cast<FileServerPrx&>(_serverCompress) = FileServerPrx::uncheckedCast(server->ice_compress(true)); const_cast<FileServerPrx&>(_serverNoCompress) = FileServerPrx::uncheckedCast(server->ice_compress(false)); } @@ -629,48 +629,48 @@ IcePatch2::Patcher::removeFiles(const FileInfoSeq& files) { if(_remove < 1) { - return true; + return true; } for(FileInfoSeq::const_reverse_iterator p = files.rbegin(); p != files.rend(); ++p) { - try - { - remove(_dataDir + '/' + p->path); - if(fputc('-', _log) == EOF || ! writeFileInfo(_log, *p)) - { - throw "error writing log file:\n" + lastError(); - } - } - catch(...) - { - if(_remove < 2) // We ignore errors if IcePatch2.Remove >= 2. - { - throw; - } - } + try + { + remove(_dataDir + '/' + p->path); + if(fputc('-', _log) == EOF || ! writeFileInfo(_log, *p)) + { + throw "error writing log file:\n" + lastError(); + } + } + catch(...) + { + if(_remove < 2) // We ignore errors if IcePatch2.Remove >= 2. + { + throw; + } + } } FileInfoSeq newLocalFiles; newLocalFiles.reserve(_localFiles.size()); set_difference(_localFiles.begin(), - _localFiles.end(), - files.begin(), - files.end(), - back_inserter(newLocalFiles), - FileInfoLess()); + _localFiles.end(), + files.begin(), + files.end(), + back_inserter(newLocalFiles), + FileInfoLess()); _localFiles.swap(newLocalFiles); FileInfoSeq newRemoveFiles; set_difference(_removeFiles.begin(), - _removeFiles.end(), - files.begin(), - files.end(), - back_inserter(newRemoveFiles), - FileInfoLess()); + _removeFiles.end(), + files.begin(), + files.end(), + back_inserter(newRemoveFiles), + FileInfoLess()); _removeFiles.swap(newRemoveFiles); @@ -694,14 +694,14 @@ IcePatch2::Patcher::updateFiles(const FileInfoSeq& files) try { - result = updateFilesInternal(files, decompressor); + result = updateFilesInternal(files, decompressor); } catch(...) { - decompressor->destroy(); - decompressor->getThreadControl().join(); - decompressor->log(_log); - throw; + decompressor->destroy(); + decompressor->getThreadControl().join(); + decompressor->log(_log); + throw; } decompressor->destroy(); @@ -717,50 +717,50 @@ class AMIGetFileCompressed : public AMI_FileServer_getFileCompressed, public Ice public: AMIGetFileCompressed() : - _done(false) + _done(false) { } ByteSeq getFileCompressed() { - IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); + IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); - while(!_done) - { - wait(); - } + while(!_done) + { + wait(); + } - _done = false; + _done = false; - if(_exception.get()) - { - auto_ptr<Exception> ex = _exception; - _bytes.clear(); - ex->ice_throw(); - } + if(_exception.get()) + { + auto_ptr<Exception> ex = _exception; + _bytes.clear(); + ex->ice_throw(); + } - ByteSeq bytes; - bytes.swap(_bytes); - return bytes; + ByteSeq bytes; + bytes.swap(_bytes); + return bytes; } virtual void ice_response(const pair<const Ice::Byte*, const Ice::Byte*>& bytes) { - IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); - ByteSeq(bytes.first, bytes.second).swap(_bytes); - _done = true; - notify(); + IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); + ByteSeq(bytes.first, bytes.second).swap(_bytes); + _done = true; + notify(); } virtual void ice_exception(const Exception& ex) { - IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); - _exception.reset(ex.ice_clone()); - _done = true; - notify(); + IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this); + _exception.reset(ex.ice_clone()); + _done = true; + notify(); } private: @@ -782,10 +782,10 @@ IcePatch2::Patcher::updateFilesInternal(const FileInfoSeq& files, const Decompre for(p = files.begin(); p != files.end(); ++p) { - if(p->size > 0) // Regular, non-empty file? - { - total += p->size; - } + if(p->size > 0) // Regular, non-empty file? + { + total += p->size; + } } AMIGetFileCompressedPtr curCB; @@ -793,164 +793,164 @@ IcePatch2::Patcher::updateFilesInternal(const FileInfoSeq& files, const Decompre for(p = files.begin(); p != files.end(); ++p) { - if(p->size < 0) // Directory? - { - createDirectoryRecursive(_dataDir + '/' + p->path); - if(fputc('+', _log) == EOF || !writeFileInfo(_log, *p)) - { - throw "error writing log file:\n" + lastError(); - } - } - else // Regular file. - { - if(!_feedback->patchStart(p->path, p->size, updated, total)) - { - return false; - } - - if(p->size == 0) - { - string path = simplify(_dataDir + '/' + p->path); - FILE* fp = OS::fopen(path, "wb"); - if(fp == 0) - { - throw "cannot open `" + path +"' for writing:\n" + lastError(); - } - fclose(fp); - } - else - { - string pathBZ2 = simplify(_dataDir + '/' + p->path + ".bz2"); - - string dir = getDirname(pathBZ2); - if(!dir.empty()) - { - createDirectoryRecursive(dir); - } - - try - { - removeRecursive(pathBZ2); - } - catch(...) - { - } - - FILE* fileBZ2 = OS::fopen(pathBZ2, "wb"); - if(fileBZ2 == 0) - { - throw "cannot open `" + pathBZ2 + "' for writing:\n" + lastError(); - } - - try - { - Int pos = 0; - - while(pos < p->size) - { - if(!curCB) - { - assert(!nxtCB); - curCB = new AMIGetFileCompressed; - nxtCB = new AMIGetFileCompressed; - _serverNoCompress->getFileCompressed_async(curCB, p->path, pos, _chunkSize); - } - else - { - assert(nxtCB); - swap(nxtCB, curCB); - } - - if(pos + _chunkSize < p->size) - { - _serverNoCompress->getFileCompressed_async(nxtCB, p->path, pos + _chunkSize, _chunkSize); - } - else - { - FileInfoSeq::const_iterator q = p + 1; - - while(q != files.end() && q->size <= 0) - { - ++q; - } - - if(q != files.end()) - { - _serverNoCompress->getFileCompressed_async(nxtCB, q->path, 0, _chunkSize); - } - } - - ByteSeq bytes; - - try - { - bytes = curCB->getFileCompressed(); - } - catch(const FileAccessException& ex) - { - throw "error from IcePatch2 server for `" + p->path + "': " + ex.reason; - } - - if(bytes.empty()) - { - throw "size mismatch for `" + p->path + "'"; - } - - if(fwrite(reinterpret_cast<char*>(&bytes[0]), bytes.size(), 1, fileBZ2) != 1) - { - throw ": cannot write `" + pathBZ2 + "':\n" + lastError(); - } - - pos += static_cast<int>(bytes.size()); - updated += bytes.size(); - - if(!_feedback->patchProgress(pos, p->size, updated, total)) - { - fclose(fileBZ2); - return false; - } - } - } - catch(...) - { - fclose(fileBZ2); - throw; - } - - fclose(fileBZ2); - - decompressor->log(_log); - decompressor->add(*p); - } - - if(!_feedback->patchEnd()) - { - return false; - } - } + if(p->size < 0) // Directory? + { + createDirectoryRecursive(_dataDir + '/' + p->path); + if(fputc('+', _log) == EOF || !writeFileInfo(_log, *p)) + { + throw "error writing log file:\n" + lastError(); + } + } + else // Regular file. + { + if(!_feedback->patchStart(p->path, p->size, updated, total)) + { + return false; + } + + if(p->size == 0) + { + string path = simplify(_dataDir + '/' + p->path); + FILE* fp = OS::fopen(path, "wb"); + if(fp == 0) + { + throw "cannot open `" + path +"' for writing:\n" + lastError(); + } + fclose(fp); + } + else + { + string pathBZ2 = simplify(_dataDir + '/' + p->path + ".bz2"); + + string dir = getDirname(pathBZ2); + if(!dir.empty()) + { + createDirectoryRecursive(dir); + } + + try + { + removeRecursive(pathBZ2); + } + catch(...) + { + } + + FILE* fileBZ2 = OS::fopen(pathBZ2, "wb"); + if(fileBZ2 == 0) + { + throw "cannot open `" + pathBZ2 + "' for writing:\n" + lastError(); + } + + try + { + Int pos = 0; + + while(pos < p->size) + { + if(!curCB) + { + assert(!nxtCB); + curCB = new AMIGetFileCompressed; + nxtCB = new AMIGetFileCompressed; + _serverNoCompress->getFileCompressed_async(curCB, p->path, pos, _chunkSize); + } + else + { + assert(nxtCB); + swap(nxtCB, curCB); + } + + if(pos + _chunkSize < p->size) + { + _serverNoCompress->getFileCompressed_async(nxtCB, p->path, pos + _chunkSize, _chunkSize); + } + else + { + FileInfoSeq::const_iterator q = p + 1; + + while(q != files.end() && q->size <= 0) + { + ++q; + } + + if(q != files.end()) + { + _serverNoCompress->getFileCompressed_async(nxtCB, q->path, 0, _chunkSize); + } + } + + ByteSeq bytes; + + try + { + bytes = curCB->getFileCompressed(); + } + catch(const FileAccessException& ex) + { + throw "error from IcePatch2 server for `" + p->path + "': " + ex.reason; + } + + if(bytes.empty()) + { + throw "size mismatch for `" + p->path + "'"; + } + + if(fwrite(reinterpret_cast<char*>(&bytes[0]), bytes.size(), 1, fileBZ2) != 1) + { + throw ": cannot write `" + pathBZ2 + "':\n" + lastError(); + } + + pos += static_cast<int>(bytes.size()); + updated += bytes.size(); + + if(!_feedback->patchProgress(pos, p->size, updated, total)) + { + fclose(fileBZ2); + return false; + } + } + } + catch(...) + { + fclose(fileBZ2); + throw; + } + + fclose(fileBZ2); + + decompressor->log(_log); + decompressor->add(*p); + } + + if(!_feedback->patchEnd()) + { + return false; + } + } } FileInfoSeq newLocalFiles; newLocalFiles.reserve(_localFiles.size()); - + set_union(_localFiles.begin(), - _localFiles.end(), - files.begin(), - files.end(), - back_inserter(newLocalFiles), - FileInfoLess()); - + _localFiles.end(), + files.begin(), + files.end(), + back_inserter(newLocalFiles), + FileInfoLess()); + _localFiles.swap(newLocalFiles); FileInfoSeq newUpdateFiles; set_difference(_updateFiles.begin(), - _updateFiles.end(), - files.begin(), - files.end(), - back_inserter(newUpdateFiles), - FileInfoLess()); - + _updateFiles.end(), + files.begin(), + files.end(), + back_inserter(newUpdateFiles), + FileInfoLess()); + _updateFiles.swap(newUpdateFiles); return true; @@ -961,10 +961,10 @@ IcePatch2::Patcher::updateFlags(const FileInfoSeq& files) { for(FileInfoSeq::const_iterator p = files.begin(); p != files.end(); ++p) { - if(p->size >= 0) // Regular file? - { - setFileFlags(_dataDir + '/' + p->path, *p); - } + if(p->size >= 0) // Regular file? + { + setFileFlags(_dataDir + '/' + p->path, *p); + } } // @@ -974,32 +974,32 @@ IcePatch2::Patcher::updateFlags(const FileInfoSeq& files) FileInfoSeq localFiles; localFiles.reserve(_localFiles.size()); set_difference(_localFiles.begin(), - _localFiles.end(), - files.begin(), - files.end(), - back_inserter(localFiles), - FileInfoWithoutFlagsLess()); // NOTE: We ignore the flags. + _localFiles.end(), + files.begin(), + files.end(), + back_inserter(localFiles), + FileInfoWithoutFlagsLess()); // NOTE: We ignore the flags. // // Add the new files to the set of local file. // _localFiles.clear(); set_union(localFiles.begin(), - localFiles.end(), - files.begin(), - files.end(), - back_inserter(_localFiles), - FileInfoLess()); + localFiles.end(), + files.begin(), + files.end(), + back_inserter(_localFiles), + FileInfoLess()); FileInfoSeq newUpdateFlags; set_difference(_updateFlags.begin(), - _updateFlags.end(), - files.begin(), - files.end(), - back_inserter(newUpdateFlags), - FileInfoLess()); - + _updateFlags.end(), + files.begin(), + files.end(), + back_inserter(newUpdateFlags), + FileInfoLess()); + _updateFlags.swap(newUpdateFlags); return true; diff --git a/cpp/src/IcePatch2/FileServerI.cpp b/cpp/src/IcePatch2/FileServerI.cpp index 3c647faf16c..c8fdcc2366c 100644 --- a/cpp/src/IcePatch2/FileServerI.cpp +++ b/cpp/src/IcePatch2/FileServerI.cpp @@ -35,7 +35,7 @@ IcePatch2::FileServerI::getFileInfoSeq(Int node0, const Current&) const { if(node0 < 0 || node0 > 255) { - throw PartitionOutOfRangeException(); + throw PartitionOutOfRangeException(); } return _tree0.nodes[node0].files; @@ -48,7 +48,7 @@ IcePatch2::FileServerI::getChecksumSeq(const Current&) const for(int node0 = 0; node0 < 256; ++node0) { - checksums[node0] = _tree0.nodes[node0].checksum; + checksums[node0] = _tree0.nodes[node0].checksum; } return checksums; @@ -62,14 +62,14 @@ IcePatch2::FileServerI::getChecksum(const Current&) const void IcePatch2::FileServerI::getFileCompressed_async(const AMD_FileServer_getFileCompressedPtr& cb, - const string& pa, Int pos, Int num, const Current&) const + const string& pa, Int pos, Int num, const Current&) const { if(isAbsolute(pa)) { - FileAccessException ex; - ex.reason = "illegal absolute path `" + pa + "'"; - cb->ice_exception(ex); - return; + FileAccessException ex; + ex.reason = "illegal absolute path `" + pa + "'"; + cb->ice_exception(ex); + return; } string path = simplify(pa); @@ -78,39 +78,39 @@ IcePatch2::FileServerI::getFileCompressed_async(const AMD_FileServer_getFileComp path.find("/../") != string::npos || path.size() >= 3 && (path.substr(0, 3) == "../" || path.substr(path.size() - 3, 3) == "/..")) { - FileAccessException ex; - ex.reason = "illegal `..' component in path `" + path + "'"; - cb->ice_exception(ex); - return; + FileAccessException ex; + ex.reason = "illegal `..' component in path `" + path + "'"; + cb->ice_exception(ex); + return; } pair<const Byte*, const Byte*> ret(0, 0); if(num <= 0 || pos < 0) - { + { cb->ice_response(ret); - return; + return; } int fd = OS::open(_dataDir + '/' + path + ".bz2", O_RDONLY|O_BINARY); if(fd == -1) { - FileAccessException ex; - ex.reason = "cannot open `" + path + "' for reading: " + strerror(errno); - cb->ice_exception(ex); - return; + FileAccessException ex; + ex.reason = "cannot open `" + path + "' for reading: " + strerror(errno); + cb->ice_exception(ex); + return; } if(lseek(fd, static_cast<off_t>(pos), SEEK_SET) != static_cast<off_t>(pos)) { - close(fd); + close(fd); - ostringstream posStr; - posStr << pos; + ostringstream posStr; + posStr << pos; - FileAccessException ex; - ex.reason = "cannot seek position " + posStr.str() + " in file `" + path + "': " + strerror(errno); - cb->ice_exception(ex); - return; + FileAccessException ex; + ex.reason = "cannot seek position " + posStr.str() + " in file `" + path + "': " + strerror(errno); + cb->ice_exception(ex); + return; } IceUtil::ScopedArray<Byte> bytes(new Byte[num]); @@ -122,12 +122,12 @@ IcePatch2::FileServerI::getFileCompressed_async(const AMD_FileServer_getFileComp if((r = read(fd, bytes.get(), static_cast<size_t>(num))) == -1) #endif { - close(fd); + close(fd); - FileAccessException ex; - ex.reason = "cannot read `" + path + "': " + strerror(errno); - cb->ice_exception(ex); - return; + FileAccessException ex; + ex.reason = "cannot read `" + path + "': " + strerror(errno); + cb->ice_exception(ex); + return; } close(fd); diff --git a/cpp/src/IcePatch2/FileServerI.h b/cpp/src/IcePatch2/FileServerI.h index ae02fca4bd2..5c38e33a91b 100644 --- a/cpp/src/IcePatch2/FileServerI.h +++ b/cpp/src/IcePatch2/FileServerI.h @@ -29,7 +29,7 @@ public: Ice::ByteSeq getChecksum(const Ice::Current&) const; void getFileCompressed_async(const AMD_FileServer_getFileCompressedPtr&, const std::string&, Ice::Int pos, - Ice::Int num, const Ice::Current&) const; + Ice::Int num, const Ice::Current&) const; private: diff --git a/cpp/src/IcePatch2/OS.cpp b/cpp/src/IcePatch2/OS.cpp index 33eedc2d0b7..b57249f82de 100644 --- a/cpp/src/IcePatch2/OS.cpp +++ b/cpp/src/IcePatch2/OS.cpp @@ -68,7 +68,7 @@ OS::getcwd(string& cwd) wchar_t cwdbuf[_MAX_PATH]; if(_wgetcwd(cwdbuf, _MAX_PATH) == NULL) { - return -1; + return -1; } cwd = IceUtil::wstringToString(cwdbuf); return 0; @@ -124,7 +124,7 @@ OS::getcwd(string& cwd) char cwdbuf[PATH_MAX]; if(::getcwd(cwdbuf, PATH_MAX) == NULL) { - return -1; + return -1; } cwd = cwdbuf; return 0; diff --git a/cpp/src/IcePatch2/Server.cpp b/cpp/src/IcePatch2/Server.cpp index a89db23f7af..98ac031b21a 100644 --- a/cpp/src/IcePatch2/Server.cpp +++ b/cpp/src/IcePatch2/Server.cpp @@ -25,14 +25,14 @@ class AdminI : public Admin public: AdminI(const CommunicatorPtr& communicator) : - _communicator(communicator) + _communicator(communicator) { } virtual void shutdown(const Current&) { - _communicator->shutdown(); + _communicator->shutdown(); } private: @@ -74,61 +74,61 @@ IcePatch2::PatcherService::start(int argc, char* argv[]) vector<string> args; try { - args = opts.parse(argc, (const char**)argv); + args = opts.parse(argc, (const char**)argv); } catch(const IceUtil::BadOptException& e) { error(e.reason); - usage(argv[0]); - return false; + usage(argv[0]); + return false; } if(opts.isSet("help")) { - usage(argv[0]); - return false; + usage(argv[0]); + return false; } if(opts.isSet("version")) { - print(ICE_STRING_VERSION); - return false; + print(ICE_STRING_VERSION); + return false; } if(args.size() > 1) { - error("too many arguments"); - usage(argv[0]); - return false; + error("too many arguments"); + usage(argv[0]); + return false; } if(args.size() == 1) { - properties->setProperty("IcePatch2.Directory", simplify(args[0])); + properties->setProperty("IcePatch2.Directory", simplify(args[0])); } string dataDir = properties->getPropertyWithDefault("IcePatch2.Directory", "."); if(dataDir.empty()) { - error("no data directory specified"); - usage(argv[0]); - return false; + error("no data directory specified"); + usage(argv[0]); + return false; } FileInfoSeq infoSeq; try { - if(!isAbsolute(dataDir)) - { - string cwd; - if(OS::getcwd(cwd) != 0) - { - throw "cannot get the current directory:\n" + lastError(); - } - - dataDir = cwd + '/' + dataDir; - } - - loadFileInfoSeq(dataDir, infoSeq); + if(!isAbsolute(dataDir)) + { + string cwd; + if(OS::getcwd(cwd) != 0) + { + throw "cannot get the current directory:\n" + lastError(); + } + + dataDir = cwd + '/' + dataDir; + } + + loadFileInfoSeq(dataDir, infoSeq); } catch(const string& ex) { @@ -146,7 +146,7 @@ IcePatch2::PatcherService::start(int argc, char* argv[]) // const string endpointsProperty = "Ice.OA.IcePatch2.Endpoints"; string endpoints = properties->getPropertyWithDefault(endpointsProperty, - properties->getProperty("IcePatch2.Endpoints")); + properties->getProperty("IcePatch2.Endpoints")); if(endpoints.empty()) { error("property `" + endpointsProperty + "' is not set"); @@ -161,7 +161,7 @@ IcePatch2::PatcherService::start(int argc, char* argv[]) if(!properties->getProperty("Ice.OA.IcePatch2.Admin.Endpoints").empty() || !properties->getProperty("IcePatch2.Admin.Endpoints").empty()) { - adminAdapter = communicator()->createObjectAdapter("IcePatch2.Admin"); + adminAdapter = communicator()->createObjectAdapter("IcePatch2.Admin"); } const string instanceNameProperty = "IcePatch2.InstanceName"; @@ -174,16 +174,16 @@ IcePatch2::PatcherService::start(int argc, char* argv[]) if(adminAdapter) { - Identity adminId; - adminId.category = instanceName; - adminId.name = "admin"; - adminAdapter->add(new AdminI(communicator()), adminId); + Identity adminId; + adminId.category = instanceName; + adminId.name = "admin"; + adminAdapter->add(new AdminI(communicator()), adminId); } adapter->activate(); if(adminAdapter) { - adminAdapter->activate(); + adminAdapter->activate(); } return true; @@ -199,30 +199,30 @@ void IcePatch2::PatcherService::usage(const string& appName) { string options = - "Options:\n" - "-h, --help Show this message.\n" - "-v, --version Display the Ice version."; + "Options:\n" + "-h, --help Show this message.\n" + "-v, --version Display the Ice version."; #ifdef _WIN32 if(checkSystem()) { options.append( - "\n" - "\n" - "--service NAME Run as the Windows service NAME.\n" - "\n" - "--install NAME [--display DISP] [--executable EXEC] [args]\n" - " Install as Windows service NAME. If DISP is\n" - " provided, use it as the display name,\n" - " otherwise NAME is used. If EXEC is provided,\n" - " use it as the service executable, otherwise\n" - " this executable is used. Any additional\n" - " arguments are passed unchanged to the\n" - " service at startup.\n" - "--uninstall NAME Uninstall Windows service NAME.\n" - "--start NAME [args] Start Windows service NAME. Any additional\n" - " arguments are passed unchanged to the\n" - " service.\n" - "--stop NAME Stop Windows service NAME." + "\n" + "\n" + "--service NAME Run as the Windows service NAME.\n" + "\n" + "--install NAME [--display DISP] [--executable EXEC] [args]\n" + " Install as Windows service NAME. If DISP is\n" + " provided, use it as the display name,\n" + " otherwise NAME is used. If EXEC is provided,\n" + " use it as the service executable, otherwise\n" + " this executable is used. Any additional\n" + " arguments are passed unchanged to the\n" + " service at startup.\n" + "--uninstall NAME Uninstall Windows service NAME.\n" + "--start NAME [args] Start Windows service NAME. Any additional\n" + " arguments are passed unchanged to the\n" + " service.\n" + "--stop NAME Stop Windows service NAME." ); } #else @@ -232,7 +232,7 @@ IcePatch2::PatcherService::usage(const string& appName) "--daemon Run as a daemon.\n" "--noclose Do not close open file descriptors." - // --nochdir is intentionally not shown here. (See the comment in main().) + // --nochdir is intentionally not shown here. (See the comment in main().) ); #endif print("Usage: " + appName + " [options] [DIR]\n" + options); @@ -269,13 +269,13 @@ main(int argc, char* argv[]) { v[i + 1] = new char[strlen(argv[i]) + 1]; strcpy(v[i + 1], argv[i]); - vsave[i + 1] = v[i + 1]; + vsave[i + 1] = v[i + 1]; } v[argc + 1] = 0; try { - int ac = argc + 1; + int ac = argc + 1; status = svc.main(ac, v); } catch(...) diff --git a/cpp/src/IcePatch2/Util.cpp b/cpp/src/IcePatch2/Util.cpp index f18a1b86b75..57b7890f723 100644 --- a/cpp/src/IcePatch2/Util.cpp +++ b/cpp/src/IcePatch2/Util.cpp @@ -44,8 +44,8 @@ const char* IcePatch2::logFile = "IcePatch2.log"; extern "C" static int ice_scandir(const char* dir, struct dirent*** namelist, - int (*select)(const struct dirent*), - int (*compar)(const void*, const void*)) + int (*select)(const struct dirent*), + int (*compar)(const void*, const void*)) { DIR* d; struct dirent* entry; @@ -54,46 +54,46 @@ ice_scandir(const char* dir, struct dirent*** namelist, if((d = opendir(dir)) == 0) { - return -1; + return -1; } *namelist = 0; while((entry = readdir(d)) != 0) { - if(select == 0 || (select != 0 && (*select)(entry))) - { - *namelist = (struct dirent**)realloc((void*)(*namelist), (size_t)((i + 1) * sizeof(struct dirent*))); - if(*namelist == 0) - { - closedir(d); - return -1; - } - - entrysize = sizeof(struct dirent) - sizeof(entry->d_name) + strlen(entry->d_name) + 1; - (*namelist)[i] = (struct dirent*)malloc(entrysize); - if((*namelist)[i] == 0) - { - closedir(d); - return -1; - } - memcpy((*namelist)[i], entry, entrysize); - ++i; - } + if(select == 0 || (select != 0 && (*select)(entry))) + { + *namelist = (struct dirent**)realloc((void*)(*namelist), (size_t)((i + 1) * sizeof(struct dirent*))); + if(*namelist == 0) + { + closedir(d); + return -1; + } + + entrysize = sizeof(struct dirent) - sizeof(entry->d_name) + strlen(entry->d_name) + 1; + (*namelist)[i] = (struct dirent*)malloc(entrysize); + if((*namelist)[i] == 0) + { + closedir(d); + return -1; + } + memcpy((*namelist)[i], entry, entrysize); + ++i; + } } if(closedir(d)) { - return -1; + return -1; } if(i == 0) { - return -1; + return -1; } if(compar != 0) { - qsort((void *)(*namelist), (size_t)i, sizeof(struct dirent *), compar); + qsort((void *)(*namelist), (size_t)i, sizeof(struct dirent *), compar); } return i; @@ -117,10 +117,10 @@ bool IcePatch2::writeFileInfo(FILE* fp, const FileInfo& info) { int rc = fprintf(fp, "%s\t%s\t%d\t%d\n", - IceUtil::escapeString(info.path, "").c_str(), - bytesToString(info.checksum).c_str(), - info.size, - static_cast<int>(info.executable)); + IceUtil::escapeString(info.path, "").c_str(), + bytesToString(info.checksum).c_str(), + info.size, + static_cast<int>(info.executable)); return rc > 0; } @@ -131,17 +131,17 @@ IcePatch2::readFileInfo(FILE* fp, FileInfo& info) char buf[BUFSIZ]; while(fgets(buf, static_cast<int>(sizeof(buf)), fp) != 0) { - data += buf; + data += buf; - size_t len = strlen(buf); - if(buf[len - 1] == '\n') - { - break; - } + size_t len = strlen(buf); + if(buf[len - 1] == '\n') + { + break; + } } if(data.empty()) { - return false; + return false; } istringstream is(data); @@ -165,31 +165,31 @@ IcePatch2::lastError() #ifdef _WIN32 LPVOID lpMsgBuf = 0; DWORD ok = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language - (LPTSTR)&lpMsgBuf, - 0, - NULL); + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPTSTR)&lpMsgBuf, + 0, + NULL); if(ok) { - LPCTSTR msg = (LPCTSTR)lpMsgBuf; - assert(msg && strlen((const char*)msg) > 0); - string result = (const char*)msg; - LocalFree(lpMsgBuf); + LPCTSTR msg = (LPCTSTR)lpMsgBuf; + assert(msg && strlen((const char*)msg) > 0); + string result = (const char*)msg; + LocalFree(lpMsgBuf); - while(result.size() >= 1 && (result[result.size() - 1] == '\n' || result[result.size() - 1] == '\r')) - { - result.erase(result.size() - 1); - } + while(result.size() >= 1 && (result[result.size() - 1] == '\n' || result[result.size() - 1] == '\r')) + { + result.erase(result.size() - 1); + } - return result; + return result; } else { - return "unknown error"; + return "unknown error"; } #else return strerror(errno); @@ -204,7 +204,7 @@ IcePatch2::bytesToString(const ByteSeq& bytes) for(ByteSeq::const_iterator p = bytes.begin(); p != bytes.end(); ++p) { - s << setw(2) << setfill('0') << hex << static_cast<int>(*p); + s << setw(2) << setfill('0') << hex << static_cast<int>(*p); } return s.str(); @@ -217,8 +217,8 @@ IcePatch2::bytesToString(const ByteSeq& bytes) for(unsigned int i = 0; i < bytes.size(); ++i) { - s[i * 2] = toHex[(bytes[i] >> 4) & 0xf]; - s[i * 2 + 1] = toHex[bytes[i] & 0xf]; + s[i * 2] = toHex[(bytes[i] >> 4) & 0xf]; + s[i * 2 + 1] = toHex[bytes[i] & 0xf]; } return s; @@ -233,37 +233,37 @@ IcePatch2::stringToBytes(const string& str) for(unsigned int i = 0; i + 1 < str.size(); i += 2) { /* - istringstream is(str.substr(i, 2)); - int byte; - is >> hex >> byte; + istringstream is(str.substr(i, 2)); + int byte; + is >> hex >> byte; */ - int byte = 0; + int byte = 0; - for(int j = 0; j < 2; ++j) - { - char c = str[i + j]; + for(int j = 0; j < 2; ++j) + { + char c = str[i + j]; - if(c >= '0' && c <= '9') - { - byte |= c - '0'; - } - else if(c >= 'a' && c <= 'f') - { - byte |= 10 + c - 'a'; - } - else if(c >= 'A' && c <= 'F') - { - byte |= 10 + c - 'A'; - } + if(c >= '0' && c <= '9') + { + byte |= c - '0'; + } + else if(c >= 'a' && c <= 'f') + { + byte |= 10 + c - 'a'; + } + else if(c >= 'A' && c <= 'F') + { + byte |= 10 + c - 'A'; + } - if(j == 0) - { - byte <<= 4; - } - } + if(j == 0) + { + byte <<= 4; + } + } - bytes.push_back(static_cast<Byte>(byte)); + bytes.push_back(static_cast<Byte>(byte)); } return bytes; @@ -279,23 +279,23 @@ IcePatch2::simplify(const string& path) #ifdef _WIN32 for(pos = 0; pos < result.size(); ++pos) { - if(result[pos] == '\\') - { - result[pos] = '/'; - } + if(result[pos] == '\\') + { + result[pos] = '/'; + } } #endif pos = 0; while((pos = result.find("//", pos)) != string::npos) { - result.erase(pos, 1); + result.erase(pos, 1); } pos = 0; while((pos = result.find("/./", pos)) != string::npos) { - result.erase(pos, 2); + result.erase(pos, 2); } while(result.substr(0, 4) == "/../") @@ -305,7 +305,7 @@ IcePatch2::simplify(const string& path) if(result.substr(0, 2) == "./") { - result.erase(0, 2); + result.erase(0, 2); } if(result == "/." || @@ -316,7 +316,7 @@ IcePatch2::simplify(const string& path) if(result.size() >= 2 && result.substr(result.size() - 2, 2) == "/.") { - result.erase(result.size() - 2, 2); + result.erase(result.size() - 2, 2); } if(result == "/" || result.size() == 3 && isalpha(result[0]) && result[1] == ':' && result[2] == '/') @@ -326,7 +326,7 @@ IcePatch2::simplify(const string& path) if(result.size() >= 1 && result[result.size() - 1] == '/') { - result.erase(result.size() - 1); + result.erase(result.size() - 1); } if(result == "/..") @@ -342,7 +342,7 @@ IcePatch2::isAbsolute(const string& pa) { if(pa.empty()) { - return false; + return false; } unsigned i = 0; @@ -378,7 +378,7 @@ IcePatch2::getSuffix(const string& pa) if(dotPos == string::npos || slashPos != string::npos && slashPos > dotPos) { - return string(); + return string(); } return path.substr(dotPos + 1); @@ -394,7 +394,7 @@ IcePatch2::getWithoutSuffix(const string& pa) if(dotPos == string::npos || slashPos != string::npos && slashPos > dotPos) { - return path; + return path; } return path.substr(0, dotPos); @@ -405,9 +405,9 @@ IcePatch2::ignoreSuffix(const string& path) { string suffix = getSuffix(path); return suffix == "md5" // For legacy IcePatch. - || suffix == "tot" // For legacy IcePatch. - || suffix == "bz2" - || suffix == "bz2temp"; + || suffix == "tot" // For legacy IcePatch. + || suffix == "bz2" + || suffix == "bz2temp"; } string @@ -418,11 +418,11 @@ IcePatch2::getBasename(const string& pa) string::size_type pos = path.rfind('/'); if(pos == string::npos) { - return path; + return path; } else { - return path.substr(pos + 1); + return path.substr(pos + 1); } } @@ -434,11 +434,11 @@ IcePatch2::getDirname(const string& pa) string::size_type pos = path.rfind('/'); if(pos == string::npos) { - return string(); + return string(); } else { - return path.substr(0, pos); + return path.substr(0, pos); } } @@ -453,7 +453,7 @@ IcePatch2::rename(const string& fromPa, const string& toPa) if(OS::rename(fromPath ,toPath) == -1) { - throw "cannot rename `" + fromPath + "' to `" + toPath + "': " + lastError(); + throw "cannot rename `" + fromPath + "' to `" + toPath + "': " + lastError(); } } @@ -465,26 +465,26 @@ IcePatch2::remove(const string& pa) OS::structstat buf; if(OS::osstat(path, &buf) == -1) { - throw "cannot stat `" + path + "':\n" + lastError(); + throw "cannot stat `" + path + "':\n" + lastError(); } if(S_ISDIR(buf.st_mode)) { - if(OS::rmdir(path) == -1) - { - if(errno == EACCES) - { - assert(false); - } - throw "cannot remove directory `" + path + "':\n" + lastError(); - } + if(OS::rmdir(path) == -1) + { + if(errno == EACCES) + { + assert(false); + } + throw "cannot remove directory `" + path + "':\n" + lastError(); + } } else { - if(OS::remove(path) == -1) - { - throw "cannot remove file `" + path + "':\n" + lastError(); - } + if(OS::remove(path) == -1) + { + throw "cannot remove file `" + path + "':\n" + lastError(); + } } } @@ -496,31 +496,31 @@ IcePatch2::removeRecursive(const string& pa) OS::structstat buf; if(OS::osstat(path, &buf) == -1) { - throw "cannot stat `" + path + "':\n" + lastError(); + throw "cannot stat `" + path + "':\n" + lastError(); } if(S_ISDIR(buf.st_mode)) { - StringSeq paths = readDirectory(path); - for(StringSeq::const_iterator p = paths.begin(); p != paths.end(); ++p) - { - removeRecursive(path + '/' + *p); - } + StringSeq paths = readDirectory(path); + for(StringSeq::const_iterator p = paths.begin(); p != paths.end(); ++p) + { + removeRecursive(path + '/' + *p); + } - if(!isRoot(path)) - { - if(OS::rmdir(path) == -1) - { - throw "cannot remove directory `" + path + "':\n" + lastError(); - } - } + if(!isRoot(path)) + { + if(OS::rmdir(path) == -1) + { + throw "cannot remove directory `" + path + "':\n" + lastError(); + } + } } else { - if(OS::remove(path) == -1) - { - throw "cannot remove file `" + path + "':\n" + lastError(); - } + if(OS::remove(path) == -1) + { + throw "cannot remove file `" + path + "':\n" + lastError(); + } } } @@ -539,35 +539,35 @@ IcePatch2::readDirectory(const string& pa) int h = _wfindfirst(fs.c_str(), &data, FA_DIREC); if(h == -1) { - if(_doserrno == ENMFILE) - { - return result; - } - throw "cannot read directory `" + path + "':\n" + lastError(); + if(_doserrno == ENMFILE) + { + return result; + } + throw "cannot read directory `" + path + "':\n" + lastError(); } while(true) { - string name = IceUtil::wstringToString(data.ff_name); - assert(!name.empty()); + string name = IceUtil::wstringToString(data.ff_name); + assert(!name.empty()); - if(name != ".." && name != ".") - { - result.push_back(name); - } + if(name != ".." && name != ".") + { + result.push_back(name); + } - if(_wfindnext(&data) == -1) - { - if(errno == ENOENT) - { - break; - } + if(_wfindnext(&data) == -1) + { + if(errno == ENOENT) + { + break; + } - string ex = "cannot read directory `" + path + "':\n" + lastError(); - _wfindclose(&data); - throw ex; - } + string ex = "cannot read directory `" + path + "':\n" + lastError(); + _wfindclose(&data); + throw ex; + } } _wfindclose(&data); @@ -623,7 +623,7 @@ IcePatch2::readDirectory(const string& pa) #endif if(n < 0) { - throw "cannot read directory `" + path + "':\n" + lastError(); + throw "cannot read directory `" + path + "':\n" + lastError(); } StringSeq result; @@ -631,15 +631,15 @@ IcePatch2::readDirectory(const string& pa) for(int i = 0; i < n; ++i) { - string name = namelist[i]->d_name; - assert(!name.empty()); + string name = namelist[i]->d_name; + assert(!name.empty()); - free(namelist[i]); + free(namelist[i]); - if(name != ".." && name != ".") - { - result.push_back(name); - } + if(name != ".." && name != ".") + { + result.push_back(name); + } } free(namelist); @@ -655,10 +655,10 @@ IcePatch2::createDirectory(const string& pa) if(OS::mkdir(path, 0777) == -1) { - if(errno != EEXIST) - { - throw "cannot create directory `" + path + "':\n" + lastError(); - } + if(errno != EEXIST) + { + throw "cannot create directory `" + path + "':\n" + lastError(); + } } } @@ -670,26 +670,26 @@ IcePatch2::createDirectoryRecursive(const string& pa) string dir = getDirname(path); if(!dir.empty()) { - createDirectoryRecursive(dir); + createDirectoryRecursive(dir); } if(!isRoot(path + "/")) { - OS::structstat buf; - if(OS::osstat(path, &buf) != -1) - { - if(S_ISDIR(buf.st_mode)) - { - return; - } - } + OS::structstat buf; + if(OS::osstat(path, &buf) != -1) + { + if(S_ISDIR(buf.st_mode)) + { + return; + } + } if(OS::mkdir(path, 0777) == -1) { - if(errno != EEXIST) - { - throw "cannot create directory `" + path + "':\n" + lastError(); - } + if(errno != EEXIST) + { + throw "cannot create directory `" + path + "':\n" + lastError(); + } } } } @@ -702,45 +702,45 @@ IcePatch2::compressBytesToFile(const string& pa, const ByteSeq& bytes, Int pos) FILE* stdioFile = OS::fopen(path, "wb"); if(!stdioFile) { - throw "cannot open `" + path + "' for writing:\n" + lastError(); + throw "cannot open `" + path + "' for writing:\n" + lastError(); } int bzError; BZFILE* bzFile = BZ2_bzWriteOpen(&bzError, stdioFile, 5, 0, 0); if(bzError != BZ_OK) { - string ex = "BZ2_bzWriteOpen failed"; - if(bzError == BZ_IO_ERROR) - { - ex += string(": ") + lastError(); - } - fclose(stdioFile); - throw ex; + string ex = "BZ2_bzWriteOpen failed"; + if(bzError == BZ_IO_ERROR) + { + ex += string(": ") + lastError(); + } + fclose(stdioFile); + throw ex; } BZ2_bzWrite(&bzError, bzFile, const_cast<Byte*>(&bytes[pos]), static_cast<int>(bytes.size() - pos)); if(bzError != BZ_OK) { - string ex = "BZ2_bzWrite failed"; - if(bzError == BZ_IO_ERROR) - { - ex += string(": ") + lastError(); - } - BZ2_bzWriteClose(&bzError, bzFile, 0, 0, 0); - fclose(stdioFile); - throw ex; + string ex = "BZ2_bzWrite failed"; + if(bzError == BZ_IO_ERROR) + { + ex += string(": ") + lastError(); + } + BZ2_bzWriteClose(&bzError, bzFile, 0, 0, 0); + fclose(stdioFile); + throw ex; } BZ2_bzWriteClose(&bzError, bzFile, 0, 0, 0); if(bzError != BZ_OK) { - string ex = "BZ2_bzWriteClose failed"; - if(bzError == BZ_IO_ERROR) - { - ex += string(": ") + lastError(); - } - fclose(stdioFile); - throw ex; + string ex = "BZ2_bzWriteClose failed"; + if(bzError == BZ_IO_ERROR) + { + ex += string(": ") + lastError(); + } + fclose(stdioFile); + throw ex; } fclose(stdioFile); @@ -759,136 +759,136 @@ IcePatch2::decompressFile(const string& pa) try { - fp = OS::fopen(path, "wb"); - if(!fp) - { - throw "cannot open `" + path + "' for writing:\n" + lastError(); - } - - stdioFileBZ2 = OS::fopen(pathBZ2, "rb"); - if(!stdioFileBZ2) - { - throw "cannot open `" + pathBZ2 + "' for reading:\n" + lastError(); - } + fp = OS::fopen(path, "wb"); + if(!fp) + { + throw "cannot open `" + path + "' for writing:\n" + lastError(); + } + + stdioFileBZ2 = OS::fopen(pathBZ2, "rb"); + if(!stdioFileBZ2) + { + throw "cannot open `" + pathBZ2 + "' for reading:\n" + lastError(); + } #ifdef __BCPLUSPLUS__ - // - // The BZ2_bzReadOpen/BZ2_bzRead/BZ2_bzReadClose functions fail with BCC - // - OS::structstat buf; - if(OS::osstat(pathBZ2, &buf) == -1) - { - throw "cannot stat `" + pathBZ2 + "':\n" + lastError(); - } - - ByteSeq compressedBytes(buf.st_size); - if(fread(&compressedBytes[0], buf.st_size, 1, stdioFileBZ2) == -1) - { - throw "cannot read from `" + pathBZ2 + "':\n" + lastError(); - } - - ByteSeq uncompressedBytes; - unsigned int uncompressedLen = buf.st_size * 2; - while(true) - { - uncompressedBytes.resize(uncompressedLen); - int bzError = BZ2_bzBuffToBuffDecompress(&uncompressedBytes[0], &uncompressedLen, &compressedBytes[0], - buf.st_size, 0, 0); - if(bzError == BZ_OK) - { - break; - } - else if(bzError == BZ_OUTBUFF_FULL) - { - uncompressedLen *= 2; - continue; - } - else - { - string ex = "BZ2_bzBuffToBuffDecompress failed"; - if(bzError == BZ_IO_ERROR) - { - ex += string(": ") + lastError(); - } - throw ex; - } - } - - if(fwrite(&uncompressedBytes[0], uncompressedLen, 1, fp) != 1) - { - throw "cannot write to `" + path + "':\n" + lastError(); - } + // + // The BZ2_bzReadOpen/BZ2_bzRead/BZ2_bzReadClose functions fail with BCC + // + OS::structstat buf; + if(OS::osstat(pathBZ2, &buf) == -1) + { + throw "cannot stat `" + pathBZ2 + "':\n" + lastError(); + } + + ByteSeq compressedBytes(buf.st_size); + if(fread(&compressedBytes[0], buf.st_size, 1, stdioFileBZ2) == -1) + { + throw "cannot read from `" + pathBZ2 + "':\n" + lastError(); + } + + ByteSeq uncompressedBytes; + unsigned int uncompressedLen = buf.st_size * 2; + while(true) + { + uncompressedBytes.resize(uncompressedLen); + int bzError = BZ2_bzBuffToBuffDecompress(&uncompressedBytes[0], &uncompressedLen, &compressedBytes[0], + buf.st_size, 0, 0); + if(bzError == BZ_OK) + { + break; + } + else if(bzError == BZ_OUTBUFF_FULL) + { + uncompressedLen *= 2; + continue; + } + else + { + string ex = "BZ2_bzBuffToBuffDecompress failed"; + if(bzError == BZ_IO_ERROR) + { + ex += string(": ") + lastError(); + } + throw ex; + } + } + + if(fwrite(&uncompressedBytes[0], uncompressedLen, 1, fp) != 1) + { + throw "cannot write to `" + path + "':\n" + lastError(); + } #else - bzFile = BZ2_bzReadOpen(&bzError, stdioFileBZ2, 0, 0, 0, 0); - if(bzError != BZ_OK) - { - string ex = "BZ2_bzReadOpen failed"; - if(bzError == BZ_IO_ERROR) - { - ex += string(": ") + lastError(); - } - throw ex; - } - - const Int numBZ2 = 64 * 1024; - Byte bytesBZ2[numBZ2]; - - while(bzError != BZ_STREAM_END) - { - int sz = BZ2_bzRead(&bzError, bzFile, bytesBZ2, numBZ2); - if(bzError != BZ_OK && bzError != BZ_STREAM_END) - { - string ex = "BZ2_bzRead failed"; - if(bzError == BZ_IO_ERROR) - { - ex += string(": ") + lastError(); - } - throw ex; - } - - if(sz > 0) - { - long pos = ftell(stdioFileBZ2); - if(pos == -1) - { - throw "cannot get read position for `" + pathBZ2 + "':\n" + lastError(); - } - - if(fwrite(bytesBZ2, sz, 1, fp) != 1) - { - throw "cannot write to `" + path + "':\n" + lastError(); - } - } - } - - BZ2_bzReadClose(&bzError, bzFile); - bzFile = 0; - if(bzError != BZ_OK) - { - string ex = "BZ2_bzReadClose failed"; - if(bzError == BZ_IO_ERROR) - { - ex += string(": ") + lastError(); - } - throw ex; - } + bzFile = BZ2_bzReadOpen(&bzError, stdioFileBZ2, 0, 0, 0, 0); + if(bzError != BZ_OK) + { + string ex = "BZ2_bzReadOpen failed"; + if(bzError == BZ_IO_ERROR) + { + ex += string(": ") + lastError(); + } + throw ex; + } + + const Int numBZ2 = 64 * 1024; + Byte bytesBZ2[numBZ2]; + + while(bzError != BZ_STREAM_END) + { + int sz = BZ2_bzRead(&bzError, bzFile, bytesBZ2, numBZ2); + if(bzError != BZ_OK && bzError != BZ_STREAM_END) + { + string ex = "BZ2_bzRead failed"; + if(bzError == BZ_IO_ERROR) + { + ex += string(": ") + lastError(); + } + throw ex; + } + + if(sz > 0) + { + long pos = ftell(stdioFileBZ2); + if(pos == -1) + { + throw "cannot get read position for `" + pathBZ2 + "':\n" + lastError(); + } + + if(fwrite(bytesBZ2, sz, 1, fp) != 1) + { + throw "cannot write to `" + path + "':\n" + lastError(); + } + } + } + + BZ2_bzReadClose(&bzError, bzFile); + bzFile = 0; + if(bzError != BZ_OK) + { + string ex = "BZ2_bzReadClose failed"; + if(bzError == BZ_IO_ERROR) + { + ex += string(": ") + lastError(); + } + throw ex; + } #endif } catch(...) { - if(bzFile != 0) - { - BZ2_bzReadClose(&bzError, bzFile); - } - if(stdioFileBZ2 != 0) - { - fclose(stdioFileBZ2); - } - if(fp != 0) - { - fclose(fp); - } - throw; + if(bzFile != 0) + { + BZ2_bzReadClose(&bzError, bzFile); + } + if(stdioFileBZ2 != 0) + { + fclose(stdioFileBZ2); + } + if(fp != 0) + { + fclose(fp); + } + throw; } fclose(stdioFileBZ2); @@ -903,7 +903,7 @@ IcePatch2::setFileFlags(const string& pa, const FileInfo& info) OS::structstat buf; if(OS::osstat(path, &buf) == -1) { - throw "cannot stat `" + path + "':\n" + lastError(); + throw "cannot stat `" + path + "':\n" + lastError(); } chmod(path.c_str(), info.executable ? buf.st_mode | S_IXUSR : buf.st_mode & ~S_IXUSR); #endif @@ -911,321 +911,321 @@ IcePatch2::setFileFlags(const string& pa, const FileInfo& info) static bool getFileInfoSeqInt(const string& basePath, const string& relPath, int compress, GetFileInfoSeqCB* cb, - FileInfoSeq& infoSeq) + FileInfoSeq& infoSeq) { if(relPath == checksumFile || relPath == logFile) { - return true; + return true; } const string path = simplify(basePath + '/' + relPath); if(ignoreSuffix(path)) { - const string pathWithoutSuffix = getWithoutSuffix(path); - - if(ignoreSuffix(pathWithoutSuffix)) - { - if(cb && !cb->remove(relPath)) - { - return false; - } - - remove(path); // Removing file with suffix for another file that already has a suffix. - } - else - { - OS::structstat buf; - if(OS::osstat(getWithoutSuffix(path), &buf) == -1) - { - if(errno == ENOENT) - { - if(cb && !cb->remove(relPath)) - { - return false; - } - - remove(path); // Removing orphaned file. - } - else - { - throw "cannot stat `" + path + "':\n" + lastError(); - } - } - else if(buf.st_size == 0) - { - if(cb && !cb->remove(relPath)) - { - return false; - } - - remove(path); // Removing file with suffix for empty file. - } - } + const string pathWithoutSuffix = getWithoutSuffix(path); + + if(ignoreSuffix(pathWithoutSuffix)) + { + if(cb && !cb->remove(relPath)) + { + return false; + } + + remove(path); // Removing file with suffix for another file that already has a suffix. + } + else + { + OS::structstat buf; + if(OS::osstat(getWithoutSuffix(path), &buf) == -1) + { + if(errno == ENOENT) + { + if(cb && !cb->remove(relPath)) + { + return false; + } + + remove(path); // Removing orphaned file. + } + else + { + throw "cannot stat `" + path + "':\n" + lastError(); + } + } + else if(buf.st_size == 0) + { + if(cb && !cb->remove(relPath)) + { + return false; + } + + remove(path); // Removing file with suffix for empty file. + } + } } else { - OS::structstat buf; - if(OS::osstat(path, &buf) == -1) - { - throw "cannot stat `" + path + "':\n" + lastError(); - } - - if(S_ISDIR(buf.st_mode)) - { - FileInfo info; - info.path = relPath; - info.size = -1; - info.executable = false; - - ByteSeq bytes(relPath.size()); - copy(relPath.begin(), relPath.end(), bytes.begin()); - - ByteSeq bytesSHA(20); - if(!bytes.empty()) - { - SHA1(reinterpret_cast<unsigned char*>(&bytes[0]), bytes.size(), - reinterpret_cast<unsigned char*>(&bytesSHA[0])); - } - else - { - fill(bytesSHA.begin(), bytesSHA.end(), 0); - } - info.checksum.swap(bytesSHA); - - infoSeq.push_back(info); - - StringSeq content = readDirectory(path); - for(StringSeq::const_iterator p = content.begin(); p != content.end() ; ++p) - { - if(!getFileInfoSeqInt(basePath, simplify(relPath + '/' + *p), compress, cb, infoSeq)) - { - return false; - } - } - } - else if(S_ISREG(buf.st_mode)) - { - FileInfo info; - info.path = relPath; - info.size = 0; + OS::structstat buf; + if(OS::osstat(path, &buf) == -1) + { + throw "cannot stat `" + path + "':\n" + lastError(); + } + + if(S_ISDIR(buf.st_mode)) + { + FileInfo info; + info.path = relPath; + info.size = -1; + info.executable = false; + + ByteSeq bytes(relPath.size()); + copy(relPath.begin(), relPath.end(), bytes.begin()); + + ByteSeq bytesSHA(20); + if(!bytes.empty()) + { + SHA1(reinterpret_cast<unsigned char*>(&bytes[0]), bytes.size(), + reinterpret_cast<unsigned char*>(&bytesSHA[0])); + } + else + { + fill(bytesSHA.begin(), bytesSHA.end(), 0); + } + info.checksum.swap(bytesSHA); + + infoSeq.push_back(info); + + StringSeq content = readDirectory(path); + for(StringSeq::const_iterator p = content.begin(); p != content.end() ; ++p) + { + if(!getFileInfoSeqInt(basePath, simplify(relPath + '/' + *p), compress, cb, infoSeq)) + { + return false; + } + } + } + else if(S_ISREG(buf.st_mode)) + { + FileInfo info; + info.path = relPath; + info.size = 0; #ifdef _WIN32 - info.executable = false; // Windows doesn't support the executable flag + info.executable = false; // Windows doesn't support the executable flag #else - info.executable = buf.st_mode & S_IXUSR; + info.executable = buf.st_mode & S_IXUSR; #endif OS::structstat bufBZ2; - const string pathBZ2 = path + ".bz2"; - bool doCompress = false; - if(buf.st_size != 0 && compress > 0) - { - // - // compress == 0: Never compress. - // compress == 1: Compress if necessary. - // compress >= 2: Always compress. - // - if(compress >= 2 || OS::osstat(pathBZ2, &bufBZ2) == -1 || buf.st_mtime >= bufBZ2.st_mtime) - { - if(cb && !cb->compress(relPath)) - { - return false; - } - - doCompress = true; - } - else - { - info.size = static_cast<Int>(bufBZ2.st_size); - } - } - - if(cb && !cb->checksum(relPath)) - { - return false; - } - - ByteSeq bytesSHA(20); - - if(relPath.size() + buf.st_size == 0) - { - fill(bytesSHA.begin(), bytesSHA.end(), 0); - } - else - { - SHA_CTX ctx; - SHA1_Init(&ctx); - if(relPath.size() != 0) - { - SHA1_Update(&ctx, reinterpret_cast<const void*>(relPath.c_str()), relPath.size()); - } - - if(buf.st_size != 0) - { + const string pathBZ2 = path + ".bz2"; + bool doCompress = false; + if(buf.st_size != 0 && compress > 0) + { + // + // compress == 0: Never compress. + // compress == 1: Compress if necessary. + // compress >= 2: Always compress. + // + if(compress >= 2 || OS::osstat(pathBZ2, &bufBZ2) == -1 || buf.st_mtime >= bufBZ2.st_mtime) + { + if(cb && !cb->compress(relPath)) + { + return false; + } + + doCompress = true; + } + else + { + info.size = static_cast<Int>(bufBZ2.st_size); + } + } + + if(cb && !cb->checksum(relPath)) + { + return false; + } + + ByteSeq bytesSHA(20); + + if(relPath.size() + buf.st_size == 0) + { + fill(bytesSHA.begin(), bytesSHA.end(), 0); + } + else + { + SHA_CTX ctx; + SHA1_Init(&ctx); + if(relPath.size() != 0) + { + SHA1_Update(&ctx, reinterpret_cast<const void*>(relPath.c_str()), relPath.size()); + } + + if(buf.st_size != 0) + { #ifdef __BCPLUSPLUS__ - // - // The BZ2_bzWriteOpen/BZ2_bzWrite/BZ2_bzWriteClose functions fail with BCC - // - if(doCompress) - { - int fd = OS::open(path.c_str(), O_BINARY|O_RDONLY); - if(fd == -1) - { - throw "cannot open `" + path + "' for reading:\n" + lastError(); - } - - ByteSeq uncompressedBytes(buf.st_size); - - if(read(fd, &uncompressedBytes[0], buf.st_size) == -1) - { - close(fd); - throw "cannot read from `" + path + "':\n" + lastError(); - } - - unsigned int compressedLen = buf.st_size * 1.01 + 600; - ByteSeq compressedBytes(compressedLen); - - int bzError = BZ2_bzBuffToBuffCompress(&compressedBytes[0], &compressedLen, - &uncompressedBytes[0], buf.st_size, 5, 0, 0); - if(bzError != BZ_OK) - { - string ex = "BZ2_bzBuffToBuffCompress failed"; - if(bzError == BZ_IO_ERROR) - { - ex += string(": ") + lastError(); - } - close(fd); - throw ex; - } - close(fd); - - const string pathBZ2Temp = path + ".bz2temp"; - FILE* stdioFile = OS::fopen(pathBZ2Temp, "wb"); - if(fwrite(&compressedBytes[0], compressedLen, 1, stdioFile) != 1) - { - fclose(stdioFile); - throw "cannot write to `" + pathBZ2Temp + "':\n" + lastError(); - } - fclose(stdioFile); - - rename(pathBZ2Temp, pathBZ2); - - } + // + // The BZ2_bzWriteOpen/BZ2_bzWrite/BZ2_bzWriteClose functions fail with BCC + // + if(doCompress) + { + int fd = OS::open(path.c_str(), O_BINARY|O_RDONLY); + if(fd == -1) + { + throw "cannot open `" + path + "' for reading:\n" + lastError(); + } + + ByteSeq uncompressedBytes(buf.st_size); + + if(read(fd, &uncompressedBytes[0], buf.st_size) == -1) + { + close(fd); + throw "cannot read from `" + path + "':\n" + lastError(); + } + + unsigned int compressedLen = buf.st_size * 1.01 + 600; + ByteSeq compressedBytes(compressedLen); + + int bzError = BZ2_bzBuffToBuffCompress(&compressedBytes[0], &compressedLen, + &uncompressedBytes[0], buf.st_size, 5, 0, 0); + if(bzError != BZ_OK) + { + string ex = "BZ2_bzBuffToBuffCompress failed"; + if(bzError == BZ_IO_ERROR) + { + ex += string(": ") + lastError(); + } + close(fd); + throw ex; + } + close(fd); + + const string pathBZ2Temp = path + ".bz2temp"; + FILE* stdioFile = OS::fopen(pathBZ2Temp, "wb"); + if(fwrite(&compressedBytes[0], compressedLen, 1, stdioFile) != 1) + { + fclose(stdioFile); + throw "cannot write to `" + pathBZ2Temp + "':\n" + lastError(); + } + fclose(stdioFile); + + rename(pathBZ2Temp, pathBZ2); + + } #endif - int fd = OS::open(path.c_str(), O_BINARY|O_RDONLY); - if(fd == -1) - { - throw "cannot open `" + path + "' for reading:\n" + lastError(); - } + int fd = OS::open(path.c_str(), O_BINARY|O_RDONLY); + if(fd == -1) + { + throw "cannot open `" + path + "' for reading:\n" + lastError(); + } #ifndef __BCPLUSPLUS__ - const string pathBZ2Temp = path + ".bz2temp"; - FILE* stdioFile = 0; - int bzError = 0; - BZFILE* bzFile = 0; - if(doCompress) - { - stdioFile = OS::fopen(simplify(pathBZ2Temp), "wb"); - if(!stdioFile) - { - close(fd); - throw "cannot open `" + pathBZ2Temp + "' for writing:\n" + lastError(); - } - - bzFile = BZ2_bzWriteOpen(&bzError, stdioFile, 5, 0, 0); - if(bzError != BZ_OK) - { - string ex = "BZ2_bzWriteOpen failed"; - if(bzError == BZ_IO_ERROR) - { - ex += string(": ") + lastError(); - } - fclose(stdioFile); - close(fd); - throw ex; - } - } + const string pathBZ2Temp = path + ".bz2temp"; + FILE* stdioFile = 0; + int bzError = 0; + BZFILE* bzFile = 0; + if(doCompress) + { + stdioFile = OS::fopen(simplify(pathBZ2Temp), "wb"); + if(!stdioFile) + { + close(fd); + throw "cannot open `" + pathBZ2Temp + "' for writing:\n" + lastError(); + } + + bzFile = BZ2_bzWriteOpen(&bzError, stdioFile, 5, 0, 0); + if(bzError != BZ_OK) + { + string ex = "BZ2_bzWriteOpen failed"; + if(bzError == BZ_IO_ERROR) + { + ex += string(": ") + lastError(); + } + fclose(stdioFile); + close(fd); + throw ex; + } + } #endif - unsigned int bytesLeft = static_cast<unsigned int>(buf.st_size); - while(bytesLeft > 0) - { - ByteSeq bytes(min(bytesLeft, 1024u*1024)); - if(read(fd, &bytes[0], static_cast<unsigned int>(bytes.size())) == -1) - { + unsigned int bytesLeft = static_cast<unsigned int>(buf.st_size); + while(bytesLeft > 0) + { + ByteSeq bytes(min(bytesLeft, 1024u*1024)); + if(read(fd, &bytes[0], static_cast<unsigned int>(bytes.size())) == -1) + { #ifndef __BCPLUSPLUS__ - if(doCompress) - { - fclose(stdioFile); - } + if(doCompress) + { + fclose(stdioFile); + } #endif - close(fd); - throw "cannot read from `" + path + "':\n" + lastError(); - } - bytesLeft -= static_cast<unsigned int>(bytes.size()); + close(fd); + throw "cannot read from `" + path + "':\n" + lastError(); + } + bytesLeft -= static_cast<unsigned int>(bytes.size()); #ifndef __BCPLUSPLUS__ - if(doCompress) - { + if(doCompress) + { BZ2_bzWrite(&bzError, bzFile, const_cast<Byte*>(&bytes[0]), static_cast<int>(bytes.size())); if(bzError != BZ_OK) { - string ex = "BZ2_bzWrite failed"; - if(bzError == BZ_IO_ERROR) - { - ex += string(": ") + lastError(); - } - BZ2_bzWriteClose(&bzError, bzFile, 0, 0, 0); - fclose(stdioFile); - close(fd); - throw ex; + string ex = "BZ2_bzWrite failed"; + if(bzError == BZ_IO_ERROR) + { + ex += string(": ") + lastError(); + } + BZ2_bzWriteClose(&bzError, bzFile, 0, 0, 0); + fclose(stdioFile); + close(fd); + throw ex; } - } + } #endif - SHA1_Update(&ctx, reinterpret_cast<const void*>(&bytes[0]), bytes.size()); - } + SHA1_Update(&ctx, reinterpret_cast<const void*>(&bytes[0]), bytes.size()); + } - close(fd); + close(fd); #ifndef __BCPLUSPLUS__ - if(doCompress) - { + if(doCompress) + { BZ2_bzWriteClose(&bzError, bzFile, 0, 0, 0); if(bzError != BZ_OK) { - string ex = "BZ2_bzWriteClose failed"; - if(bzError == BZ_IO_ERROR) - { - ex += string(": ") + lastError(); - } - fclose(stdioFile); - throw ex; + string ex = "BZ2_bzWriteClose failed"; + if(bzError == BZ_IO_ERROR) + { + ex += string(": ") + lastError(); + } + fclose(stdioFile); + throw ex; } - fclose(stdioFile); + fclose(stdioFile); - rename(pathBZ2Temp, pathBZ2); + rename(pathBZ2Temp, pathBZ2); - if(OS::osstat(pathBZ2, &bufBZ2) == -1) - { - throw "cannot stat `" + pathBZ2 + "':\n" + lastError(); - } + if(OS::osstat(pathBZ2, &bufBZ2) == -1) + { + throw "cannot stat `" + pathBZ2 + "':\n" + lastError(); + } - info.size = static_cast<Int>(bufBZ2.st_size); - } + info.size = static_cast<Int>(bufBZ2.st_size); + } #endif - } + } - SHA1_Final(reinterpret_cast<unsigned char*>(&bytesSHA[0]), &ctx); - } + SHA1_Final(reinterpret_cast<unsigned char*>(&bytesSHA[0]), &ctx); + } - info.checksum.swap(bytesSHA); + info.checksum.swap(bytesSHA); - infoSeq.push_back(info); - } + infoSeq.push_back(info); + } } return true; @@ -1233,21 +1233,21 @@ getFileInfoSeqInt(const string& basePath, const string& relPath, int compress, G bool IcePatch2::getFileInfoSeq(const string& basePath, int compress, GetFileInfoSeqCB* cb, - FileInfoSeq& infoSeq) + FileInfoSeq& infoSeq) { return getFileInfoSeqSubDir(basePath, ".", compress, cb, infoSeq); } bool IcePatch2::getFileInfoSeqSubDir(const string& basePa, const string& relPa, int compress, GetFileInfoSeqCB* cb, - FileInfoSeq& infoSeq) + FileInfoSeq& infoSeq) { const string basePath = simplify(basePa); const string relPath = simplify(relPa); if(!getFileInfoSeqInt(basePath, relPath, compress, cb, infoSeq)) { - return false; + return false; } sort(infoSeq.begin(), infoSeq.end(), FileInfoLess()); @@ -1260,41 +1260,41 @@ void IcePatch2::saveFileInfoSeq(const string& pa, const FileInfoSeq& infoSeq) { { - const string path = simplify(pa + '/' + checksumFile); - - FILE* fp = OS::fopen(path, "w"); - if(!fp) - { - throw "cannot open `" + path + "' for writing:\n" + lastError(); - } - try - { - for(FileInfoSeq::const_iterator p = infoSeq.begin(); p != infoSeq.end(); ++p) - { - if(!writeFileInfo(fp, *p)) - { - throw "error writing `" + path + "':\n" + lastError(); - } - } - } - catch(...) - { - fclose(fp); - throw; - } - fclose(fp); - } - - { - const string pathLog = simplify(pa + '/' + logFile); - - try - { - remove(pathLog); - } - catch(...) - { - } + const string path = simplify(pa + '/' + checksumFile); + + FILE* fp = OS::fopen(path, "w"); + if(!fp) + { + throw "cannot open `" + path + "' for writing:\n" + lastError(); + } + try + { + for(FileInfoSeq::const_iterator p = infoSeq.begin(); p != infoSeq.end(); ++p) + { + if(!writeFileInfo(fp, *p)) + { + throw "error writing `" + path + "':\n" + lastError(); + } + } + } + catch(...) + { + fclose(fp); + throw; + } + fclose(fp); + } + + { + const string pathLog = simplify(pa + '/' + logFile); + + try + { + remove(pathLog); + } + catch(...) + { + } } } @@ -1302,98 +1302,98 @@ void IcePatch2::loadFileInfoSeq(const string& pa, FileInfoSeq& infoSeq) { { - const string path = simplify(pa + '/' + checksumFile); - - FILE* fp = OS::fopen(path, "r"); - if(!fp) - { - throw "cannot open `" + path + "' for reading:\n" + lastError(); - } - - while(true) - { - FileInfo info; - if(readFileInfo(fp, info)) - { - infoSeq.push_back(info); - } - else - { - break; - } - } - fclose(fp); - - sort(infoSeq.begin(), infoSeq.end(), FileInfoLess()); - infoSeq.erase(unique(infoSeq.begin(), infoSeq.end(), FileInfoEqual()), infoSeq.end()); - } - - { - const string pathLog = simplify(pa + '/' + logFile); - - FILE* fp = OS::fopen(pathLog, "r"); - if(fp != 0) - { - FileInfoSeq remove; - FileInfoSeq update; - - while(true) - { - int c = fgetc(fp); - if(c == EOF) - { - break; - } - - FileInfo info; - if(!readFileInfo(fp, info)) - { - break; - } - - if(c == '-') - { - remove.push_back(info); - } - else if(c == '+') - { - update.push_back(info); - } - } - fclose(fp); - - sort(remove.begin(), remove.end(), FileInfoLess()); - remove.erase(unique(remove.begin(), remove.end(), FileInfoEqual()), remove.end()); - - sort(update.begin(), update.end(), FileInfoLess()); - update.erase(unique(update.begin(), update.end(), FileInfoEqual()), update.end()); - - FileInfoSeq newInfoSeq; - newInfoSeq.reserve(infoSeq.size()); - - set_difference(infoSeq.begin(), - infoSeq.end(), - remove.begin(), - remove.end(), - back_inserter(newInfoSeq), - FileInfoLess()); - - infoSeq.swap(newInfoSeq); - - newInfoSeq.clear(); - newInfoSeq.reserve(infoSeq.size()); - - set_union(infoSeq.begin(), - infoSeq.end(), - update.begin(), - update.end(), - back_inserter(newInfoSeq), - FileInfoLess()); - - infoSeq.swap(newInfoSeq); - - saveFileInfoSeq(pa, infoSeq); - } + const string path = simplify(pa + '/' + checksumFile); + + FILE* fp = OS::fopen(path, "r"); + if(!fp) + { + throw "cannot open `" + path + "' for reading:\n" + lastError(); + } + + while(true) + { + FileInfo info; + if(readFileInfo(fp, info)) + { + infoSeq.push_back(info); + } + else + { + break; + } + } + fclose(fp); + + sort(infoSeq.begin(), infoSeq.end(), FileInfoLess()); + infoSeq.erase(unique(infoSeq.begin(), infoSeq.end(), FileInfoEqual()), infoSeq.end()); + } + + { + const string pathLog = simplify(pa + '/' + logFile); + + FILE* fp = OS::fopen(pathLog, "r"); + if(fp != 0) + { + FileInfoSeq remove; + FileInfoSeq update; + + while(true) + { + int c = fgetc(fp); + if(c == EOF) + { + break; + } + + FileInfo info; + if(!readFileInfo(fp, info)) + { + break; + } + + if(c == '-') + { + remove.push_back(info); + } + else if(c == '+') + { + update.push_back(info); + } + } + fclose(fp); + + sort(remove.begin(), remove.end(), FileInfoLess()); + remove.erase(unique(remove.begin(), remove.end(), FileInfoEqual()), remove.end()); + + sort(update.begin(), update.end(), FileInfoLess()); + update.erase(unique(update.begin(), update.end(), FileInfoEqual()), update.end()); + + FileInfoSeq newInfoSeq; + newInfoSeq.reserve(infoSeq.size()); + + set_difference(infoSeq.begin(), + infoSeq.end(), + remove.begin(), + remove.end(), + back_inserter(newInfoSeq), + FileInfoLess()); + + infoSeq.swap(newInfoSeq); + + newInfoSeq.clear(); + newInfoSeq.reserve(infoSeq.size()); + + set_union(infoSeq.begin(), + infoSeq.end(), + update.begin(), + update.end(), + back_inserter(newInfoSeq), + FileInfoLess()); + + infoSeq.swap(newInfoSeq); + + saveFileInfoSeq(pa, infoSeq); + } } } @@ -1409,52 +1409,52 @@ IcePatch2::getFileTree0(const FileInfoSeq& infoSeq, FileTree0& tree0) for(int i = 0; i < 256; ++i, c0 += 20) { - FileTree1& tree1 = tree0.nodes[i]; - - tree1.files.clear(); - tree1.checksum.resize(20); - - FileInfoSeq::const_iterator p; - - for(p = infoSeq.begin(); p != infoSeq.end(); ++p) - { - if(i == static_cast<int>(p->checksum[0])) - { - tree1.files.push_back(*p); - } - } - - ByteSeq allChecksums1; - allChecksums1.resize(tree1.files.size() * 21); // 20 bytes for the checksum + 1 byte for the flag - ByteSeq::iterator c1 = allChecksums1.begin(); - - for(p = tree1.files.begin(); p != tree1.files.end(); ++p, c1 += 21) - { - copy(p->checksum.begin(), p->checksum.end(), c1); - *(c1 + 20) = p->executable; - } - - if(!allChecksums1.empty()) - { - SHA1(reinterpret_cast<unsigned char*>(&allChecksums1[0]), allChecksums1.size(), - reinterpret_cast<unsigned char*>(&tree1.checksum[0])); - } - else - { - fill(tree1.checksum.begin(), tree1.checksum.end(), 0); - } - - copy(tree1.checksum.begin(), tree1.checksum.end(), c0); + FileTree1& tree1 = tree0.nodes[i]; + + tree1.files.clear(); + tree1.checksum.resize(20); + + FileInfoSeq::const_iterator p; + + for(p = infoSeq.begin(); p != infoSeq.end(); ++p) + { + if(i == static_cast<int>(p->checksum[0])) + { + tree1.files.push_back(*p); + } + } + + ByteSeq allChecksums1; + allChecksums1.resize(tree1.files.size() * 21); // 20 bytes for the checksum + 1 byte for the flag + ByteSeq::iterator c1 = allChecksums1.begin(); + + for(p = tree1.files.begin(); p != tree1.files.end(); ++p, c1 += 21) + { + copy(p->checksum.begin(), p->checksum.end(), c1); + *(c1 + 20) = p->executable; + } + + if(!allChecksums1.empty()) + { + SHA1(reinterpret_cast<unsigned char*>(&allChecksums1[0]), allChecksums1.size(), + reinterpret_cast<unsigned char*>(&tree1.checksum[0])); + } + else + { + fill(tree1.checksum.begin(), tree1.checksum.end(), 0); + } + + copy(tree1.checksum.begin(), tree1.checksum.end(), c0); } if(!allChecksums0.empty()) { - SHA1(reinterpret_cast<unsigned char*>(&allChecksums0[0]), allChecksums0.size(), - reinterpret_cast<unsigned char*>(&tree0.checksum[0])); + SHA1(reinterpret_cast<unsigned char*>(&allChecksums0[0]), allChecksums0.size(), + reinterpret_cast<unsigned char*>(&tree0.checksum[0])); } else { - fill(tree0.checksum.begin(), tree0.checksum.end(), 0); + fill(tree0.checksum.begin(), tree0.checksum.end(), 0); } } |