From 697968b15f64ee042deb216e2a0376c8a5391601 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Fri, 1 Dec 2017 21:07:30 -0800 Subject: [PATCH] Group cache files by projectRoot --- src/cache.cc | 25 +++++++++++++++---------- src/command_line.cc | 3 ++- src/utils.cc | 9 +++++++++ src/utils.h | 4 ++++ 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/cache.cc b/src/cache.cc index f8bfcba1..a5905dab 100644 --- a/src/cache.cc +++ b/src/cache.cc @@ -10,14 +10,19 @@ namespace { -std::string GetCachedBaseFileName(const std::string& cache_directory, - std::string source_file) { - assert(!cache_directory.empty()); - std::replace(source_file.begin(), source_file.end(), '\\', '_'); - std::replace(source_file.begin(), source_file.end(), '/', '_'); - std::replace(source_file.begin(), source_file.end(), ':', '_'); +std::string GetCachedBaseFileName(Config* config, + const std::string& source_file, + bool create_dir = false) { + assert(!config->cacheDirectory.empty()); + std::string cache_file; + size_t len = config->projectRoot.size(); + if (StartsWith(source_file, config->projectRoot)) { + cache_file = EscapeFileName(config->projectRoot) + '/' + + EscapeFileName(source_file.substr(len)); + } else + cache_file = EscapeFileName(source_file); - return cache_directory + source_file; + return config->cacheDirectory + cache_file; } } // namespace @@ -28,7 +33,7 @@ std::unique_ptr LoadCachedIndex(Config* config, return nullptr; optional file_content = ReadContent( - GetCachedBaseFileName(config->cacheDirectory, filename) + ".json"); + GetCachedBaseFileName(config, filename) + ".json"); if (!file_content) return nullptr; @@ -40,7 +45,7 @@ optional LoadCachedFileContents(Config* config, if (!config->enableCacheRead) return nullopt; - return ReadContent(GetCachedBaseFileName(config->cacheDirectory, filename)); + return ReadContent(GetCachedBaseFileName(config, filename)); } void WriteToCache(Config* config, IndexFile& file) { @@ -48,7 +53,7 @@ void WriteToCache(Config* config, IndexFile& file) { return; std::string cache_basename = - GetCachedBaseFileName(config->cacheDirectory, file.path); + GetCachedBaseFileName(config, file.path); if (file.file_contents_.empty()) { LOG_S(ERROR) << "No cached file contents; performing potentially stale " diff --git a/src/command_line.cc b/src/command_line.cc index 4ffcd201..aab64bff 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -1474,7 +1474,6 @@ bool QueryDbMainLoop(Config* config, config->cacheDirectory = NormalizePath(config->cacheDirectory); EnsureEndsInSlash(config->cacheDirectory); - MakeDirectoryRecursive(config->cacheDirectory); // Ensure there is a resource directory. if (config->resourceDirectory.empty()) { @@ -1553,6 +1552,8 @@ bool QueryDbMainLoop(Config* config, config->projectRoot = NormalizePath(request->params.rootUri->GetPath()); EnsureEndsInSlash(config->projectRoot); + MakeDirectoryRecursive( + config->cacheDirectory + EscapeFileName(config->projectRoot)); // Start indexer threads. if (config->indexerCount == 0) { diff --git a/src/utils.cc b/src/utils.cc index 58dfb97f..0c8d2d2e 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -195,6 +195,15 @@ void EnsureEndsInSlash(std::string& path) { path += '/'; } +std::string EscapeFileName(std::string path) { + if (path.size() && path.back() == '/') + path.pop_back(); + std::replace(path.begin(), path.end(), '\\', '_'); + std::replace(path.begin(), path.end(), '/', '_'); + std::replace(path.begin(), path.end(), ':', '_'); + return path; +} + // http://stackoverflow.com/a/6089413 std::istream& SafeGetline(std::istream& is, std::string& t) { t.clear(); diff --git a/src/utils.h b/src/utils.h index bf38864f..a15575d9 100644 --- a/src/utils.h +++ b/src/utils.h @@ -69,6 +69,10 @@ void GetFilesInFolder(std::string folder, // Ensures that |path| ends in a slash. void EnsureEndsInSlash(std::string& path); +// Converts a file path to one that can be used as filename. +// e.g. foo/bar.c => foo_bar.c +std::string EscapeFileName(std::string path); + optional ReadContent(const std::string& filename); std::vector ReadLines(std::string filename); std::vector ToLines(const std::string& content,