Browse Source

No longer possible for processes to simultaneously write to the same usage cache file

merge-requests/365/head
eidheim 8 years ago
parent
commit
7b6a3915f2
  1. 26
      src/usages_clang.cc

26
src/usages_clang.cc

@ -8,7 +8,17 @@
#include <regex> #include <regex>
#include <thread> #include <thread>
// #include <iostream> //TODO: remove #ifdef _WIN32
#include <windows.h>
DWORD get_current_process_id() {
return GetCurrentProcessId();
}
#else
#include <unistd.h>
pid_t get_current_process_id() {
return getpid();
}
#endif
const boost::filesystem::path Usages::Clang::cache_folder = ".usages_clang"; const boost::filesystem::path Usages::Clang::cache_folder = ".usages_clang";
std::map<boost::filesystem::path, Usages::Clang::Cache> Usages::Clang::caches; std::map<boost::filesystem::path, Usages::Clang::Cache> Usages::Clang::caches;
@ -675,15 +685,27 @@ void Usages::Clang::write_cache(const boost::filesystem::path &path, const Clang
if(chr == '/' || chr == '\\') if(chr == '/' || chr == '\\')
chr = '_'; chr = '_';
} }
auto full_cache_path = cache_path / (path_str + ".usages"); auto full_cache_path = cache_path / (path_str + ".usages");
auto tmp_file = boost::filesystem::temp_directory_path(ec);
if(ec)
return;
tmp_file /= ("jucipp" + std::to_string(get_current_process_id()) + path_str + ".usages");
std::ofstream stream(full_cache_path.string()); std::ofstream stream(tmp_file.string());
if(stream) { if(stream) {
try { try {
boost::archive::text_oarchive text_oarchive(stream); boost::archive::text_oarchive text_oarchive(stream);
text_oarchive << cache; text_oarchive << cache;
stream.close();
boost::filesystem::rename(tmp_file, full_cache_path, ec);
if(ec) {
boost::filesystem::copy_file(tmp_file, full_cache_path, boost::filesystem::copy_option::overwrite_if_exists);
boost::filesystem::remove(tmp_file, ec);
}
} }
catch(...) { catch(...) {
boost::filesystem::remove(tmp_file, ec);
} }
} }
} }

Loading…
Cancel
Save