diff --git a/src/usages_clang.cc b/src/usages_clang.cc index 896fdd0..4ad143f 100644 --- a/src/usages_clang.cc +++ b/src/usages_clang.cc @@ -8,7 +8,17 @@ #include #include -// #include //TODO: remove +#ifdef _WIN32 +#include +DWORD get_current_process_id() { + return GetCurrentProcessId(); +} +#else +#include +pid_t get_current_process_id() { + return getpid(); +} +#endif const boost::filesystem::path Usages::Clang::cache_folder = ".usages_clang"; std::map Usages::Clang::caches; @@ -675,15 +685,27 @@ void Usages::Clang::write_cache(const boost::filesystem::path &path, const Clang if(chr == '/' || chr == '\\') chr = '_'; } + 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) { try { boost::archive::text_oarchive text_oarchive(stream); 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(...) { + boost::filesystem::remove(tmp_file, ec); } } }