180814287SRaphael Isemann //===-- FileSystem.cpp ----------------------------------------------------===//
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 
11*2165c36bSJonas Devlieghere #include "lldb/Utility/DataBufferLLVM.h"
120bca15a3SJonas Devlieghere #include "lldb/Utility/LLDBAssert.h"
1346376966SJonas Devlieghere #include "lldb/Utility/TildeExpressionResolver.h"
1446376966SJonas Devlieghere 
1546575176SJonas Devlieghere #include "llvm/Support/Errc.h"
1650bc1ed2SJonas Devlieghere #include "llvm/Support/Errno.h"
1746575176SJonas Devlieghere #include "llvm/Support/Error.h"
181408bf72SPavel Labath #include "llvm/Support/FileSystem.h"
192c22c800SJonas Devlieghere #include "llvm/Support/Path.h"
202c22c800SJonas Devlieghere #include "llvm/Support/Program.h"
2146376966SJonas Devlieghere #include "llvm/Support/Threading.h"
227a9e7621SOleksiy Vyalov 
2376e47d48SRaphael Isemann #include <cerrno>
2476e47d48SRaphael Isemann #include <climits>
2576e47d48SRaphael Isemann #include <cstdarg>
2676e47d48SRaphael Isemann #include <cstdio>
2750bc1ed2SJonas Devlieghere #include <fcntl.h>
2850bc1ed2SJonas Devlieghere 
2950bc1ed2SJonas Devlieghere #ifdef _WIN32
3050bc1ed2SJonas Devlieghere #include "lldb/Host/windows/windows.h"
3150bc1ed2SJonas Devlieghere #else
3250bc1ed2SJonas Devlieghere #include <sys/ioctl.h>
3350bc1ed2SJonas Devlieghere #include <sys/stat.h>
3450bc1ed2SJonas Devlieghere #include <termios.h>
3550bc1ed2SJonas Devlieghere #include <unistd.h>
3650bc1ed2SJonas Devlieghere #endif
3750bc1ed2SJonas Devlieghere 
386801be33SOleksiy Vyalov #include <algorithm>
397a9e7621SOleksiy Vyalov #include <fstream>
407a9e7621SOleksiy Vyalov #include <vector>
417a9e7621SOleksiy Vyalov 
427a9e7621SOleksiy Vyalov using namespace lldb;
437a9e7621SOleksiy Vyalov using namespace lldb_private;
4446376966SJonas Devlieghere using namespace llvm;
457a9e7621SOleksiy Vyalov 
4646376966SJonas Devlieghere FileSystem &FileSystem::Instance() { return *InstanceImpl(); }
4746376966SJonas Devlieghere 
4846376966SJonas Devlieghere void FileSystem::Initialize() {
490bca15a3SJonas Devlieghere   lldbassert(!InstanceImpl() && "Already initialized.");
5046376966SJonas Devlieghere   InstanceImpl().emplace();
5146376966SJonas Devlieghere }
5246376966SJonas Devlieghere 
5346376966SJonas Devlieghere void FileSystem::Initialize(IntrusiveRefCntPtr<vfs::FileSystem> fs) {
540bca15a3SJonas Devlieghere   lldbassert(!InstanceImpl() && "Already initialized.");
5546376966SJonas Devlieghere   InstanceImpl().emplace(fs);
5646376966SJonas Devlieghere }
5746376966SJonas Devlieghere 
5846376966SJonas Devlieghere void FileSystem::Terminate() {
590bca15a3SJonas Devlieghere   lldbassert(InstanceImpl() && "Already terminated.");
6046376966SJonas Devlieghere   InstanceImpl().reset();
6146376966SJonas Devlieghere }
6246376966SJonas Devlieghere 
6346376966SJonas Devlieghere Optional<FileSystem> &FileSystem::InstanceImpl() {
6446376966SJonas Devlieghere   static Optional<FileSystem> g_fs;
6546376966SJonas Devlieghere   return g_fs;
6646376966SJonas Devlieghere }
6746376966SJonas Devlieghere 
68edaf2bccSJonas Devlieghere vfs::directory_iterator FileSystem::DirBegin(const FileSpec &file_spec,
69edaf2bccSJonas Devlieghere                                              std::error_code &ec) {
70d144087cSJonas Devlieghere   if (!file_spec) {
71d144087cSJonas Devlieghere     ec = std::error_code(static_cast<int>(errc::no_such_file_or_directory),
72d144087cSJonas Devlieghere                          std::system_category());
73d144087cSJonas Devlieghere     return {};
74d144087cSJonas Devlieghere   }
75edaf2bccSJonas Devlieghere   return DirBegin(file_spec.GetPath(), ec);
76edaf2bccSJonas Devlieghere }
77edaf2bccSJonas Devlieghere 
78edaf2bccSJonas Devlieghere vfs::directory_iterator FileSystem::DirBegin(const Twine &dir,
79edaf2bccSJonas Devlieghere                                              std::error_code &ec) {
80edaf2bccSJonas Devlieghere   return m_fs->dir_begin(dir, ec);
81edaf2bccSJonas Devlieghere }
82edaf2bccSJonas Devlieghere 
83edaf2bccSJonas Devlieghere llvm::ErrorOr<vfs::Status>
84edaf2bccSJonas Devlieghere FileSystem::GetStatus(const FileSpec &file_spec) const {
85d144087cSJonas Devlieghere   if (!file_spec)
86d144087cSJonas Devlieghere     return std::error_code(static_cast<int>(errc::no_such_file_or_directory),
87d144087cSJonas Devlieghere                            std::system_category());
88edaf2bccSJonas Devlieghere   return GetStatus(file_spec.GetPath());
89edaf2bccSJonas Devlieghere }
90edaf2bccSJonas Devlieghere 
91edaf2bccSJonas Devlieghere llvm::ErrorOr<vfs::Status> FileSystem::GetStatus(const Twine &path) const {
92edaf2bccSJonas Devlieghere   return m_fs->status(path);
93edaf2bccSJonas Devlieghere }
94edaf2bccSJonas Devlieghere 
9546376966SJonas Devlieghere sys::TimePoint<>
96010b56beSJonas Devlieghere FileSystem::GetModificationTime(const FileSpec &file_spec) const {
97d144087cSJonas Devlieghere   if (!file_spec)
98d144087cSJonas Devlieghere     return sys::TimePoint<>();
99010b56beSJonas Devlieghere   return GetModificationTime(file_spec.GetPath());
10046376966SJonas Devlieghere }
10146376966SJonas Devlieghere 
102010b56beSJonas Devlieghere sys::TimePoint<> FileSystem::GetModificationTime(const Twine &path) const {
10346376966SJonas Devlieghere   ErrorOr<vfs::Status> status = m_fs->status(path);
10446376966SJonas Devlieghere   if (!status)
10546376966SJonas Devlieghere     return sys::TimePoint<>();
10646376966SJonas Devlieghere   return status->getLastModificationTime();
10746376966SJonas Devlieghere }
10846376966SJonas Devlieghere 
10946376966SJonas Devlieghere uint64_t FileSystem::GetByteSize(const FileSpec &file_spec) const {
110d144087cSJonas Devlieghere   if (!file_spec)
111d144087cSJonas Devlieghere     return 0;
11246376966SJonas Devlieghere   return GetByteSize(file_spec.GetPath());
11346376966SJonas Devlieghere }
11446376966SJonas Devlieghere 
11546376966SJonas Devlieghere uint64_t FileSystem::GetByteSize(const Twine &path) const {
11646376966SJonas Devlieghere   ErrorOr<vfs::Status> status = m_fs->status(path);
11746376966SJonas Devlieghere   if (!status)
11846376966SJonas Devlieghere     return 0;
11946376966SJonas Devlieghere   return status->getSize();
12046376966SJonas Devlieghere }
12146376966SJonas Devlieghere 
12246376966SJonas Devlieghere uint32_t FileSystem::GetPermissions(const FileSpec &file_spec) const {
12346376966SJonas Devlieghere   return GetPermissions(file_spec.GetPath());
12446376966SJonas Devlieghere }
12546376966SJonas Devlieghere 
12673ed6071SJonas Devlieghere uint32_t FileSystem::GetPermissions(const FileSpec &file_spec,
12773ed6071SJonas Devlieghere                                     std::error_code &ec) const {
128d144087cSJonas Devlieghere   if (!file_spec)
129d144087cSJonas Devlieghere     return sys::fs::perms::perms_not_known;
13073ed6071SJonas Devlieghere   return GetPermissions(file_spec.GetPath(), ec);
13173ed6071SJonas Devlieghere }
13273ed6071SJonas Devlieghere 
13346376966SJonas Devlieghere uint32_t FileSystem::GetPermissions(const Twine &path) const {
13473ed6071SJonas Devlieghere   std::error_code ec;
13573ed6071SJonas Devlieghere   return GetPermissions(path, ec);
13673ed6071SJonas Devlieghere }
13773ed6071SJonas Devlieghere 
13873ed6071SJonas Devlieghere uint32_t FileSystem::GetPermissions(const Twine &path,
13973ed6071SJonas Devlieghere                                     std::error_code &ec) const {
14046376966SJonas Devlieghere   ErrorOr<vfs::Status> status = m_fs->status(path);
14173ed6071SJonas Devlieghere   if (!status) {
14273ed6071SJonas Devlieghere     ec = status.getError();
14346376966SJonas Devlieghere     return sys::fs::perms::perms_not_known;
14473ed6071SJonas Devlieghere   }
14546376966SJonas Devlieghere   return status->getPermissions();
14646376966SJonas Devlieghere }
14746376966SJonas Devlieghere 
14846376966SJonas Devlieghere bool FileSystem::Exists(const Twine &path) const { return m_fs->exists(path); }
14946376966SJonas Devlieghere 
15046376966SJonas Devlieghere bool FileSystem::Exists(const FileSpec &file_spec) const {
151d144087cSJonas Devlieghere   return file_spec && Exists(file_spec.GetPath());
15246376966SJonas Devlieghere }
15346376966SJonas Devlieghere 
15446376966SJonas Devlieghere bool FileSystem::Readable(const Twine &path) const {
15546376966SJonas Devlieghere   return GetPermissions(path) & sys::fs::perms::all_read;
15646376966SJonas Devlieghere }
15746376966SJonas Devlieghere 
15846376966SJonas Devlieghere bool FileSystem::Readable(const FileSpec &file_spec) const {
159d144087cSJonas Devlieghere   return file_spec && Readable(file_spec.GetPath());
16046376966SJonas Devlieghere }
16146376966SJonas Devlieghere 
1623a58d898SJonas Devlieghere bool FileSystem::IsDirectory(const Twine &path) const {
1633a58d898SJonas Devlieghere   ErrorOr<vfs::Status> status = m_fs->status(path);
1643a58d898SJonas Devlieghere   if (!status)
1653a58d898SJonas Devlieghere     return false;
1663a58d898SJonas Devlieghere   return status->isDirectory();
1673a58d898SJonas Devlieghere }
1683a58d898SJonas Devlieghere 
1693a58d898SJonas Devlieghere bool FileSystem::IsDirectory(const FileSpec &file_spec) const {
170d144087cSJonas Devlieghere   return file_spec && IsDirectory(file_spec.GetPath());
1713a58d898SJonas Devlieghere }
1723a58d898SJonas Devlieghere 
17387e403aaSJonas Devlieghere bool FileSystem::IsLocal(const Twine &path) const {
17487e403aaSJonas Devlieghere   bool b = false;
17587e403aaSJonas Devlieghere   m_fs->isLocal(path, b);
17687e403aaSJonas Devlieghere   return b;
17787e403aaSJonas Devlieghere }
17887e403aaSJonas Devlieghere 
17987e403aaSJonas Devlieghere bool FileSystem::IsLocal(const FileSpec &file_spec) const {
180d144087cSJonas Devlieghere   return file_spec && IsLocal(file_spec.GetPath());
18187e403aaSJonas Devlieghere }
18287e403aaSJonas Devlieghere 
1839ca491daSJonas Devlieghere void FileSystem::EnumerateDirectory(Twine path, bool find_directories,
1849ca491daSJonas Devlieghere                                     bool find_files, bool find_other,
1859ca491daSJonas Devlieghere                                     EnumerateDirectoryCallbackType callback,
1869ca491daSJonas Devlieghere                                     void *callback_baton) {
1879ca491daSJonas Devlieghere   std::error_code EC;
1889ca491daSJonas Devlieghere   vfs::recursive_directory_iterator Iter(*m_fs, path, EC);
1899ca491daSJonas Devlieghere   vfs::recursive_directory_iterator End;
1909ca491daSJonas Devlieghere   for (; Iter != End && !EC; Iter.increment(EC)) {
1919ca491daSJonas Devlieghere     const auto &Item = *Iter;
1929ca491daSJonas Devlieghere     ErrorOr<vfs::Status> Status = m_fs->status(Item.path());
1939ca491daSJonas Devlieghere     if (!Status)
1949ca491daSJonas Devlieghere       break;
1959ca491daSJonas Devlieghere     if (!find_files && Status->isRegularFile())
1969ca491daSJonas Devlieghere       continue;
1979ca491daSJonas Devlieghere     if (!find_directories && Status->isDirectory())
1989ca491daSJonas Devlieghere       continue;
1999ca491daSJonas Devlieghere     if (!find_other && Status->isOther())
2009ca491daSJonas Devlieghere       continue;
2019ca491daSJonas Devlieghere 
2029ca491daSJonas Devlieghere     auto Result = callback(callback_baton, Status->getType(), Item.path());
2039ca491daSJonas Devlieghere     if (Result == eEnumerateDirectoryResultQuit)
2049ca491daSJonas Devlieghere       return;
2059ca491daSJonas Devlieghere     if (Result == eEnumerateDirectoryResultNext) {
2069ca491daSJonas Devlieghere       // Default behavior is to recurse. Opt out if the callback doesn't want
2079ca491daSJonas Devlieghere       // this behavior.
2089ca491daSJonas Devlieghere       Iter.no_push();
2099ca491daSJonas Devlieghere     }
2109ca491daSJonas Devlieghere   }
2119ca491daSJonas Devlieghere }
2129ca491daSJonas Devlieghere 
21346376966SJonas Devlieghere std::error_code FileSystem::MakeAbsolute(SmallVectorImpl<char> &path) const {
21446376966SJonas Devlieghere   return m_fs->makeAbsolute(path);
21546376966SJonas Devlieghere }
21646376966SJonas Devlieghere 
21746376966SJonas Devlieghere std::error_code FileSystem::MakeAbsolute(FileSpec &file_spec) const {
21846376966SJonas Devlieghere   SmallString<128> path;
21946376966SJonas Devlieghere   file_spec.GetPath(path, false);
22046376966SJonas Devlieghere 
22146376966SJonas Devlieghere   auto EC = MakeAbsolute(path);
22246376966SJonas Devlieghere   if (EC)
22346376966SJonas Devlieghere     return EC;
22446376966SJonas Devlieghere 
2258f3be7a3SJonas Devlieghere   FileSpec new_file_spec(path, file_spec.GetPathStyle());
22646376966SJonas Devlieghere   file_spec = new_file_spec;
22746376966SJonas Devlieghere   return {};
22846376966SJonas Devlieghere }
22946376966SJonas Devlieghere 
23046376966SJonas Devlieghere std::error_code FileSystem::GetRealPath(const Twine &path,
23172787ac6SJonas Devlieghere                                         SmallVectorImpl<char> &output) const {
23272787ac6SJonas Devlieghere   return m_fs->getRealPath(path, output);
23346376966SJonas Devlieghere }
23446376966SJonas Devlieghere 
23546376966SJonas Devlieghere void FileSystem::Resolve(SmallVectorImpl<char> &path) {
23646376966SJonas Devlieghere   if (path.empty())
23746376966SJonas Devlieghere     return;
23846376966SJonas Devlieghere 
239feb99530SJonas Devlieghere   // Resolve tilde in path.
240feb99530SJonas Devlieghere   SmallString<128> resolved(path.begin(), path.end());
24172787ac6SJonas Devlieghere   StandardTildeExpressionResolver Resolver;
242feb99530SJonas Devlieghere   Resolver.ResolveFullPath(llvm::StringRef(path.begin(), path.size()),
243feb99530SJonas Devlieghere                            resolved);
24446376966SJonas Devlieghere 
24546376966SJonas Devlieghere   // Try making the path absolute if it exists.
246feb99530SJonas Devlieghere   SmallString<128> absolute(resolved.begin(), resolved.end());
247feb99530SJonas Devlieghere   MakeAbsolute(absolute);
248feb99530SJonas Devlieghere 
24946376966SJonas Devlieghere   path.clear();
250feb99530SJonas Devlieghere   if (Exists(absolute)) {
251feb99530SJonas Devlieghere     path.append(absolute.begin(), absolute.end());
252feb99530SJonas Devlieghere   } else {
253feb99530SJonas Devlieghere     path.append(resolved.begin(), resolved.end());
25446376966SJonas Devlieghere   }
25546376966SJonas Devlieghere }
25646376966SJonas Devlieghere 
25746376966SJonas Devlieghere void FileSystem::Resolve(FileSpec &file_spec) {
258d144087cSJonas Devlieghere   if (!file_spec)
259d144087cSJonas Devlieghere     return;
260d144087cSJonas Devlieghere 
26146376966SJonas Devlieghere   // Extract path from the FileSpec.
26246376966SJonas Devlieghere   SmallString<128> path;
26346376966SJonas Devlieghere   file_spec.GetPath(path);
26446376966SJonas Devlieghere 
26546376966SJonas Devlieghere   // Resolve the path.
26646376966SJonas Devlieghere   Resolve(path);
26746376966SJonas Devlieghere 
26846376966SJonas Devlieghere   // Update the FileSpec with the resolved path.
26981d03f3aSAdrian Prantl   if (file_spec.GetFilename().IsEmpty())
27081d03f3aSAdrian Prantl     file_spec.GetDirectory().SetString(path);
27181d03f3aSAdrian Prantl   else
27246376966SJonas Devlieghere     file_spec.SetPath(path);
2738f3be7a3SJonas Devlieghere   file_spec.SetIsResolved(true);
2741408bf72SPavel Labath }
2752c22c800SJonas Devlieghere 
276*2165c36bSJonas Devlieghere std::shared_ptr<DataBuffer>
27787e403aaSJonas Devlieghere FileSystem::CreateDataBuffer(const llvm::Twine &path, uint64_t size,
27887e403aaSJonas Devlieghere                              uint64_t offset) {
27987e403aaSJonas Devlieghere   const bool is_volatile = !IsLocal(path);
28087e403aaSJonas Devlieghere   std::unique_ptr<llvm::WritableMemoryBuffer> buffer;
28187e403aaSJonas Devlieghere   if (size == 0) {
28287e403aaSJonas Devlieghere     auto buffer_or_error =
28359eb7052SJonas Devlieghere         llvm::WritableMemoryBuffer::getFile(path, is_volatile);
28487e403aaSJonas Devlieghere     if (!buffer_or_error)
28587e403aaSJonas Devlieghere       return nullptr;
28687e403aaSJonas Devlieghere     buffer = std::move(*buffer_or_error);
28787e403aaSJonas Devlieghere   } else {
28887e403aaSJonas Devlieghere     auto buffer_or_error = llvm::WritableMemoryBuffer::getFileSlice(
28959eb7052SJonas Devlieghere         path, size, offset, is_volatile);
29087e403aaSJonas Devlieghere     if (!buffer_or_error)
29187e403aaSJonas Devlieghere       return nullptr;
29287e403aaSJonas Devlieghere     buffer = std::move(*buffer_or_error);
29387e403aaSJonas Devlieghere   }
29487e403aaSJonas Devlieghere   return std::shared_ptr<DataBufferLLVM>(new DataBufferLLVM(std::move(buffer)));
29587e403aaSJonas Devlieghere }
29687e403aaSJonas Devlieghere 
297*2165c36bSJonas Devlieghere std::shared_ptr<DataBuffer>
29887e403aaSJonas Devlieghere FileSystem::CreateDataBuffer(const FileSpec &file_spec, uint64_t size,
29987e403aaSJonas Devlieghere                              uint64_t offset) {
30087e403aaSJonas Devlieghere   return CreateDataBuffer(file_spec.GetPath(), size, offset);
30187e403aaSJonas Devlieghere }
30287e403aaSJonas Devlieghere 
3032c22c800SJonas Devlieghere bool FileSystem::ResolveExecutableLocation(FileSpec &file_spec) {
3042c22c800SJonas Devlieghere   // If the directory is set there's nothing to do.
3050e4c4821SAdrian Prantl   ConstString directory = file_spec.GetDirectory();
3062c22c800SJonas Devlieghere   if (directory)
3072c22c800SJonas Devlieghere     return false;
3082c22c800SJonas Devlieghere 
3092c22c800SJonas Devlieghere   // We cannot look for a file if there's no file name.
3100e4c4821SAdrian Prantl   ConstString filename = file_spec.GetFilename();
3112c22c800SJonas Devlieghere   if (!filename)
3122c22c800SJonas Devlieghere     return false;
3132c22c800SJonas Devlieghere 
3142c22c800SJonas Devlieghere   // Search for the file on the host.
3152c22c800SJonas Devlieghere   const std::string filename_str(filename.GetCString());
3162c22c800SJonas Devlieghere   llvm::ErrorOr<std::string> error_or_path =
3172c22c800SJonas Devlieghere       llvm::sys::findProgramByName(filename_str);
3182c22c800SJonas Devlieghere   if (!error_or_path)
3192c22c800SJonas Devlieghere     return false;
3202c22c800SJonas Devlieghere 
3212c22c800SJonas Devlieghere   // findProgramByName returns "." if it can't find the file.
3222c22c800SJonas Devlieghere   llvm::StringRef path = *error_or_path;
3232c22c800SJonas Devlieghere   llvm::StringRef parent = llvm::sys::path::parent_path(path);
3242c22c800SJonas Devlieghere   if (parent.empty() || parent == ".")
3252c22c800SJonas Devlieghere     return false;
3262c22c800SJonas Devlieghere 
3272c22c800SJonas Devlieghere   // Make sure that the result exists.
3288f3be7a3SJonas Devlieghere   FileSpec result(*error_or_path);
3292c22c800SJonas Devlieghere   if (!Exists(result))
3302c22c800SJonas Devlieghere     return false;
3312c22c800SJonas Devlieghere 
3322c22c800SJonas Devlieghere   file_spec = result;
3332c22c800SJonas Devlieghere   return true;
3342c22c800SJonas Devlieghere }
33550bc1ed2SJonas Devlieghere 
336921c1b7dSJonas Devlieghere bool FileSystem::GetHomeDirectory(SmallVectorImpl<char> &path) const {
33773af341bSJonas Devlieghere   if (!m_home_directory.empty()) {
33873af341bSJonas Devlieghere     path.assign(m_home_directory.begin(), m_home_directory.end());
33973af341bSJonas Devlieghere     return true;
34073af341bSJonas Devlieghere   }
341921c1b7dSJonas Devlieghere   return llvm::sys::path::home_directory(path);
342921c1b7dSJonas Devlieghere }
343921c1b7dSJonas Devlieghere 
344921c1b7dSJonas Devlieghere bool FileSystem::GetHomeDirectory(FileSpec &file_spec) const {
345921c1b7dSJonas Devlieghere   SmallString<128> home_dir;
346921c1b7dSJonas Devlieghere   if (!GetHomeDirectory(home_dir))
347921c1b7dSJonas Devlieghere     return false;
348921c1b7dSJonas Devlieghere   file_spec.SetPath(home_dir);
349921c1b7dSJonas Devlieghere   return true;
350921c1b7dSJonas Devlieghere }
351921c1b7dSJonas Devlieghere 
35250bc1ed2SJonas Devlieghere static int OpenWithFS(const FileSystem &fs, const char *path, int flags,
35350bc1ed2SJonas Devlieghere                       int mode) {
35450bc1ed2SJonas Devlieghere   return const_cast<FileSystem &>(fs).Open(path, flags, mode);
35550bc1ed2SJonas Devlieghere }
35650bc1ed2SJonas Devlieghere 
35714735cabSMichał Górny static int GetOpenFlags(File::OpenOptions options) {
35850bc1ed2SJonas Devlieghere   int open_flags = 0;
35914735cabSMichał Górny   File::OpenOptions rw =
36014735cabSMichał Górny       options & (File::eOpenOptionReadOnly | File::eOpenOptionWriteOnly |
36114735cabSMichał Górny                  File::eOpenOptionReadWrite);
36214735cabSMichał Górny   if (rw == File::eOpenOptionWriteOnly || rw == File::eOpenOptionReadWrite) {
36314735cabSMichał Górny     if (rw == File::eOpenOptionReadWrite)
36450bc1ed2SJonas Devlieghere       open_flags |= O_RDWR;
36550bc1ed2SJonas Devlieghere     else
36650bc1ed2SJonas Devlieghere       open_flags |= O_WRONLY;
36750bc1ed2SJonas Devlieghere 
36850bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionAppend)
36950bc1ed2SJonas Devlieghere       open_flags |= O_APPEND;
37050bc1ed2SJonas Devlieghere 
37150bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionTruncate)
37250bc1ed2SJonas Devlieghere       open_flags |= O_TRUNC;
37350bc1ed2SJonas Devlieghere 
37450bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionCanCreate)
37550bc1ed2SJonas Devlieghere       open_flags |= O_CREAT;
37650bc1ed2SJonas Devlieghere 
37750bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionCanCreateNewOnly)
37850bc1ed2SJonas Devlieghere       open_flags |= O_CREAT | O_EXCL;
37914735cabSMichał Górny   } else if (rw == File::eOpenOptionReadOnly) {
38050bc1ed2SJonas Devlieghere     open_flags |= O_RDONLY;
38150bc1ed2SJonas Devlieghere 
38250bc1ed2SJonas Devlieghere #ifndef _WIN32
38350bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionDontFollowSymlinks)
38450bc1ed2SJonas Devlieghere       open_flags |= O_NOFOLLOW;
38550bc1ed2SJonas Devlieghere #endif
38650bc1ed2SJonas Devlieghere   }
38750bc1ed2SJonas Devlieghere 
38850bc1ed2SJonas Devlieghere #ifndef _WIN32
38950bc1ed2SJonas Devlieghere   if (options & File::eOpenOptionNonBlocking)
39050bc1ed2SJonas Devlieghere     open_flags |= O_NONBLOCK;
39150bc1ed2SJonas Devlieghere   if (options & File::eOpenOptionCloseOnExec)
39250bc1ed2SJonas Devlieghere     open_flags |= O_CLOEXEC;
39350bc1ed2SJonas Devlieghere #else
39450bc1ed2SJonas Devlieghere   open_flags |= O_BINARY;
39550bc1ed2SJonas Devlieghere #endif
39650bc1ed2SJonas Devlieghere 
39750bc1ed2SJonas Devlieghere   return open_flags;
39850bc1ed2SJonas Devlieghere }
39950bc1ed2SJonas Devlieghere 
40050bc1ed2SJonas Devlieghere static mode_t GetOpenMode(uint32_t permissions) {
40150bc1ed2SJonas Devlieghere   mode_t mode = 0;
40250bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsUserRead)
40350bc1ed2SJonas Devlieghere     mode |= S_IRUSR;
40450bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsUserWrite)
40550bc1ed2SJonas Devlieghere     mode |= S_IWUSR;
40650bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsUserExecute)
40750bc1ed2SJonas Devlieghere     mode |= S_IXUSR;
40850bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsGroupRead)
40950bc1ed2SJonas Devlieghere     mode |= S_IRGRP;
41050bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsGroupWrite)
41150bc1ed2SJonas Devlieghere     mode |= S_IWGRP;
41250bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsGroupExecute)
41350bc1ed2SJonas Devlieghere     mode |= S_IXGRP;
41450bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsWorldRead)
41550bc1ed2SJonas Devlieghere     mode |= S_IROTH;
41650bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsWorldWrite)
41750bc1ed2SJonas Devlieghere     mode |= S_IWOTH;
41850bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsWorldExecute)
41950bc1ed2SJonas Devlieghere     mode |= S_IXOTH;
42050bc1ed2SJonas Devlieghere   return mode;
42150bc1ed2SJonas Devlieghere }
42250bc1ed2SJonas Devlieghere 
42362c9fe42SLawrence D'Anna Expected<FileUP> FileSystem::Open(const FileSpec &file_spec,
42462c9fe42SLawrence D'Anna                                   File::OpenOptions options,
4253a142495SAaron Smith                                   uint32_t permissions, bool should_close_fd) {
42650bc1ed2SJonas Devlieghere   const int open_flags = GetOpenFlags(options);
42750bc1ed2SJonas Devlieghere   const mode_t open_mode =
42850bc1ed2SJonas Devlieghere       (open_flags & O_CREAT) ? GetOpenMode(permissions) : 0;
42946575176SJonas Devlieghere 
43059eb7052SJonas Devlieghere   auto path = file_spec.GetPath();
43150bc1ed2SJonas Devlieghere 
43250bc1ed2SJonas Devlieghere   int descriptor = llvm::sys::RetryAfterSignal(
43359eb7052SJonas Devlieghere       -1, OpenWithFS, *this, path.c_str(), open_flags, open_mode);
43450bc1ed2SJonas Devlieghere 
4352fce1137SLawrence D'Anna   if (!File::DescriptorIsValid(descriptor))
4362fce1137SLawrence D'Anna     return llvm::errorCodeToError(
4372fce1137SLawrence D'Anna         std::error_code(errno, std::system_category()));
4382fce1137SLawrence D'Anna 
439f913fd6eSLawrence D'Anna   auto file = std::unique_ptr<File>(
440f913fd6eSLawrence D'Anna       new NativeFile(descriptor, options, should_close_fd));
4412fce1137SLawrence D'Anna   assert(file->IsValid());
4422fce1137SLawrence D'Anna   return std::move(file);
44350bc1ed2SJonas Devlieghere }
44446575176SJonas Devlieghere 
44573af341bSJonas Devlieghere void FileSystem::SetHomeDirectory(std::string home_directory) {
44673af341bSJonas Devlieghere   m_home_directory = std::move(home_directory);
44773af341bSJonas Devlieghere }
448da816ca0SGreg Clayton 
449da816ca0SGreg Clayton Status FileSystem::RemoveFile(const FileSpec &file_spec) {
450da816ca0SGreg Clayton   return RemoveFile(file_spec.GetPath());
451da816ca0SGreg Clayton }
452da816ca0SGreg Clayton 
453da816ca0SGreg Clayton Status FileSystem::RemoveFile(const llvm::Twine &path) {
454da816ca0SGreg Clayton   return Status(llvm::sys::fs::remove(path));
455da816ca0SGreg Clayton }
456