diff options
| -rw-r--r-- | lldb/source/Target/Platform.cpp | 79 | ||||
| -rw-r--r-- | lldb/source/Utility/ModuleCache.cpp | 39 | ||||
| -rw-r--r-- | lldb/source/Utility/ModuleCache.h | 11 |
3 files changed, 73 insertions, 56 deletions
diff --git a/lldb/source/Target/Platform.cpp b/lldb/source/Target/Platform.cpp index 6517d108cc9..63f8f4184cd 100644 --- a/lldb/source/Target/Platform.cpp +++ b/lldb/source/Target/Platform.cpp @@ -37,7 +37,6 @@ #include "lldb/Target/Target.h" #include "lldb/Utility/Utils.h" #include "llvm/Support/FileSystem.h" -#include "llvm/Support/FileUtilities.h" #include "Utility/ModuleCache.h" @@ -1837,67 +1836,35 @@ Platform::GetCachedSharedModule (const ModuleSpec &module_spec, Log *log = GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PLATFORM); // Check local cache for a module. - auto error = m_module_cache->Get (GetModuleCacheRoot (), - GetCacheHostname (), - module_spec, - module_sp, - did_create_ptr); + auto error = m_module_cache->GetAndPut ( + GetModuleCacheRoot (), + GetCacheHostname (), + module_spec, + [=](const ModuleSpec &module_spec, FileSpec &tmp_download_file_spec) + { + // Get temporary file name for a downloaded module. + llvm::SmallString<PATH_MAX> tmp_download_file_path; + const auto err_code = llvm::sys::fs::createTemporaryFile ( + "lldb", module_spec.GetUUID ().GetAsString ().c_str (), tmp_download_file_path); + if (err_code) + return Error ("Failed to create temp file: %s", err_code.message ().c_str ()); + + tmp_download_file_spec.SetFile (tmp_download_file_path.c_str (), true); + return DownloadModuleSlice (module_spec.GetFileSpec (), + module_spec.GetObjectOffset (), + module_spec.GetObjectSize (), + tmp_download_file_spec); + + }, + module_sp, + did_create_ptr); if (error.Success ()) return true; if (log) log->Printf("Platform::%s - module %s not found in local cache: %s", __FUNCTION__, module_spec.GetUUID ().GetAsString ().c_str (), error.AsCString ()); - - // Get temporary file name for a downloaded module. - llvm::SmallString<PATH_MAX> tmp_download_file_path; - const auto err_code = llvm::sys::fs::createTemporaryFile ( - "lldb", module_spec.GetUUID ().GetAsString ().c_str (), tmp_download_file_path); - if (err_code) - { - if (log) - log->Printf ("Platform::%s - failed to create unique file: %s", - __FUNCTION__, err_code.message ().c_str ()); - return false; - } - - llvm::FileRemover tmp_file_remover (tmp_download_file_path.c_str ()); - - const FileSpec tmp_download_file_spec (tmp_download_file_path.c_str (), true); - // Download a module file. - error = DownloadModuleSlice (module_spec.GetFileSpec (), - module_spec.GetObjectOffset (), - module_spec.GetObjectSize (), - tmp_download_file_spec); - if (error.Fail ()) - { - if (log) - log->Printf("Platform::%s - failed to download %s to %s: %s", - __FUNCTION__, module_spec.GetFileSpec ().GetPath ().c_str (), - tmp_download_file_path.c_str (), error.AsCString ()); - return false; - } - - // Put downloaded file into local module cache. - error = m_module_cache->Put (GetModuleCacheRoot (), - GetCacheHostname (), - module_spec, - tmp_download_file_spec); - if (error.Fail ()) - { - if (log) - log->Printf("Platform::%s - failed to put module %s into cache: %s", - __FUNCTION__, module_spec.GetUUID ().GetAsString ().c_str (), - error.AsCString ()); - return false; - } - - error = m_module_cache->Get (GetModuleCacheRoot (), - GetCacheHostname (), - module_spec, - module_sp, - did_create_ptr); - return error.Success (); + return false; } Error diff --git a/lldb/source/Utility/ModuleCache.cpp b/lldb/source/Utility/ModuleCache.cpp index 41b99bb3f96..095a4c76cae 100644 --- a/lldb/source/Utility/ModuleCache.cpp +++ b/lldb/source/Utility/ModuleCache.cpp @@ -13,6 +13,7 @@ #include "lldb/Core/ModuleSpec.h" #include "lldb/Host/FileSystem.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/FileUtilities.h" #include <assert.h> @@ -118,6 +119,44 @@ ModuleCache::Get (const FileSpec &root_dir_spec, return Error (); } +Error +ModuleCache::GetAndPut (const FileSpec &root_dir_spec, + const char *hostname, + const ModuleSpec &module_spec, + const Downloader &downloader, + lldb::ModuleSP &cached_module_sp, + bool *did_create_ptr) +{ + // Check local cache for a module. + auto error = Get (root_dir_spec, + hostname, + module_spec, + cached_module_sp, + did_create_ptr); + if (error.Success ()) + return error; + + FileSpec tmp_download_file_spec; + error = downloader (module_spec, tmp_download_file_spec); + llvm::FileRemover tmp_file_remover (tmp_download_file_spec.GetPath ().c_str ()); + if (error.Fail ()) + return Error("Failed to download module: %s", error.AsCString ()); + + // Put downloaded file into local module cache. + error = Put (root_dir_spec, + hostname, + module_spec, + tmp_download_file_spec); + if (error.Fail ()) + return Error ("Failed to put module into cache: %s", error.AsCString ()); + + return Get (root_dir_spec, + hostname, + module_spec, + cached_module_sp, + did_create_ptr); +} + FileSpec ModuleCache::GetModuleDirectory (const FileSpec &root_dir_spec, const UUID &uuid) { diff --git a/lldb/source/Utility/ModuleCache.h b/lldb/source/Utility/ModuleCache.h index edab6cbb29d..5b484aae523 100644 --- a/lldb/source/Utility/ModuleCache.h +++ b/lldb/source/Utility/ModuleCache.h @@ -16,6 +16,7 @@ #include "lldb/Core/Error.h" #include "lldb/Host/FileSpec.h" +#include <functional> #include <string> #include <unordered_map> @@ -44,6 +45,8 @@ class UUID; class ModuleCache { public: + using Downloader = std::function<Error (const ModuleSpec&, FileSpec&)>; + Error Put (const FileSpec &root_dir_spec, const char *hostname, @@ -57,6 +60,14 @@ public: lldb::ModuleSP &cached_module_sp, bool *did_create_ptr); + Error + GetAndPut(const FileSpec &root_dir_spec, + const char *hostname, + const ModuleSpec &module_spec, + const Downloader &downloader, + lldb::ModuleSP &cached_module_sp, + bool *did_create_ptr); + private: static FileSpec GetModuleDirectory (const FileSpec &root_dir_spec, const UUID &uuid); |

