1 //===-- FileSystem.cpp ------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Host/FileSystem.h" 10 11 #include "lldb/Utility/LLDBAssert.h" 12 #include "lldb/Utility/TildeExpressionResolver.h" 13 14 #include "llvm/Support/Errc.h" 15 #include "llvm/Support/Errno.h" 16 #include "llvm/Support/Error.h" 17 #include "llvm/Support/FileSystem.h" 18 #include "llvm/Support/Path.h" 19 #include "llvm/Support/Program.h" 20 #include "llvm/Support/Threading.h" 21 22 #include <errno.h> 23 #include <fcntl.h> 24 #include <limits.h> 25 #include <stdarg.h> 26 #include <stdio.h> 27 28 #ifdef _WIN32 29 #include "lldb/Host/windows/windows.h" 30 #else 31 #include <sys/ioctl.h> 32 #include <sys/stat.h> 33 #include <termios.h> 34 #include <unistd.h> 35 #endif 36 37 #include <algorithm> 38 #include <fstream> 39 #include <vector> 40 41 using namespace lldb; 42 using namespace lldb_private; 43 using namespace llvm; 44 45 FileSystem &FileSystem::Instance() { return *InstanceImpl(); } 46 47 void FileSystem::Initialize() { 48 lldbassert(!InstanceImpl() && "Already initialized."); 49 InstanceImpl().emplace(); 50 } 51 52 void FileSystem::Initialize(std::shared_ptr<FileCollector> collector) { 53 lldbassert(!InstanceImpl() && "Already initialized."); 54 InstanceImpl().emplace(collector); 55 } 56 57 llvm::Error FileSystem::Initialize(const FileSpec &mapping) { 58 lldbassert(!InstanceImpl() && "Already initialized."); 59 60 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer = 61 llvm::vfs::getRealFileSystem()->getBufferForFile(mapping.GetPath()); 62 63 if (!buffer) 64 return llvm::errorCodeToError(buffer.getError()); 65 66 InstanceImpl().emplace(llvm::vfs::getVFSFromYAML(std::move(buffer.get()), 67 nullptr, mapping.GetPath()), 68 true); 69 70 return llvm::Error::success(); 71 } 72 73 void FileSystem::Initialize(IntrusiveRefCntPtr<vfs::FileSystem> fs) { 74 lldbassert(!InstanceImpl() && "Already initialized."); 75 InstanceImpl().emplace(fs); 76 } 77 78 void FileSystem::Terminate() { 79 lldbassert(InstanceImpl() && "Already terminated."); 80 InstanceImpl().reset(); 81 } 82 83 Optional<FileSystem> &FileSystem::InstanceImpl() { 84 static Optional<FileSystem> g_fs; 85 return g_fs; 86 } 87 88 vfs::directory_iterator FileSystem::DirBegin(const FileSpec &file_spec, 89 std::error_code &ec) { 90 return DirBegin(file_spec.GetPath(), ec); 91 } 92 93 vfs::directory_iterator FileSystem::DirBegin(const Twine &dir, 94 std::error_code &ec) { 95 return m_fs->dir_begin(dir, ec); 96 } 97 98 llvm::ErrorOr<vfs::Status> 99 FileSystem::GetStatus(const FileSpec &file_spec) const { 100 return GetStatus(file_spec.GetPath()); 101 } 102 103 llvm::ErrorOr<vfs::Status> FileSystem::GetStatus(const Twine &path) const { 104 return m_fs->status(path); 105 } 106 107 sys::TimePoint<> 108 FileSystem::GetModificationTime(const FileSpec &file_spec) const { 109 return GetModificationTime(file_spec.GetPath()); 110 } 111 112 sys::TimePoint<> FileSystem::GetModificationTime(const Twine &path) const { 113 ErrorOr<vfs::Status> status = m_fs->status(path); 114 if (!status) 115 return sys::TimePoint<>(); 116 return status->getLastModificationTime(); 117 } 118 119 uint64_t FileSystem::GetByteSize(const FileSpec &file_spec) const { 120 return GetByteSize(file_spec.GetPath()); 121 } 122 123 uint64_t FileSystem::GetByteSize(const Twine &path) const { 124 ErrorOr<vfs::Status> status = m_fs->status(path); 125 if (!status) 126 return 0; 127 return status->getSize(); 128 } 129 130 uint32_t FileSystem::GetPermissions(const FileSpec &file_spec) const { 131 return GetPermissions(file_spec.GetPath()); 132 } 133 134 uint32_t FileSystem::GetPermissions(const FileSpec &file_spec, 135 std::error_code &ec) const { 136 return GetPermissions(file_spec.GetPath(), ec); 137 } 138 139 uint32_t FileSystem::GetPermissions(const Twine &path) const { 140 std::error_code ec; 141 return GetPermissions(path, ec); 142 } 143 144 uint32_t FileSystem::GetPermissions(const Twine &path, 145 std::error_code &ec) const { 146 ErrorOr<vfs::Status> status = m_fs->status(path); 147 if (!status) { 148 ec = status.getError(); 149 return sys::fs::perms::perms_not_known; 150 } 151 return status->getPermissions(); 152 } 153 154 bool FileSystem::Exists(const Twine &path) const { return m_fs->exists(path); } 155 156 bool FileSystem::Exists(const FileSpec &file_spec) const { 157 return Exists(file_spec.GetPath()); 158 } 159 160 bool FileSystem::Readable(const Twine &path) const { 161 return GetPermissions(path) & sys::fs::perms::all_read; 162 } 163 164 bool FileSystem::Readable(const FileSpec &file_spec) const { 165 return Readable(file_spec.GetPath()); 166 } 167 168 bool FileSystem::IsDirectory(const Twine &path) const { 169 ErrorOr<vfs::Status> status = m_fs->status(path); 170 if (!status) 171 return false; 172 return status->isDirectory(); 173 } 174 175 bool FileSystem::IsDirectory(const FileSpec &file_spec) const { 176 return IsDirectory(file_spec.GetPath()); 177 } 178 179 bool FileSystem::IsLocal(const Twine &path) const { 180 bool b = false; 181 m_fs->isLocal(path, b); 182 return b; 183 } 184 185 bool FileSystem::IsLocal(const FileSpec &file_spec) const { 186 return IsLocal(file_spec.GetPath()); 187 } 188 189 void FileSystem::EnumerateDirectory(Twine path, bool find_directories, 190 bool find_files, bool find_other, 191 EnumerateDirectoryCallbackType callback, 192 void *callback_baton) { 193 std::error_code EC; 194 vfs::recursive_directory_iterator Iter(*m_fs, path, EC); 195 vfs::recursive_directory_iterator End; 196 for (; Iter != End && !EC; Iter.increment(EC)) { 197 const auto &Item = *Iter; 198 ErrorOr<vfs::Status> Status = m_fs->status(Item.path()); 199 if (!Status) 200 break; 201 if (!find_files && Status->isRegularFile()) 202 continue; 203 if (!find_directories && Status->isDirectory()) 204 continue; 205 if (!find_other && Status->isOther()) 206 continue; 207 208 auto Result = callback(callback_baton, Status->getType(), Item.path()); 209 if (Result == eEnumerateDirectoryResultQuit) 210 return; 211 if (Result == eEnumerateDirectoryResultNext) { 212 // Default behavior is to recurse. Opt out if the callback doesn't want 213 // this behavior. 214 Iter.no_push(); 215 } 216 } 217 } 218 219 std::error_code FileSystem::MakeAbsolute(SmallVectorImpl<char> &path) const { 220 return m_fs->makeAbsolute(path); 221 } 222 223 std::error_code FileSystem::MakeAbsolute(FileSpec &file_spec) const { 224 SmallString<128> path; 225 file_spec.GetPath(path, false); 226 227 auto EC = MakeAbsolute(path); 228 if (EC) 229 return EC; 230 231 FileSpec new_file_spec(path, file_spec.GetPathStyle()); 232 file_spec = new_file_spec; 233 return {}; 234 } 235 236 std::error_code FileSystem::GetRealPath(const Twine &path, 237 SmallVectorImpl<char> &output) const { 238 return m_fs->getRealPath(path, output); 239 } 240 241 void FileSystem::Resolve(SmallVectorImpl<char> &path) { 242 if (path.empty()) 243 return; 244 245 // Resolve tilde in path. 246 SmallString<128> resolved(path.begin(), path.end()); 247 StandardTildeExpressionResolver Resolver; 248 Resolver.ResolveFullPath(llvm::StringRef(path.begin(), path.size()), 249 resolved); 250 251 // Try making the path absolute if it exists. 252 SmallString<128> absolute(resolved.begin(), resolved.end()); 253 MakeAbsolute(absolute); 254 255 path.clear(); 256 if (Exists(absolute)) { 257 path.append(absolute.begin(), absolute.end()); 258 } else { 259 path.append(resolved.begin(), resolved.end()); 260 } 261 } 262 263 void FileSystem::Resolve(FileSpec &file_spec) { 264 // Extract path from the FileSpec. 265 SmallString<128> path; 266 file_spec.GetPath(path); 267 268 // Resolve the path. 269 Resolve(path); 270 271 // Update the FileSpec with the resolved path. 272 if (file_spec.GetFilename().IsEmpty()) 273 file_spec.GetDirectory().SetString(path); 274 else 275 file_spec.SetPath(path); 276 file_spec.SetIsResolved(true); 277 } 278 279 std::shared_ptr<DataBufferLLVM> 280 FileSystem::CreateDataBuffer(const llvm::Twine &path, uint64_t size, 281 uint64_t offset) { 282 AddFile(path); 283 284 const bool is_volatile = !IsLocal(path); 285 const ErrorOr<std::string> external_path = GetExternalPath(path); 286 287 if (!external_path) 288 return nullptr; 289 290 std::unique_ptr<llvm::WritableMemoryBuffer> buffer; 291 if (size == 0) { 292 auto buffer_or_error = 293 llvm::WritableMemoryBuffer::getFile(*external_path, -1, is_volatile); 294 if (!buffer_or_error) 295 return nullptr; 296 buffer = std::move(*buffer_or_error); 297 } else { 298 auto buffer_or_error = llvm::WritableMemoryBuffer::getFileSlice( 299 *external_path, size, offset, is_volatile); 300 if (!buffer_or_error) 301 return nullptr; 302 buffer = std::move(*buffer_or_error); 303 } 304 return std::shared_ptr<DataBufferLLVM>(new DataBufferLLVM(std::move(buffer))); 305 } 306 307 std::shared_ptr<DataBufferLLVM> 308 FileSystem::CreateDataBuffer(const FileSpec &file_spec, uint64_t size, 309 uint64_t offset) { 310 return CreateDataBuffer(file_spec.GetPath(), size, offset); 311 } 312 313 bool FileSystem::ResolveExecutableLocation(FileSpec &file_spec) { 314 // If the directory is set there's nothing to do. 315 ConstString directory = file_spec.GetDirectory(); 316 if (directory) 317 return false; 318 319 // We cannot look for a file if there's no file name. 320 ConstString filename = file_spec.GetFilename(); 321 if (!filename) 322 return false; 323 324 // Search for the file on the host. 325 const std::string filename_str(filename.GetCString()); 326 llvm::ErrorOr<std::string> error_or_path = 327 llvm::sys::findProgramByName(filename_str); 328 if (!error_or_path) 329 return false; 330 331 // findProgramByName returns "." if it can't find the file. 332 llvm::StringRef path = *error_or_path; 333 llvm::StringRef parent = llvm::sys::path::parent_path(path); 334 if (parent.empty() || parent == ".") 335 return false; 336 337 // Make sure that the result exists. 338 FileSpec result(*error_or_path); 339 if (!Exists(result)) 340 return false; 341 342 file_spec = result; 343 return true; 344 } 345 346 static int OpenWithFS(const FileSystem &fs, const char *path, int flags, 347 int mode) { 348 return const_cast<FileSystem &>(fs).Open(path, flags, mode); 349 } 350 351 static int GetOpenFlags(uint32_t options) { 352 const bool read = options & File::eOpenOptionRead; 353 const bool write = options & File::eOpenOptionWrite; 354 355 int open_flags = 0; 356 if (write) { 357 if (read) 358 open_flags |= O_RDWR; 359 else 360 open_flags |= O_WRONLY; 361 362 if (options & File::eOpenOptionAppend) 363 open_flags |= O_APPEND; 364 365 if (options & File::eOpenOptionTruncate) 366 open_flags |= O_TRUNC; 367 368 if (options & File::eOpenOptionCanCreate) 369 open_flags |= O_CREAT; 370 371 if (options & File::eOpenOptionCanCreateNewOnly) 372 open_flags |= O_CREAT | O_EXCL; 373 } else if (read) { 374 open_flags |= O_RDONLY; 375 376 #ifndef _WIN32 377 if (options & File::eOpenOptionDontFollowSymlinks) 378 open_flags |= O_NOFOLLOW; 379 #endif 380 } 381 382 #ifndef _WIN32 383 if (options & File::eOpenOptionNonBlocking) 384 open_flags |= O_NONBLOCK; 385 if (options & File::eOpenOptionCloseOnExec) 386 open_flags |= O_CLOEXEC; 387 #else 388 open_flags |= O_BINARY; 389 #endif 390 391 return open_flags; 392 } 393 394 static mode_t GetOpenMode(uint32_t permissions) { 395 mode_t mode = 0; 396 if (permissions & lldb::eFilePermissionsUserRead) 397 mode |= S_IRUSR; 398 if (permissions & lldb::eFilePermissionsUserWrite) 399 mode |= S_IWUSR; 400 if (permissions & lldb::eFilePermissionsUserExecute) 401 mode |= S_IXUSR; 402 if (permissions & lldb::eFilePermissionsGroupRead) 403 mode |= S_IRGRP; 404 if (permissions & lldb::eFilePermissionsGroupWrite) 405 mode |= S_IWGRP; 406 if (permissions & lldb::eFilePermissionsGroupExecute) 407 mode |= S_IXGRP; 408 if (permissions & lldb::eFilePermissionsWorldRead) 409 mode |= S_IROTH; 410 if (permissions & lldb::eFilePermissionsWorldWrite) 411 mode |= S_IWOTH; 412 if (permissions & lldb::eFilePermissionsWorldExecute) 413 mode |= S_IXOTH; 414 return mode; 415 } 416 417 Expected<FileUP> FileSystem::Open(const FileSpec &file_spec, 418 File::OpenOptions options, 419 uint32_t permissions, bool should_close_fd) { 420 AddFile(file_spec.GetPath()); 421 422 const int open_flags = GetOpenFlags(options); 423 const mode_t open_mode = 424 (open_flags & O_CREAT) ? GetOpenMode(permissions) : 0; 425 426 auto path = GetExternalPath(file_spec); 427 if (!path) 428 return errorCodeToError(path.getError()); 429 430 int descriptor = llvm::sys::RetryAfterSignal( 431 -1, OpenWithFS, *this, path->c_str(), open_flags, open_mode); 432 433 if (!File::DescriptorIsValid(descriptor)) 434 return llvm::errorCodeToError( 435 std::error_code(errno, std::system_category())); 436 437 auto file = std::unique_ptr<File>( 438 new NativeFile(descriptor, options, should_close_fd)); 439 assert(file->IsValid()); 440 return std::move(file); 441 } 442 443 ErrorOr<std::string> FileSystem::GetExternalPath(const llvm::Twine &path) { 444 if (!m_mapped) 445 return path.str(); 446 447 // If VFS mapped we know the underlying FS is a RedirectingFileSystem. 448 ErrorOr<vfs::RedirectingFileSystem::Entry *> E = 449 static_cast<vfs::RedirectingFileSystem &>(*m_fs).lookupPath(path); 450 if (!E) { 451 if (E.getError() == llvm::errc::no_such_file_or_directory) { 452 return path.str(); 453 } 454 return E.getError(); 455 } 456 457 auto *F = dyn_cast<vfs::RedirectingFileSystem::RedirectingFileEntry>(*E); 458 if (!F) 459 return make_error_code(llvm::errc::not_supported); 460 461 return F->getExternalContentsPath().str(); 462 } 463 464 ErrorOr<std::string> FileSystem::GetExternalPath(const FileSpec &file_spec) { 465 return GetExternalPath(file_spec.GetPath()); 466 } 467 468 void FileSystem::AddFile(const llvm::Twine &file) { 469 if (m_collector && !llvm::sys::fs::is_directory(file)) { 470 m_collector->addFile(file); 471 } 472 } 473