diff options
author | Marc Laukien <marc@zeroc.com> | 2004-11-23 21:31:25 +0000 |
---|---|---|
committer | Marc Laukien <marc@zeroc.com> | 2004-11-23 21:31:25 +0000 |
commit | 2d597e196ac2f75cf96516469281bd61a434c58d (patch) | |
tree | de75e9418d3e83e89b0bfff75de9e950d724abc9 /cpp | |
parent | more IcePatch2 (diff) | |
download | ice-2d597e196ac2f75cf96516469281bd61a434c58d.tar.bz2 ice-2d597e196ac2f75cf96516469281bd61a434c58d.tar.xz ice-2d597e196ac2f75cf96516469281bd61a434c58d.zip |
more IcePatch2
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/include/IcePatch2/Util.h | 4 | ||||
-rw-r--r-- | cpp/src/IcePatch2/Client.cpp | 54 | ||||
-rw-r--r-- | cpp/src/IcePatch2/Util.cpp | 84 |
3 files changed, 118 insertions, 24 deletions
diff --git a/cpp/include/IcePatch2/Util.h b/cpp/include/IcePatch2/Util.h index 097392c1cc3..508ebc568a3 100644 --- a/cpp/include/IcePatch2/Util.h +++ b/cpp/include/IcePatch2/Util.h @@ -34,8 +34,8 @@ ICEPATCH2_API void removeRecursive(const std::string&); ICEPATCH2_API Ice::StringSeq readDirectory(const std::string&); ICEPATCH2_API void createDirectoryRecursive(const std::string&); -ICEPATCH2_API void compressToFile(const std::string&, const Ice::ByteSeq&, Ice::Int); -ICEPATCH2_API void uncompressToFile(const std::string&, const Ice::ByteSeq&, Ice::Int); +ICEPATCH2_API void compressBytesToFile(const std::string&, const Ice::ByteSeq&, Ice::Int); +ICEPATCH2_API void uncompressFile(const std::string&); struct FileInfoCompare : public std::binary_function<const FileInfo&, const FileInfo&, bool> { diff --git a/cpp/src/IcePatch2/Client.cpp b/cpp/src/IcePatch2/Client.cpp index f8bf5578bd2..2d1fb1536e8 100644 --- a/cpp/src/IcePatch2/Client.cpp +++ b/cpp/src/IcePatch2/Client.cpp @@ -122,15 +122,37 @@ IcePatch2::Client::run(int argc, char* argv[]) throw "cannot change directory to `" + dataDir + "': " + strerror(errno); } - if(thorough) + if(!thorough) { - getFileInfoSeq(".", infoSeq, false, false); + try + { + loadFileInfoSeq(dataDir + ".sum", infoSeq); + } + catch(const string& ex) + { + cout << "Cannot load file summary:\n" << ex << 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 EXIT_SUCCESS; + } + } + while(answer != "yes"); + thorough = true; + } } - else + + if(thorough) { - loadFileInfoSeq(dataDir + ".sum", infoSeq); + getFileInfoSeq(".", infoSeq, false, false); + saveFileInfoSeq(dataDir + ".sum", infoSeq); } - + sort(infoSeq.begin(), infoSeq.end(), FileInfoCompare()); const char* endpointsProperty = "IcePatch2.Endpoints"; @@ -260,7 +282,7 @@ IcePatch2::Client::run(int argc, char* argv[]) else // Regular file. { string pathBZ2 = p->path + ".bz2"; - ofstream os; + ofstream fileBZ2; if(!dry) { @@ -278,8 +300,8 @@ IcePatch2::Client::run(int argc, char* argv[]) { } - os.open(pathBZ2.c_str(), ios::binary); - if(!os) + fileBZ2.open(pathBZ2.c_str(), ios::binary); + if(!fileBZ2) { cerr << argv[0] << ": cannot open `" + pathBZ2 + "' for writing: " + strerror(errno); return EXIT_FAILURE; @@ -311,9 +333,9 @@ IcePatch2::Client::run(int argc, char* argv[]) if(!dry) { - os.write(reinterpret_cast<char*>(&bytes[0]), bytes.size()); + fileBZ2.write(reinterpret_cast<char*>(&bytes[0]), bytes.size()); - if(!os.good()) + if(!fileBZ2) { cerr << argv[0] << ": cannot write `" + pathBZ2 + "': " + strerror(errno); return EXIT_FAILURE; @@ -335,15 +357,9 @@ IcePatch2::Client::run(int argc, char* argv[]) if(!dry) { - os.close(); - - try - { -// removeRecursive(p->path); - } - catch(...) - { - } + fileBZ2.close(); + uncompressFile(p->path); + remove(pathBZ2); } } diff --git a/cpp/src/IcePatch2/Util.cpp b/cpp/src/IcePatch2/Util.cpp index 4220ce0bb0d..92abb5b039d 100644 --- a/cpp/src/IcePatch2/Util.cpp +++ b/cpp/src/IcePatch2/Util.cpp @@ -388,7 +388,7 @@ IcePatch2::createDirectoryRecursive(const string& pa) } void -IcePatch2::compressToFile(const string& pa, const ByteSeq& bytes, Int pos) +IcePatch2::compressBytesToFile(const string& pa, const ByteSeq& bytes, Int pos) { const string path = normalize(pa); @@ -440,9 +440,87 @@ IcePatch2::compressToFile(const string& pa, const ByteSeq& bytes, Int pos) } void -IcePatch2::uncompressToFile(const string& pa, const ByteSeq& bytes, Int pos) +IcePatch2::uncompressFile(const string& pa) { const string path = normalize(pa); + const string pathBZ2 = path + ".bz2"; + + ofstream file(path.c_str(), ios::binary); + if(!file) + { + throw "cannot open `" + path + "' for writing: " + strerror(errno); + } + + FILE* stdioFileBZ2 = fopen(pathBZ2.c_str(), "rb"); + if(!stdioFileBZ2) + { + throw "cannot open `" + pathBZ2 + "' for reading: " + strerror(errno); + } + + int bzError; + BZFILE* 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(": ") + strerror(errno); + } + fclose(stdioFileBZ2); + 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(": ") + strerror(errno); + } + BZ2_bzReadClose(&bzError, bzFile); + fclose(stdioFileBZ2); + throw ex; + } + + if(sz > 0) + { + long pos = ftell(stdioFileBZ2); + if(pos == -1) + { + BZ2_bzReadClose(&bzError, bzFile); + fclose(stdioFileBZ2); + throw "cannot get read position for `" + pathBZ2 + "': " + strerror(errno); + } + + file.write(reinterpret_cast<char*>(bytesBZ2), sz); + if(!file) + { + BZ2_bzReadClose(&bzError, bzFile); + fclose(stdioFileBZ2); + throw "cannot write `" + path + "': " + strerror(errno); + } + } + } + + BZ2_bzReadClose(&bzError, bzFile); + if(bzError != BZ_OK) + { + string ex = "BZ2_bzReadClose failed"; + if(bzError == BZ_IO_ERROR) + { + ex += string(": ") + strerror(errno); + } + fclose(stdioFileBZ2); + throw ex; + } + + fclose(stdioFileBZ2); } void @@ -563,7 +641,7 @@ IcePatch2::getFileInfoSeq(const string& pa, FileInfoSeq& infoSeq, bool compress, if(compress) { string pathBZ2 = path + ".bz2"; - compressToFile(pathBZ2, bytes, path.size()); + compressBytesToFile(pathBZ2, bytes, path.size()); struct stat bufBZ2; if(stat(pathBZ2.c_str(), &bufBZ2) == -1) |