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 
6646376966SJonas Devlieghere sys::TimePoint<>
6746376966SJonas Devlieghere FileSystem::GetModificationTime(const FileSpec &file_spec) const {
6846376966SJonas Devlieghere   return GetModificationTime(file_spec.GetPath());
6946376966SJonas Devlieghere }
7046376966SJonas Devlieghere 
7146376966SJonas Devlieghere sys::TimePoint<> FileSystem::GetModificationTime(const Twine &path) const {
7246376966SJonas Devlieghere   ErrorOr<vfs::Status> status = m_fs->status(path);
7346376966SJonas Devlieghere   if (!status)
7446376966SJonas Devlieghere     return sys::TimePoint<>();
7546376966SJonas Devlieghere   return status->getLastModificationTime();
7646376966SJonas Devlieghere }
7746376966SJonas Devlieghere 
7846376966SJonas Devlieghere uint64_t FileSystem::GetByteSize(const FileSpec &file_spec) const {
7946376966SJonas Devlieghere   return GetByteSize(file_spec.GetPath());
8046376966SJonas Devlieghere }
8146376966SJonas Devlieghere 
8246376966SJonas Devlieghere uint64_t FileSystem::GetByteSize(const Twine &path) const {
8346376966SJonas Devlieghere   ErrorOr<vfs::Status> status = m_fs->status(path);
8446376966SJonas Devlieghere   if (!status)
8546376966SJonas Devlieghere     return 0;
8646376966SJonas Devlieghere   return status->getSize();
8746376966SJonas Devlieghere }
8846376966SJonas Devlieghere 
8946376966SJonas Devlieghere uint32_t FileSystem::GetPermissions(const FileSpec &file_spec) const {
9046376966SJonas Devlieghere   return GetPermissions(file_spec.GetPath());
9146376966SJonas Devlieghere }
9246376966SJonas Devlieghere 
9373ed6071SJonas Devlieghere uint32_t FileSystem::GetPermissions(const FileSpec &file_spec,
9473ed6071SJonas Devlieghere                                     std::error_code &ec) const {
9573ed6071SJonas Devlieghere   return GetPermissions(file_spec.GetPath(), ec);
9673ed6071SJonas Devlieghere }
9773ed6071SJonas Devlieghere 
9846376966SJonas Devlieghere uint32_t FileSystem::GetPermissions(const Twine &path) const {
9973ed6071SJonas Devlieghere   std::error_code ec;
10073ed6071SJonas Devlieghere   return GetPermissions(path, ec);
10173ed6071SJonas Devlieghere }
10273ed6071SJonas Devlieghere 
10373ed6071SJonas Devlieghere uint32_t FileSystem::GetPermissions(const Twine &path,
10473ed6071SJonas Devlieghere                                     std::error_code &ec) const {
10546376966SJonas Devlieghere   ErrorOr<vfs::Status> status = m_fs->status(path);
10673ed6071SJonas Devlieghere   if (!status) {
10773ed6071SJonas Devlieghere     ec = status.getError();
10846376966SJonas Devlieghere     return sys::fs::perms::perms_not_known;
10973ed6071SJonas Devlieghere   }
11046376966SJonas Devlieghere   return status->getPermissions();
11146376966SJonas Devlieghere }
11246376966SJonas Devlieghere 
11346376966SJonas Devlieghere bool FileSystem::Exists(const Twine &path) const { return m_fs->exists(path); }
11446376966SJonas Devlieghere 
11546376966SJonas Devlieghere bool FileSystem::Exists(const FileSpec &file_spec) const {
11646376966SJonas Devlieghere   return Exists(file_spec.GetPath());
11746376966SJonas Devlieghere }
11846376966SJonas Devlieghere 
11946376966SJonas Devlieghere bool FileSystem::Readable(const Twine &path) const {
12046376966SJonas Devlieghere   return GetPermissions(path) & sys::fs::perms::all_read;
12146376966SJonas Devlieghere }
12246376966SJonas Devlieghere 
12346376966SJonas Devlieghere bool FileSystem::Readable(const FileSpec &file_spec) const {
12446376966SJonas Devlieghere   return Readable(file_spec.GetPath());
12546376966SJonas Devlieghere }
12646376966SJonas Devlieghere 
127*3a58d898SJonas Devlieghere bool FileSystem::IsDirectory(const Twine &path) const {
128*3a58d898SJonas Devlieghere   ErrorOr<vfs::Status> status = m_fs->status(path);
129*3a58d898SJonas Devlieghere   if (!status)
130*3a58d898SJonas Devlieghere     return false;
131*3a58d898SJonas Devlieghere   return status->isDirectory();
132*3a58d898SJonas Devlieghere }
133*3a58d898SJonas Devlieghere 
134*3a58d898SJonas Devlieghere bool FileSystem::IsDirectory(const FileSpec &file_spec) const {
135*3a58d898SJonas Devlieghere   return IsDirectory(file_spec.GetPath());
136*3a58d898SJonas Devlieghere }
137*3a58d898SJonas Devlieghere 
1389ca491daSJonas Devlieghere void FileSystem::EnumerateDirectory(Twine path, bool find_directories,
1399ca491daSJonas Devlieghere                                     bool find_files, bool find_other,
1409ca491daSJonas Devlieghere                                     EnumerateDirectoryCallbackType callback,
1419ca491daSJonas Devlieghere                                     void *callback_baton) {
1429ca491daSJonas Devlieghere   std::error_code EC;
1439ca491daSJonas Devlieghere   vfs::recursive_directory_iterator Iter(*m_fs, path, EC);
1449ca491daSJonas Devlieghere   vfs::recursive_directory_iterator End;
1459ca491daSJonas Devlieghere   for (; Iter != End && !EC; Iter.increment(EC)) {
1469ca491daSJonas Devlieghere     const auto &Item = *Iter;
1479ca491daSJonas Devlieghere     ErrorOr<vfs::Status> Status = m_fs->status(Item.path());
1489ca491daSJonas Devlieghere     if (!Status)
1499ca491daSJonas Devlieghere       break;
1509ca491daSJonas Devlieghere     if (!find_files && Status->isRegularFile())
1519ca491daSJonas Devlieghere       continue;
1529ca491daSJonas Devlieghere     if (!find_directories && Status->isDirectory())
1539ca491daSJonas Devlieghere       continue;
1549ca491daSJonas Devlieghere     if (!find_other && Status->isOther())
1559ca491daSJonas Devlieghere       continue;
1569ca491daSJonas Devlieghere 
1579ca491daSJonas Devlieghere     auto Result = callback(callback_baton, Status->getType(), Item.path());
1589ca491daSJonas Devlieghere     if (Result == eEnumerateDirectoryResultQuit)
1599ca491daSJonas Devlieghere       return;
1609ca491daSJonas Devlieghere     if (Result == eEnumerateDirectoryResultNext) {
1619ca491daSJonas Devlieghere       // Default behavior is to recurse. Opt out if the callback doesn't want
1629ca491daSJonas Devlieghere       // this behavior.
1639ca491daSJonas Devlieghere       Iter.no_push();
1649ca491daSJonas Devlieghere     }
1659ca491daSJonas Devlieghere   }
1669ca491daSJonas Devlieghere }
1679ca491daSJonas Devlieghere 
16846376966SJonas Devlieghere std::error_code FileSystem::MakeAbsolute(SmallVectorImpl<char> &path) const {
16946376966SJonas Devlieghere   return m_fs->makeAbsolute(path);
17046376966SJonas Devlieghere }
17146376966SJonas Devlieghere 
17246376966SJonas Devlieghere std::error_code FileSystem::MakeAbsolute(FileSpec &file_spec) const {
17346376966SJonas Devlieghere   SmallString<128> path;
17446376966SJonas Devlieghere   file_spec.GetPath(path, false);
17546376966SJonas Devlieghere 
17646376966SJonas Devlieghere   auto EC = MakeAbsolute(path);
17746376966SJonas Devlieghere   if (EC)
17846376966SJonas Devlieghere     return EC;
17946376966SJonas Devlieghere 
1808f3be7a3SJonas Devlieghere   FileSpec new_file_spec(path, file_spec.GetPathStyle());
18146376966SJonas Devlieghere   file_spec = new_file_spec;
18246376966SJonas Devlieghere   return {};
18346376966SJonas Devlieghere }
18446376966SJonas Devlieghere 
18546376966SJonas Devlieghere std::error_code FileSystem::GetRealPath(const Twine &path,
18646376966SJonas Devlieghere                                         SmallVectorImpl<char> &output) const {
18746376966SJonas Devlieghere   return m_fs->getRealPath(path, output);
18846376966SJonas Devlieghere }
18946376966SJonas Devlieghere 
19046376966SJonas Devlieghere void FileSystem::Resolve(SmallVectorImpl<char> &path) {
19146376966SJonas Devlieghere   if (path.empty())
19246376966SJonas Devlieghere     return;
19346376966SJonas Devlieghere 
19446376966SJonas Devlieghere   // Resolve tilde.
19546376966SJonas Devlieghere   SmallString<128> original_path(path.begin(), path.end());
19646376966SJonas Devlieghere   StandardTildeExpressionResolver Resolver;
19746376966SJonas Devlieghere   Resolver.ResolveFullPath(original_path, path);
19846376966SJonas Devlieghere 
19946376966SJonas Devlieghere   // Try making the path absolute if it exists.
20046376966SJonas Devlieghere   SmallString<128> absolute_path(path.begin(), path.end());
20146376966SJonas Devlieghere   MakeAbsolute(path);
20246376966SJonas Devlieghere   if (!Exists(path)) {
20346376966SJonas Devlieghere     path.clear();
20446376966SJonas Devlieghere     path.append(original_path.begin(), original_path.end());
20546376966SJonas Devlieghere   }
20646376966SJonas Devlieghere }
20746376966SJonas Devlieghere 
20846376966SJonas Devlieghere void FileSystem::Resolve(FileSpec &file_spec) {
20946376966SJonas Devlieghere   // Extract path from the FileSpec.
21046376966SJonas Devlieghere   SmallString<128> path;
21146376966SJonas Devlieghere   file_spec.GetPath(path);
21246376966SJonas Devlieghere 
21346376966SJonas Devlieghere   // Resolve the path.
21446376966SJonas Devlieghere   Resolve(path);
21546376966SJonas Devlieghere 
21646376966SJonas Devlieghere   // Update the FileSpec with the resolved path.
21746376966SJonas Devlieghere   file_spec.SetPath(path);
2188f3be7a3SJonas Devlieghere   file_spec.SetIsResolved(true);
2191408bf72SPavel Labath }
2202c22c800SJonas Devlieghere 
2212c22c800SJonas Devlieghere bool FileSystem::ResolveExecutableLocation(FileSpec &file_spec) {
2222c22c800SJonas Devlieghere   // If the directory is set there's nothing to do.
2232c22c800SJonas Devlieghere   const ConstString &directory = file_spec.GetDirectory();
2242c22c800SJonas Devlieghere   if (directory)
2252c22c800SJonas Devlieghere     return false;
2262c22c800SJonas Devlieghere 
2272c22c800SJonas Devlieghere   // We cannot look for a file if there's no file name.
2282c22c800SJonas Devlieghere   const ConstString &filename = file_spec.GetFilename();
2292c22c800SJonas Devlieghere   if (!filename)
2302c22c800SJonas Devlieghere     return false;
2312c22c800SJonas Devlieghere 
2322c22c800SJonas Devlieghere   // Search for the file on the host.
2332c22c800SJonas Devlieghere   const std::string filename_str(filename.GetCString());
2342c22c800SJonas Devlieghere   llvm::ErrorOr<std::string> error_or_path =
2352c22c800SJonas Devlieghere       llvm::sys::findProgramByName(filename_str);
2362c22c800SJonas Devlieghere   if (!error_or_path)
2372c22c800SJonas Devlieghere     return false;
2382c22c800SJonas Devlieghere 
2392c22c800SJonas Devlieghere   // findProgramByName returns "." if it can't find the file.
2402c22c800SJonas Devlieghere   llvm::StringRef path = *error_or_path;
2412c22c800SJonas Devlieghere   llvm::StringRef parent = llvm::sys::path::parent_path(path);
2422c22c800SJonas Devlieghere   if (parent.empty() || parent == ".")
2432c22c800SJonas Devlieghere     return false;
2442c22c800SJonas Devlieghere 
2452c22c800SJonas Devlieghere   // Make sure that the result exists.
2468f3be7a3SJonas Devlieghere   FileSpec result(*error_or_path);
2472c22c800SJonas Devlieghere   if (!Exists(result))
2482c22c800SJonas Devlieghere     return false;
2492c22c800SJonas Devlieghere 
2502c22c800SJonas Devlieghere   file_spec = result;
2512c22c800SJonas Devlieghere   return true;
2522c22c800SJonas Devlieghere }
25350bc1ed2SJonas Devlieghere 
25450bc1ed2SJonas Devlieghere static int OpenWithFS(const FileSystem &fs, const char *path, int flags,
25550bc1ed2SJonas Devlieghere                       int mode) {
25650bc1ed2SJonas Devlieghere   return const_cast<FileSystem &>(fs).Open(path, flags, mode);
25750bc1ed2SJonas Devlieghere }
25850bc1ed2SJonas Devlieghere 
25950bc1ed2SJonas Devlieghere static int GetOpenFlags(uint32_t options) {
26050bc1ed2SJonas Devlieghere   const bool read = options & File::eOpenOptionRead;
26150bc1ed2SJonas Devlieghere   const bool write = options & File::eOpenOptionWrite;
26250bc1ed2SJonas Devlieghere 
26350bc1ed2SJonas Devlieghere   int open_flags = 0;
26450bc1ed2SJonas Devlieghere   if (write) {
26550bc1ed2SJonas Devlieghere     if (read)
26650bc1ed2SJonas Devlieghere       open_flags |= O_RDWR;
26750bc1ed2SJonas Devlieghere     else
26850bc1ed2SJonas Devlieghere       open_flags |= O_WRONLY;
26950bc1ed2SJonas Devlieghere 
27050bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionAppend)
27150bc1ed2SJonas Devlieghere       open_flags |= O_APPEND;
27250bc1ed2SJonas Devlieghere 
27350bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionTruncate)
27450bc1ed2SJonas Devlieghere       open_flags |= O_TRUNC;
27550bc1ed2SJonas Devlieghere 
27650bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionCanCreate)
27750bc1ed2SJonas Devlieghere       open_flags |= O_CREAT;
27850bc1ed2SJonas Devlieghere 
27950bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionCanCreateNewOnly)
28050bc1ed2SJonas Devlieghere       open_flags |= O_CREAT | O_EXCL;
28150bc1ed2SJonas Devlieghere   } else if (read) {
28250bc1ed2SJonas Devlieghere     open_flags |= O_RDONLY;
28350bc1ed2SJonas Devlieghere 
28450bc1ed2SJonas Devlieghere #ifndef _WIN32
28550bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionDontFollowSymlinks)
28650bc1ed2SJonas Devlieghere       open_flags |= O_NOFOLLOW;
28750bc1ed2SJonas Devlieghere #endif
28850bc1ed2SJonas Devlieghere   }
28950bc1ed2SJonas Devlieghere 
29050bc1ed2SJonas Devlieghere #ifndef _WIN32
29150bc1ed2SJonas Devlieghere   if (options & File::eOpenOptionNonBlocking)
29250bc1ed2SJonas Devlieghere     open_flags |= O_NONBLOCK;
29350bc1ed2SJonas Devlieghere   if (options & File::eOpenOptionCloseOnExec)
29450bc1ed2SJonas Devlieghere     open_flags |= O_CLOEXEC;
29550bc1ed2SJonas Devlieghere #else
29650bc1ed2SJonas Devlieghere   open_flags |= O_BINARY;
29750bc1ed2SJonas Devlieghere #endif
29850bc1ed2SJonas Devlieghere 
29950bc1ed2SJonas Devlieghere   return open_flags;
30050bc1ed2SJonas Devlieghere }
30150bc1ed2SJonas Devlieghere 
30250bc1ed2SJonas Devlieghere static mode_t GetOpenMode(uint32_t permissions) {
30350bc1ed2SJonas Devlieghere   mode_t mode = 0;
30450bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsUserRead)
30550bc1ed2SJonas Devlieghere     mode |= S_IRUSR;
30650bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsUserWrite)
30750bc1ed2SJonas Devlieghere     mode |= S_IWUSR;
30850bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsUserExecute)
30950bc1ed2SJonas Devlieghere     mode |= S_IXUSR;
31050bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsGroupRead)
31150bc1ed2SJonas Devlieghere     mode |= S_IRGRP;
31250bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsGroupWrite)
31350bc1ed2SJonas Devlieghere     mode |= S_IWGRP;
31450bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsGroupExecute)
31550bc1ed2SJonas Devlieghere     mode |= S_IXGRP;
31650bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsWorldRead)
31750bc1ed2SJonas Devlieghere     mode |= S_IROTH;
31850bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsWorldWrite)
31950bc1ed2SJonas Devlieghere     mode |= S_IWOTH;
32050bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsWorldExecute)
32150bc1ed2SJonas Devlieghere     mode |= S_IXOTH;
32250bc1ed2SJonas Devlieghere   return mode;
32350bc1ed2SJonas Devlieghere }
32450bc1ed2SJonas Devlieghere 
32550bc1ed2SJonas Devlieghere Status FileSystem::Open(File &File, const FileSpec &file_spec, uint32_t options,
32650bc1ed2SJonas Devlieghere                         uint32_t permissions) {
32750bc1ed2SJonas Devlieghere   if (File.IsValid())
32850bc1ed2SJonas Devlieghere     File.Close();
32950bc1ed2SJonas Devlieghere 
33050bc1ed2SJonas Devlieghere   const int open_flags = GetOpenFlags(options);
33150bc1ed2SJonas Devlieghere   const mode_t open_mode =
33250bc1ed2SJonas Devlieghere       (open_flags & O_CREAT) ? GetOpenMode(permissions) : 0;
33350bc1ed2SJonas Devlieghere   const std::string path = file_spec.GetPath();
33450bc1ed2SJonas Devlieghere 
33550bc1ed2SJonas Devlieghere   int descriptor = llvm::sys::RetryAfterSignal(
33650bc1ed2SJonas Devlieghere       -1, OpenWithFS, *this, path.c_str(), open_flags, open_mode);
33750bc1ed2SJonas Devlieghere 
33850bc1ed2SJonas Devlieghere   Status error;
33950bc1ed2SJonas Devlieghere   if (!File::DescriptorIsValid(descriptor)) {
34050bc1ed2SJonas Devlieghere     File.SetDescriptor(descriptor, false);
34150bc1ed2SJonas Devlieghere     error.SetErrorToErrno();
34250bc1ed2SJonas Devlieghere   } else {
34350bc1ed2SJonas Devlieghere     File.SetDescriptor(descriptor, true);
34450bc1ed2SJonas Devlieghere     File.SetOptions(options);
34550bc1ed2SJonas Devlieghere   }
34650bc1ed2SJonas Devlieghere   return error;
34750bc1ed2SJonas Devlieghere }
348