summaryrefslogtreecommitdiff
path: root/cpp/src/IcePatch/Util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp/src/IcePatch/Util.cpp')
-rw-r--r--cpp/src/IcePatch/Util.cpp277
1 files changed, 99 insertions, 178 deletions
diff --git a/cpp/src/IcePatch/Util.cpp b/cpp/src/IcePatch/Util.cpp
index dbbed190626..2906620819f 100644
--- a/cpp/src/IcePatch/Util.cpp
+++ b/cpp/src/IcePatch/Util.cpp
@@ -9,6 +9,7 @@
// **********************************************************************
#include <IcePatch/Util.h>
+#include <IceUtil/InputUtil.h>
#include <fstream>
#include <sys/stat.h>
#include <openssl/md5.h>
@@ -344,18 +345,21 @@ IcePatch::createMD5(const string& path)
ByteSeq bytes;
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()))
+ if(bytes.size() > 0)
{
- FileAccessException ex;
- ex.reason = "could not read all bytes from `" + path + "'";
- throw ex;
+ 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();
@@ -365,7 +369,14 @@ IcePatch::createMD5(const string& path)
//
ByteSeq bytesMD5;
bytesMD5.resize(16);
- MD5(reinterpret_cast<unsigned char*>(&bytes[0]), bytes.size(), reinterpret_cast<unsigned char*>(&bytesMD5[0]));
+ if(bytes.size() > 0)
+ {
+ MD5(reinterpret_cast<unsigned char*>(&bytes[0]), bytes.size(), reinterpret_cast<unsigned char*>(&bytesMD5[0]));
+ }
+ else
+ {
+ fill(bytesMD5.begin(), bytesMD5.end(), 0);
+ }
//
// Save the MD5 hash value to a temporary MD5 file.
@@ -436,18 +447,21 @@ IcePatch::calcPartialMD5(const string& path, Int size)
ByteSeq bytes;
bytes.resize(size);
- file.read(&bytes[0], bytes.size());
- if(!file)
+ if(bytes.size() > 0)
{
- 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.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();
@@ -457,7 +471,14 @@ IcePatch::calcPartialMD5(const string& path, Int size)
//
ByteSeq bytesMD5;
bytesMD5.resize(16);
- MD5(reinterpret_cast<unsigned char*>(&bytes[0]), bytes.size(), reinterpret_cast<unsigned char*>(&bytesMD5[0]));
+ if(bytes.size() > 0)
+ {
+ MD5(reinterpret_cast<unsigned char*>(&bytes[0]), bytes.size(), reinterpret_cast<unsigned char*>(&bytesMD5[0]));
+ }
+ else
+ {
+ fill(bytesMD5.begin(), bytesMD5.end(), 0);
+ }
return bytesMD5;
}
@@ -632,184 +653,84 @@ IcePatch::createBZ2(const string& path)
}
}
-void
-IcePatch::getRegular(const RegularPrx& regular, ProgressCB& progressCB)
+Long
+IcePatch::readStamp()
{
- string path = identityToPath(regular->ice_getIdentity());
- string pathBZ2 = path + ".bz2";
- Int totalBZ2 = regular->getBZ2Size();
- Int posBZ2 = 0;
-
- //
- // Check for partial BZ2 file.
- //
- FileInfo infoBZ2 = getFileInfo(pathBZ2, false);
- if(infoBZ2.type == FileTypeRegular)
+ ifstream fileStamp(".icepatch/stamp");
+ if(!fileStamp) // Ignore any errors if the file cannot be read.
{
- ByteSeq remoteBZ2MD5 = regular->getBZ2MD5(infoBZ2.size);
- ByteSeq localBZ2MD5 = calcPartialMD5(pathBZ2, infoBZ2.size);
-
- if(remoteBZ2MD5 == localBZ2MD5)
- {
- posBZ2 = infoBZ2.size;
- }
+ return -1;
}
- //
- // Get the BZ2 file.
- //
- progressCB.startDownload(totalBZ2, posBZ2);
-
- ofstream fileBZ2(pathBZ2.c_str(), ios::binary | (posBZ2 ? ios::app : 0));
- if(!fileBZ2)
- {
- FileAccessException ex;
- ex.reason = "cannot open `" + pathBZ2 + "' for writing: " + strerror(errno);
- throw ex;
- }
+ string s;
+ fileStamp >> s;
+ fileStamp.close();
- while(posBZ2 < totalBZ2)
+ Long stamp;
+ string::size_type dummy;
+ if(!IceUtil::stringToInt64(s, stamp, dummy)) // Ignore errors during conversion.
{
- static const Int numBZ2 = 64 * 1024;
-
- ByteSeq bytesBZ2 = regular->getBZ2(posBZ2, numBZ2);
- if(bytesBZ2.empty())
- {
- break;
- }
-
- posBZ2 += bytesBZ2.size();
-
- fileBZ2.write(&bytesBZ2[0], bytesBZ2.size());
- if(!fileBZ2)
- {
- FileAccessException ex;
- ex.reason = "cannot write `" + pathBZ2 + "': " + strerror(errno);
- throw ex;
- }
-
- if(static_cast<Int>(bytesBZ2.size()) < numBZ2)
- {
- break;
- }
-
- progressCB.updateDownload(totalBZ2, posBZ2);
+ return -1;
}
- progressCB.finishedDownload(totalBZ2);
+ return stamp;
+}
- fileBZ2.close();
-
- //
- // Read the BZ2 file in blocks and write the original file.
- //
- ofstream file(path.c_str(), ios::binary);
- if(!file)
- {
- FileAccessException ex;
- ex.reason = "cannot open `" + path + "' for writing: " + strerror(errno);
- throw ex;
- }
-
- FILE* stdioFileBZ2 = fopen(pathBZ2.c_str(), "rb");
- if(!stdioFileBZ2)
- {
- FileAccessException 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)
+void
+IcePatch::writeStamp(Long stamp)
+{
+ ofstream fileStamp(".icepatch/stamp");
+ if(!fileStamp)
{
+ //
+ // Prepare an exception, but don't throw yet.
+ //
FileAccessException ex;
- ex.reason = "BZ2_bzReadOpen failed";
- if(bzError == BZ_IO_ERROR)
- {
- ex.reason += string(": ") + strerror(errno);
- }
- fclose(stdioFileBZ2);
- throw ex;
- }
-
- static const Int numBZ2 = 64 * 1024;
- Byte bytesBZ2[numBZ2];
-
- progressCB.startUncompress(totalBZ2, 0);
-
- while(bzError != BZ_STREAM_END)
- {
- int sz = BZ2_bzRead(&bzError, bzFile, bytesBZ2, numBZ2);
- if(bzError != BZ_OK && bzError != BZ_STREAM_END)
+ ex.reason = string("cannot open `.icepatch/stamp' for writing: ") + strerror(errno);
+
+ //
+ // Check if the directory exists, and only throw the exception
+ // if it does exist. Otherwise create the directory, and try
+ // again.
+ //
+ FileInfo info = getFileInfo(".icepatch", false);
+ if(info.type == FileTypeDirectory)
{
- FileAccessException ex;
- ex.reason = "BZ2_bzRead failed";
- if(bzError == BZ_IO_ERROR)
- {
- ex.reason += string(": ") + strerror(errno);
- }
- BZ2_bzReadClose(&bzError, bzFile);
- fclose(stdioFileBZ2);
+ //
+ // If the directory exists, throw the exception.
+ //
throw ex;
}
-
- if(sz > 0)
+ else
{
- long pos = ftell(stdioFileBZ2);
- if(pos == -1)
+ //
+ // If the directory does not exist, create it, and try
+ // again.
+ //
+ if(info.type != FileTypeNotExist)
{
- FileAccessException ex;
- ex.reason = "cannot get read position for `" + pathBZ2 + "': " + strerror(errno);
- BZ2_bzReadClose(&bzError, bzFile);
- fclose(stdioFileBZ2);
- throw ex;
+ removeRecursive(".icepatch");
}
-
- progressCB.updateUncompress(totalBZ2, pos);
- file.write(bytesBZ2, sz);
- if(!file)
+ createDirectory(".icepatch");
+
+ ofstream fileStamp(".icepatch/stamp");
+ if(!fileStamp)
{
FileAccessException ex;
- ex.reason = "cannot write `" + path + "': " + strerror(errno);
- BZ2_bzReadClose(&bzError, bzFile);
- fclose(stdioFileBZ2);
+ ex.reason = string("cannot open `.icepatch/stamp' for writing: ") + strerror(errno);
throw ex;
}
+ else
+ {
+ fileStamp << stamp << endl;
+ fileStamp.close();
+ }
}
}
-
- progressCB.finishedUncompress(totalBZ2);
-
- BZ2_bzReadClose(&bzError, bzFile);
- if(bzError != BZ_OK)
- {
- FileAccessException ex;
- ex.reason = "BZ2_bzReadClose failed";
- if(bzError == BZ_IO_ERROR)
- {
- ex.reason += string(": ") + strerror(errno);
- }
- fclose(stdioFileBZ2);
- throw ex;
- }
-
- fclose(stdioFileBZ2);
- file.close();
-
- //
- // Remove the BZ2 file, it is not needed anymore.
- //
- if(::remove(pathBZ2.c_str()) == -1)
+ else
{
- FileAccessException ex;
- ex.reason = "cannot remove file `" + pathBZ2 + "': " + strerror(errno);
- throw ex;
+ fileStamp << stamp << endl;
+ fileStamp.close();
}
-
- //
- // Create a MD5 file for the original file.
- //
- createMD5(path);
}