1 //===-- FileSystem.cpp ----------------------------------------------------===// 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 if (!file_spec) { 91 ec = std::error_code(static_cast<int>(errc::no_such_file_or_directory), 92 std::system_category()); 93 return {}; 94 } 95 return DirBegin(file_spec.GetPath(), ec); 96 } 97 98 vfs::directory_iterator FileSystem::DirBegin(const Twine &dir, 99 std::error_code &ec) { 100 return m_fs->dir_begin(dir, ec); 101 } 102 103 llvm::ErrorOr<vfs::Status> 104 FileSystem::GetStatus(const FileSpec &file_spec) const { 105 if (!file_spec) 106 return std::error_code(static_cast<int>(errc::no_such_file_or_directory), 107 std::system_category()); 108 return GetStatus(file_spec.GetPath()); 109 } 110 111 llvm::ErrorOr<vfs::Status> FileSystem::GetStatus(const Twine &path) const { 112 return m_fs->status(path); 113 } 114 115 sys::TimePoint<> 116 FileSystem::GetModificationTime(const FileSpec &file_spec) const { 117 if (!file_spec) 118 return sys::TimePoint<>(); 119 return GetModificationTime(file_spec.GetPath()); 120 } 121 122 sys::TimePoint<> FileSystem::GetModificationTime(const Twine &path) const { 123 ErrorOr<vfs::Status> status = m_fs->status(path); 124 if (!status) 125 return sys::TimePoint<>(); 126 return status->getLastModificationTime(); 127 } 128 129 uint64_t FileSystem::GetByteSize(const FileSpec &file_spec) const { 130 if (!file_spec) 131 return 0; 132 return GetByteSize(file_spec.GetPath()); 133 } 134 135 uint64_t FileSystem::GetByteSize(const Twine &path) const { 136 ErrorOr<vfs::Status> status = m_fs->status(path); 137 if (!status) 138 return 0; 139 return status->getSize(); 140 } 141 142 uint32_t FileSystem::GetPermissions(const FileSpec &file_spec) const { 143 return GetPermissions(file_spec.GetPath()); 144 } 145 146 uint32_t FileSystem::GetPermissions(const FileSpec &file_spec, 147 std::error_code &ec) const { 148 if (!file_spec) 149 return sys::fs::perms::perms_not_known; 150 return GetPermissions(file_spec.GetPath(), ec); 151 } 152 153 uint32_t FileSystem::GetPermissions(const Twine &path) const { 154 std::error_code ec; 155 return GetPermissions(path, ec); 156 } 157 158 uint32_t FileSystem::GetPermissions(const Twine &path, 159 std::error_code &ec) const { 160 ErrorOr<vfs::Status> status = m_fs->status(path); 161 if (!status) { 162 ec = status.getError(); 163 return sys::fs::perms::perms_not_known; 164 } 165 return status->getPermissions(); 166 } 167 168 bool FileSystem::Exists(const Twine &path) const { return m_fs->exists(path); } 169 170 bool FileSystem::Exists(const FileSpec &file_spec) const { 171 return file_spec && Exists(file_spec.GetPath()); 172 } 173 174 bool FileSystem::Readable(const Twine &path) const { 175 return GetPermissions(path) & sys::fs::perms::all_read; 176 } 177 178 bool FileSystem::Readable(const FileSpec &file_spec) const { 179 return file_spec && Readable(file_spec.GetPath()); 180 } 181 182 bool FileSystem::IsDirectory(const Twine &path) const { 183 ErrorOr<vfs::Status> status = m_fs->status(path); 184 if (!status) 185 return false; 186 return status->isDirectory(); 187 } 188 189 bool FileSystem::IsDirectory(const FileSpec &file_spec) const { 190 return file_spec && IsDirectory(file_spec.GetPath()); 191 } 192 193 bool FileSystem::IsLocal(const Twine &path) const { 194 bool b = false; 195 m_fs->isLocal(path, b); 196 return b; 197 } 198 199 bool FileSystem::IsLocal(const FileSpec &file_spec) const { 200 return file_spec && IsLocal(file_spec.GetPath()); 201 } 202 203 void FileSystem::EnumerateDirectory(Twine path, bool find_directories, 204 bool find_files, bool find_other, 205 EnumerateDirectoryCallbackType callback, 206 void *callback_baton) { 207 std::error_code EC; 208 vfs::recursive_directory_iterator Iter(*m_fs, path, EC); 209 vfs::recursive_directory_iterator End; 210 for (; Iter != End && !EC; Iter.increment(EC)) { 211 const auto &Item = *Iter; 212 ErrorOr<vfs::Status> Status = m_fs->status(Item.path()); 213 if (!Status) 214 break; 215 if (!find_files && Status->isRegularFile()) 216 continue; 217 if (!find_directories && Status->isDirectory()) 218 continue; 219 if (!find_other && Status->isOther()) 220 continue; 221 222 auto Result = callback(callback_baton, Status->getType(), Item.path()); 223 if (Result == eEnumerateDirectoryResultQuit) 224 return; 225 if (Result == eEnumerateDirectoryResultNext) { 226 // Default behavior is to recurse. Opt out if the callback doesn't want 227 // this behavior. 228 Iter.no_push(); 229 } 230 } 231 } 232 233 std::error_code FileSystem::MakeAbsolute(SmallVectorImpl<char> &path) const { 234 return m_fs->makeAbsolute(path); 235 } 236 237 std::error_code FileSystem::MakeAbsolute(FileSpec &file_spec) const { 238 SmallString<128> path; 239 file_spec.GetPath(path, false); 240 241 auto EC = MakeAbsolute(path); 242 if (EC) 243 return EC; 244 245 FileSpec new_file_spec(path, file_spec.GetPathStyle()); 246 file_spec = new_file_spec; 247 return {}; 248 } 249 250 std::error_code FileSystem::GetRealPath(const Twine &path, 251 SmallVectorImpl<char> &output) const { 252 return m_fs->getRealPath(path, output); 253 } 254 255 void FileSystem::Resolve(SmallVectorImpl<char> &path) { 256 if (path.empty()) 257 return; 258 259 // Resolve tilde in path. 260 SmallString<128> resolved(path.begin(), path.end()); 261 StandardTildeExpressionResolver Resolver; 262 Resolver.ResolveFullPath(llvm::StringRef(path.begin(), path.size()), 263 resolved); 264 265 // Try making the path absolute if it exists. 266 SmallString<128> absolute(resolved.begin(), resolved.end()); 267 MakeAbsolute(absolute); 268 269 path.clear(); 270 if (Exists(absolute)) { 271 path.append(absolute.begin(), absolute.end()); 272 } else { 273 path.append(resolved.begin(), resolved.end()); 274 } 275 } 276 277 void FileSystem::Resolve(FileSpec &file_spec) { 278 if (!file_spec) 279 return; 280 281 // Extract path from the FileSpec. 282 SmallString<128> path; 283 file_spec.GetPath(path); 284 285 // Resolve the path. 286 Resolve(path); 287 288 // Update the FileSpec with the resolved path. 289 if (file_spec.GetFilename().IsEmpty()) 290 file_spec.GetDirectory().SetString(path); 291 else 292 file_spec.SetPath(path); 293 file_spec.SetIsResolved(true); 294 } 295 296 std::shared_ptr<DataBufferLLVM> 297 FileSystem::CreateDataBuffer(const llvm::Twine &path, uint64_t size, 298 uint64_t offset) { 299 Collect(path); 300 301 const bool is_volatile = !IsLocal(path); 302 const ErrorOr<std::string> external_path = GetExternalPath(path); 303 304 if (!external_path) 305 return nullptr; 306 307 std::unique_ptr<llvm::WritableMemoryBuffer> buffer; 308 if (size == 0) { 309 auto buffer_or_error = 310 llvm::WritableMemoryBuffer::getFile(*external_path, -1, is_volatile); 311 if (!buffer_or_error) 312 return nullptr; 313 buffer = std::move(*buffer_or_error); 314 } else { 315 auto buffer_or_error = llvm::WritableMemoryBuffer::getFileSlice( 316 *external_path, size, offset, is_volatile); 317 if (!buffer_or_error) 318 return nullptr; 319 buffer = std::move(*buffer_or_error); 320 } 321 return std::shared_ptr<DataBufferLLVM>(new DataBufferLLVM(std::move(buffer))); 322 } 323 324 std::shared_ptr<DataBufferLLVM> 325 FileSystem::CreateDataBuffer(const FileSpec &file_spec, uint64_t size, 326 uint64_t offset) { 327 return CreateDataBuffer(file_spec.GetPath(), size, offset); 328 } 329 330 bool FileSystem::ResolveExecutableLocation(FileSpec &file_spec) { 331 // If the directory is set there's nothing to do. 332 ConstString directory = file_spec.GetDirectory(); 333 if (directory) 334 return false; 335 336 // We cannot look for a file if there's no file name. 337 ConstString filename = file_spec.GetFilename(); 338 if (!filename) 339 return false; 340 341 // Search for the file on the host. 342 const std::string filename_str(filename.GetCString()); 343 llvm::ErrorOr<std::string> error_or_path = 344 llvm::sys::findProgramByName(filename_str); 345 if (!error_or_path) 346 return false; 347 348 // findProgramByName returns "." if it can't find the file. 349 llvm::StringRef path = *error_or_path; 350 llvm::StringRef parent = llvm::sys::path::parent_path(path); 351 if (parent.empty() || parent == ".") 352 return false; 353 354 // Make sure that the result exists. 355 FileSpec result(*error_or_path); 356 if (!Exists(result)) 357 return false; 358 359 file_spec = result; 360 return true; 361 } 362 363 static int OpenWithFS(const FileSystem &fs, const char *path, int flags, 364 int mode) { 365 return const_cast<FileSystem &>(fs).Open(path, flags, mode); 366 } 367 368 static int GetOpenFlags(uint32_t options) { 369 const bool read = options & File::eOpenOptionRead; 370 const bool write = options & File::eOpenOptionWrite; 371 372 int open_flags = 0; 373 if (write) { 374 if (read) 375 open_flags |= O_RDWR; 376 else 377 open_flags |= O_WRONLY; 378 379 if (options & File::eOpenOptionAppend) 380 open_flags |= O_APPEND; 381 382 if (options & File::eOpenOptionTruncate) 383 open_flags |= O_TRUNC; 384 385 if (options & File::eOpenOptionCanCreate) 386 open_flags |= O_CREAT; 387 388 if (options & File::eOpenOptionCanCreateNewOnly) 389 open_flags |= O_CREAT | O_EXCL; 390 } else if (read) { 391 open_flags |= O_RDONLY; 392 393 #ifndef _WIN32 394 if (options & File::eOpenOptionDontFollowSymlinks) 395 open_flags |= O_NOFOLLOW; 396 #endif 397 } 398 399 #ifndef _WIN32 400 if (options & File::eOpenOptionNonBlocking) 401 open_flags |= O_NONBLOCK; 402 if (options & File::eOpenOptionCloseOnExec) 403 open_flags |= O_CLOEXEC; 404 #else 405 open_flags |= O_BINARY; 406 #endif 407 408 return open_flags; 409 } 410 411 static mode_t GetOpenMode(uint32_t permissions) { 412 mode_t mode = 0; 413 if (permissions & lldb::eFilePermissionsUserRead) 414 mode |= S_IRUSR; 415 if (permissions & lldb::eFilePermissionsUserWrite) 416 mode |= S_IWUSR; 417 if (permissions & lldb::eFilePermissionsUserExecute) 418 mode |= S_IXUSR; 419 if (permissions & lldb::eFilePermissionsGroupRead) 420 mode |= S_IRGRP; 421 if (permissions & lldb::eFilePermissionsGroupWrite) 422 mode |= S_IWGRP; 423 if (permissions & lldb::eFilePermissionsGroupExecute) 424 mode |= S_IXGRP; 425 if (permissions & lldb::eFilePermissionsWorldRead) 426 mode |= S_IROTH; 427 if (permissions & lldb::eFilePermissionsWorldWrite) 428 mode |= S_IWOTH; 429 if (permissions & lldb::eFilePermissionsWorldExecute) 430 mode |= S_IXOTH; 431 return mode; 432 } 433 434 Expected<FileUP> FileSystem::Open(const FileSpec &file_spec, 435 File::OpenOptions options, 436 uint32_t permissions, bool should_close_fd) { 437 Collect(file_spec.GetPath()); 438 439 const int open_flags = GetOpenFlags(options); 440 const mode_t open_mode = 441 (open_flags & O_CREAT) ? GetOpenMode(permissions) : 0; 442 443 auto path = GetExternalPath(file_spec); 444 if (!path) 445 return errorCodeToError(path.getError()); 446 447 int descriptor = llvm::sys::RetryAfterSignal( 448 -1, OpenWithFS, *this, path->c_str(), open_flags, open_mode); 449 450 if (!File::DescriptorIsValid(descriptor)) 451 return llvm::errorCodeToError( 452 std::error_code(errno, std::system_category())); 453 454 auto file = std::unique_ptr<File>( 455 new NativeFile(descriptor, options, should_close_fd)); 456 assert(file->IsValid()); 457 return std::move(file); 458 } 459 460 ErrorOr<std::string> FileSystem::GetExternalPath(const llvm::Twine &path) { 461 if (!m_mapped) 462 return path.str(); 463 464 // If VFS mapped we know the underlying FS is a RedirectingFileSystem. 465 ErrorOr<vfs::RedirectingFileSystem::Entry *> E = 466 static_cast<vfs::RedirectingFileSystem &>(*m_fs).lookupPath(path); 467 if (!E) { 468 if (E.getError() == llvm::errc::no_such_file_or_directory) { 469 return path.str(); 470 } 471 return E.getError(); 472 } 473 474 auto *F = dyn_cast<vfs::RedirectingFileSystem::RedirectingFileEntry>(*E); 475 if (!F) 476 return make_error_code(llvm::errc::not_supported); 477 478 return F->getExternalContentsPath().str(); 479 } 480 481 ErrorOr<std::string> FileSystem::GetExternalPath(const FileSpec &file_spec) { 482 return GetExternalPath(file_spec.GetPath()); 483 } 484 485 void FileSystem::Collect(const FileSpec &file_spec) { 486 Collect(file_spec.GetPath()); 487 } 488 489 void FileSystem::Collect(const llvm::Twine &file) { 490 if (!m_collector) 491 return; 492 493 if (llvm::sys::fs::is_directory(file)) 494 m_collector->addDirectory(file); 495 else 496 m_collector->addFile(file); 497 } 498