1*0b57cec5SDimitry Andric //===-- ModuleCache.cpp ---------------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "lldb/Target/ModuleCache.h"
10*0b57cec5SDimitry Andric 
11*0b57cec5SDimitry Andric #include "lldb/Core/Module.h"
12*0b57cec5SDimitry Andric #include "lldb/Core/ModuleList.h"
13*0b57cec5SDimitry Andric #include "lldb/Core/ModuleSpec.h"
14*0b57cec5SDimitry Andric #include "lldb/Host/File.h"
15*0b57cec5SDimitry Andric #include "lldb/Host/LockFile.h"
16*0b57cec5SDimitry Andric #include "lldb/Utility/LLDBLog.h"
17*0b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
18*0b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
19*0b57cec5SDimitry Andric #include "llvm/Support/FileUtilities.h"
20*0b57cec5SDimitry Andric 
21*0b57cec5SDimitry Andric #include <cassert>
22*0b57cec5SDimitry Andric 
23*0b57cec5SDimitry Andric #include <cstdio>
24*0b57cec5SDimitry Andric 
25*0b57cec5SDimitry Andric using namespace lldb;
26*0b57cec5SDimitry Andric using namespace lldb_private;
27*0b57cec5SDimitry Andric 
28*0b57cec5SDimitry Andric namespace {
29*0b57cec5SDimitry Andric 
30*0b57cec5SDimitry Andric const char *kModulesSubdir = ".cache";
31*0b57cec5SDimitry Andric const char *kLockDirName = ".lock";
32*0b57cec5SDimitry Andric const char *kTempFileName = ".temp";
33*0b57cec5SDimitry Andric const char *kTempSymFileName = ".symtemp";
34*0b57cec5SDimitry Andric const char *kSymFileExtension = ".sym";
35*0b57cec5SDimitry Andric const char *kFSIllegalChars = "\\/:*?\"<>|";
36*0b57cec5SDimitry Andric 
GetEscapedHostname(const char * hostname)37*0b57cec5SDimitry Andric std::string GetEscapedHostname(const char *hostname) {
38*0b57cec5SDimitry Andric   if (hostname == nullptr)
39*0b57cec5SDimitry Andric     hostname = "unknown";
40*0b57cec5SDimitry Andric   std::string result(hostname);
41*0b57cec5SDimitry Andric   size_t size = result.size();
42*0b57cec5SDimitry Andric   for (size_t i = 0; i < size; ++i) {
43*0b57cec5SDimitry Andric     if ((result[i] >= 1 && result[i] <= 31) ||
44*0b57cec5SDimitry Andric         strchr(kFSIllegalChars, result[i]) != nullptr)
45*0b57cec5SDimitry Andric       result[i] = '_';
46*0b57cec5SDimitry Andric   }
47*0b57cec5SDimitry Andric   return result;
48*0b57cec5SDimitry Andric }
49*0b57cec5SDimitry Andric 
50*0b57cec5SDimitry Andric class ModuleLock {
51*0b57cec5SDimitry Andric private:
52*0b57cec5SDimitry Andric   FileUP m_file_up;
53*0b57cec5SDimitry Andric   std::unique_ptr<lldb_private::LockFile> m_lock;
54*0b57cec5SDimitry Andric   FileSpec m_file_spec;
55*0b57cec5SDimitry Andric 
56*0b57cec5SDimitry Andric public:
57*0b57cec5SDimitry Andric   ModuleLock(const FileSpec &root_dir_spec, const UUID &uuid, Status &error);
58*0b57cec5SDimitry Andric   void Delete();
59*0b57cec5SDimitry Andric };
60*0b57cec5SDimitry Andric 
JoinPath(const FileSpec & path1,const char * path2)61*0b57cec5SDimitry Andric static FileSpec JoinPath(const FileSpec &path1, const char *path2) {
62*0b57cec5SDimitry Andric   FileSpec result_spec(path1);
63*0b57cec5SDimitry Andric   result_spec.AppendPathComponent(path2);
64*0b57cec5SDimitry Andric   return result_spec;
65*0b57cec5SDimitry Andric }
66*0b57cec5SDimitry Andric 
MakeDirectory(const FileSpec & dir_path)67*0b57cec5SDimitry Andric static Status MakeDirectory(const FileSpec &dir_path) {
68*0b57cec5SDimitry Andric   namespace fs = llvm::sys::fs;
69*0b57cec5SDimitry Andric 
70*0b57cec5SDimitry Andric   return fs::create_directories(dir_path.GetPath(), true, fs::perms::owner_all);
71*0b57cec5SDimitry Andric }
72*0b57cec5SDimitry Andric 
GetModuleDirectory(const FileSpec & root_dir_spec,const UUID & uuid)73*0b57cec5SDimitry Andric FileSpec GetModuleDirectory(const FileSpec &root_dir_spec, const UUID &uuid) {
74*0b57cec5SDimitry Andric   const auto modules_dir_spec = JoinPath(root_dir_spec, kModulesSubdir);
75*0b57cec5SDimitry Andric   return JoinPath(modules_dir_spec, uuid.GetAsString().c_str());
76*0b57cec5SDimitry Andric }
77*0b57cec5SDimitry Andric 
GetSymbolFileSpec(const FileSpec & module_file_spec)78*0b57cec5SDimitry Andric FileSpec GetSymbolFileSpec(const FileSpec &module_file_spec) {
79*0b57cec5SDimitry Andric   return FileSpec(module_file_spec.GetPath() + kSymFileExtension);
80*0b57cec5SDimitry Andric }
81*0b57cec5SDimitry Andric 
DeleteExistingModule(const FileSpec & root_dir_spec,const FileSpec & sysroot_module_path_spec)82*0b57cec5SDimitry Andric void DeleteExistingModule(const FileSpec &root_dir_spec,
83*0b57cec5SDimitry Andric                           const FileSpec &sysroot_module_path_spec) {
84*0b57cec5SDimitry Andric   Log *log = GetLog(LLDBLog::Modules);
85*0b57cec5SDimitry Andric   UUID module_uuid;
86*0b57cec5SDimitry Andric   {
87*0b57cec5SDimitry Andric     auto module_sp =
88*0b57cec5SDimitry Andric         std::make_shared<Module>(ModuleSpec(sysroot_module_path_spec));
89*0b57cec5SDimitry Andric     module_uuid = module_sp->GetUUID();
90*0b57cec5SDimitry Andric   }
91*0b57cec5SDimitry Andric 
92*0b57cec5SDimitry Andric   if (!module_uuid.IsValid())
93*0b57cec5SDimitry Andric     return;
94*0b57cec5SDimitry Andric 
95*0b57cec5SDimitry Andric   Status error;
96*0b57cec5SDimitry Andric   ModuleLock lock(root_dir_spec, module_uuid, error);
97*0b57cec5SDimitry Andric   if (error.Fail()) {
98*0b57cec5SDimitry Andric     LLDB_LOGF(log, "Failed to lock module %s: %s",
99*0b57cec5SDimitry Andric               module_uuid.GetAsString().c_str(), error.AsCString());
100*0b57cec5SDimitry Andric   }
101*0b57cec5SDimitry Andric 
102*0b57cec5SDimitry Andric   namespace fs = llvm::sys::fs;
103*0b57cec5SDimitry Andric   fs::file_status st;
104*0b57cec5SDimitry Andric   if (status(sysroot_module_path_spec.GetPath(), st))
105*0b57cec5SDimitry Andric     return;
106*0b57cec5SDimitry Andric 
107*0b57cec5SDimitry Andric   if (st.getLinkCount() > 2) // module is referred by other hosts.
108*0b57cec5SDimitry Andric     return;
109*0b57cec5SDimitry Andric 
110*0b57cec5SDimitry Andric   const auto module_spec_dir = GetModuleDirectory(root_dir_spec, module_uuid);
111*0b57cec5SDimitry Andric   llvm::sys::fs::remove_directories(module_spec_dir.GetPath());
112*0b57cec5SDimitry Andric   lock.Delete();
113*0b57cec5SDimitry Andric }
114*0b57cec5SDimitry Andric 
DecrementRefExistingModule(const FileSpec & root_dir_spec,const FileSpec & sysroot_module_path_spec)115*0b57cec5SDimitry Andric void DecrementRefExistingModule(const FileSpec &root_dir_spec,
116*0b57cec5SDimitry Andric                                 const FileSpec &sysroot_module_path_spec) {
117*0b57cec5SDimitry Andric   // Remove $platform/.cache/$uuid folder if nobody else references it.
118*0b57cec5SDimitry Andric   DeleteExistingModule(root_dir_spec, sysroot_module_path_spec);
119*0b57cec5SDimitry Andric 
120*0b57cec5SDimitry Andric   // Remove sysroot link.
121*0b57cec5SDimitry Andric   llvm::sys::fs::remove(sysroot_module_path_spec.GetPath());
122*0b57cec5SDimitry Andric 
123*0b57cec5SDimitry Andric   FileSpec symfile_spec = GetSymbolFileSpec(sysroot_module_path_spec);
124*0b57cec5SDimitry Andric   llvm::sys::fs::remove(symfile_spec.GetPath());
125*0b57cec5SDimitry Andric }
126*0b57cec5SDimitry Andric 
CreateHostSysRootModuleLink(const FileSpec & root_dir_spec,const char * hostname,const FileSpec & platform_module_spec,const FileSpec & local_module_spec,bool delete_existing)127*0b57cec5SDimitry Andric Status CreateHostSysRootModuleLink(const FileSpec &root_dir_spec,
128*0b57cec5SDimitry Andric                                    const char *hostname,
129*0b57cec5SDimitry Andric                                    const FileSpec &platform_module_spec,
130*0b57cec5SDimitry Andric                                    const FileSpec &local_module_spec,
131*0b57cec5SDimitry Andric                                    bool delete_existing) {
132*0b57cec5SDimitry Andric   const auto sysroot_module_path_spec =
133*0b57cec5SDimitry Andric       JoinPath(JoinPath(root_dir_spec, hostname),
134*0b57cec5SDimitry Andric                platform_module_spec.GetPath().c_str());
135*0b57cec5SDimitry Andric   if (FileSystem::Instance().Exists(sysroot_module_path_spec)) {
136*0b57cec5SDimitry Andric     if (!delete_existing)
137*0b57cec5SDimitry Andric       return Status();
138*0b57cec5SDimitry Andric 
139*0b57cec5SDimitry Andric     DecrementRefExistingModule(root_dir_spec, sysroot_module_path_spec);
140*0b57cec5SDimitry Andric   }
141*0b57cec5SDimitry Andric 
142*0b57cec5SDimitry Andric   const auto error = MakeDirectory(
143*0b57cec5SDimitry Andric       FileSpec(sysroot_module_path_spec.GetDirectory().AsCString()));
144*0b57cec5SDimitry Andric   if (error.Fail())
145*0b57cec5SDimitry Andric     return error;
146*0b57cec5SDimitry Andric 
147*0b57cec5SDimitry Andric   return llvm::sys::fs::create_hard_link(local_module_spec.GetPath(),
148*0b57cec5SDimitry Andric                                          sysroot_module_path_spec.GetPath());
149*0b57cec5SDimitry Andric }
150*0b57cec5SDimitry Andric 
151*0b57cec5SDimitry Andric } // namespace
152*0b57cec5SDimitry Andric 
ModuleLock(const FileSpec & root_dir_spec,const UUID & uuid,Status & error)153*0b57cec5SDimitry Andric ModuleLock::ModuleLock(const FileSpec &root_dir_spec, const UUID &uuid,
154*0b57cec5SDimitry Andric                        Status &error) {
155*0b57cec5SDimitry Andric   const auto lock_dir_spec = JoinPath(root_dir_spec, kLockDirName);
156*0b57cec5SDimitry Andric   error = MakeDirectory(lock_dir_spec);
157*0b57cec5SDimitry Andric   if (error.Fail())
158*0b57cec5SDimitry Andric     return;
159*0b57cec5SDimitry Andric 
160*0b57cec5SDimitry Andric   m_file_spec = JoinPath(lock_dir_spec, uuid.GetAsString().c_str());
161*0b57cec5SDimitry Andric 
162*0b57cec5SDimitry Andric   auto file = FileSystem::Instance().Open(
163*0b57cec5SDimitry Andric       m_file_spec, File::eOpenOptionWriteOnly | File::eOpenOptionCanCreate |
164*0b57cec5SDimitry Andric                        File::eOpenOptionCloseOnExec);
165*0b57cec5SDimitry Andric   if (file)
166*0b57cec5SDimitry Andric     m_file_up = std::move(file.get());
167*0b57cec5SDimitry Andric   else {
168*0b57cec5SDimitry Andric     m_file_up.reset();
169*0b57cec5SDimitry Andric     error = Status(file.takeError());
170*0b57cec5SDimitry Andric     return;
171*0b57cec5SDimitry Andric   }
172*0b57cec5SDimitry Andric 
173*0b57cec5SDimitry Andric   m_lock = std::make_unique<lldb_private::LockFile>(m_file_up->GetDescriptor());
174*0b57cec5SDimitry Andric   error = m_lock->WriteLock(0, 1);
175*0b57cec5SDimitry Andric   if (error.Fail())
176*0b57cec5SDimitry Andric     error.SetErrorStringWithFormat("Failed to lock file: %s",
177*0b57cec5SDimitry Andric                                    error.AsCString());
178*0b57cec5SDimitry Andric }
179*0b57cec5SDimitry Andric 
Delete()180*0b57cec5SDimitry Andric void ModuleLock::Delete() {
181*0b57cec5SDimitry Andric   if (!m_file_up)
182*0b57cec5SDimitry Andric     return;
183*0b57cec5SDimitry Andric 
184*0b57cec5SDimitry Andric   m_file_up->Close();
185*0b57cec5SDimitry Andric   m_file_up.reset();
186*0b57cec5SDimitry Andric   llvm::sys::fs::remove(m_file_spec.GetPath());
187*0b57cec5SDimitry Andric }
188*0b57cec5SDimitry Andric 
189*0b57cec5SDimitry Andric /////////////////////////////////////////////////////////////////////////
190*0b57cec5SDimitry Andric 
Put(const FileSpec & root_dir_spec,const char * hostname,const ModuleSpec & module_spec,const FileSpec & tmp_file,const FileSpec & target_file)191*0b57cec5SDimitry Andric Status ModuleCache::Put(const FileSpec &root_dir_spec, const char *hostname,
192*0b57cec5SDimitry Andric                         const ModuleSpec &module_spec, const FileSpec &tmp_file,
193*0b57cec5SDimitry Andric                         const FileSpec &target_file) {
194*0b57cec5SDimitry Andric   const auto module_spec_dir =
195*0b57cec5SDimitry Andric       GetModuleDirectory(root_dir_spec, module_spec.GetUUID());
196*0b57cec5SDimitry Andric   const auto module_file_path =
197*0b57cec5SDimitry Andric       JoinPath(module_spec_dir, target_file.GetFilename().AsCString());
198*0b57cec5SDimitry Andric 
199*0b57cec5SDimitry Andric   const auto tmp_file_path = tmp_file.GetPath();
200*0b57cec5SDimitry Andric   const auto err_code =
201*0b57cec5SDimitry Andric       llvm::sys::fs::rename(tmp_file_path, module_file_path.GetPath());
202*0b57cec5SDimitry Andric   if (err_code)
203*0b57cec5SDimitry Andric     return Status("Failed to rename file %s to %s: %s", tmp_file_path.c_str(),
204*0b57cec5SDimitry Andric                   module_file_path.GetPath().c_str(),
205*0b57cec5SDimitry Andric                   err_code.message().c_str());
206*0b57cec5SDimitry Andric 
207*0b57cec5SDimitry Andric   const auto error = CreateHostSysRootModuleLink(
208*0b57cec5SDimitry Andric       root_dir_spec, hostname, target_file, module_file_path, true);
209*0b57cec5SDimitry Andric   if (error.Fail())
210*0b57cec5SDimitry Andric     return Status("Failed to create link to %s: %s",
211*0b57cec5SDimitry Andric                   module_file_path.GetPath().c_str(), error.AsCString());
212*0b57cec5SDimitry Andric   return Status();
213*0b57cec5SDimitry Andric }
214*0b57cec5SDimitry Andric 
Get(const FileSpec & root_dir_spec,const char * hostname,const ModuleSpec & module_spec,ModuleSP & cached_module_sp,bool * did_create_ptr)215*0b57cec5SDimitry Andric Status ModuleCache::Get(const FileSpec &root_dir_spec, const char *hostname,
216*0b57cec5SDimitry Andric                         const ModuleSpec &module_spec,
217*0b57cec5SDimitry Andric                         ModuleSP &cached_module_sp, bool *did_create_ptr) {
218*0b57cec5SDimitry Andric   const auto find_it =
219*0b57cec5SDimitry Andric       m_loaded_modules.find(module_spec.GetUUID().GetAsString());
220*0b57cec5SDimitry Andric   if (find_it != m_loaded_modules.end()) {
221*0b57cec5SDimitry Andric     cached_module_sp = (*find_it).second.lock();
222*0b57cec5SDimitry Andric     if (cached_module_sp)
223*0b57cec5SDimitry Andric       return Status();
224*0b57cec5SDimitry Andric     m_loaded_modules.erase(find_it);
225*0b57cec5SDimitry Andric   }
226*0b57cec5SDimitry Andric 
227*0b57cec5SDimitry Andric   const auto module_spec_dir =
228*0b57cec5SDimitry Andric       GetModuleDirectory(root_dir_spec, module_spec.GetUUID());
229*0b57cec5SDimitry Andric   const auto module_file_path = JoinPath(
230*0b57cec5SDimitry Andric       module_spec_dir, module_spec.GetFileSpec().GetFilename().AsCString());
231*0b57cec5SDimitry Andric 
232*0b57cec5SDimitry Andric   if (!FileSystem::Instance().Exists(module_file_path))
233*0b57cec5SDimitry Andric     return Status("Module %s not found", module_file_path.GetPath().c_str());
234*0b57cec5SDimitry Andric   if (FileSystem::Instance().GetByteSize(module_file_path) !=
235*0b57cec5SDimitry Andric       module_spec.GetObjectSize())
236*0b57cec5SDimitry Andric     return Status("Module %s has invalid file size",
237*0b57cec5SDimitry Andric                   module_file_path.GetPath().c_str());
238*0b57cec5SDimitry Andric 
239*0b57cec5SDimitry Andric   // We may have already cached module but downloaded from an another host - in
240*0b57cec5SDimitry Andric   // this case let's create a link to it.
241*0b57cec5SDimitry Andric   auto error = CreateHostSysRootModuleLink(root_dir_spec, hostname,
242*0b57cec5SDimitry Andric                                            module_spec.GetFileSpec(),
243*0b57cec5SDimitry Andric                                            module_file_path, false);
244*0b57cec5SDimitry Andric   if (error.Fail())
245*0b57cec5SDimitry Andric     return Status("Failed to create link to %s: %s",
246*0b57cec5SDimitry Andric                   module_file_path.GetPath().c_str(), error.AsCString());
247*0b57cec5SDimitry Andric 
248*0b57cec5SDimitry Andric   auto cached_module_spec(module_spec);
249*0b57cec5SDimitry Andric   cached_module_spec.GetUUID().Clear(); // Clear UUID since it may contain md5
250*0b57cec5SDimitry Andric                                         // content hash instead of real UUID.
251*0b57cec5SDimitry Andric   cached_module_spec.GetFileSpec() = module_file_path;
252*0b57cec5SDimitry Andric   cached_module_spec.GetPlatformFileSpec() = module_spec.GetFileSpec();
253*0b57cec5SDimitry Andric 
254*0b57cec5SDimitry Andric   error = ModuleList::GetSharedModule(cached_module_spec, cached_module_sp,
255*0b57cec5SDimitry Andric                                       nullptr, nullptr, did_create_ptr, false);
256*0b57cec5SDimitry Andric   if (error.Fail())
257*0b57cec5SDimitry Andric     return error;
258*0b57cec5SDimitry Andric 
259*0b57cec5SDimitry Andric   FileSpec symfile_spec = GetSymbolFileSpec(cached_module_sp->GetFileSpec());
260*0b57cec5SDimitry Andric   if (FileSystem::Instance().Exists(symfile_spec))
261*0b57cec5SDimitry Andric     cached_module_sp->SetSymbolFileFileSpec(symfile_spec);
262*0b57cec5SDimitry Andric 
263*0b57cec5SDimitry Andric   m_loaded_modules.insert(
264*0b57cec5SDimitry Andric       std::make_pair(module_spec.GetUUID().GetAsString(), cached_module_sp));
265*0b57cec5SDimitry Andric 
266*0b57cec5SDimitry Andric   return Status();
267*0b57cec5SDimitry Andric }
268*0b57cec5SDimitry Andric 
GetAndPut(const FileSpec & root_dir_spec,const char * hostname,const ModuleSpec & module_spec,const ModuleDownloader & module_downloader,const SymfileDownloader & symfile_downloader,lldb::ModuleSP & cached_module_sp,bool * did_create_ptr)269*0b57cec5SDimitry Andric Status ModuleCache::GetAndPut(const FileSpec &root_dir_spec,
270*0b57cec5SDimitry Andric                               const char *hostname,
271*0b57cec5SDimitry Andric                               const ModuleSpec &module_spec,
272*0b57cec5SDimitry Andric                               const ModuleDownloader &module_downloader,
273*0b57cec5SDimitry Andric                               const SymfileDownloader &symfile_downloader,
274*0b57cec5SDimitry Andric                               lldb::ModuleSP &cached_module_sp,
275*0b57cec5SDimitry Andric                               bool *did_create_ptr) {
276*0b57cec5SDimitry Andric   const auto module_spec_dir =
277*0b57cec5SDimitry Andric       GetModuleDirectory(root_dir_spec, module_spec.GetUUID());
278*0b57cec5SDimitry Andric   auto error = MakeDirectory(module_spec_dir);
279*0b57cec5SDimitry Andric   if (error.Fail())
280*0b57cec5SDimitry Andric     return error;
281*0b57cec5SDimitry Andric 
282*0b57cec5SDimitry Andric   ModuleLock lock(root_dir_spec, module_spec.GetUUID(), error);
283*0b57cec5SDimitry Andric   if (error.Fail())
284*0b57cec5SDimitry Andric     return Status("Failed to lock module %s: %s",
285*0b57cec5SDimitry Andric                   module_spec.GetUUID().GetAsString().c_str(),
286*0b57cec5SDimitry Andric                   error.AsCString());
287*0b57cec5SDimitry Andric 
288*0b57cec5SDimitry Andric   const auto escaped_hostname(GetEscapedHostname(hostname));
289*0b57cec5SDimitry Andric   // Check local cache for a module.
290*0b57cec5SDimitry Andric   error = Get(root_dir_spec, escaped_hostname.c_str(), module_spec,
291*0b57cec5SDimitry Andric               cached_module_sp, did_create_ptr);
292*0b57cec5SDimitry Andric   if (error.Success())
293*0b57cec5SDimitry Andric     return error;
294*0b57cec5SDimitry Andric 
295*0b57cec5SDimitry Andric   const auto tmp_download_file_spec = JoinPath(module_spec_dir, kTempFileName);
296*0b57cec5SDimitry Andric   error = module_downloader(module_spec, tmp_download_file_spec);
297*0b57cec5SDimitry Andric   llvm::FileRemover tmp_file_remover(tmp_download_file_spec.GetPath());
298*0b57cec5SDimitry Andric   if (error.Fail())
299*0b57cec5SDimitry Andric     return Status("Failed to download module: %s", error.AsCString());
300*0b57cec5SDimitry Andric 
301*0b57cec5SDimitry Andric   // Put downloaded file into local module cache.
302*0b57cec5SDimitry Andric   error = Put(root_dir_spec, escaped_hostname.c_str(), module_spec,
303*0b57cec5SDimitry Andric               tmp_download_file_spec, module_spec.GetFileSpec());
304*0b57cec5SDimitry Andric   if (error.Fail())
305*0b57cec5SDimitry Andric     return Status("Failed to put module into cache: %s", error.AsCString());
306*0b57cec5SDimitry Andric 
307*0b57cec5SDimitry Andric   tmp_file_remover.releaseFile();
308*0b57cec5SDimitry Andric   error = Get(root_dir_spec, escaped_hostname.c_str(), module_spec,
309*0b57cec5SDimitry Andric               cached_module_sp, did_create_ptr);
310*0b57cec5SDimitry Andric   if (error.Fail())
311*0b57cec5SDimitry Andric     return error;
312*0b57cec5SDimitry Andric 
313*0b57cec5SDimitry Andric   // Fetching a symbol file for the module
314*0b57cec5SDimitry Andric   const auto tmp_download_sym_file_spec =
315*0b57cec5SDimitry Andric       JoinPath(module_spec_dir, kTempSymFileName);
316*0b57cec5SDimitry Andric   error = symfile_downloader(cached_module_sp, tmp_download_sym_file_spec);
317*0b57cec5SDimitry Andric   llvm::FileRemover tmp_symfile_remover(tmp_download_sym_file_spec.GetPath());
318*0b57cec5SDimitry Andric   if (error.Fail())
319*0b57cec5SDimitry Andric     // Failed to download a symfile but fetching the module was successful. The
320*0b57cec5SDimitry Andric     // module might contain the necessary symbols and the debugging is also
321*0b57cec5SDimitry Andric     // possible without a symfile.
322*0b57cec5SDimitry Andric     return Status();
323*0b57cec5SDimitry Andric 
324*0b57cec5SDimitry Andric   error = Put(root_dir_spec, escaped_hostname.c_str(), module_spec,
325*0b57cec5SDimitry Andric               tmp_download_sym_file_spec,
326*0b57cec5SDimitry Andric               GetSymbolFileSpec(module_spec.GetFileSpec()));
327*0b57cec5SDimitry Andric   if (error.Fail())
328*0b57cec5SDimitry Andric     return Status("Failed to put symbol file into cache: %s",
329*0b57cec5SDimitry Andric                   error.AsCString());
330*0b57cec5SDimitry Andric 
331*0b57cec5SDimitry Andric   tmp_symfile_remover.releaseFile();
332*0b57cec5SDimitry Andric 
333   FileSpec symfile_spec = GetSymbolFileSpec(cached_module_sp->GetFileSpec());
334   cached_module_sp->SetSymbolFileFileSpec(symfile_spec);
335   return Status();
336 }
337