diff options
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/Ice/Application.cpp | 11 | ||||
-rw-r--r-- | cpp/src/IcePatch/IcePatchI.cpp | 88 | ||||
-rw-r--r-- | cpp/src/IcePatch/Server.cpp | 27 | ||||
-rw-r--r-- | cpp/src/IcePatch/Util.cpp | 193 |
4 files changed, 125 insertions, 194 deletions
diff --git a/cpp/src/Ice/Application.cpp b/cpp/src/Ice/Application.cpp index d0480baa916..a9672c2cf4f 100644 --- a/cpp/src/Ice/Application.cpp +++ b/cpp/src/Ice/Application.cpp @@ -18,7 +18,6 @@ using namespace Ice; const char* Application::_appName = 0; CommunicatorPtr Application::_communicator; -bool Application::_shutdown = false; Ice::Application::Application() { @@ -105,8 +104,6 @@ Ice::Application::communicator() BOOL WINAPI Ice::interruptHandler(DWORD) { - Application::_shutdown = true; - // // Don't use Application::communicator(), this is not signal-safe. // @@ -141,8 +138,6 @@ Ice::Application::defaultInterrupt() void Ice::interruptHandler(int) { - Application::_shutdown = true; - // // Don't use Application::communicator(), this is not signal-safe. // @@ -190,9 +185,3 @@ Ice::Application::defaultInterrupt() } #endif - -bool -Ice::Application::isShutdownFromInterrupt() -{ - return _shutdown; -} diff --git a/cpp/src/IcePatch/IcePatchI.cpp b/cpp/src/IcePatch/IcePatchI.cpp index a9c2f480cac..502757b3232 100644 --- a/cpp/src/IcePatch/IcePatchI.cpp +++ b/cpp/src/IcePatch/IcePatchI.cpp @@ -34,66 +34,15 @@ IcePatch::DirectoryI::DirectoryI(const ObjectAdapterPtr& adapter) : FileDescPtr IcePatch::DirectoryI::describe(const Current& current) { - string path = identityToPath(current.id); - + // No mutex lock necessary. DirectoryDescPtr desc = new DirectoryDesc; desc->directory = DirectoryPrx::uncheckedCast(_adapter->createProxy(current.id)); - - try - { - IceUtil::RWRecMutex::TryRLock sync(_globalMutex, _busyTimeout); - - FileInfo info = getFileInfo(path, true); - FileInfo infoMD5 = getFileInfo(path + ".md5", false); - if(infoMD5.type == FileTypeRegular && infoMD5.time > info.time) - { - desc->md5 = getMD5(path); - return desc; - } - } - catch(const IceUtil::LockedException&) - { - throw BusyException(); - } - - // - // Force .md5 files to be created and orphaned files to be - // removed. This must be done outside the thread - // synchronization, to avoid deadlocks. - // - getContents(current); - - try - { - IceUtil::RWRecMutex::TryWLock sync(_globalMutex, _busyTimeout); - - FileInfo info = getFileInfo(path, true); - FileInfo infoMD5 = getFileInfo(path + ".md5", false); - if(infoMD5.type != FileTypeRegular || infoMD5.time <= info.time) - { - createMD5(path); - - if(_traceLevel > 0) - { - Trace out(_logger, "IcePatch"); - out << "created .md5 file for directory `" << path << "'"; - } - } - - desc->md5 = getMD5(path); - return desc; - } - catch(const IceUtil::LockedException&) - { - throw BusyException(); - } + return desc; } FileDescSeq IcePatch::DirectoryI::getContents(const Current& current) { - string path = identityToPath(current.id); - StringSeq filteredPaths; try @@ -101,6 +50,7 @@ IcePatch::DirectoryI::getContents(const Current& current) IceUtil::RWRecMutex::TryRLock sync(_globalMutex, _busyTimeout); bool syncUpgraded = false; + string path = identityToPath(current.id); StringSeq paths = readDirectory(path); filteredPaths.reserve(paths.size() / 3); for(StringSeq::const_iterator p = paths.begin(); p != paths.end(); ++p) @@ -174,17 +124,19 @@ IcePatch::RegularI::RegularI(const ObjectAdapterPtr& adapter) : FileDescPtr IcePatch::RegularI::describe(const Current& current) { - string path = identityToPath(current.id); - try { IceUtil::RWRecMutex::TryRLock sync(_globalMutex, _busyTimeout); + string path = identityToPath(current.id); FileInfo info = getFileInfo(path, true); + assert(info.type == FileTypeRegular); + FileInfo infoMD5 = getFileInfo(path + ".md5", false); if(infoMD5.type != FileTypeRegular || infoMD5.time <= info.time) { sync.timedUpgrade(_busyTimeout); + infoMD5 = getFileInfo(path + ".md5", false); if(infoMD5.type != FileTypeRegular || infoMD5.time <= info.time) { @@ -193,7 +145,7 @@ IcePatch::RegularI::describe(const Current& current) if(_traceLevel > 0) { Trace out(_logger, "IcePatch"); - out << "created .md5 file for regular file `" << path << "'"; + out << "created MD5 file for file `" << path << "'"; } } } @@ -212,17 +164,19 @@ IcePatch::RegularI::describe(const Current& current) Int IcePatch::RegularI::getBZ2Size(const Current& current) { - string path = identityToPath(current.id); - try { IceUtil::RWRecMutex::TryRLock sync(_globalMutex, _busyTimeout); + string path = identityToPath(current.id); FileInfo info = getFileInfo(path, true); + assert(info.type == FileTypeRegular); + FileInfo infoBZ2 = getFileInfo(path + ".bz2", false); if(infoBZ2.type != FileTypeRegular || infoBZ2.time <= info.time) { sync.timedUpgrade(_busyTimeout); + infoBZ2 = getFileInfo(path + ".bz2", false); if(infoBZ2.type != FileTypeRegular || infoBZ2.time <= info.time) { @@ -231,12 +185,14 @@ IcePatch::RegularI::getBZ2Size(const Current& current) if(_traceLevel > 0) { Trace out(_logger, "IcePatch"); - out << "created .bz2 file for `" << path << "'"; + out << "created BZ2 file for `" << path << "'"; } + // // Get the .bz2 file info again, so that we can return the // size below. This time the .bz2 file must exist, // otherwise an exception is raised. + // infoBZ2 = getFileInfo(path + ".bz2", true); } } @@ -252,17 +208,19 @@ IcePatch::RegularI::getBZ2Size(const Current& current) ByteSeq IcePatch::RegularI::getBZ2(Int pos, Int num, const Current& current) { - string path = identityToPath(current.id); - try { IceUtil::RWRecMutex::TryRLock sync(_globalMutex, _busyTimeout); + string path = identityToPath(current.id); FileInfo info = getFileInfo(path, true); + assert(info.type == FileTypeRegular); + FileInfo infoBZ2 = getFileInfo(path + ".bz2", false); if(infoBZ2.type != FileTypeRegular || infoBZ2.time <= info.time) { sync.timedUpgrade(_busyTimeout); + infoBZ2 = getFileInfo(path + ".bz2", false); if(infoBZ2.type != FileTypeRegular || infoBZ2.time <= info.time) { @@ -287,17 +245,19 @@ IcePatch::RegularI::getBZ2(Int pos, Int num, const Current& current) ByteSeq IcePatch::RegularI::getBZ2MD5(Int size, const Current& current) { - string path = identityToPath(current.id); - try { IceUtil::RWRecMutex::TryRLock sync(_globalMutex, _busyTimeout); + string path = identityToPath(current.id); FileInfo info = getFileInfo(path, true); + assert(info.type == FileTypeRegular); + FileInfo infoBZ2 = getFileInfo(path + ".bz2", false); if(infoBZ2.type != FileTypeRegular || infoBZ2.time <= info.time) { sync.timedUpgrade(_busyTimeout); + infoBZ2 = getFileInfo(path + ".bz2", false); if(infoBZ2.type != FileTypeRegular || infoBZ2.time <= info.time) { @@ -311,7 +271,7 @@ IcePatch::RegularI::getBZ2MD5(Int size, const Current& current) } } - return IcePatch::getPartialMD5(path + ".bz2", size); + return IcePatch::calcPartialMD5(path + ".bz2", size); } catch(const IceUtil::LockedException&) { diff --git a/cpp/src/IcePatch/Server.cpp b/cpp/src/IcePatch/Server.cpp index 4b24fd43fa8..1e804670ddd 100644 --- a/cpp/src/IcePatch/Server.cpp +++ b/cpp/src/IcePatch/Server.cpp @@ -134,9 +134,9 @@ IcePatch::Server::run(int argc, char* argv[]) properties->getPropertyAsIntWithDefault("IcePatch.UpdatePeriod", 60)); if(updatePeriod != IceUtil::Time()) { - if(updatePeriod < IceUtil::Time::seconds(10)) + if(updatePeriod < IceUtil::Time::seconds(1)) { - updatePeriod = IceUtil::Time::seconds(10); + updatePeriod = IceUtil::Time::seconds(1); } updater = new Updater(adapter, updatePeriod); updater->start(); @@ -198,18 +198,19 @@ IcePatch::Updater::run() // Just loop if we're busy. // } - catch(const Exception& ex) + catch(const ConnectFailedException&) { // - // If we are interrupted due to a shutdown, don't print - // any exceptions. Exceptions are normal in such case, for - // example, ObjectAdapterDeactivatedException. + // This exception can be raised if the adapter is shutdown + // while this thread is still running. In such case, we + // terminate this thread. // - if(!Application::isShutdownFromInterrupt()) - { - Error out(_logger); - out << "exception during update:\n" << ex; - } + break; + } + catch(const Exception& ex) + { + Error out(_logger); + out << "exception during update:\n" << ex; } if(_destroy) @@ -243,7 +244,7 @@ IcePatch::Updater::cleanup(const FileDescSeq& fileDescSeq) if(directoryDesc) { // - // Force .md5 files to be created and orphaned files to be + // Force MD5 files to be created and orphaned files to be // removed. // cleanup(directoryDesc->directory->getContents()); @@ -254,7 +255,7 @@ IcePatch::Updater::cleanup(const FileDescSeq& fileDescSeq) assert(regularDesc); // - // Force .bz2 files to be created. + // Force BZ2 files to be created. // regularDesc->regular->getBZ2Size(); } diff --git a/cpp/src/IcePatch/Util.cpp b/cpp/src/IcePatch/Util.cpp index d8ebeef999b..dbbed190626 100644 --- a/cpp/src/IcePatch/Util.cpp +++ b/cpp/src/IcePatch/Util.cpp @@ -165,7 +165,9 @@ IcePatch::getFileInfo(const string& path, bool exceptionIfNotExist) } else { - result.type = FileTypeUnknown; + FileAccessException ex; + ex.reason = "file type of `" + path + "' is not supported"; + throw ex; } return result; @@ -182,7 +184,7 @@ IcePatch::removeRecursive(const string& path) removeRecursive(*p); } } - + if(::remove(path.c_str()) == -1) { FileAccessException ex; @@ -296,6 +298,7 @@ IcePatch::getMD5(const string& path) ex.reason = "cannot open `" + pathMD5 + "' for reading: " + strerror(errno); throw ex; } + ByteSeq bytesMD5; bytesMD5.resize(16); fileMD5.read(&bytesMD5[0], 16); @@ -311,25 +314,26 @@ IcePatch::getMD5(const string& path) ex.reason = "could not read 16 bytes from `" + pathMD5 + "'"; throw ex; } + fileMD5.close(); + return bytesMD5; } -ByteSeq -IcePatch::getPartialMD5(const string& path, Int size) +void +IcePatch::createMD5(const string& path) { - if(size < 0) + FileInfo info = getFileInfo(path, true); + if(info.type == FileTypeDirectory) { FileAccessException ex; - ex.reason = "negative file size is illegal"; + ex.reason = "cannot create MD5 file for `" + path + "' because this is a directory"; throw ex; } - + // - // Read the original file partially. + // Read the original file. // - FileInfo info = getFileInfo(path, true); - size = std::min(size, static_cast<Int>(info.size)); ifstream file(path.c_str(), ios::binary); if(!file) { @@ -337,8 +341,9 @@ IcePatch::getPartialMD5(const string& path, Int size) ex.reason = "cannot open `" + path + "' for reading: " + strerror(errno); throw ex; } + ByteSeq bytes; - bytes.resize(size); + bytes.resize(info.size); file.read(&bytes[0], bytes.size()); if(!file) { @@ -352,93 +357,8 @@ IcePatch::getPartialMD5(const string& path, Int size) ex.reason = "could not read all bytes from `" + path + "'"; throw ex; } - file.close(); - // - // Create the MD5 hash value. - // - ByteSeq bytesMD5; - bytesMD5.resize(16); - MD5(reinterpret_cast<unsigned char*>(&bytes[0]), bytes.size(), reinterpret_cast<unsigned char*>(&bytesMD5[0])); - - return bytesMD5; -} - -void -IcePatch::createMD5(const string& path) -{ - FileInfo info = getFileInfo(path, true); - if(info.type == FileTypeUnknown) - { - FileAccessException ex; - ex.reason = "cannot create .md5 file for `" + path + "' because file type is unknown"; - throw ex; - } - - string pathMD5; - string pathMD5Temp; - ByteSeq bytes; - if(info.type == FileTypeDirectory) - { - pathMD5 = path + "/.md5"; - pathMD5Temp = path + "/.md5temp"; - - // - // Read all MD5 files in the directory. - // - StringSeq paths = readDirectory(path); - for(StringSeq::const_iterator p = paths.begin(); p != paths.end(); ++p) - { - if(!ignoreSuffix(*p)) - { - FileInfo subInfo = getFileInfo(*p, true); - - if(subInfo.type == FileTypeDirectory) - { - ByteSeq subBytesMD5 = getMD5(*p + "/.md5"); - copy(subBytesMD5.begin(), subBytesMD5.end(), back_inserter(bytes)); - } - else if(subInfo.type == FileTypeRegular) - { - ByteSeq subBytesMD5 = getMD5(removeSuffix(*p)); - copy(subBytesMD5.begin(), subBytesMD5.end(), back_inserter(bytes)); - } - } - } - } - else - { - assert(info.type == FileTypeRegular); - - pathMD5 = path + ".md5"; - pathMD5Temp = path + ".md5temp"; - - // - // Read the original file. - // - ifstream file(path.c_str(), ios::binary); - if(!file) - { - FileAccessException ex; - ex.reason = "cannot open `" + path + "' for reading: " + strerror(errno); - throw ex; - } - bytes.resize(info.size); - file.read(&bytes[0], bytes.size()); - if(!file) - { - FileAccessException ex; - ex.reason = "cannot read `" + path + "': " + strerror(errno); - throw ex; - } - if(file.gcount() < static_cast<int>(bytes.size())) - { - FileAccessException ex; - ex.reason = "could not read all bytes from `" + path + "'"; - throw ex; - } - file.close(); - } + file.close(); // // Create the MD5 hash value. @@ -450,6 +370,7 @@ IcePatch::createMD5(const string& path) // // Save the MD5 hash value to a temporary MD5 file. // + string pathMD5Temp = path + ".md5temp"; ofstream fileMD5(pathMD5Temp.c_str(), ios::binary); if(!fileMD5) { @@ -457,6 +378,7 @@ IcePatch::createMD5(const string& path) ex.reason = "cannot open `" + pathMD5Temp + "' for writing: " + strerror(errno); throw ex; } + fileMD5.write(&bytesMD5[0], 16); if(!fileMD5) { @@ -464,6 +386,7 @@ IcePatch::createMD5(const string& path) ex.reason = "cannot write `" + pathMD5Temp + "': " + strerror(errno); throw ex; } + fileMD5.close(); // @@ -471,6 +394,7 @@ IcePatch::createMD5(const string& path) // done so that there can be no partial MD5 files after an // abortive application termination. // + string pathMD5 = path + ".md5"; ::remove(pathMD5.c_str()); if(::rename(pathMD5Temp.c_str(), pathMD5.c_str()) == -1) { @@ -481,6 +405,64 @@ IcePatch::createMD5(const string& path) } ByteSeq +IcePatch::calcPartialMD5(const string& path, Int size) +{ + if(size < 0) + { + FileAccessException ex; + ex.reason = "negative file size is illegal"; + throw ex; + } + + FileInfo info = getFileInfo(path, true); + if(info.type == FileTypeDirectory) + { + FileAccessException ex; + ex.reason = "cannot calculate partial MD5 for `" + path + "' because this is a directory"; + throw ex; + } + + // + // Read the original file partially. + // + size = std::min(size, static_cast<Int>(info.size)); + ifstream file(path.c_str(), ios::binary); + if(!file) + { + FileAccessException ex; + ex.reason = "cannot open `" + path + "' for reading: " + strerror(errno); + throw ex; + } + + ByteSeq bytes; + bytes.resize(size); + file.read(&bytes[0], bytes.size()); + if(!file) + { + FileAccessException ex; + ex.reason = "cannot read `" + path + "': " + strerror(errno); + throw ex; + } + if(file.gcount() < static_cast<int>(bytes.size())) + { + FileAccessException ex; + ex.reason = "could not read all bytes from `" + path + "'"; + throw ex; + } + + file.close(); + + // + // Create the MD5 hash value. + // + ByteSeq bytesMD5; + bytesMD5.resize(16); + MD5(reinterpret_cast<unsigned char*>(&bytes[0]), bytes.size(), reinterpret_cast<unsigned char*>(&bytesMD5[0])); + + return bytesMD5; +} + +ByteSeq IcePatch::getBZ2(const string& path, Int pos, Int num) { if(pos < 0) @@ -512,6 +494,7 @@ IcePatch::getBZ2(const string& path, Int pos, Int num) ex.reason = "cannot open `" + pathBZ2 + "' for reading: " + strerror(errno); throw ex; } + fileBZ2.seekg(pos); if(!fileBZ2) { @@ -521,6 +504,7 @@ IcePatch::getBZ2(const string& path, Int pos, Int num) ex.reason = out.str(); throw ex; } + ByteSeq bytesBZ2; bytesBZ2.resize(num); fileBZ2.read(&bytesBZ2[0], bytesBZ2.size()); @@ -530,8 +514,11 @@ IcePatch::getBZ2(const string& path, Int pos, Int num) ex.reason = "cannot read `" + pathBZ2 + "': " + strerror(errno); throw ex; } + bytesBZ2.resize(fileBZ2.gcount()); + fileBZ2.close(); + return bytesBZ2; } @@ -539,16 +526,10 @@ void IcePatch::createBZ2(const string& path) { FileInfo info = getFileInfo(path, true); - if(info.type == FileTypeUnknown) - { - FileAccessException ex; - ex.reason = "cannot create .bz2 file for `" + path + "' because file type is unknown"; - throw ex; - } if(info.type == FileTypeDirectory) { FileAccessException ex; - ex.reason = "cannot create .bz2 file for `" + path + "' because this is a directory"; + ex.reason = "cannot create BZ2 file for `" + path + "' because this is a directory"; throw ex; } @@ -564,7 +545,6 @@ IcePatch::createBZ2(const string& path) throw ex; } - string pathBZ2 = path + ".bz2"; string pathBZ2Temp = path + ".bz2temp"; FILE* stdioFileBZ2 = fopen(pathBZ2Temp.c_str(), "wb"); if(!stdioFileBZ2) @@ -642,6 +622,7 @@ IcePatch::createBZ2(const string& path) // done so that there can be no partial BZ2 files after an // abortive application termination. // + string pathBZ2 = path + ".bz2"; ::remove(pathBZ2.c_str()); if(::rename(pathBZ2Temp.c_str(), pathBZ2.c_str()) == -1) { @@ -666,7 +647,7 @@ IcePatch::getRegular(const RegularPrx& regular, ProgressCB& progressCB) if(infoBZ2.type == FileTypeRegular) { ByteSeq remoteBZ2MD5 = regular->getBZ2MD5(infoBZ2.size); - ByteSeq localBZ2MD5 = getPartialMD5(pathBZ2, infoBZ2.size); + ByteSeq localBZ2MD5 = calcPartialMD5(pathBZ2, infoBZ2.size); if(remoteBZ2MD5 == localBZ2MD5) { |