diff options
author | Marc Laukien <marc@zeroc.com> | 2005-01-27 21:22:48 +0000 |
---|---|---|
committer | Marc Laukien <marc@zeroc.com> | 2005-01-27 21:22:48 +0000 |
commit | 1decea8e7dcbf1c7836d27401d63229c49191556 (patch) | |
tree | d4bd9e931ea34841bc194b711f77f9cec4a499b3 /cpp/src/IcePatch2/Util.cpp | |
parent | bug fix for thread per connection (diff) | |
download | ice-1decea8e7dcbf1c7836d27401d63229c49191556.tar.bz2 ice-1decea8e7dcbf1c7836d27401d63229c49191556.tar.xz ice-1decea8e7dcbf1c7836d27401d63229c49191556.zip |
icepatch2 changes
Diffstat (limited to 'cpp/src/IcePatch2/Util.cpp')
-rw-r--r-- | cpp/src/IcePatch2/Util.cpp | 300 |
1 files changed, 105 insertions, 195 deletions
diff --git a/cpp/src/IcePatch2/Util.cpp b/cpp/src/IcePatch2/Util.cpp index eae69fe9c76..0a71e26b11f 100644 --- a/cpp/src/IcePatch2/Util.cpp +++ b/cpp/src/IcePatch2/Util.cpp @@ -26,6 +26,9 @@ # include <dirent.h> #endif +const char* IcePatch2::checksumFile = "IcePatch2.sum"; +const char* IcePatch2::logFile = "IcePatch2.log"; + // // Sun-OS doesn't have scandir() or alphasort(). // @@ -100,9 +103,6 @@ using namespace std; using namespace Ice; using namespace IcePatch2; -const char* IcePatch2::checksumFile = "IcePatch2.sum"; -const char* IcePatch2::logFile = "IcePatch2.log"; - string IcePatch2::lastError() { @@ -214,23 +214,13 @@ IcePatch2::stringToBytes(const string& str) } string -IcePatch2::normalize(const string& path) +IcePatch2::simplify(const string& path) { - static string cwd; - static string drive; - string result = path; - string::size_type pos; - if(result.empty()) - { - throw "Invalid empty path"; - } + string::size_type pos; #ifdef _WIN32 - // - // Turn backslashes into forward slashes. - // for(pos = 0; pos < result.size(); ++pos) { if(result[pos] == '\\') @@ -240,147 +230,66 @@ IcePatch2::normalize(const string& path) } #endif - // - // Get current working directory the first time we come through here. - // - { - static bool cwdDone = false; - static IceUtil::StaticMutex m = ICE_STATIC_MUTEX_INITIALIZER; - - IceUtil::StaticMutex::Lock sync(m); - - if(!cwdDone) - { -#ifdef _WIN32 - char buf[MAX_PATH]; - if(_getcwd(buf, sizeof(buf)) == NULL) -#else - char buf[PATH_MAX]; - if(getcwd(buf, sizeof(buf)) == NULL) -#endif - { - throw "cannot get the current directory:\n" + lastError(); - } -#ifdef _WIN32 - for(pos = 0; buf[pos] != '\0'; ++pos) - { - if(buf[pos] == '\\') - { - buf[pos] = '/'; - } - } -#endif - cwd = buf; - drive = cwd.substr(0, 2); - cwdDone = true; - } - } - - // - // Create absolute path. For Windows, we make sure that there always is a drive letter. - // Otherwise, if the data directory is specified as C:/, and we ask for a file /a, we - // end up with C:/a. This avoids erroneously concluding that C:/a is not the same file as /a. - // - bool isAbsolute; -#ifdef _WIN32 - isAbsolute = result[0] == '/' || (result.size() > 1 && isalpha(result[0]) && result[1] == ':'); - if(result[0] == '/') - { - result = drive + result; - } -#else - isAbsolute = result[0] == '/'; -#endif - if(!isAbsolute) - { - result = cwd + "/" + result; - } - - // - // Get rid of multiple adjacent slashes. - // pos = 0; while((pos = result.find("//", pos)) != string::npos) { result.erase(pos, 1); } - // - // Split out the path components and put them into a vector. - // -#ifdef _WIN32 - pos = 2; // Slash follows the drive letter and colon. -#else pos = 0; -#endif - vector<string> vec; - while(pos != string::npos) + while((pos = result.find("/./", pos)) != string::npos) { - string::size_type end = result.find('/', pos + 1); - string component; - if(end != string::npos) - { - component = result.substr(pos + 1, end - (pos + 1)); - } - else - { - component = result.substr(pos + 1); - } - - if(component.empty()) - { - ; // Ignore empty component caused by trailing slash in original path. - } - else if(component == ".") - { - ; // Ignore '.' - } - else if(component == "..") - { - if(!vec.empty()) - { - vec.erase(vec.end() - 1); // Jump up one level, except at the root. - } - } - else - { - vec.push_back(component); - } + result.erase(pos, 2); + } - pos = end; + if(result.substr(0, 2) == "./") + { + result.erase(0, 2); } -#ifdef _WIN32 - result = drive; -#else - result.clear(); -#endif + if(result.size() >= 2 && result.substr(result.size() - 2, 2) == "/.") + { + result.erase(result.size() - 2, 2); + } - for(vector<string>::const_iterator i = vec.begin(); i != vec.end(); ++i) + if(result.size() >= 1 && result[result.size() - 1] == '/') { - result += "/" + *i; + result.erase(result.size() - 1); } + return result; } string IcePatch2::getSuffix(const string& pa) { - const string name = getBasename(pa); - string::size_type pos = name.rfind('.'); // '.' must appear in final path component. - return pos == string::npos ? string() : name.substr(pos + 1); + const string path = simplify(pa); + + string::size_type pos = path.rfind('.'); + if(pos == string::npos) + { + return string(); + } + else + { + return path.substr(pos + 1); + } } string IcePatch2::getWithoutSuffix(const string& pa) { - const string path = normalize(pa); - if(getBasename(pa).rfind('.') == string::npos) // '.' must appear in final path component. + const string path = simplify(pa); + + string::size_type pos = path.rfind('.'); + if(pos == string::npos) { - return path; + return path; + } + else + { + return path.substr(0, pos); } - string::size_type pos = path.rfind('.'); - return path.substr(0, pos); } bool @@ -393,59 +302,43 @@ IcePatch2::ignoreSuffix(const string& path) || suffix == "bz2temp"; } -static bool -isRoot(const string& pa) -{ -#ifdef _WIN32 - if(pa.size() == 3 && isalpha(pa[0]) && pa[1] == ':' && pa[2] == '/') - { - return true; - } -#endif - return pa == "/"; -} - string IcePatch2::getBasename(const string& pa) { - const string path = normalize(pa); - if(isRoot(path)) + const string path = simplify(pa); + + string::size_type pos = path.rfind('/'); + if(pos == string::npos) { - return path; + return path; + } + else + { + return path.substr(pos + 1); } - string::size_type pos = path.rfind('/'); - return path.substr(pos + 1); } string IcePatch2::getDirname(const string& pa) { - string path = normalize(pa); - if(isRoot(path)) - { - return path; - } + const string path = simplify(pa); + string::size_type pos = path.rfind('/'); - path = path.substr(0, pos); -#ifdef _WIN32 - if(path.size() == 2 && isalpha(path[0]) && path[1] == ':') + if(pos == string::npos) { - return path + "/"; + return string(); } -#else - if(path.empty()) + else { - return "/"; + return path.substr(0, pos); } -#endif - return path; } void IcePatch2::rename(const string& fromPa, const string& toPa) { - const string fromPath = normalize(fromPa); - const string toPath = normalize(toPa); + const string fromPath = simplify(fromPa); + const string toPath = simplify(toPa); ::remove(toPath.c_str()); // We ignore errors, as the file we are renaming to might not exist. @@ -458,7 +351,7 @@ IcePatch2::rename(const string& fromPa, const string& toPa) void IcePatch2::remove(const string& pa) { - const string path = normalize(pa); + const string path = simplify(pa); struct stat buf; if(stat(path.c_str(), &buf) == -1) @@ -489,7 +382,7 @@ IcePatch2::remove(const string& pa) void IcePatch2::removeRecursive(const string& pa) { - const string path = normalize(pa); + const string path = simplify(pa); struct stat buf; if(stat(path.c_str(), &buf) == -1) @@ -526,7 +419,7 @@ IcePatch2::removeRecursive(const string& pa) StringSeq IcePatch2::readDirectory(const string& pa) { - const string path = normalize(pa); + const string path = simplify(pa); #ifdef _WIN32 @@ -602,7 +495,7 @@ IcePatch2::readDirectory(const string& pa) void IcePatch2::createDirectory(const string& pa) { - const string path = normalize(pa); + const string path = simplify(pa); #ifdef _WIN32 if(_mkdir(path.c_str()) == -1) @@ -620,10 +513,10 @@ IcePatch2::createDirectory(const string& pa) void IcePatch2::createDirectoryRecursive(const string& pa) { - const string path = normalize(pa); + const string path = simplify(pa); string dir = getDirname(path); - if(!isRoot(dir)) + if(!dir.empty()) { createDirectoryRecursive(dir); } @@ -644,7 +537,7 @@ IcePatch2::createDirectoryRecursive(const string& pa) void IcePatch2::compressBytesToFile(const string& pa, const ByteSeq& bytes, Int pos) { - const string path = normalize(pa); + const string path = simplify(pa); FILE* stdioFile = fopen(path.c_str(), "wb"); if(!stdioFile) @@ -696,7 +589,7 @@ IcePatch2::compressBytesToFile(const string& pa, const ByteSeq& bytes, Int pos) void IcePatch2::decompressFile(const string& pa) { - const string path = normalize(pa); + const string path = simplify(pa); const string pathBZ2 = path + ".bz2"; ofstream file(path.c_str(), ios::binary); @@ -778,21 +671,23 @@ IcePatch2::decompressFile(const string& pa) } static bool -getFileInfoSeqInt(const string& path, int compress, GetFileInfoSeqCB* cb, FileInfoSeq& infoSeq) +getFileInfoSeqInt(const string& basePath, const string& relPath, int compress, GetFileInfoSeqCB* cb, + FileInfoSeq& infoSeq) { - string basename = getBasename(path); - if(basename == checksumFile || basename == logFile) // Don't transmit these. + if(relPath == checksumFile || relPath == logFile) { return true; } + const string path = basePath + '/' + relPath; + if(ignoreSuffix(path)) { const string pathWithoutSuffix = getWithoutSuffix(path); if(ignoreSuffix(pathWithoutSuffix)) { - if(cb && !cb->remove(path)) + if(cb && !cb->remove(relPath)) { return false; } @@ -806,7 +701,7 @@ getFileInfoSeqInt(const string& path, int compress, GetFileInfoSeqCB* cb, FileIn { if(errno == ENOENT) { - if(cb && !cb->remove(path)) + if(cb && !cb->remove(relPath)) { return false; } @@ -820,7 +715,7 @@ getFileInfoSeqInt(const string& path, int compress, GetFileInfoSeqCB* cb, FileIn } else if(buf.st_size == 0) { - if(cb && !cb->remove(path)) + if(cb && !cb->remove(relPath)) { return false; } @@ -837,14 +732,14 @@ getFileInfoSeqInt(const string& path, int compress, GetFileInfoSeqCB* cb, FileIn throw "cannot stat `" + path + "':\n" + lastError(); } - FileInfo info; - info.path = path; if(S_ISDIR(buf.st_mode)) { + FileInfo info; + info.path = relPath; info.size = -1; - ByteSeq bytes(path.size()); - copy(path.begin(), path.end(), bytes.begin()); + ByteSeq bytes(relPath.size()); + copy(relPath.begin(), relPath.end(), bytes.begin()); ByteSeq bytesSHA(20); if(!bytes.empty()) @@ -863,7 +758,7 @@ getFileInfoSeqInt(const string& path, int compress, GetFileInfoSeqCB* cb, FileIn StringSeq content = readDirectory(path); for(StringSeq::const_iterator p = content.begin(); p != content.end() ; ++p) { - if(!getFileInfoSeqInt(path + "/" + *p, compress, cb, infoSeq)) + if(!getFileInfoSeqInt(basePath, simplify(relPath + '/' + *p), compress, cb, infoSeq)) { return false; } @@ -871,10 +766,12 @@ getFileInfoSeqInt(const string& path, int compress, GetFileInfoSeqCB* cb, FileIn } else if(S_ISREG(buf.st_mode)) { + FileInfo info; + info.path = relPath; info.size = 0; - ByteSeq bytes(path.size() + buf.st_size); - copy(path.begin(), path.end(), bytes.begin()); + ByteSeq bytes(relPath.size() + buf.st_size); + copy(relPath.begin(), relPath.end(), bytes.begin()); if(buf.st_size != 0) { @@ -888,7 +785,7 @@ getFileInfoSeqInt(const string& path, int compress, GetFileInfoSeqCB* cb, FileIn throw "cannot open `" + path + "' for reading:\n" + lastError(); } - if(read(fd, &bytes[path.size()], buf.st_size) == -1) + if(read(fd, &bytes[relPath.size()], buf.st_size) == -1) { close(fd); throw "cannot read from `" + path + "':\n" + lastError(); @@ -908,7 +805,7 @@ getFileInfoSeqInt(const string& path, int compress, GetFileInfoSeqCB* cb, FileIn if(compress >= 2 || stat(pathBZ2.c_str(), &bufBZ2) == -1 || buf.st_mtime >= bufBZ2.st_mtime) { - if(cb && !cb->compress(path)) + if(cb && !cb->compress(relPath)) { return false; } @@ -921,7 +818,7 @@ getFileInfoSeqInt(const string& path, int compress, GetFileInfoSeqCB* cb, FileIn // const string pathBZ2Temp = path + ".bz2temp"; - compressBytesToFile(pathBZ2Temp, bytes, path.size()); + compressBytesToFile(pathBZ2Temp, bytes, relPath.size()); rename(pathBZ2Temp, pathBZ2); @@ -935,7 +832,7 @@ getFileInfoSeqInt(const string& path, int compress, GetFileInfoSeqCB* cb, FileIn } } - if(cb && !cb->checksum(path)) + if(cb && !cb->checksum(relPath)) { return false; } @@ -959,23 +856,36 @@ getFileInfoSeqInt(const string& path, int compress, GetFileInfoSeqCB* cb, FileIn return true; } -void -IcePatch2::getFileInfoSeq(const string& path, int compress, GetFileInfoSeqCB* cb, FileInfoSeq& infoSeq) +bool +IcePatch2::getFileInfoSeq(const string& basePath, int compress, GetFileInfoSeqCB* cb, + FileInfoSeq& infoSeq) { - if(!getFileInfoSeqInt(path, compress, cb, infoSeq)) + return getFileInfoSeqSubDir(basePath, ".", compress, cb, infoSeq); +} + +bool +IcePatch2::getFileInfoSeqSubDir(const string& basePa, const string& relPa, int compress, GetFileInfoSeqCB* cb, + FileInfoSeq& infoSeq) +{ + const string basePath = simplify(basePa); + const string relPath = simplify(relPa); + + if(!getFileInfoSeqInt(basePath, relPath, compress, cb, infoSeq)) { - return; + return false; } sort(infoSeq.begin(), infoSeq.end(), FileInfoLess()); infoSeq.erase(unique(infoSeq.begin(), infoSeq.end(), FileInfoEqual()), infoSeq.end()); + + return true; } void IcePatch2::saveFileInfoSeq(const string& pa, const FileInfoSeq& infoSeq) { { - const string path = normalize(pa + "/" + checksumFile); + const string path = simplify(pa + '/' + checksumFile); ofstream os(path.c_str()); if(!os) @@ -990,7 +900,7 @@ IcePatch2::saveFileInfoSeq(const string& pa, const FileInfoSeq& infoSeq) } { - const string pathLog = normalize(pa + "/" + logFile); + const string pathLog = simplify(pa + '/' + logFile); try { @@ -1006,7 +916,7 @@ void IcePatch2::loadFileInfoSeq(const string& pa, FileInfoSeq& infoSeq) { { - const string path = normalize(pa + "/" + checksumFile); + const string path = simplify(pa + '/' + checksumFile); ifstream is(path.c_str()); if(!is) @@ -1030,7 +940,7 @@ IcePatch2::loadFileInfoSeq(const string& pa, FileInfoSeq& infoSeq) } { - const string pathLog = normalize(pa + "/" + logFile); + const string pathLog = simplify(pa + '/' + logFile); ifstream is(pathLog.c_str()); if(is) |