17a9e7621SOleksiy Vyalov //===-- FileSystem.cpp ------------------------------------------*- C++ -*-===//
27a9e7621SOleksiy Vyalov //
37a9e7621SOleksiy Vyalov //                     The LLVM Compiler Infrastructure
47a9e7621SOleksiy Vyalov //
57a9e7621SOleksiy Vyalov // This file is distributed under the University of Illinois Open Source
67a9e7621SOleksiy Vyalov // License. See LICENSE.TXT for details.
77a9e7621SOleksiy Vyalov //
87a9e7621SOleksiy Vyalov //===----------------------------------------------------------------------===//
97a9e7621SOleksiy Vyalov 
107a9e7621SOleksiy Vyalov #include "lldb/Host/FileSystem.h"
117a9e7621SOleksiy Vyalov 
120bca15a3SJonas Devlieghere #include "lldb/Utility/LLDBAssert.h"
1346376966SJonas Devlieghere #include "lldb/Utility/TildeExpressionResolver.h"
1446376966SJonas Devlieghere 
1550bc1ed2SJonas Devlieghere #include "llvm/Support/Errno.h"
161408bf72SPavel Labath #include "llvm/Support/FileSystem.h"
172c22c800SJonas Devlieghere #include "llvm/Support/Path.h"
182c22c800SJonas Devlieghere #include "llvm/Support/Program.h"
1946376966SJonas Devlieghere #include "llvm/Support/Threading.h"
207a9e7621SOleksiy Vyalov 
2150bc1ed2SJonas Devlieghere #include <errno.h>
2250bc1ed2SJonas Devlieghere #include <fcntl.h>
2350bc1ed2SJonas Devlieghere #include <limits.h>
2450bc1ed2SJonas Devlieghere #include <stdarg.h>
2550bc1ed2SJonas Devlieghere #include <stdio.h>
2650bc1ed2SJonas Devlieghere 
2750bc1ed2SJonas Devlieghere #ifdef _WIN32
2850bc1ed2SJonas Devlieghere #include "lldb/Host/windows/windows.h"
2950bc1ed2SJonas Devlieghere #else
3050bc1ed2SJonas Devlieghere #include <sys/ioctl.h>
3150bc1ed2SJonas Devlieghere #include <sys/stat.h>
3250bc1ed2SJonas Devlieghere #include <termios.h>
3350bc1ed2SJonas Devlieghere #include <unistd.h>
3450bc1ed2SJonas Devlieghere #endif
3550bc1ed2SJonas Devlieghere 
366801be33SOleksiy Vyalov #include <algorithm>
377a9e7621SOleksiy Vyalov #include <fstream>
387a9e7621SOleksiy Vyalov #include <vector>
397a9e7621SOleksiy Vyalov 
407a9e7621SOleksiy Vyalov using namespace lldb;
417a9e7621SOleksiy Vyalov using namespace lldb_private;
4246376966SJonas Devlieghere using namespace llvm;
437a9e7621SOleksiy Vyalov 
4446376966SJonas Devlieghere FileSystem &FileSystem::Instance() { return *InstanceImpl(); }
4546376966SJonas Devlieghere 
4646376966SJonas Devlieghere void FileSystem::Initialize() {
470bca15a3SJonas Devlieghere   lldbassert(!InstanceImpl() && "Already initialized.");
4846376966SJonas Devlieghere   InstanceImpl().emplace();
4946376966SJonas Devlieghere }
5046376966SJonas Devlieghere 
5146376966SJonas Devlieghere void FileSystem::Initialize(IntrusiveRefCntPtr<vfs::FileSystem> fs) {
520bca15a3SJonas Devlieghere   lldbassert(!InstanceImpl() && "Already initialized.");
5346376966SJonas Devlieghere   InstanceImpl().emplace(fs);
5446376966SJonas Devlieghere }
5546376966SJonas Devlieghere 
5646376966SJonas Devlieghere void FileSystem::Terminate() {
570bca15a3SJonas Devlieghere   lldbassert(InstanceImpl() && "Already terminated.");
5846376966SJonas Devlieghere   InstanceImpl().reset();
5946376966SJonas Devlieghere }
6046376966SJonas Devlieghere 
6146376966SJonas Devlieghere Optional<FileSystem> &FileSystem::InstanceImpl() {
6246376966SJonas Devlieghere   static Optional<FileSystem> g_fs;
6346376966SJonas Devlieghere   return g_fs;
6446376966SJonas Devlieghere }
6546376966SJonas Devlieghere 
66*edaf2bccSJonas Devlieghere vfs::directory_iterator FileSystem::DirBegin(const FileSpec &file_spec,
67*edaf2bccSJonas Devlieghere                                              std::error_code &ec) {
68*edaf2bccSJonas Devlieghere   return DirBegin(file_spec.GetPath(), ec);
69*edaf2bccSJonas Devlieghere }
70*edaf2bccSJonas Devlieghere 
71*edaf2bccSJonas Devlieghere vfs::directory_iterator FileSystem::DirBegin(const Twine &dir,
72*edaf2bccSJonas Devlieghere                                              std::error_code &ec) {
73*edaf2bccSJonas Devlieghere   return m_fs->dir_begin(dir, ec);
74*edaf2bccSJonas Devlieghere }
75*edaf2bccSJonas Devlieghere 
76*edaf2bccSJonas Devlieghere llvm::ErrorOr<vfs::Status>
77*edaf2bccSJonas Devlieghere FileSystem::GetStatus(const FileSpec &file_spec) const {
78*edaf2bccSJonas Devlieghere   return GetStatus(file_spec.GetPath());
79*edaf2bccSJonas Devlieghere }
80*edaf2bccSJonas Devlieghere 
81*edaf2bccSJonas Devlieghere llvm::ErrorOr<vfs::Status> FileSystem::GetStatus(const Twine &path) const {
82*edaf2bccSJonas Devlieghere   return m_fs->status(path);
83*edaf2bccSJonas Devlieghere }
84*edaf2bccSJonas Devlieghere 
8546376966SJonas Devlieghere sys::TimePoint<>
86010b56beSJonas Devlieghere FileSystem::GetModificationTime(const FileSpec &file_spec) const {
87010b56beSJonas Devlieghere   return GetModificationTime(file_spec.GetPath());
8846376966SJonas Devlieghere }
8946376966SJonas Devlieghere 
90010b56beSJonas Devlieghere sys::TimePoint<> FileSystem::GetModificationTime(const Twine &path) const {
9146376966SJonas Devlieghere   ErrorOr<vfs::Status> status = m_fs->status(path);
9246376966SJonas Devlieghere   if (!status)
9346376966SJonas Devlieghere     return sys::TimePoint<>();
9446376966SJonas Devlieghere   return status->getLastModificationTime();
9546376966SJonas Devlieghere }
9646376966SJonas Devlieghere 
9746376966SJonas Devlieghere uint64_t FileSystem::GetByteSize(const FileSpec &file_spec) const {
9846376966SJonas Devlieghere   return GetByteSize(file_spec.GetPath());
9946376966SJonas Devlieghere }
10046376966SJonas Devlieghere 
10146376966SJonas Devlieghere uint64_t FileSystem::GetByteSize(const Twine &path) const {
10246376966SJonas Devlieghere   ErrorOr<vfs::Status> status = m_fs->status(path);
10346376966SJonas Devlieghere   if (!status)
10446376966SJonas Devlieghere     return 0;
10546376966SJonas Devlieghere   return status->getSize();
10646376966SJonas Devlieghere }
10746376966SJonas Devlieghere 
10846376966SJonas Devlieghere uint32_t FileSystem::GetPermissions(const FileSpec &file_spec) const {
10946376966SJonas Devlieghere   return GetPermissions(file_spec.GetPath());
11046376966SJonas Devlieghere }
11146376966SJonas Devlieghere 
11273ed6071SJonas Devlieghere uint32_t FileSystem::GetPermissions(const FileSpec &file_spec,
11373ed6071SJonas Devlieghere                                     std::error_code &ec) const {
11473ed6071SJonas Devlieghere   return GetPermissions(file_spec.GetPath(), ec);
11573ed6071SJonas Devlieghere }
11673ed6071SJonas Devlieghere 
11746376966SJonas Devlieghere uint32_t FileSystem::GetPermissions(const Twine &path) const {
11873ed6071SJonas Devlieghere   std::error_code ec;
11973ed6071SJonas Devlieghere   return GetPermissions(path, ec);
12073ed6071SJonas Devlieghere }
12173ed6071SJonas Devlieghere 
12273ed6071SJonas Devlieghere uint32_t FileSystem::GetPermissions(const Twine &path,
12373ed6071SJonas Devlieghere                                     std::error_code &ec) const {
12446376966SJonas Devlieghere   ErrorOr<vfs::Status> status = m_fs->status(path);
12573ed6071SJonas Devlieghere   if (!status) {
12673ed6071SJonas Devlieghere     ec = status.getError();
12746376966SJonas Devlieghere     return sys::fs::perms::perms_not_known;
12873ed6071SJonas Devlieghere   }
12946376966SJonas Devlieghere   return status->getPermissions();
13046376966SJonas Devlieghere }
13146376966SJonas Devlieghere 
13246376966SJonas Devlieghere bool FileSystem::Exists(const Twine &path) const { return m_fs->exists(path); }
13346376966SJonas Devlieghere 
13446376966SJonas Devlieghere bool FileSystem::Exists(const FileSpec &file_spec) const {
13546376966SJonas Devlieghere   return Exists(file_spec.GetPath());
13646376966SJonas Devlieghere }
13746376966SJonas Devlieghere 
13846376966SJonas Devlieghere bool FileSystem::Readable(const Twine &path) const {
13946376966SJonas Devlieghere   return GetPermissions(path) & sys::fs::perms::all_read;
14046376966SJonas Devlieghere }
14146376966SJonas Devlieghere 
14246376966SJonas Devlieghere bool FileSystem::Readable(const FileSpec &file_spec) const {
14346376966SJonas Devlieghere   return Readable(file_spec.GetPath());
14446376966SJonas Devlieghere }
14546376966SJonas Devlieghere 
1463a58d898SJonas Devlieghere bool FileSystem::IsDirectory(const Twine &path) const {
1473a58d898SJonas Devlieghere   ErrorOr<vfs::Status> status = m_fs->status(path);
1483a58d898SJonas Devlieghere   if (!status)
1493a58d898SJonas Devlieghere     return false;
1503a58d898SJonas Devlieghere   return status->isDirectory();
1513a58d898SJonas Devlieghere }
1523a58d898SJonas Devlieghere 
1533a58d898SJonas Devlieghere bool FileSystem::IsDirectory(const FileSpec &file_spec) const {
1543a58d898SJonas Devlieghere   return IsDirectory(file_spec.GetPath());
1553a58d898SJonas Devlieghere }
1563a58d898SJonas Devlieghere 
15787e403aaSJonas Devlieghere bool FileSystem::IsLocal(const Twine &path) const {
15887e403aaSJonas Devlieghere   bool b = false;
15987e403aaSJonas Devlieghere   m_fs->isLocal(path, b);
16087e403aaSJonas Devlieghere   return b;
16187e403aaSJonas Devlieghere }
16287e403aaSJonas Devlieghere 
16387e403aaSJonas Devlieghere bool FileSystem::IsLocal(const FileSpec &file_spec) const {
16487e403aaSJonas Devlieghere   return IsLocal(file_spec.GetPath());
16587e403aaSJonas Devlieghere }
16687e403aaSJonas Devlieghere 
1679ca491daSJonas Devlieghere void FileSystem::EnumerateDirectory(Twine path, bool find_directories,
1689ca491daSJonas Devlieghere                                     bool find_files, bool find_other,
1699ca491daSJonas Devlieghere                                     EnumerateDirectoryCallbackType callback,
1709ca491daSJonas Devlieghere                                     void *callback_baton) {
1719ca491daSJonas Devlieghere   std::error_code EC;
1729ca491daSJonas Devlieghere   vfs::recursive_directory_iterator Iter(*m_fs, path, EC);
1739ca491daSJonas Devlieghere   vfs::recursive_directory_iterator End;
1749ca491daSJonas Devlieghere   for (; Iter != End && !EC; Iter.increment(EC)) {
1759ca491daSJonas Devlieghere     const auto &Item = *Iter;
1769ca491daSJonas Devlieghere     ErrorOr<vfs::Status> Status = m_fs->status(Item.path());
1779ca491daSJonas Devlieghere     if (!Status)
1789ca491daSJonas Devlieghere       break;
1799ca491daSJonas Devlieghere     if (!find_files && Status->isRegularFile())
1809ca491daSJonas Devlieghere       continue;
1819ca491daSJonas Devlieghere     if (!find_directories && Status->isDirectory())
1829ca491daSJonas Devlieghere       continue;
1839ca491daSJonas Devlieghere     if (!find_other && Status->isOther())
1849ca491daSJonas Devlieghere       continue;
1859ca491daSJonas Devlieghere 
1869ca491daSJonas Devlieghere     auto Result = callback(callback_baton, Status->getType(), Item.path());
1879ca491daSJonas Devlieghere     if (Result == eEnumerateDirectoryResultQuit)
1889ca491daSJonas Devlieghere       return;
1899ca491daSJonas Devlieghere     if (Result == eEnumerateDirectoryResultNext) {
1909ca491daSJonas Devlieghere       // Default behavior is to recurse. Opt out if the callback doesn't want
1919ca491daSJonas Devlieghere       // this behavior.
1929ca491daSJonas Devlieghere       Iter.no_push();
1939ca491daSJonas Devlieghere     }
1949ca491daSJonas Devlieghere   }
1959ca491daSJonas Devlieghere }
1969ca491daSJonas Devlieghere 
19746376966SJonas Devlieghere std::error_code FileSystem::MakeAbsolute(SmallVectorImpl<char> &path) const {
19846376966SJonas Devlieghere   return m_fs->makeAbsolute(path);
19946376966SJonas Devlieghere }
20046376966SJonas Devlieghere 
20146376966SJonas Devlieghere std::error_code FileSystem::MakeAbsolute(FileSpec &file_spec) const {
20246376966SJonas Devlieghere   SmallString<128> path;
20346376966SJonas Devlieghere   file_spec.GetPath(path, false);
20446376966SJonas Devlieghere 
20546376966SJonas Devlieghere   auto EC = MakeAbsolute(path);
20646376966SJonas Devlieghere   if (EC)
20746376966SJonas Devlieghere     return EC;
20846376966SJonas Devlieghere 
2098f3be7a3SJonas Devlieghere   FileSpec new_file_spec(path, file_spec.GetPathStyle());
21046376966SJonas Devlieghere   file_spec = new_file_spec;
21146376966SJonas Devlieghere   return {};
21246376966SJonas Devlieghere }
21346376966SJonas Devlieghere 
21446376966SJonas Devlieghere std::error_code FileSystem::GetRealPath(const Twine &path,
21572787ac6SJonas Devlieghere                                         SmallVectorImpl<char> &output) const {
21672787ac6SJonas Devlieghere   return m_fs->getRealPath(path, output);
21746376966SJonas Devlieghere }
21846376966SJonas Devlieghere 
21946376966SJonas Devlieghere void FileSystem::Resolve(SmallVectorImpl<char> &path) {
22046376966SJonas Devlieghere   if (path.empty())
22146376966SJonas Devlieghere     return;
22246376966SJonas Devlieghere 
22346376966SJonas Devlieghere   // Resolve tilde.
22446376966SJonas Devlieghere   SmallString<128> original_path(path.begin(), path.end());
22572787ac6SJonas Devlieghere   StandardTildeExpressionResolver Resolver;
22672787ac6SJonas Devlieghere   Resolver.ResolveFullPath(original_path, path);
22746376966SJonas Devlieghere 
22846376966SJonas Devlieghere   // Try making the path absolute if it exists.
22946376966SJonas Devlieghere   SmallString<128> absolute_path(path.begin(), path.end());
23046376966SJonas Devlieghere   MakeAbsolute(path);
23146376966SJonas Devlieghere   if (!Exists(path)) {
23246376966SJonas Devlieghere     path.clear();
23346376966SJonas Devlieghere     path.append(original_path.begin(), original_path.end());
23446376966SJonas Devlieghere   }
23546376966SJonas Devlieghere }
23646376966SJonas Devlieghere 
23746376966SJonas Devlieghere void FileSystem::Resolve(FileSpec &file_spec) {
23846376966SJonas Devlieghere   // Extract path from the FileSpec.
23946376966SJonas Devlieghere   SmallString<128> path;
24046376966SJonas Devlieghere   file_spec.GetPath(path);
24146376966SJonas Devlieghere 
24246376966SJonas Devlieghere   // Resolve the path.
24346376966SJonas Devlieghere   Resolve(path);
24446376966SJonas Devlieghere 
24546376966SJonas Devlieghere   // Update the FileSpec with the resolved path.
24646376966SJonas Devlieghere   file_spec.SetPath(path);
2478f3be7a3SJonas Devlieghere   file_spec.SetIsResolved(true);
2481408bf72SPavel Labath }
2492c22c800SJonas Devlieghere 
25087e403aaSJonas Devlieghere std::shared_ptr<DataBufferLLVM>
25187e403aaSJonas Devlieghere FileSystem::CreateDataBuffer(const llvm::Twine &path, uint64_t size,
25287e403aaSJonas Devlieghere                              uint64_t offset) {
25387e403aaSJonas Devlieghere   const bool is_volatile = !IsLocal(path);
25487e403aaSJonas Devlieghere 
25587e403aaSJonas Devlieghere   std::unique_ptr<llvm::WritableMemoryBuffer> buffer;
25687e403aaSJonas Devlieghere   if (size == 0) {
25787e403aaSJonas Devlieghere     auto buffer_or_error =
25887e403aaSJonas Devlieghere         llvm::WritableMemoryBuffer::getFile(path, -1, is_volatile);
25987e403aaSJonas Devlieghere     if (!buffer_or_error)
26087e403aaSJonas Devlieghere       return nullptr;
26187e403aaSJonas Devlieghere     buffer = std::move(*buffer_or_error);
26287e403aaSJonas Devlieghere   } else {
26387e403aaSJonas Devlieghere     auto buffer_or_error = llvm::WritableMemoryBuffer::getFileSlice(
26487e403aaSJonas Devlieghere         path, size, offset, is_volatile);
26587e403aaSJonas Devlieghere     if (!buffer_or_error)
26687e403aaSJonas Devlieghere       return nullptr;
26787e403aaSJonas Devlieghere     buffer = std::move(*buffer_or_error);
26887e403aaSJonas Devlieghere   }
26987e403aaSJonas Devlieghere   return std::shared_ptr<DataBufferLLVM>(new DataBufferLLVM(std::move(buffer)));
27087e403aaSJonas Devlieghere }
27187e403aaSJonas Devlieghere 
27287e403aaSJonas Devlieghere std::shared_ptr<DataBufferLLVM>
27387e403aaSJonas Devlieghere FileSystem::CreateDataBuffer(const FileSpec &file_spec, uint64_t size,
27487e403aaSJonas Devlieghere                              uint64_t offset) {
27587e403aaSJonas Devlieghere   return CreateDataBuffer(file_spec.GetPath(), size, offset);
27687e403aaSJonas Devlieghere }
27787e403aaSJonas Devlieghere 
2782c22c800SJonas Devlieghere bool FileSystem::ResolveExecutableLocation(FileSpec &file_spec) {
2792c22c800SJonas Devlieghere   // If the directory is set there's nothing to do.
2802c22c800SJonas Devlieghere   const ConstString &directory = file_spec.GetDirectory();
2812c22c800SJonas Devlieghere   if (directory)
2822c22c800SJonas Devlieghere     return false;
2832c22c800SJonas Devlieghere 
2842c22c800SJonas Devlieghere   // We cannot look for a file if there's no file name.
2852c22c800SJonas Devlieghere   const ConstString &filename = file_spec.GetFilename();
2862c22c800SJonas Devlieghere   if (!filename)
2872c22c800SJonas Devlieghere     return false;
2882c22c800SJonas Devlieghere 
2892c22c800SJonas Devlieghere   // Search for the file on the host.
2902c22c800SJonas Devlieghere   const std::string filename_str(filename.GetCString());
2912c22c800SJonas Devlieghere   llvm::ErrorOr<std::string> error_or_path =
2922c22c800SJonas Devlieghere       llvm::sys::findProgramByName(filename_str);
2932c22c800SJonas Devlieghere   if (!error_or_path)
2942c22c800SJonas Devlieghere     return false;
2952c22c800SJonas Devlieghere 
2962c22c800SJonas Devlieghere   // findProgramByName returns "." if it can't find the file.
2972c22c800SJonas Devlieghere   llvm::StringRef path = *error_or_path;
2982c22c800SJonas Devlieghere   llvm::StringRef parent = llvm::sys::path::parent_path(path);
2992c22c800SJonas Devlieghere   if (parent.empty() || parent == ".")
3002c22c800SJonas Devlieghere     return false;
3012c22c800SJonas Devlieghere 
3022c22c800SJonas Devlieghere   // Make sure that the result exists.
3038f3be7a3SJonas Devlieghere   FileSpec result(*error_or_path);
3042c22c800SJonas Devlieghere   if (!Exists(result))
3052c22c800SJonas Devlieghere     return false;
3062c22c800SJonas Devlieghere 
3072c22c800SJonas Devlieghere   file_spec = result;
3082c22c800SJonas Devlieghere   return true;
3092c22c800SJonas Devlieghere }
31050bc1ed2SJonas Devlieghere 
31150bc1ed2SJonas Devlieghere static int OpenWithFS(const FileSystem &fs, const char *path, int flags,
31250bc1ed2SJonas Devlieghere                       int mode) {
31350bc1ed2SJonas Devlieghere   return const_cast<FileSystem &>(fs).Open(path, flags, mode);
31450bc1ed2SJonas Devlieghere }
31550bc1ed2SJonas Devlieghere 
31650bc1ed2SJonas Devlieghere static int GetOpenFlags(uint32_t options) {
31750bc1ed2SJonas Devlieghere   const bool read = options & File::eOpenOptionRead;
31850bc1ed2SJonas Devlieghere   const bool write = options & File::eOpenOptionWrite;
31950bc1ed2SJonas Devlieghere 
32050bc1ed2SJonas Devlieghere   int open_flags = 0;
32150bc1ed2SJonas Devlieghere   if (write) {
32250bc1ed2SJonas Devlieghere     if (read)
32350bc1ed2SJonas Devlieghere       open_flags |= O_RDWR;
32450bc1ed2SJonas Devlieghere     else
32550bc1ed2SJonas Devlieghere       open_flags |= O_WRONLY;
32650bc1ed2SJonas Devlieghere 
32750bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionAppend)
32850bc1ed2SJonas Devlieghere       open_flags |= O_APPEND;
32950bc1ed2SJonas Devlieghere 
33050bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionTruncate)
33150bc1ed2SJonas Devlieghere       open_flags |= O_TRUNC;
33250bc1ed2SJonas Devlieghere 
33350bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionCanCreate)
33450bc1ed2SJonas Devlieghere       open_flags |= O_CREAT;
33550bc1ed2SJonas Devlieghere 
33650bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionCanCreateNewOnly)
33750bc1ed2SJonas Devlieghere       open_flags |= O_CREAT | O_EXCL;
33850bc1ed2SJonas Devlieghere   } else if (read) {
33950bc1ed2SJonas Devlieghere     open_flags |= O_RDONLY;
34050bc1ed2SJonas Devlieghere 
34150bc1ed2SJonas Devlieghere #ifndef _WIN32
34250bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionDontFollowSymlinks)
34350bc1ed2SJonas Devlieghere       open_flags |= O_NOFOLLOW;
34450bc1ed2SJonas Devlieghere #endif
34550bc1ed2SJonas Devlieghere   }
34650bc1ed2SJonas Devlieghere 
34750bc1ed2SJonas Devlieghere #ifndef _WIN32
34850bc1ed2SJonas Devlieghere   if (options & File::eOpenOptionNonBlocking)
34950bc1ed2SJonas Devlieghere     open_flags |= O_NONBLOCK;
35050bc1ed2SJonas Devlieghere   if (options & File::eOpenOptionCloseOnExec)
35150bc1ed2SJonas Devlieghere     open_flags |= O_CLOEXEC;
35250bc1ed2SJonas Devlieghere #else
35350bc1ed2SJonas Devlieghere   open_flags |= O_BINARY;
35450bc1ed2SJonas Devlieghere #endif
35550bc1ed2SJonas Devlieghere 
35650bc1ed2SJonas Devlieghere   return open_flags;
35750bc1ed2SJonas Devlieghere }
35850bc1ed2SJonas Devlieghere 
35950bc1ed2SJonas Devlieghere static mode_t GetOpenMode(uint32_t permissions) {
36050bc1ed2SJonas Devlieghere   mode_t mode = 0;
36150bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsUserRead)
36250bc1ed2SJonas Devlieghere     mode |= S_IRUSR;
36350bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsUserWrite)
36450bc1ed2SJonas Devlieghere     mode |= S_IWUSR;
36550bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsUserExecute)
36650bc1ed2SJonas Devlieghere     mode |= S_IXUSR;
36750bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsGroupRead)
36850bc1ed2SJonas Devlieghere     mode |= S_IRGRP;
36950bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsGroupWrite)
37050bc1ed2SJonas Devlieghere     mode |= S_IWGRP;
37150bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsGroupExecute)
37250bc1ed2SJonas Devlieghere     mode |= S_IXGRP;
37350bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsWorldRead)
37450bc1ed2SJonas Devlieghere     mode |= S_IROTH;
37550bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsWorldWrite)
37650bc1ed2SJonas Devlieghere     mode |= S_IWOTH;
37750bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsWorldExecute)
37850bc1ed2SJonas Devlieghere     mode |= S_IXOTH;
37950bc1ed2SJonas Devlieghere   return mode;
38050bc1ed2SJonas Devlieghere }
38150bc1ed2SJonas Devlieghere 
38250bc1ed2SJonas Devlieghere Status FileSystem::Open(File &File, const FileSpec &file_spec, uint32_t options,
38350bc1ed2SJonas Devlieghere                         uint32_t permissions) {
38450bc1ed2SJonas Devlieghere   if (File.IsValid())
38550bc1ed2SJonas Devlieghere     File.Close();
38650bc1ed2SJonas Devlieghere 
38750bc1ed2SJonas Devlieghere   const int open_flags = GetOpenFlags(options);
38850bc1ed2SJonas Devlieghere   const mode_t open_mode =
38950bc1ed2SJonas Devlieghere       (open_flags & O_CREAT) ? GetOpenMode(permissions) : 0;
39050bc1ed2SJonas Devlieghere   const std::string path = file_spec.GetPath();
39150bc1ed2SJonas Devlieghere 
39250bc1ed2SJonas Devlieghere   int descriptor = llvm::sys::RetryAfterSignal(
39350bc1ed2SJonas Devlieghere       -1, OpenWithFS, *this, path.c_str(), open_flags, open_mode);
39450bc1ed2SJonas Devlieghere 
39550bc1ed2SJonas Devlieghere   Status error;
39650bc1ed2SJonas Devlieghere   if (!File::DescriptorIsValid(descriptor)) {
39750bc1ed2SJonas Devlieghere     File.SetDescriptor(descriptor, false);
39850bc1ed2SJonas Devlieghere     error.SetErrorToErrno();
39950bc1ed2SJonas Devlieghere   } else {
40050bc1ed2SJonas Devlieghere     File.SetDescriptor(descriptor, true);
40150bc1ed2SJonas Devlieghere     File.SetOptions(options);
40250bc1ed2SJonas Devlieghere   }
40350bc1ed2SJonas Devlieghere   return error;
40450bc1ed2SJonas Devlieghere }
405