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);
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);
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());
204 "Failed to rename file %s to %s: %s", tmp_file_path.c_str(),
205 module_file_path.GetPath().c_str(), err_code.message().c_str());
207 const auto error = CreateHostSysRootModuleLink(
208 root_dir_spec, hostname, target_file, module_file_path,
true);
211 module_file_path.GetPath().c_str(),
218 ModuleSP &cached_module_sp,
bool *did_create_ptr) {
222 cached_module_sp = (*find_it).second.lock();
223 if (cached_module_sp)
228 const auto module_spec_dir =
229 GetModuleDirectory(root_dir_spec, module_spec.
GetUUID());
230 const auto module_file_path = JoinPath(
235 "Module %s not found", module_file_path.GetPath().c_str());
239 "Module %s has invalid file size", module_file_path.GetPath().c_str());
243 auto error = CreateHostSysRootModuleLink(root_dir_spec, hostname,
245 module_file_path,
false);
248 module_file_path.GetPath().c_str(),
251 auto cached_module_spec(module_spec);
252 cached_module_spec.GetUUID().Clear();
254 cached_module_spec.GetFileSpec() = module_file_path;
255 cached_module_spec.GetPlatformFileSpec() = module_spec.
GetFileSpec();
258 nullptr,
nullptr, did_create_ptr,
false);
262 FileSpec symfile_spec = GetSymbolFileSpec(cached_module_sp->GetFileSpec());
264 cached_module_sp->SetSymbolFileFileSpec(symfile_spec);
273 const char *hostname,
278 bool *did_create_ptr) {
279 const auto module_spec_dir =
280 GetModuleDirectory(root_dir_spec, module_spec.
GetUUID());
281 auto error = MakeDirectory(module_spec_dir);
285 ModuleLock lock(root_dir_spec, module_spec.
GetUUID(),
error);
288 "Failed to lock module %s: %s",
291 const auto escaped_hostname(GetEscapedHostname(hostname));
293 error =
Get(root_dir_spec, escaped_hostname.c_str(), module_spec,
294 cached_module_sp, did_create_ptr);
298 const auto tmp_download_file_spec = JoinPath(module_spec_dir, kTempFileName);
299 error = module_downloader(module_spec, tmp_download_file_spec);
300 llvm::FileRemover tmp_file_remover(tmp_download_file_spec.GetPath());
306 error =
Put(root_dir_spec, escaped_hostname.c_str(), module_spec,
307 tmp_download_file_spec, module_spec.
GetFileSpec());
310 "Failed to put module into cache: %s",
error.AsCString());
312 tmp_file_remover.releaseFile();
313 error =
Get(root_dir_spec, escaped_hostname.c_str(), module_spec,
314 cached_module_sp, did_create_ptr);
319 const auto tmp_download_sym_file_spec =
320 JoinPath(module_spec_dir, kTempSymFileName);
321 error = symfile_downloader(cached_module_sp, tmp_download_sym_file_spec);
322 llvm::FileRemover tmp_symfile_remover(tmp_download_sym_file_spec.GetPath());
329 error =
Put(root_dir_spec, escaped_hostname.c_str(), module_spec,
330 tmp_download_sym_file_spec,
334 "Failed to put symbol file into cache: %s",
error.AsCString());
336 tmp_symfile_remover.releaseFile();
338 FileSpec symfile_spec = GetSymbolFileSpec(cached_module_sp->GetFileSpec());
339 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
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
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