diff --git a/fdbmonitor/fdbmonitor.cpp b/fdbmonitor/fdbmonitor.cpp index c1b57dfa6d..b5a74fec10 100644 --- a/fdbmonitor/fdbmonitor.cpp +++ b/fdbmonitor/fdbmonitor.cpp @@ -240,13 +240,17 @@ std::string abspath(std::string const& filename) { return std::string(r); } -std::string parentDirectory(std::string const& filename) { +std::string parentDirectory(std::string filename) { size_t sep = filename.find_last_of( CANONICAL_PATH_SEPARATOR ); if (sep == std::string::npos) { return ""; } - return filename.substr(0, sep); + while(filename.size() && (filename.back() == '/' || filename.back() == CANONICAL_PATH_SEPARATOR)) { + filename = filename.substr(0, filename.size()-1); + } + + return filename.substr(0, sep+1); } int mkdir(std::string const& directory) { @@ -944,7 +948,7 @@ std::unordered_map> set_watches(std::string if(exists) { log_msg(SevInfo, "Watching parent directory of symlink %s (%d)\n", subpath.c_str(), wd); - additional_watch_wds[wd].insert(subpath.substr(parent.size()+1)); + additional_watch_wds[wd].insert(subpath.substr(parent.size())); } else { /* If the subpath has appeared since we set the watch, we should cancel it and resume traversing the path */ @@ -955,7 +959,7 @@ std::unordered_map> set_watches(std::string } log_msg(SevInfo, "Watching parent directory of missing directory %s (%d)\n", subpath.c_str(), wd); - additional_watch_wds[wd].insert(subpath.substr(parent.size()+1)); + additional_watch_wds[wd].insert(subpath.substr(parent.size())); break; } @@ -1043,7 +1047,7 @@ int main(int argc, char** argv) { // Will always succeed given an absolute path std::string confdir = parentDirectory(confpath); - std::string conffile = confpath.substr(confdir.size()+1); + std::string conffile = confpath.substr(confdir.size()); #ifdef __linux__ // Setup inotify diff --git a/flow/Platform.cpp b/flow/Platform.cpp index 88ac4ef587..f07cc9338d 100644 --- a/flow/Platform.cpp +++ b/flow/Platform.cpp @@ -1633,7 +1633,7 @@ void atomicReplace( std::string const& path, std::string const& content ) { try { INJECT_FAULT( io_error, "atomicReplace" ); - std::string tempfilename = parentDirectory(path) + CANONICAL_PATH_SEPARATOR + g_random->randomUniqueID().toString() + ".tmp"; + std::string tempfilename = joinPath(parentDirectory(path), g_random->randomUniqueID().toString() + ".tmp"); f = fopen( tempfilename.c_str(), "wt" ); if(!f) throw io_error(); @@ -1812,7 +1812,12 @@ std::string parentDirectory( std::string const& filename ) { .GetLastError(); throw platform_error(); } - return abs.substr(0, sep); + + while (abs.size() && (abs.back() == '/' || abs.back() == CANONICAL_PATH_SEPARATOR)) { + abs = abs.substr(0, abs.size()-1); + } + + return abs.substr(0, sep+1); } std::string getUserHomeDirectory() {