17a9e7621SOleksiy Vyalov //===-- FileSystem.cpp ------------------------------------------*- C++ -*-===//
27a9e7621SOleksiy Vyalov //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67a9e7621SOleksiy Vyalov //
77a9e7621SOleksiy Vyalov //===----------------------------------------------------------------------===//
87a9e7621SOleksiy Vyalov 
97a9e7621SOleksiy Vyalov #include "lldb/Host/FileSystem.h"
107a9e7621SOleksiy Vyalov 
110bca15a3SJonas Devlieghere #include "lldb/Utility/LLDBAssert.h"
1246376966SJonas Devlieghere #include "lldb/Utility/TildeExpressionResolver.h"
1346376966SJonas Devlieghere 
1446575176SJonas Devlieghere #include "llvm/Support/Errc.h"
1550bc1ed2SJonas Devlieghere #include "llvm/Support/Errno.h"
1646575176SJonas Devlieghere #include "llvm/Support/Error.h"
171408bf72SPavel Labath #include "llvm/Support/FileSystem.h"
182c22c800SJonas Devlieghere #include "llvm/Support/Path.h"
192c22c800SJonas Devlieghere #include "llvm/Support/Program.h"
2046376966SJonas Devlieghere #include "llvm/Support/Threading.h"
217a9e7621SOleksiy Vyalov 
2250bc1ed2SJonas Devlieghere #include <errno.h>
2350bc1ed2SJonas Devlieghere #include <fcntl.h>
2450bc1ed2SJonas Devlieghere #include <limits.h>
2550bc1ed2SJonas Devlieghere #include <stdarg.h>
2650bc1ed2SJonas Devlieghere #include <stdio.h>
2750bc1ed2SJonas Devlieghere 
2850bc1ed2SJonas Devlieghere #ifdef _WIN32
2950bc1ed2SJonas Devlieghere #include "lldb/Host/windows/windows.h"
3050bc1ed2SJonas Devlieghere #else
3150bc1ed2SJonas Devlieghere #include <sys/ioctl.h>
3250bc1ed2SJonas Devlieghere #include <sys/stat.h>
3350bc1ed2SJonas Devlieghere #include <termios.h>
3450bc1ed2SJonas Devlieghere #include <unistd.h>
3550bc1ed2SJonas Devlieghere #endif
3650bc1ed2SJonas Devlieghere 
376801be33SOleksiy Vyalov #include <algorithm>
387a9e7621SOleksiy Vyalov #include <fstream>
397a9e7621SOleksiy Vyalov #include <vector>
407a9e7621SOleksiy Vyalov 
417a9e7621SOleksiy Vyalov using namespace lldb;
427a9e7621SOleksiy Vyalov using namespace lldb_private;
4346376966SJonas Devlieghere using namespace llvm;
447a9e7621SOleksiy Vyalov 
4546376966SJonas Devlieghere FileSystem &FileSystem::Instance() { return *InstanceImpl(); }
4646376966SJonas Devlieghere 
4746376966SJonas Devlieghere void FileSystem::Initialize() {
480bca15a3SJonas Devlieghere   lldbassert(!InstanceImpl() && "Already initialized.");
4946376966SJonas Devlieghere   InstanceImpl().emplace();
5046376966SJonas Devlieghere }
5146376966SJonas Devlieghere 
5246575176SJonas Devlieghere void FileSystem::Initialize(FileCollector &collector) {
5346575176SJonas Devlieghere   lldbassert(!InstanceImpl() && "Already initialized.");
5446575176SJonas Devlieghere   InstanceImpl().emplace(collector);
5546575176SJonas Devlieghere }
5646575176SJonas Devlieghere 
5746575176SJonas Devlieghere llvm::Error FileSystem::Initialize(const FileSpec &mapping) {
5846575176SJonas Devlieghere   lldbassert(!InstanceImpl() && "Already initialized.");
5946575176SJonas Devlieghere 
6046575176SJonas Devlieghere   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer =
6146575176SJonas Devlieghere       llvm::vfs::getRealFileSystem()->getBufferForFile(mapping.GetPath());
6246575176SJonas Devlieghere 
6346575176SJonas Devlieghere   if (!buffer)
6446575176SJonas Devlieghere     return llvm::errorCodeToError(buffer.getError());
6546575176SJonas Devlieghere 
6646575176SJonas Devlieghere   InstanceImpl().emplace(
6746575176SJonas Devlieghere       llvm::vfs::getVFSFromYAML(std::move(buffer.get()), nullptr, ""), true);
6846575176SJonas Devlieghere 
6946575176SJonas Devlieghere   return llvm::Error::success();
7046575176SJonas Devlieghere }
7146575176SJonas Devlieghere 
7246376966SJonas Devlieghere void FileSystem::Initialize(IntrusiveRefCntPtr<vfs::FileSystem> fs) {
730bca15a3SJonas Devlieghere   lldbassert(!InstanceImpl() && "Already initialized.");
7446376966SJonas Devlieghere   InstanceImpl().emplace(fs);
7546376966SJonas Devlieghere }
7646376966SJonas Devlieghere 
7746376966SJonas Devlieghere void FileSystem::Terminate() {
780bca15a3SJonas Devlieghere   lldbassert(InstanceImpl() && "Already terminated.");
7946376966SJonas Devlieghere   InstanceImpl().reset();
8046376966SJonas Devlieghere }
8146376966SJonas Devlieghere 
8246376966SJonas Devlieghere Optional<FileSystem> &FileSystem::InstanceImpl() {
8346376966SJonas Devlieghere   static Optional<FileSystem> g_fs;
8446376966SJonas Devlieghere   return g_fs;
8546376966SJonas Devlieghere }
8646376966SJonas Devlieghere 
87edaf2bccSJonas Devlieghere vfs::directory_iterator FileSystem::DirBegin(const FileSpec &file_spec,
88edaf2bccSJonas Devlieghere                                              std::error_code &ec) {
89edaf2bccSJonas Devlieghere   return DirBegin(file_spec.GetPath(), ec);
90edaf2bccSJonas Devlieghere }
91edaf2bccSJonas Devlieghere 
92edaf2bccSJonas Devlieghere vfs::directory_iterator FileSystem::DirBegin(const Twine &dir,
93edaf2bccSJonas Devlieghere                                              std::error_code &ec) {
94edaf2bccSJonas Devlieghere   return m_fs->dir_begin(dir, ec);
95edaf2bccSJonas Devlieghere }
96edaf2bccSJonas Devlieghere 
97edaf2bccSJonas Devlieghere llvm::ErrorOr<vfs::Status>
98edaf2bccSJonas Devlieghere FileSystem::GetStatus(const FileSpec &file_spec) const {
99edaf2bccSJonas Devlieghere   return GetStatus(file_spec.GetPath());
100edaf2bccSJonas Devlieghere }
101edaf2bccSJonas Devlieghere 
102edaf2bccSJonas Devlieghere llvm::ErrorOr<vfs::Status> FileSystem::GetStatus(const Twine &path) const {
103edaf2bccSJonas Devlieghere   return m_fs->status(path);
104edaf2bccSJonas Devlieghere }
105edaf2bccSJonas Devlieghere 
10646376966SJonas Devlieghere sys::TimePoint<>
107010b56beSJonas Devlieghere FileSystem::GetModificationTime(const FileSpec &file_spec) const {
108010b56beSJonas Devlieghere   return GetModificationTime(file_spec.GetPath());
10946376966SJonas Devlieghere }
11046376966SJonas Devlieghere 
111010b56beSJonas Devlieghere sys::TimePoint<> FileSystem::GetModificationTime(const Twine &path) const {
11246376966SJonas Devlieghere   ErrorOr<vfs::Status> status = m_fs->status(path);
11346376966SJonas Devlieghere   if (!status)
11446376966SJonas Devlieghere     return sys::TimePoint<>();
11546376966SJonas Devlieghere   return status->getLastModificationTime();
11646376966SJonas Devlieghere }
11746376966SJonas Devlieghere 
11846376966SJonas Devlieghere uint64_t FileSystem::GetByteSize(const FileSpec &file_spec) const {
11946376966SJonas Devlieghere   return GetByteSize(file_spec.GetPath());
12046376966SJonas Devlieghere }
12146376966SJonas Devlieghere 
12246376966SJonas Devlieghere uint64_t FileSystem::GetByteSize(const Twine &path) const {
12346376966SJonas Devlieghere   ErrorOr<vfs::Status> status = m_fs->status(path);
12446376966SJonas Devlieghere   if (!status)
12546376966SJonas Devlieghere     return 0;
12646376966SJonas Devlieghere   return status->getSize();
12746376966SJonas Devlieghere }
12846376966SJonas Devlieghere 
12946376966SJonas Devlieghere uint32_t FileSystem::GetPermissions(const FileSpec &file_spec) const {
13046376966SJonas Devlieghere   return GetPermissions(file_spec.GetPath());
13146376966SJonas Devlieghere }
13246376966SJonas Devlieghere 
13373ed6071SJonas Devlieghere uint32_t FileSystem::GetPermissions(const FileSpec &file_spec,
13473ed6071SJonas Devlieghere                                     std::error_code &ec) const {
13573ed6071SJonas Devlieghere   return GetPermissions(file_spec.GetPath(), ec);
13673ed6071SJonas Devlieghere }
13773ed6071SJonas Devlieghere 
13846376966SJonas Devlieghere uint32_t FileSystem::GetPermissions(const Twine &path) const {
13973ed6071SJonas Devlieghere   std::error_code ec;
14073ed6071SJonas Devlieghere   return GetPermissions(path, ec);
14173ed6071SJonas Devlieghere }
14273ed6071SJonas Devlieghere 
14373ed6071SJonas Devlieghere uint32_t FileSystem::GetPermissions(const Twine &path,
14473ed6071SJonas Devlieghere                                     std::error_code &ec) const {
14546376966SJonas Devlieghere   ErrorOr<vfs::Status> status = m_fs->status(path);
14673ed6071SJonas Devlieghere   if (!status) {
14773ed6071SJonas Devlieghere     ec = status.getError();
14846376966SJonas Devlieghere     return sys::fs::perms::perms_not_known;
14973ed6071SJonas Devlieghere   }
15046376966SJonas Devlieghere   return status->getPermissions();
15146376966SJonas Devlieghere }
15246376966SJonas Devlieghere 
15346376966SJonas Devlieghere bool FileSystem::Exists(const Twine &path) const { return m_fs->exists(path); }
15446376966SJonas Devlieghere 
15546376966SJonas Devlieghere bool FileSystem::Exists(const FileSpec &file_spec) const {
15646376966SJonas Devlieghere   return Exists(file_spec.GetPath());
15746376966SJonas Devlieghere }
15846376966SJonas Devlieghere 
15946376966SJonas Devlieghere bool FileSystem::Readable(const Twine &path) const {
16046376966SJonas Devlieghere   return GetPermissions(path) & sys::fs::perms::all_read;
16146376966SJonas Devlieghere }
16246376966SJonas Devlieghere 
16346376966SJonas Devlieghere bool FileSystem::Readable(const FileSpec &file_spec) const {
16446376966SJonas Devlieghere   return Readable(file_spec.GetPath());
16546376966SJonas Devlieghere }
16646376966SJonas Devlieghere 
1673a58d898SJonas Devlieghere bool FileSystem::IsDirectory(const Twine &path) const {
1683a58d898SJonas Devlieghere   ErrorOr<vfs::Status> status = m_fs->status(path);
1693a58d898SJonas Devlieghere   if (!status)
1703a58d898SJonas Devlieghere     return false;
1713a58d898SJonas Devlieghere   return status->isDirectory();
1723a58d898SJonas Devlieghere }
1733a58d898SJonas Devlieghere 
1743a58d898SJonas Devlieghere bool FileSystem::IsDirectory(const FileSpec &file_spec) const {
1753a58d898SJonas Devlieghere   return IsDirectory(file_spec.GetPath());
1763a58d898SJonas Devlieghere }
1773a58d898SJonas Devlieghere 
17887e403aaSJonas Devlieghere bool FileSystem::IsLocal(const Twine &path) const {
17987e403aaSJonas Devlieghere   bool b = false;
18087e403aaSJonas Devlieghere   m_fs->isLocal(path, b);
18187e403aaSJonas Devlieghere   return b;
18287e403aaSJonas Devlieghere }
18387e403aaSJonas Devlieghere 
18487e403aaSJonas Devlieghere bool FileSystem::IsLocal(const FileSpec &file_spec) const {
18587e403aaSJonas Devlieghere   return IsLocal(file_spec.GetPath());
18687e403aaSJonas Devlieghere }
18787e403aaSJonas Devlieghere 
1889ca491daSJonas Devlieghere void FileSystem::EnumerateDirectory(Twine path, bool find_directories,
1899ca491daSJonas Devlieghere                                     bool find_files, bool find_other,
1909ca491daSJonas Devlieghere                                     EnumerateDirectoryCallbackType callback,
1919ca491daSJonas Devlieghere                                     void *callback_baton) {
1929ca491daSJonas Devlieghere   std::error_code EC;
1939ca491daSJonas Devlieghere   vfs::recursive_directory_iterator Iter(*m_fs, path, EC);
1949ca491daSJonas Devlieghere   vfs::recursive_directory_iterator End;
1959ca491daSJonas Devlieghere   for (; Iter != End && !EC; Iter.increment(EC)) {
1969ca491daSJonas Devlieghere     const auto &Item = *Iter;
1979ca491daSJonas Devlieghere     ErrorOr<vfs::Status> Status = m_fs->status(Item.path());
1989ca491daSJonas Devlieghere     if (!Status)
1999ca491daSJonas Devlieghere       break;
2009ca491daSJonas Devlieghere     if (!find_files && Status->isRegularFile())
2019ca491daSJonas Devlieghere       continue;
2029ca491daSJonas Devlieghere     if (!find_directories && Status->isDirectory())
2039ca491daSJonas Devlieghere       continue;
2049ca491daSJonas Devlieghere     if (!find_other && Status->isOther())
2059ca491daSJonas Devlieghere       continue;
2069ca491daSJonas Devlieghere 
2079ca491daSJonas Devlieghere     auto Result = callback(callback_baton, Status->getType(), Item.path());
2089ca491daSJonas Devlieghere     if (Result == eEnumerateDirectoryResultQuit)
2099ca491daSJonas Devlieghere       return;
2109ca491daSJonas Devlieghere     if (Result == eEnumerateDirectoryResultNext) {
2119ca491daSJonas Devlieghere       // Default behavior is to recurse. Opt out if the callback doesn't want
2129ca491daSJonas Devlieghere       // this behavior.
2139ca491daSJonas Devlieghere       Iter.no_push();
2149ca491daSJonas Devlieghere     }
2159ca491daSJonas Devlieghere   }
2169ca491daSJonas Devlieghere }
2179ca491daSJonas Devlieghere 
21846376966SJonas Devlieghere std::error_code FileSystem::MakeAbsolute(SmallVectorImpl<char> &path) const {
21946376966SJonas Devlieghere   return m_fs->makeAbsolute(path);
22046376966SJonas Devlieghere }
22146376966SJonas Devlieghere 
22246376966SJonas Devlieghere std::error_code FileSystem::MakeAbsolute(FileSpec &file_spec) const {
22346376966SJonas Devlieghere   SmallString<128> path;
22446376966SJonas Devlieghere   file_spec.GetPath(path, false);
22546376966SJonas Devlieghere 
22646376966SJonas Devlieghere   auto EC = MakeAbsolute(path);
22746376966SJonas Devlieghere   if (EC)
22846376966SJonas Devlieghere     return EC;
22946376966SJonas Devlieghere 
2308f3be7a3SJonas Devlieghere   FileSpec new_file_spec(path, file_spec.GetPathStyle());
23146376966SJonas Devlieghere   file_spec = new_file_spec;
23246376966SJonas Devlieghere   return {};
23346376966SJonas Devlieghere }
23446376966SJonas Devlieghere 
23546376966SJonas Devlieghere std::error_code FileSystem::GetRealPath(const Twine &path,
23672787ac6SJonas Devlieghere                                         SmallVectorImpl<char> &output) const {
23772787ac6SJonas Devlieghere   return m_fs->getRealPath(path, output);
23846376966SJonas Devlieghere }
23946376966SJonas Devlieghere 
24046376966SJonas Devlieghere void FileSystem::Resolve(SmallVectorImpl<char> &path) {
24146376966SJonas Devlieghere   if (path.empty())
24246376966SJonas Devlieghere     return;
24346376966SJonas Devlieghere 
244*feb99530SJonas Devlieghere   // Resolve tilde in path.
245*feb99530SJonas Devlieghere   SmallString<128> resolved(path.begin(), path.end());
24672787ac6SJonas Devlieghere   StandardTildeExpressionResolver Resolver;
247*feb99530SJonas Devlieghere   Resolver.ResolveFullPath(llvm::StringRef(path.begin(), path.size()),
248*feb99530SJonas Devlieghere                            resolved);
24946376966SJonas Devlieghere 
25046376966SJonas Devlieghere   // Try making the path absolute if it exists.
251*feb99530SJonas Devlieghere   SmallString<128> absolute(resolved.begin(), resolved.end());
252*feb99530SJonas Devlieghere   MakeAbsolute(absolute);
253*feb99530SJonas Devlieghere 
25446376966SJonas Devlieghere   path.clear();
255*feb99530SJonas Devlieghere   if (Exists(absolute)) {
256*feb99530SJonas Devlieghere     path.append(absolute.begin(), absolute.end());
257*feb99530SJonas Devlieghere   } else {
258*feb99530SJonas Devlieghere     path.append(resolved.begin(), resolved.end());
25946376966SJonas Devlieghere   }
26046376966SJonas Devlieghere }
26146376966SJonas Devlieghere 
26246376966SJonas Devlieghere void FileSystem::Resolve(FileSpec &file_spec) {
26346376966SJonas Devlieghere   // Extract path from the FileSpec.
26446376966SJonas Devlieghere   SmallString<128> path;
26546376966SJonas Devlieghere   file_spec.GetPath(path);
26646376966SJonas Devlieghere 
26746376966SJonas Devlieghere   // Resolve the path.
26846376966SJonas Devlieghere   Resolve(path);
26946376966SJonas Devlieghere 
27046376966SJonas Devlieghere   // Update the FileSpec with the resolved path.
27181d03f3aSAdrian Prantl   if (file_spec.GetFilename().IsEmpty())
27281d03f3aSAdrian Prantl     file_spec.GetDirectory().SetString(path);
27381d03f3aSAdrian Prantl   else
27446376966SJonas Devlieghere     file_spec.SetPath(path);
2758f3be7a3SJonas Devlieghere   file_spec.SetIsResolved(true);
2761408bf72SPavel Labath }
2772c22c800SJonas Devlieghere 
27887e403aaSJonas Devlieghere std::shared_ptr<DataBufferLLVM>
27987e403aaSJonas Devlieghere FileSystem::CreateDataBuffer(const llvm::Twine &path, uint64_t size,
28087e403aaSJonas Devlieghere                              uint64_t offset) {
28146575176SJonas Devlieghere   if (m_collector)
28246575176SJonas Devlieghere     m_collector->AddFile(path);
28346575176SJonas Devlieghere 
28487e403aaSJonas Devlieghere   const bool is_volatile = !IsLocal(path);
28546575176SJonas Devlieghere   const ErrorOr<std::string> external_path = GetExternalPath(path);
28646575176SJonas Devlieghere 
28746575176SJonas Devlieghere   if (!external_path)
28846575176SJonas Devlieghere     return nullptr;
28987e403aaSJonas Devlieghere 
29087e403aaSJonas Devlieghere   std::unique_ptr<llvm::WritableMemoryBuffer> buffer;
29187e403aaSJonas Devlieghere   if (size == 0) {
29287e403aaSJonas Devlieghere     auto buffer_or_error =
29346575176SJonas Devlieghere         llvm::WritableMemoryBuffer::getFile(*external_path, -1, is_volatile);
29487e403aaSJonas Devlieghere     if (!buffer_or_error)
29587e403aaSJonas Devlieghere       return nullptr;
29687e403aaSJonas Devlieghere     buffer = std::move(*buffer_or_error);
29787e403aaSJonas Devlieghere   } else {
29887e403aaSJonas Devlieghere     auto buffer_or_error = llvm::WritableMemoryBuffer::getFileSlice(
29946575176SJonas Devlieghere         *external_path, size, offset, is_volatile);
30087e403aaSJonas Devlieghere     if (!buffer_or_error)
30187e403aaSJonas Devlieghere       return nullptr;
30287e403aaSJonas Devlieghere     buffer = std::move(*buffer_or_error);
30387e403aaSJonas Devlieghere   }
30487e403aaSJonas Devlieghere   return std::shared_ptr<DataBufferLLVM>(new DataBufferLLVM(std::move(buffer)));
30587e403aaSJonas Devlieghere }
30687e403aaSJonas Devlieghere 
30787e403aaSJonas Devlieghere std::shared_ptr<DataBufferLLVM>
30887e403aaSJonas Devlieghere FileSystem::CreateDataBuffer(const FileSpec &file_spec, uint64_t size,
30987e403aaSJonas Devlieghere                              uint64_t offset) {
31087e403aaSJonas Devlieghere   return CreateDataBuffer(file_spec.GetPath(), size, offset);
31187e403aaSJonas Devlieghere }
31287e403aaSJonas Devlieghere 
3132c22c800SJonas Devlieghere bool FileSystem::ResolveExecutableLocation(FileSpec &file_spec) {
3142c22c800SJonas Devlieghere   // If the directory is set there's nothing to do.
3150e4c4821SAdrian Prantl   ConstString directory = file_spec.GetDirectory();
3162c22c800SJonas Devlieghere   if (directory)
3172c22c800SJonas Devlieghere     return false;
3182c22c800SJonas Devlieghere 
3192c22c800SJonas Devlieghere   // We cannot look for a file if there's no file name.
3200e4c4821SAdrian Prantl   ConstString filename = file_spec.GetFilename();
3212c22c800SJonas Devlieghere   if (!filename)
3222c22c800SJonas Devlieghere     return false;
3232c22c800SJonas Devlieghere 
3242c22c800SJonas Devlieghere   // Search for the file on the host.
3252c22c800SJonas Devlieghere   const std::string filename_str(filename.GetCString());
3262c22c800SJonas Devlieghere   llvm::ErrorOr<std::string> error_or_path =
3272c22c800SJonas Devlieghere       llvm::sys::findProgramByName(filename_str);
3282c22c800SJonas Devlieghere   if (!error_or_path)
3292c22c800SJonas Devlieghere     return false;
3302c22c800SJonas Devlieghere 
3312c22c800SJonas Devlieghere   // findProgramByName returns "." if it can't find the file.
3322c22c800SJonas Devlieghere   llvm::StringRef path = *error_or_path;
3332c22c800SJonas Devlieghere   llvm::StringRef parent = llvm::sys::path::parent_path(path);
3342c22c800SJonas Devlieghere   if (parent.empty() || parent == ".")
3352c22c800SJonas Devlieghere     return false;
3362c22c800SJonas Devlieghere 
3372c22c800SJonas Devlieghere   // Make sure that the result exists.
3388f3be7a3SJonas Devlieghere   FileSpec result(*error_or_path);
3392c22c800SJonas Devlieghere   if (!Exists(result))
3402c22c800SJonas Devlieghere     return false;
3412c22c800SJonas Devlieghere 
3422c22c800SJonas Devlieghere   file_spec = result;
3432c22c800SJonas Devlieghere   return true;
3442c22c800SJonas Devlieghere }
34550bc1ed2SJonas Devlieghere 
34650bc1ed2SJonas Devlieghere static int OpenWithFS(const FileSystem &fs, const char *path, int flags,
34750bc1ed2SJonas Devlieghere                       int mode) {
34850bc1ed2SJonas Devlieghere   return const_cast<FileSystem &>(fs).Open(path, flags, mode);
34950bc1ed2SJonas Devlieghere }
35050bc1ed2SJonas Devlieghere 
35150bc1ed2SJonas Devlieghere static int GetOpenFlags(uint32_t options) {
35250bc1ed2SJonas Devlieghere   const bool read = options & File::eOpenOptionRead;
35350bc1ed2SJonas Devlieghere   const bool write = options & File::eOpenOptionWrite;
35450bc1ed2SJonas Devlieghere 
35550bc1ed2SJonas Devlieghere   int open_flags = 0;
35650bc1ed2SJonas Devlieghere   if (write) {
35750bc1ed2SJonas Devlieghere     if (read)
35850bc1ed2SJonas Devlieghere       open_flags |= O_RDWR;
35950bc1ed2SJonas Devlieghere     else
36050bc1ed2SJonas Devlieghere       open_flags |= O_WRONLY;
36150bc1ed2SJonas Devlieghere 
36250bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionAppend)
36350bc1ed2SJonas Devlieghere       open_flags |= O_APPEND;
36450bc1ed2SJonas Devlieghere 
36550bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionTruncate)
36650bc1ed2SJonas Devlieghere       open_flags |= O_TRUNC;
36750bc1ed2SJonas Devlieghere 
36850bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionCanCreate)
36950bc1ed2SJonas Devlieghere       open_flags |= O_CREAT;
37050bc1ed2SJonas Devlieghere 
37150bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionCanCreateNewOnly)
37250bc1ed2SJonas Devlieghere       open_flags |= O_CREAT | O_EXCL;
37350bc1ed2SJonas Devlieghere   } else if (read) {
37450bc1ed2SJonas Devlieghere     open_flags |= O_RDONLY;
37550bc1ed2SJonas Devlieghere 
37650bc1ed2SJonas Devlieghere #ifndef _WIN32
37750bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionDontFollowSymlinks)
37850bc1ed2SJonas Devlieghere       open_flags |= O_NOFOLLOW;
37950bc1ed2SJonas Devlieghere #endif
38050bc1ed2SJonas Devlieghere   }
38150bc1ed2SJonas Devlieghere 
38250bc1ed2SJonas Devlieghere #ifndef _WIN32
38350bc1ed2SJonas Devlieghere   if (options & File::eOpenOptionNonBlocking)
38450bc1ed2SJonas Devlieghere     open_flags |= O_NONBLOCK;
38550bc1ed2SJonas Devlieghere   if (options & File::eOpenOptionCloseOnExec)
38650bc1ed2SJonas Devlieghere     open_flags |= O_CLOEXEC;
38750bc1ed2SJonas Devlieghere #else
38850bc1ed2SJonas Devlieghere   open_flags |= O_BINARY;
38950bc1ed2SJonas Devlieghere #endif
39050bc1ed2SJonas Devlieghere 
39150bc1ed2SJonas Devlieghere   return open_flags;
39250bc1ed2SJonas Devlieghere }
39350bc1ed2SJonas Devlieghere 
39450bc1ed2SJonas Devlieghere static mode_t GetOpenMode(uint32_t permissions) {
39550bc1ed2SJonas Devlieghere   mode_t mode = 0;
39650bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsUserRead)
39750bc1ed2SJonas Devlieghere     mode |= S_IRUSR;
39850bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsUserWrite)
39950bc1ed2SJonas Devlieghere     mode |= S_IWUSR;
40050bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsUserExecute)
40150bc1ed2SJonas Devlieghere     mode |= S_IXUSR;
40250bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsGroupRead)
40350bc1ed2SJonas Devlieghere     mode |= S_IRGRP;
40450bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsGroupWrite)
40550bc1ed2SJonas Devlieghere     mode |= S_IWGRP;
40650bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsGroupExecute)
40750bc1ed2SJonas Devlieghere     mode |= S_IXGRP;
40850bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsWorldRead)
40950bc1ed2SJonas Devlieghere     mode |= S_IROTH;
41050bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsWorldWrite)
41150bc1ed2SJonas Devlieghere     mode |= S_IWOTH;
41250bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsWorldExecute)
41350bc1ed2SJonas Devlieghere     mode |= S_IXOTH;
41450bc1ed2SJonas Devlieghere   return mode;
41550bc1ed2SJonas Devlieghere }
41650bc1ed2SJonas Devlieghere 
41750bc1ed2SJonas Devlieghere Status FileSystem::Open(File &File, const FileSpec &file_spec, uint32_t options,
4183a142495SAaron Smith                         uint32_t permissions, bool should_close_fd) {
41946575176SJonas Devlieghere   if (m_collector)
42046575176SJonas Devlieghere     m_collector->AddFile(file_spec);
42146575176SJonas Devlieghere 
42250bc1ed2SJonas Devlieghere   if (File.IsValid())
42350bc1ed2SJonas Devlieghere     File.Close();
42450bc1ed2SJonas Devlieghere 
42550bc1ed2SJonas Devlieghere   const int open_flags = GetOpenFlags(options);
42650bc1ed2SJonas Devlieghere   const mode_t open_mode =
42750bc1ed2SJonas Devlieghere       (open_flags & O_CREAT) ? GetOpenMode(permissions) : 0;
42846575176SJonas Devlieghere 
42946575176SJonas Devlieghere   auto path = GetExternalPath(file_spec);
43046575176SJonas Devlieghere   if (!path)
43146575176SJonas Devlieghere     return Status(path.getError());
43250bc1ed2SJonas Devlieghere 
43350bc1ed2SJonas Devlieghere   int descriptor = llvm::sys::RetryAfterSignal(
43446575176SJonas Devlieghere       -1, OpenWithFS, *this, path->c_str(), open_flags, open_mode);
43550bc1ed2SJonas Devlieghere 
43650bc1ed2SJonas Devlieghere   Status error;
43750bc1ed2SJonas Devlieghere   if (!File::DescriptorIsValid(descriptor)) {
43850bc1ed2SJonas Devlieghere     File.SetDescriptor(descriptor, false);
43950bc1ed2SJonas Devlieghere     error.SetErrorToErrno();
44050bc1ed2SJonas Devlieghere   } else {
4413a142495SAaron Smith     File.SetDescriptor(descriptor, should_close_fd);
44250bc1ed2SJonas Devlieghere     File.SetOptions(options);
44350bc1ed2SJonas Devlieghere   }
44450bc1ed2SJonas Devlieghere   return error;
44550bc1ed2SJonas Devlieghere }
44646575176SJonas Devlieghere 
44746575176SJonas Devlieghere ErrorOr<std::string> FileSystem::GetExternalPath(const llvm::Twine &path) {
44846575176SJonas Devlieghere   if (!m_mapped)
44946575176SJonas Devlieghere     return path.str();
45046575176SJonas Devlieghere 
45146575176SJonas Devlieghere   // If VFS mapped we know the underlying FS is a RedirectingFileSystem.
45246575176SJonas Devlieghere   ErrorOr<vfs::RedirectingFileSystem::Entry *> E =
45346575176SJonas Devlieghere       static_cast<vfs::RedirectingFileSystem &>(*m_fs).lookupPath(path);
45446575176SJonas Devlieghere   if (!E) {
45546575176SJonas Devlieghere     if (E.getError() == llvm::errc::no_such_file_or_directory) {
45646575176SJonas Devlieghere       return path.str();
45746575176SJonas Devlieghere     }
45846575176SJonas Devlieghere     return E.getError();
45946575176SJonas Devlieghere   }
46046575176SJonas Devlieghere 
46146575176SJonas Devlieghere   auto *F = dyn_cast<vfs::RedirectingFileSystem::RedirectingFileEntry>(*E);
46246575176SJonas Devlieghere   if (!F)
46346575176SJonas Devlieghere     return make_error_code(llvm::errc::not_supported);
46446575176SJonas Devlieghere 
46546575176SJonas Devlieghere   return F->getExternalContentsPath().str();
46646575176SJonas Devlieghere }
46746575176SJonas Devlieghere 
46846575176SJonas Devlieghere ErrorOr<std::string> FileSystem::GetExternalPath(const FileSpec &file_spec) {
46946575176SJonas Devlieghere   return GetExternalPath(file_spec.GetPath());
47046575176SJonas Devlieghere }
471