18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/FileUtilities.h"
30 const char *kModulesSubdir =
".cache";
31 const char *kLockDirName =
".lock";
32 const char *kTempFileName =
".temp";
33 const char *kTempSymFileName =
".symtemp";
34 const char *kSymFileExtension =
".sym";
35 const char *kFSIllegalChars =
"\\/:*?\"<>|";
37 std::string GetEscapedHostname(
const char *hostname) {
38 if (hostname ==
nullptr)
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());
82 void 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());
115 void 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());
135 if (FileSystem::Instance().Exists(sysroot_module_path_spec)) {
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());
153 ModuleLock::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());
162 auto file = FileSystem::Instance().Open(
163 m_file_spec, File::eOpenOptionWriteOnly | File::eOpenOptionCanCreate |
164 File::eOpenOptionCloseOnExec);
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",
180 void 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) {
220 if (find_it != m_loaded_modules.end()) {
221 cached_module_sp = (*find_it).second.lock();
222 if (cached_module_sp)
224 m_loaded_modules.erase(find_it);
227 const auto module_spec_dir =
228 GetModuleDirectory(root_dir_spec, module_spec.
GetUUID());
229 const auto module_file_path = JoinPath(
232 if (!FileSystem::Instance().Exists(module_file_path))
233 return Status(
"Module %s not found", module_file_path.GetPath().c_str());
234 if (FileSystem::Instance().GetByteSize(module_file_path) !=
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();
254 error = ModuleList::GetSharedModule(cached_module_spec, cached_module_sp,
255 nullptr,
nullptr, did_create_ptr,
false);
259 FileSpec symfile_spec = GetSymbolFileSpec(cached_module_sp->GetFileSpec());
260 if (FileSystem::Instance().Exists(symfile_spec))
261 cached_module_sp->SetSymbolFileFileSpec(symfile_spec);
263 m_loaded_modules.insert(
270 const char *hostname,
274 lldb::ModuleSP &cached_module_sp,
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);