diff options
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/Ice/Instance.cpp | 6 | ||||
-rw-r--r-- | cpp/src/Ice/Network.cpp | 50 | ||||
-rw-r--r-- | cpp/src/Ice/Network.h | 1 | ||||
-rw-r--r-- | cpp/src/Ice/ThreadPool.cpp | 2 | ||||
-rw-r--r-- | cpp/src/IcePatch/Client.cpp | 58 | ||||
-rw-r--r-- | cpp/src/IcePatch/NodeLocator.cpp | 6 | ||||
-rw-r--r-- | cpp/src/IcePatch/Server.cpp | 95 | ||||
-rw-r--r-- | cpp/src/IcePatch/Util.cpp | 656 |
8 files changed, 417 insertions, 457 deletions
diff --git a/cpp/src/Ice/Instance.cpp b/cpp/src/Ice/Instance.cpp index 199e5e1b4da..d883f3dbb0f 100644 --- a/cpp/src/Ice/Instance.cpp +++ b/cpp/src/Ice/Instance.cpp @@ -277,7 +277,11 @@ IceInternal::Instance::Instance(const CommunicatorPtr& communicator, const Prope #endif _traceLevels = new TraceLevels(_properties); _defaultProtocol = _properties->getPropertyWithDefault("Ice.DefaultProtocol", "tcp"); - _defaultHost = _properties->getPropertyWithDefault("Ice.DefaultHost", "127.0.0.1"); + _defaultHost = _properties->getProperty("Ice.DefaultHost"); + if (_defaultHost.empty()) + { + _defaultHost = getLocalHost(true); + } _routerManager = new RouterManager; _referenceFactory = new ReferenceFactory(this); _proxyFactory = new ProxyFactory(this); diff --git a/cpp/src/Ice/Network.cpp b/cpp/src/Ice/Network.cpp index f774fa8b8f5..6b20964273c 100644 --- a/cpp/src/Ice/Network.cpp +++ b/cpp/src/Ice/Network.cpp @@ -43,7 +43,9 @@ bool IceInternal::acceptInterrupted() { if (interrupted()) + { return true; + } #ifdef _WIN32 int error = WSAGetLastError(); @@ -610,6 +612,54 @@ IceInternal::getAddress(const string& host, int port, struct sockaddr_in& addr) } } +string +IceInternal::getLocalHost(bool numeric) +{ + char host[1024 + 1]; + if (gethostname(host, 1024) == SOCKET_ERROR) + { + SystemException ex(__FILE__, __LINE__); + ex.error = getSystemErrno(); + throw ex; + } + + { + IceUtil::Mutex::Lock sync(getHostByNameMutex); + + struct hostent* entry; + int retry = 5; + + do + { + entry = gethostbyname(host); + } +#ifdef WIN32 + while (!entry && WSAGetLastError() == WSATRY_AGAIN && --retry >= 0); +#else + while (!entry && h_errno == TRY_AGAIN && --retry >= 0); +#endif + + if (!entry) + { + DNSException ex(__FILE__, __LINE__); + ex.error = getDNSErrno(); + throw ex; + } + + if (numeric) + { + struct sockaddr_in addr; + memset(&addr, 0, sizeof(struct sockaddr_in)); + memcpy(&addr.sin_addr, entry->h_addr, entry->h_length); + return string(inet_ntoa(addr.sin_addr)); + } + else + { + return string(entry->h_name); + } + } +} + bool IceInternal::compareAddress(const struct sockaddr_in& addr1, const struct sockaddr_in& addr2) { diff --git a/cpp/src/Ice/Network.h b/cpp/src/Ice/Network.h index f4526776a46..3fbe2108971 100644 --- a/cpp/src/Ice/Network.h +++ b/cpp/src/Ice/Network.h @@ -82,6 +82,7 @@ void doConnect(SOCKET, struct sockaddr_in&, int); SOCKET doAccept(SOCKET, int); void getAddress(const std::string&, int, struct sockaddr_in&); +std::string getLocalHost(bool); bool compareAddress(const struct sockaddr_in&, const struct sockaddr_in&); void createPipe(SOCKET fds[2]); diff --git a/cpp/src/Ice/ThreadPool.cpp b/cpp/src/Ice/ThreadPool.cpp index 6632fc6b578..6b7827663aa 100644 --- a/cpp/src/Ice/ThreadPool.cpp +++ b/cpp/src/Ice/ThreadPool.cpp @@ -110,7 +110,7 @@ IceInternal::ThreadPool::joinWithAllThreads() // for (vector<IceUtil::ThreadControl>::iterator p = _threads.begin(); p != _threads.end(); ++p) { - (*p).join(); + p->join(); } } diff --git a/cpp/src/IcePatch/Client.cpp b/cpp/src/IcePatch/Client.cpp index d365edcb918..528d5c6eda3 100644 --- a/cpp/src/IcePatch/Client.cpp +++ b/cpp/src/IcePatch/Client.cpp @@ -110,13 +110,6 @@ IcePatch::Client::run(int argc, char* argv[]) cout << pathToName(path) << endl; cout << "|" << endl; patch(topDesc->directory->getContents(), ""); - - // - // Remove orphaned MD5 and BZ2 files. - // - cout << "removing orphaned .bz2 and .md5 files... " << flush; - removeOrphanedRecursive("."); - cout << "ok" << endl; } catch (const NodeAccessException& ex) { @@ -131,21 +124,36 @@ class MyProgressCB : public ProgressCB { public: - virtual void start(Int) + virtual void startDownload(Int) { - cout << " 0% " << flush; + cout << "download 0% " << flush; } - virtual void update(Int total, Int pos) + virtual void updateDownload(Int total, Int pos) { Ice::Int percent = pos * 100 / total; cout << "\b\b\b\b\b" << setw(3) << percent << "% " << flush; } - virtual void finished(Int total) + virtual void finishedDownload(Int total) { cout << "\b\b\b\b\b" << "100% " << flush; } + + virtual void startUncompress(Int) + { + cout << "uncompress 0% " << flush; + } + + virtual void updateUncompress(Int total, Int pos) + { + updateDownload(total, pos); + } + + virtual void finishedUncompress(Int total) + { + finishedDownload(total); + } }; void @@ -188,21 +196,21 @@ IcePatch::Client::patch(const NodeDescSeq& nodeDescSeq, const string& indent) cout << indent << "+-" << pathToName(path) << ": " << flush; FileInfo info = getFileInfo(path); - switch (info) + switch (info.type) { - case FileInfoNotExist: + case FileTypeNotExist: { cout << "creating directory... " << flush; createDirectory(path); break; } - case FileInfoDirectory: + case FileTypeDirectory: { break; } - case FileInfoRegular: + case FileTypeRegular: { cout << "removing file... " << flush; removeRecursive(path); @@ -211,7 +219,7 @@ IcePatch::Client::patch(const NodeDescSeq& nodeDescSeq, const string& indent) break; } - case FileInfoUnknown: + case FileTypeUnknown: { cout << "removing unknown file... " << flush; removeRecursive(path); @@ -234,16 +242,16 @@ IcePatch::Client::patch(const NodeDescSeq& nodeDescSeq, const string& indent) MyProgressCB progressCB; FileInfo info = getFileInfo(path); - switch (info) + switch (info.type) { - case FileInfoNotExist: + case FileTypeNotExist: { cout << "getting file... " << flush; getFile(fileDesc->file, progressCB); break; } - case FileInfoDirectory: + case FileTypeDirectory: { cout << "removing directory... " << flush; removeRecursive(path); @@ -252,14 +260,17 @@ IcePatch::Client::patch(const NodeDescSeq& nodeDescSeq, const string& indent) break; } - case FileInfoRegular: + case FileTypeRegular: { ByteSeq md5; - FileInfo infoMD5 = getFileInfo(path + ".md5"); - if (infoMD5 == FileInfoRegular) + + string pathMD5 = path + ".md5"; + FileInfo infoMD5 = getFileInfo(pathMD5); + if (infoMD5.type == FileTypeRegular && infoMD5.time >= info.time) { md5 = getMD5(path); } + if (md5 != fileDesc->md5) { cout << "removing file... " << flush; @@ -267,10 +278,11 @@ IcePatch::Client::patch(const NodeDescSeq& nodeDescSeq, const string& indent) cout << "getting file... " << flush; getFile(fileDesc->file, progressCB); } + break; } - case FileInfoUnknown: + case FileTypeUnknown: { cout << "removing unknown file... " << flush; removeRecursive(path); diff --git a/cpp/src/IcePatch/NodeLocator.cpp b/cpp/src/IcePatch/NodeLocator.cpp index 30235cea2db..c5af823e5c2 100644 --- a/cpp/src/IcePatch/NodeLocator.cpp +++ b/cpp/src/IcePatch/NodeLocator.cpp @@ -61,14 +61,14 @@ IcePatch::NodeLocator::locate(const ObjectAdapterPtr& adapter, const Current& cu return 0; } - switch (info) + switch (info.type) { - case FileInfoDirectory: + case FileTypeDirectory: { return _directory; } - case FileInfoRegular: + case FileTypeRegular: { return _file; } diff --git a/cpp/src/IcePatch/Server.cpp b/cpp/src/IcePatch/Server.cpp index 3d515aacfd9..9046280762d 100644 --- a/cpp/src/IcePatch/Server.cpp +++ b/cpp/src/IcePatch/Server.cpp @@ -25,6 +25,8 @@ public: void usage(); virtual int run(int, char*[]); + void removeOrphanedRecursive(const string&); + void updateRecursive(const string&); }; }; @@ -90,14 +92,10 @@ IcePatch::Server::run(int argc, char* argv[]) // // Remove orphaned MD5 and BZ2 files. + // Create MD5 and BZ2 files. // removeOrphanedRecursive("."); - - // - // Create MD5 and BZ2 files. - // - createMD5Recursive("."); - createBZ2Recursive("."); + updateRecursive("."); // // Create and initialize the object adapter and the node locator. @@ -121,6 +119,91 @@ IcePatch::Server::run(int argc, char* argv[]) return EXIT_SUCCESS; } +void +IcePatch::Server::removeOrphanedRecursive(const string& path) +{ + assert(getFileInfo(path).type == FileTypeDirectory); + + StringSeq paths = readDirectory(path); + StringSeq::const_iterator p; + for (p = paths.begin(); p != paths.end(); ++p) + { + string suffix = getSuffix(*p); + if (suffix == "md5" || suffix == "bz2") + { + pair<StringSeq::const_iterator, StringSeq::const_iterator> r = + equal_range(paths.begin(), paths.end(), removeSuffix(*p)); + if (r.first == r.second) + { + cout << "removing orphaned file `" << *p << "'... " << flush; + removeRecursive(*p); + cout << "ok" << endl; + } + } + else + { + if (getFileInfo(*p).type == FileTypeDirectory) + { + removeOrphanedRecursive(*p); + } + } + } + + if (readDirectory(path).empty()) + { + cout << "removing empty directory `" << *p << "'... " << flush; + removeRecursive(path); + cout << "ok" << endl; + } +} + +void +IcePatch::Server::updateRecursive(const string& path) +{ + string suffix = getSuffix(path); + if (suffix == "md5" || suffix == "bz2") + { + return; + } + + if (pathToName(path) == tmpName) + { + return; + } + + FileInfo info = getFileInfo(path); + + if (info.type == FileTypeDirectory) + { + StringSeq paths = readDirectory(path); + StringSeq::const_iterator p; + for (p = paths.begin(); p != paths.end(); ++p) + { + updateRecursive(*p); + } + } + else if (info.type == FileTypeRegular) + { + string pathMD5 = path + ".md5"; + FileInfo infoMD5 = getFileInfo(pathMD5); + if (infoMD5.type != FileTypeRegular || infoMD5.time < info.time) + { + cout << "creating .md5 file for `" << path << "'... " << flush; + createMD5(pathMD5); + cout << "ok" << endl; + } + + string pathBZ2 = path + ".bz2"; + FileInfo infoBZ2 = getFileInfo(pathBZ2); + if (infoBZ2.type != FileTypeRegular || infoBZ2.time < info.time) + { + cout << "creating .bz2 file for `" << path << "'... " << flush; + createBZ2(pathBZ2); + cout << "ok" << endl; + } + } +} + int main(int argc, char* argv[]) { diff --git a/cpp/src/IcePatch/Util.cpp b/cpp/src/IcePatch/Util.cpp index e6b46ad3bd6..f45ea5fcccd 100644 --- a/cpp/src/IcePatch/Util.cpp +++ b/cpp/src/IcePatch/Util.cpp @@ -11,7 +11,6 @@ #include <IcePatch/Util.h> #include <IcePatch/Node.h> #include <fstream> -#include <sys/types.h> #include <sys/stat.h> #include <openssl/md5.h> #include <bzlib.h> @@ -128,7 +127,10 @@ IcePatch::getFileInfo(const string& path) { if (errno == ENOENT) { - return FileInfoNotExist; + FileInfo result; + result.size = 0; + result.time = 0; + result.type = FileTypeNotExist; } else { @@ -138,23 +140,30 @@ IcePatch::getFileInfo(const string& path) } } + FileInfo result; + result.size = buf.st_size; + result.time = buf.st_mtime; + if (S_ISDIR(buf.st_mode)) { - return FileInfoDirectory; + result.type = FileTypeDirectory; } - - if (S_ISREG(buf.st_mode)) + else if (S_ISREG(buf.st_mode)) { - return FileInfoRegular; + result.type = FileTypeRegular; } - - return FileInfoUnknown; + else + { + result.type = FileTypeUnknown; + } + + return result; } void IcePatch::removeRecursive(const string& path) { - if (getFileInfo(path) == FileInfoDirectory) + if (getFileInfo(path).type == FileTypeDirectory) { StringSeq paths = readDirectory(path); StringSeq::const_iterator p; @@ -309,169 +318,80 @@ IcePatch::getMD5(const string& path) void IcePatch::createMD5(const string& path) { - if (pathToName(path) == tmpName) + assert(pathToName(path) != tmpName); + FileInfo info = getFileInfo(path); + assert(info.type == FileTypeRegular); + + // + // Read the original file. + // + ifstream file(path.c_str(), ios::binary); + if (!file) { - return; + NodeAccessException ex; + ex.reason = "cannot open `" + path + "' for reading: " + strerror(errno); + throw ex; } - string suffix = getSuffix(path); - if (suffix == "md5" || suffix == "bz2") + ByteSeq bytes; + bytes.resize(info.size); + file.read(&bytes[0], bytes.size()); + if (!file) { - return; + NodeAccessException ex; + ex.reason = "cannot read `" + path + "': " + strerror(errno); + throw ex; } - // - // Stat the file to get a MD5 hash value for. - // - struct stat buf; - if (::stat(path.c_str(), &buf) == -1) + if (file.gcount() < static_cast<int>(bytes.size())) { NodeAccessException ex; - ex.reason = "cannot stat `" + path + "': " + strerror(errno); + ex.reason = "could not read all bytes from `" + path + "'"; throw ex; } - else - { - if (!S_ISREG(buf.st_mode)) - { - NodeAccessException ex; - ex.reason = "`" + path + "' is not a regular file"; - throw ex; - } - } + file.close(); + // - // Stat the MD5 file. If it doesn't exist, or if it's outdated, - // set a flag to create a new MD5 hash value. + // Create the MD5 hash value. // - struct stat bufMD5; - string pathMD5 = path + ".md5"; - bool makeMD5 = false; - if (::stat(pathMD5.c_str(), &bufMD5) == -1) - { - if (errno == ENOENT) - { - makeMD5 = true; - } - else - { - NodeAccessException ex; - ex.reason = "cannot stat `" + path + "': " + strerror(errno); - throw ex; - } - } - else + ByteSeq bytesMD5; + bytesMD5.resize(16); + MD5(reinterpret_cast<unsigned char*>(&bytes[0]), bytes.size(), reinterpret_cast<unsigned char*>(&bytesMD5[0])); + + // + // Save the MD5 hash value to the MD5 file. + // + ofstream fileMD5(tmpName.c_str(), ios::binary); + if (!fileMD5) { - if (!S_ISREG(bufMD5.st_mode)) - { - NodeAccessException ex; - ex.reason = "`" + pathMD5 + "' is not a regular file"; - throw ex; - } - - if (bufMD5.st_size != 16) - { - NodeAccessException ex; - ex.reason = "`" + pathMD5 + "' isn't 16 bytes in size"; - throw ex; - } - - if (bufMD5.st_mtime <= buf.st_mtime) - { - makeMD5 = true; - } + NodeAccessException ex; + ex.reason = "cannot open `" + tmpName + "' for writing: " + strerror(errno); + throw ex; } - if (makeMD5) + fileMD5.write(&bytesMD5[0], 16); + if (!fileMD5) { - ByteSeq bytes; - bytes.resize(buf.st_size); - - // - // Read the original file. - // - { - ifstream file(path.c_str(), ios::binary); - if (!file) - { - NodeAccessException ex; - ex.reason = "cannot open `" + path + "' for reading: " + strerror(errno); - throw ex; - } - file.read(&bytes[0], bytes.size()); - if (!file) - { - NodeAccessException ex; - ex.reason = "cannot read `" + path + "': " + strerror(errno); - throw ex; - } - if (file.gcount() < static_cast<int>(bytes.size())) - { - NodeAccessException ex; - ex.reason = "could not read all bytes from `" + path + "'"; - throw ex; - } - } - - // - // 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])); - - // - // Save the MD5 hash value to the MD5 file. - // - { - ofstream fileMD5(tmpName.c_str(), ios::binary); - if (!fileMD5) - { - NodeAccessException ex; - ex.reason = "cannot open `" + tmpName + "' for writing: " + strerror(errno); - throw ex; - } - fileMD5.write(&bytesMD5[0], 16); - if (!fileMD5) - { - NodeAccessException ex; - ex.reason = "cannot write `" + tmpName + "': " + strerror(errno); - throw ex; - } - } - - // - // Rename the temporary MD5 file to the "real" MD5 file. This - // is done so that there can be no partial MD5 files after an - // abortive application termination. - // - ::remove(pathMD5.c_str()); - if (rename(tmpName.c_str(), pathMD5.c_str()) == -1) - { - NodeAccessException ex; - ex.reason = "cannot rename `" + tmpName + "' to `" + pathMD5 + "': " + strerror(errno); - throw ex; - } + NodeAccessException ex; + ex.reason = "cannot write `" + tmpName + "': " + strerror(errno); + throw ex; } -} - -void -IcePatch::createMD5Recursive(const string& path) -{ - FileInfo info = getFileInfo(path); - if (info == FileInfoDirectory) - { - StringSeq paths = readDirectory(path); - StringSeq::const_iterator p; - for (p = paths.begin(); p != paths.end(); ++p) - { - createMD5Recursive(*p); - } - } - else if (info == FileInfoRegular) + fileMD5.close(); + + // + // Rename the temporary MD5 file to the "real" MD5 file. This + // is 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(tmpName.c_str(), pathMD5.c_str()) == -1) { - createMD5(path); + NodeAccessException ex; + ex.reason = "cannot rename `" + tmpName + "' to `" + pathMD5 + "': " + strerror(errno); + throw ex; } } @@ -525,360 +445,250 @@ IcePatch::getBytesBZ2(const string& path, Int pos, Int num) void IcePatch::createBZ2(const string& path) { - if (pathToName(path) == tmpName) - { - return; - } - - string suffix = getSuffix(path); - if (suffix == "md5" || suffix == "bz2") - { - return; - } + assert(pathToName(path) != tmpName); + FileInfo info = getFileInfo(path); + assert(info.type == FileTypeRegular); // - // Stat the file to get a BZ2 file for. + // Read the original file in blocks and write the BZ2 file. // - struct stat buf; - if (::stat(path.c_str(), &buf) == -1) + ifstream file(path.c_str(), ios::binary); + if (!file) { NodeAccessException ex; - ex.reason = "cannot stat `" + path + "': " + strerror(errno); + ex.reason = "cannot open `" + path + "' for reading: " + strerror(errno); throw ex; } - else + + FILE* stdioFileBZ2 = fopen(tmpName.c_str(), "wb"); + if (!stdioFileBZ2) { - if (!S_ISREG(buf.st_mode)) - { - NodeAccessException ex; - ex.reason = "`" + path + "' is not a regular file"; - throw ex; - } + NodeAccessException ex; + ex.reason = "cannot open `" + tmpName + "' for writing: " + strerror(errno); + throw ex; } - - // - // Stat the BZ2 file. If it doesn't exist, or if it's outdated, - // set a flag to create a new BZ2 file. - // - struct stat bufBZ2; - string pathBZ2 = path + ".bz2"; - bool makeBZ2 = false; - if (::stat(pathBZ2.c_str(), &bufBZ2) == -1) + + int bzError; + BZFILE* bzFile = BZ2_bzWriteOpen(&bzError, stdioFileBZ2, 5, 0, 0); + if (bzError != BZ_OK) { - if (errno == ENOENT) - { - makeBZ2 = true; - } - else + NodeAccessException ex; + ex.reason = "BZ2_bzWriteOpen failed"; + if (bzError == BZ_IO_ERROR) { - NodeAccessException ex; - ex.reason = "cannot stat `" + path + "': " + strerror(errno); - throw ex; + ex.reason += string(": ") + strerror(errno); } + fclose(stdioFileBZ2); + throw ex; } - else + + static const Int num = 64 * 1024; + Byte bytes[num]; + + while (!file.eof()) { - if (!S_ISREG(bufBZ2.st_mode)) + file.read(bytes, num); + if (!file && !file.eof()) { NodeAccessException ex; - ex.reason = "`" + pathBZ2 + "' is not a regular file"; + ex.reason = "cannot read `" + path + "': " + strerror(errno); + BZ2_bzWriteClose(&bzError, bzFile, 0, 0, 0); + fclose(stdioFileBZ2); throw ex; } - - if (bufBZ2.st_mtime <= buf.st_mtime) - { - makeBZ2 = true; - } - } - - if (makeBZ2) - { - // - // Read the original file in blocks and write the BZ2 file. - // + + if (file.gcount() > 0) { - ifstream file(path.c_str(), ios::binary); - if (!file) - { - NodeAccessException ex; - ex.reason = "cannot open `" + path + "' for reading: " + strerror(errno); - throw ex; - } - - FILE* fileBZ2 = fopen(tmpName.c_str(), "wb"); - if (!fileBZ2) - { - NodeAccessException ex; - ex.reason = "cannot open `" + tmpName + "' for writing: " + strerror(errno); - throw ex; - } - - int bzError; - BZFILE* bzFile = BZ2_bzWriteOpen(&bzError, fileBZ2, 5, 0, 0); - if (bzError != BZ_OK) - { - NodeAccessException ex; - ex.reason = "BZ2_bzWriteOpen failed"; - if (bzError == BZ_IO_ERROR) - { - ex.reason += string(": ") + strerror(errno); - } - fclose(fileBZ2); - throw ex; - } - - static const Int num = 64 * 1024; - Byte bytes[num]; - - while (!file.eof()) - { - file.read(bytes, num); - if (!file && !file.eof()) - { - NodeAccessException ex; - ex.reason = "cannot read `" + path + "': " + strerror(errno); - BZ2_bzWriteClose(&bzError, bzFile, 0, 0, 0); - fclose(fileBZ2); - throw ex; - } - - if (file.gcount() > 0) - { - BZ2_bzWrite(&bzError, bzFile, bytes, file.gcount()); - if (bzError != BZ_OK) - { - NodeAccessException ex; - ex.reason = "BZ2_bzWrite failed"; - if (bzError == BZ_IO_ERROR) - { - ex.reason += string(": ") + strerror(errno); - } - BZ2_bzWriteClose(&bzError, bzFile, 0, 0, 0); - fclose(fileBZ2); - throw ex; - } - } - } - - BZ2_bzWriteClose(&bzError, bzFile, 0, 0, 0); + BZ2_bzWrite(&bzError, bzFile, bytes, file.gcount()); if (bzError != BZ_OK) { NodeAccessException ex; - ex.reason = "BZ2_bzWriteClose failed"; + ex.reason = "BZ2_bzWrite failed"; if (bzError == BZ_IO_ERROR) { ex.reason += string(": ") + strerror(errno); } - fclose(fileBZ2); + BZ2_bzWriteClose(&bzError, bzFile, 0, 0, 0); + fclose(stdioFileBZ2); throw ex; } - - fclose(fileBZ2); - } - - // - // Rename the temporary BZ2 file to the "real" BZ2 file. This - // is done so that there can be no partial BZ2 files after an - // abortive application termination. - // - ::remove(pathBZ2.c_str()); - if (rename(tmpName.c_str(), pathBZ2.c_str()) == -1) - { - NodeAccessException ex; - ex.reason = "cannot rename `" + tmpName + "' to `" + pathBZ2 + "': " + strerror(errno); - throw ex; } } -} - -void -IcePatch::createBZ2Recursive(const string& path) -{ - FileInfo info = getFileInfo(path); - - if (info == FileInfoDirectory) - { - StringSeq paths = readDirectory(path); - StringSeq::const_iterator p; - for (p = paths.begin(); p != paths.end(); ++p) - { - createBZ2Recursive(*p); - } - } - else if (info == FileInfoRegular) - { - createBZ2(path); - } -} - -void -IcePatch::removeOrphanedRecursive(const string& path) -{ - assert(getFileInfo(path) == FileInfoDirectory); - StringSeq paths = readDirectory(path); - StringSeq::const_iterator p; - for (p = paths.begin(); p != paths.end(); ++p) + BZ2_bzWriteClose(&bzError, bzFile, 0, 0, 0); + if (bzError != BZ_OK) { - string suffix = getSuffix(*p); - if (suffix == "md5" || suffix == "bz2") - { - pair<StringSeq::const_iterator, StringSeq::const_iterator> r = - equal_range(paths.begin(), paths.end(), removeSuffix(*p)); - if (r.first == r.second) - { - removeRecursive(*p); - } - } - else + NodeAccessException ex; + ex.reason = "BZ2_bzWriteClose failed"; + if (bzError == BZ_IO_ERROR) { - if (getFileInfo(*p) == FileInfoDirectory) - { - removeOrphanedRecursive(*p); - } + ex.reason += string(": ") + strerror(errno); } + fclose(stdioFileBZ2); + throw ex; } - - if (readDirectory(path).empty()) + + fclose(stdioFileBZ2); + file.close(); + + // + // Rename the temporary BZ2 file to the "real" BZ2 file. This + // is 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(tmpName.c_str(), pathBZ2.c_str()) == -1) { - removeRecursive(path); + NodeAccessException ex; + ex.reason = "cannot rename `" + tmpName + "' to `" + pathBZ2 + "': " + strerror(errno); + throw ex; } } void -IcePatch::getFile(const FilePrx& file, ProgressCB& progressCB) +IcePatch::getFile(const FilePrx& filePrx, ProgressCB& progressCB) { - string path = identityToPath(file->ice_getIdentity()); + string path = identityToPath(filePrx->ice_getIdentity()); string pathBZ2 = path + ".bz2"; + Int totalBZ2 = filePrx->getSizeBZ2(); // // Get the BZ2 file. // + progressCB.startDownload(totalBZ2); + + ofstream fileBZ2(pathBZ2.c_str(), ios::binary); + if (!fileBZ2) + { + NodeAccessException ex; + ex.reason = "cannot open `" + pathBZ2 + "' for writing: " + strerror(errno); + throw ex; + } + + ByteSeq bytesBZ2; + Int pos = 0; + while(pos < totalBZ2) { - Int totalBZ2 = file->getSizeBZ2(); - progressCB.start(totalBZ2); + static const Int num = 64 * 1024; - ofstream fileBZ2(pathBZ2.c_str(), ios::binary); + bytesBZ2 = filePrx->getBytesBZ2(pos, num); + if (bytesBZ2.empty()) + { + break; + } + + pos += bytesBZ2.size(); + + fileBZ2.write(&bytesBZ2[0], bytesBZ2.size()); if (!fileBZ2) { NodeAccessException ex; - ex.reason = "cannot open `" + pathBZ2 + "' for writing: " + strerror(errno); + ex.reason = "cannot write `" + pathBZ2 + "': " + strerror(errno); throw ex; } - ByteSeq bytesBZ2; - Int pos = 0; - while(pos < totalBZ2) + + if (static_cast<Int>(bytesBZ2.size()) < num) { - static const Int num = 64 * 1024; - - bytesBZ2 = file->getBytesBZ2(pos, num); - if (bytesBZ2.empty()) - { - break; - } - - pos += bytesBZ2.size(); - - fileBZ2.write(&bytesBZ2[0], bytesBZ2.size()); - if (!fileBZ2) - { - NodeAccessException ex; - ex.reason = "cannot write `" + pathBZ2 + "': " + strerror(errno); - throw ex; - } - - if (static_cast<Int>(bytesBZ2.size()) < num) - { - break; - } - - progressCB.update(totalBZ2, pos); + break; } - progressCB.finished(totalBZ2); + progressCB.updateDownload(totalBZ2, pos); } + + progressCB.finishedDownload(totalBZ2); + + fileBZ2.close(); // // Read the BZ2 file in blocks and write the original file. // + ofstream file(path.c_str(), ios::binary); + if (!file) { - ofstream file(path.c_str(), ios::binary); - if (!file) - { - NodeAccessException ex; - ex.reason = "cannot open `" + path + "' for writing: " + strerror(errno); - throw ex; - } - - FILE* fileBZ2 = fopen(pathBZ2.c_str(), "rb"); - if (!fileBZ2) + NodeAccessException ex; + ex.reason = "cannot open `" + path + "' for writing: " + strerror(errno); + throw ex; + } + + FILE* stdioFileBZ2 = fopen(pathBZ2.c_str(), "rb"); + if (!stdioFileBZ2) + { + NodeAccessException ex; + ex.reason = "cannot open `" + pathBZ2 + "' for reading: " + strerror(errno); + throw ex; + } + + int bzError; + BZFILE* bzFile = BZ2_bzReadOpen(&bzError, stdioFileBZ2, 0, 0, 0, 0); + if (bzError != BZ_OK) + { + NodeAccessException ex; + ex.reason = "BZ2_bzReadOpen failed"; + if (bzError == BZ_IO_ERROR) { - NodeAccessException ex; - ex.reason = "cannot open `" + pathBZ2 + "' for reading: " + strerror(errno); - throw ex; + ex.reason += string(": ") + strerror(errno); } - - int bzError; - BZFILE* bzFile = BZ2_bzReadOpen(&bzError, fileBZ2, 0, 0, 0, 0); - if (bzError != BZ_OK) + fclose(stdioFileBZ2); + throw ex; + } + + static const Int num = 64 * 1024; + Byte bytes[num]; + + progressCB.startUncompress(totalBZ2); + int countBZ2 = 0; + + while (bzError != BZ_STREAM_END) + { + int sz = BZ2_bzRead(&bzError, bzFile, bytes, num); + if (bzError != BZ_OK && bzError != BZ_STREAM_END) { NodeAccessException ex; - ex.reason = "BZ2_bzReadOpen failed"; + ex.reason = "BZ2_bzRead failed"; if (bzError == BZ_IO_ERROR) { ex.reason += string(": ") + strerror(errno); } - fclose(fileBZ2); + BZ2_bzReadClose(&bzError, bzFile); + fclose(stdioFileBZ2); throw ex; } - static const Int num = 64 * 1024; - Byte bytes[num]; - - while (bzError != BZ_STREAM_END) + if (sz > 0) { - int sz = BZ2_bzRead(&bzError, bzFile, bytes, num); - if (bzError != BZ_OK && bzError != BZ_STREAM_END) + countBZ2 += sz; + progressCB.updateUncompress(totalBZ2, countBZ2); + + file.write(bytes, sz); + if (!file) { NodeAccessException ex; - ex.reason = "BZ2_bzRead failed"; - if (bzError == BZ_IO_ERROR) - { - ex.reason += string(": ") + strerror(errno); - } + ex.reason = "cannot write `" + path + "': " + strerror(errno); BZ2_bzReadClose(&bzError, bzFile); - fclose(fileBZ2); + fclose(stdioFileBZ2); throw ex; } - - if (sz > 0) - { - file.write(bytes, sz); - if (!file) - { - NodeAccessException ex; - ex.reason = "cannot write `" + path + "': " + strerror(errno); - BZ2_bzReadClose(&bzError, bzFile); - fclose(fileBZ2); - throw ex; - } - } } - - BZ2_bzReadClose(&bzError, bzFile); - if (bzError != BZ_OK) + } + + progressCB.finishedUncompress(totalBZ2); + + BZ2_bzReadClose(&bzError, bzFile); + if (bzError != BZ_OK) + { + NodeAccessException ex; + ex.reason = "BZ2_bzReadClose failed"; + if (bzError == BZ_IO_ERROR) { - NodeAccessException ex; - ex.reason = "BZ2_bzReadClose failed"; - if (bzError == BZ_IO_ERROR) - { - ex.reason += string(": ") + strerror(errno); - } - fclose(fileBZ2); - throw ex; + ex.reason += string(": ") + strerror(errno); } - fclose(fileBZ2); + fclose(stdioFileBZ2); + throw ex; } + + fclose(stdioFileBZ2); + file.close(); // // Remove the BZ2 file, it is not needed anymore. |