18#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/FileUtilities.h"
30const char *kModulesSubdir =
".cache";
31const char *kLockDirName =
".lock";
32const char *kTempFileName =
".temp";
33const char *kTempSymFileName =
".symtemp";
34const char *kSymFileExtension =
".sym";
35const char *kFSIllegalChars =
"\\/:*?\"<>|";
37std::string GetEscapedHostname(
const char *hostname) {
38 if (hostname ==
nullptr)
40 std::string result(hostname);
41 size_t size = result.size();
42 for (
size_t i = 0; i < size; ++i) {
43 if ((result[i] >= 1 && result[i] <= 31) ||
44 strchr(kFSIllegalChars, result[i]) !=
nullptr)
53 std::unique_ptr<lldb_private::LockFile> m_lock;
63 result_spec.AppendPathComponent(path2);
68 namespace fs = llvm::sys::fs;
70 return fs::create_directories(dir_path.
GetPath(),
true, fs::perms::owner_all);
74 const auto modules_dir_spec = JoinPath(root_dir_spec, kModulesSubdir);
75 return JoinPath(modules_dir_spec, uuid.
GetAsString().c_str());
82void DeleteExistingModule(
const FileSpec &root_dir_spec,
83 const FileSpec &sysroot_module_path_spec) {
88 std::make_shared<Module>(
ModuleSpec(sysroot_module_path_spec));
89 module_uuid = module_sp->GetUUID();
96 ModuleLock lock(root_dir_spec, module_uuid,
error);
98 LLDB_LOGF(log,
"Failed to lock module %s: %s",
102 namespace fs = llvm::sys::fs;
104 if (status(sysroot_module_path_spec.
GetPath(), st))
107 if (st.getLinkCount() > 2)
110 const auto module_spec_dir = GetModuleDirectory(root_dir_spec, module_uuid);
111 llvm::sys::fs::remove_directories(module_spec_dir.GetPath());
115void DecrementRefExistingModule(
const FileSpec &root_dir_spec,
116 const FileSpec &sysroot_module_path_spec) {
118 DeleteExistingModule(root_dir_spec, sysroot_module_path_spec);
121 llvm::sys::fs::remove(sysroot_module_path_spec.
GetPath());
123 FileSpec symfile_spec = GetSymbolFileSpec(sysroot_module_path_spec);
124 llvm::sys::fs::remove(symfile_spec.
GetPath());
128 const char *hostname,
129 const FileSpec &platform_module_spec,
131 bool delete_existing) {
132 const auto sysroot_module_path_spec =
133 JoinPath(JoinPath(root_dir_spec, hostname),
134 platform_module_spec.
GetPath().c_str());
136 if (!delete_existing)
139 DecrementRefExistingModule(root_dir_spec, sysroot_module_path_spec);
142 const auto error = MakeDirectory(
147 return llvm::sys::fs::create_hard_link(local_module_spec.
GetPath(),
148 sysroot_module_path_spec.
GetPath());
153ModuleLock::ModuleLock(
const FileSpec &root_dir_spec,
const UUID &uuid,
155 const auto lock_dir_spec = JoinPath(root_dir_spec, kLockDirName);
156 error = MakeDirectory(lock_dir_spec);
160 m_file_spec = JoinPath(lock_dir_spec, uuid.
GetAsString().c_str());
166 m_file_up = std::move(file.get());
173 m_lock = std::make_unique<lldb_private::LockFile>(m_file_up->GetDescriptor());
174 error = m_lock->WriteLock(0, 1);
176 error.SetErrorStringWithFormat(
"Failed to lock file: %s",
180void ModuleLock::Delete() {
186 llvm::sys::fs::remove(m_file_spec.GetPath());
194 const auto module_spec_dir =
195 GetModuleDirectory(root_dir_spec, module_spec.
GetUUID());
196 const auto module_file_path =
199 const auto tmp_file_path = tmp_file.
GetPath();
200 const auto err_code =
201 llvm::sys::fs::rename(tmp_file_path, module_file_path.GetPath());
203 return Status(
"Failed to rename file %s to %s: %s", tmp_file_path.c_str(),
204 module_file_path.GetPath().c_str(),
205 err_code.message().c_str());
207 const auto error = CreateHostSysRootModuleLink(
208 root_dir_spec, hostname, target_file, module_file_path,
true);
210 return Status(
"Failed to create link to %s: %s",
211 module_file_path.GetPath().c_str(),
error.AsCString());
217 ModuleSP &cached_module_sp,
bool *did_create_ptr) {
221 cached_module_sp = (*find_it).second.lock();
222 if (cached_module_sp)
227 const auto module_spec_dir =
228 GetModuleDirectory(root_dir_spec, module_spec.
GetUUID());
229 const auto module_file_path = JoinPath(
233 return Status(
"Module %s not found", module_file_path.GetPath().c_str());
236 return Status(
"Module %s has invalid file size",
237 module_file_path.GetPath().c_str());
241 auto error = CreateHostSysRootModuleLink(root_dir_spec, hostname,
243 module_file_path,
false);
245 return Status(
"Failed to create link to %s: %s",
246 module_file_path.GetPath().c_str(),
error.AsCString());
248 auto cached_module_spec(module_spec);
249 cached_module_spec.GetUUID().Clear();
251 cached_module_spec.GetFileSpec() = module_file_path;
252 cached_module_spec.GetPlatformFileSpec() = module_spec.
GetFileSpec();
255 nullptr,
nullptr, did_create_ptr,
false);
259 FileSpec symfile_spec = GetSymbolFileSpec(cached_module_sp->GetFileSpec());
261 cached_module_sp->SetSymbolFileFileSpec(symfile_spec);
270 const char *hostname,
275 bool *did_create_ptr) {
276 const auto module_spec_dir =
277 GetModuleDirectory(root_dir_spec, module_spec.
GetUUID());
278 auto error = MakeDirectory(module_spec_dir);
282 ModuleLock lock(root_dir_spec, module_spec.
GetUUID(),
error);
284 return Status(
"Failed to lock module %s: %s",
288 const auto escaped_hostname(GetEscapedHostname(hostname));
290 error =
Get(root_dir_spec, escaped_hostname.c_str(), module_spec,
291 cached_module_sp, did_create_ptr);
295 const auto tmp_download_file_spec = JoinPath(module_spec_dir, kTempFileName);
296 error = module_downloader(module_spec, tmp_download_file_spec);
297 llvm::FileRemover tmp_file_remover(tmp_download_file_spec.GetPath());
299 return Status(
"Failed to download module: %s",
error.AsCString());
302 error =
Put(root_dir_spec, escaped_hostname.c_str(), module_spec,
303 tmp_download_file_spec, module_spec.
GetFileSpec());
305 return Status(
"Failed to put module into cache: %s",
error.AsCString());
307 tmp_file_remover.releaseFile();
308 error =
Get(root_dir_spec, escaped_hostname.c_str(), module_spec,
309 cached_module_sp, did_create_ptr);
314 const auto tmp_download_sym_file_spec =
315 JoinPath(module_spec_dir, kTempSymFileName);
316 error = symfile_downloader(cached_module_sp, tmp_download_sym_file_spec);
317 llvm::FileRemover tmp_symfile_remover(tmp_download_sym_file_spec.GetPath());
324 error =
Put(root_dir_spec, escaped_hostname.c_str(), module_spec,
325 tmp_download_sym_file_spec,
328 return Status(
"Failed to put symbol file into cache: %s",
331 tmp_symfile_remover.releaseFile();
333 FileSpec symfile_spec = GetSymbolFileSpec(cached_module_sp->GetFileSpec());
334 cached_module_sp->SetSymbolFileFileSpec(symfile_spec);
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
const ConstString & GetFilename() const
Filename string const get accessor.
const ConstString & GetDirectory() const
Directory string const get accessor.
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
int Open(const char *path, int flags, int mode=0600)
Wraps ::open in a platform-independent way.
static FileSystem & Instance()
Status Get(const FileSpec &root_dir_spec, const char *hostname, const ModuleSpec &module_spec, lldb::ModuleSP &cached_module_sp, bool *did_create_ptr)
std::unordered_map< std::string, lldb::ModuleWP > m_loaded_modules
std::function< Status(const lldb::ModuleSP &, const FileSpec &)> SymfileDownloader
Status 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)
Status Put(const FileSpec &root_dir_spec, const char *hostname, const ModuleSpec &module_spec, const FileSpec &tmp_file, const FileSpec &target_file)
std::function< Status(const ModuleSpec &, const FileSpec &)> ModuleDownloader
static Status GetSharedModule(const ModuleSpec &module_spec, lldb::ModuleSP &module_sp, const FileSpecList *module_search_paths_ptr, llvm::SmallVectorImpl< lldb::ModuleSP > *old_modules, bool *did_create_ptr, bool always_create=false)
uint64_t GetObjectSize() const
std::string GetAsString(llvm::StringRef separator="-") const
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
std::unique_ptr< lldb_private::File > FileUP
std::shared_ptr< lldb_private::Module > ModuleSP