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 
15*50bc1ed2SJonas 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 
21*50bc1ed2SJonas Devlieghere #include <errno.h>
22*50bc1ed2SJonas Devlieghere #include <fcntl.h>
23*50bc1ed2SJonas Devlieghere #include <limits.h>
24*50bc1ed2SJonas Devlieghere #include <stdarg.h>
25*50bc1ed2SJonas Devlieghere #include <stdio.h>
26*50bc1ed2SJonas Devlieghere 
27*50bc1ed2SJonas Devlieghere #ifdef _WIN32
28*50bc1ed2SJonas Devlieghere #include "lldb/Host/windows/windows.h"
29*50bc1ed2SJonas Devlieghere #else
30*50bc1ed2SJonas Devlieghere #include <sys/ioctl.h>
31*50bc1ed2SJonas Devlieghere #include <sys/stat.h>
32*50bc1ed2SJonas Devlieghere #include <termios.h>
33*50bc1ed2SJonas Devlieghere #include <unistd.h>
34*50bc1ed2SJonas Devlieghere #endif
35*50bc1ed2SJonas 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 
1279ca491daSJonas Devlieghere void FileSystem::EnumerateDirectory(Twine path, bool find_directories,
1289ca491daSJonas Devlieghere                                     bool find_files, bool find_other,
1299ca491daSJonas Devlieghere                                     EnumerateDirectoryCallbackType callback,
1309ca491daSJonas Devlieghere                                     void *callback_baton) {
1319ca491daSJonas Devlieghere   std::error_code EC;
1329ca491daSJonas Devlieghere   vfs::recursive_directory_iterator Iter(*m_fs, path, EC);
1339ca491daSJonas Devlieghere   vfs::recursive_directory_iterator End;
1349ca491daSJonas Devlieghere   for (; Iter != End && !EC; Iter.increment(EC)) {
1359ca491daSJonas Devlieghere     const auto &Item = *Iter;
1369ca491daSJonas Devlieghere     ErrorOr<vfs::Status> Status = m_fs->status(Item.path());
1379ca491daSJonas Devlieghere     if (!Status)
1389ca491daSJonas Devlieghere       break;
1399ca491daSJonas Devlieghere     if (!find_files && Status->isRegularFile())
1409ca491daSJonas Devlieghere       continue;
1419ca491daSJonas Devlieghere     if (!find_directories && Status->isDirectory())
1429ca491daSJonas Devlieghere       continue;
1439ca491daSJonas Devlieghere     if (!find_other && Status->isOther())
1449ca491daSJonas Devlieghere       continue;
1459ca491daSJonas Devlieghere 
1469ca491daSJonas Devlieghere     auto Result = callback(callback_baton, Status->getType(), Item.path());
1479ca491daSJonas Devlieghere     if (Result == eEnumerateDirectoryResultQuit)
1489ca491daSJonas Devlieghere       return;
1499ca491daSJonas Devlieghere     if (Result == eEnumerateDirectoryResultNext) {
1509ca491daSJonas Devlieghere       // Default behavior is to recurse. Opt out if the callback doesn't want
1519ca491daSJonas Devlieghere       // this behavior.
1529ca491daSJonas Devlieghere       Iter.no_push();
1539ca491daSJonas Devlieghere     }
1549ca491daSJonas Devlieghere   }
1559ca491daSJonas Devlieghere }
1569ca491daSJonas Devlieghere 
15746376966SJonas Devlieghere std::error_code FileSystem::MakeAbsolute(SmallVectorImpl<char> &path) const {
15846376966SJonas Devlieghere   return m_fs->makeAbsolute(path);
15946376966SJonas Devlieghere }
16046376966SJonas Devlieghere 
16146376966SJonas Devlieghere std::error_code FileSystem::MakeAbsolute(FileSpec &file_spec) const {
16246376966SJonas Devlieghere   SmallString<128> path;
16346376966SJonas Devlieghere   file_spec.GetPath(path, false);
16446376966SJonas Devlieghere 
16546376966SJonas Devlieghere   auto EC = MakeAbsolute(path);
16646376966SJonas Devlieghere   if (EC)
16746376966SJonas Devlieghere     return EC;
16846376966SJonas Devlieghere 
1698f3be7a3SJonas Devlieghere   FileSpec new_file_spec(path, file_spec.GetPathStyle());
17046376966SJonas Devlieghere   file_spec = new_file_spec;
17146376966SJonas Devlieghere   return {};
17246376966SJonas Devlieghere }
17346376966SJonas Devlieghere 
17446376966SJonas Devlieghere std::error_code FileSystem::GetRealPath(const Twine &path,
17546376966SJonas Devlieghere                                         SmallVectorImpl<char> &output) const {
17646376966SJonas Devlieghere   return m_fs->getRealPath(path, output);
17746376966SJonas Devlieghere }
17846376966SJonas Devlieghere 
17946376966SJonas Devlieghere void FileSystem::Resolve(SmallVectorImpl<char> &path) {
18046376966SJonas Devlieghere   if (path.empty())
18146376966SJonas Devlieghere     return;
18246376966SJonas Devlieghere 
18346376966SJonas Devlieghere   // Resolve tilde.
18446376966SJonas Devlieghere   SmallString<128> original_path(path.begin(), path.end());
18546376966SJonas Devlieghere   StandardTildeExpressionResolver Resolver;
18646376966SJonas Devlieghere   Resolver.ResolveFullPath(original_path, path);
18746376966SJonas Devlieghere 
18846376966SJonas Devlieghere   // Try making the path absolute if it exists.
18946376966SJonas Devlieghere   SmallString<128> absolute_path(path.begin(), path.end());
19046376966SJonas Devlieghere   MakeAbsolute(path);
19146376966SJonas Devlieghere   if (!Exists(path)) {
19246376966SJonas Devlieghere     path.clear();
19346376966SJonas Devlieghere     path.append(original_path.begin(), original_path.end());
19446376966SJonas Devlieghere   }
19546376966SJonas Devlieghere }
19646376966SJonas Devlieghere 
19746376966SJonas Devlieghere void FileSystem::Resolve(FileSpec &file_spec) {
19846376966SJonas Devlieghere   // Extract path from the FileSpec.
19946376966SJonas Devlieghere   SmallString<128> path;
20046376966SJonas Devlieghere   file_spec.GetPath(path);
20146376966SJonas Devlieghere 
20246376966SJonas Devlieghere   // Resolve the path.
20346376966SJonas Devlieghere   Resolve(path);
20446376966SJonas Devlieghere 
20546376966SJonas Devlieghere   // Update the FileSpec with the resolved path.
20646376966SJonas Devlieghere   file_spec.SetPath(path);
2078f3be7a3SJonas Devlieghere   file_spec.SetIsResolved(true);
2081408bf72SPavel Labath }
2092c22c800SJonas Devlieghere 
2102c22c800SJonas Devlieghere bool FileSystem::ResolveExecutableLocation(FileSpec &file_spec) {
2112c22c800SJonas Devlieghere   // If the directory is set there's nothing to do.
2122c22c800SJonas Devlieghere   const ConstString &directory = file_spec.GetDirectory();
2132c22c800SJonas Devlieghere   if (directory)
2142c22c800SJonas Devlieghere     return false;
2152c22c800SJonas Devlieghere 
2162c22c800SJonas Devlieghere   // We cannot look for a file if there's no file name.
2172c22c800SJonas Devlieghere   const ConstString &filename = file_spec.GetFilename();
2182c22c800SJonas Devlieghere   if (!filename)
2192c22c800SJonas Devlieghere     return false;
2202c22c800SJonas Devlieghere 
2212c22c800SJonas Devlieghere   // Search for the file on the host.
2222c22c800SJonas Devlieghere   const std::string filename_str(filename.GetCString());
2232c22c800SJonas Devlieghere   llvm::ErrorOr<std::string> error_or_path =
2242c22c800SJonas Devlieghere       llvm::sys::findProgramByName(filename_str);
2252c22c800SJonas Devlieghere   if (!error_or_path)
2262c22c800SJonas Devlieghere     return false;
2272c22c800SJonas Devlieghere 
2282c22c800SJonas Devlieghere   // findProgramByName returns "." if it can't find the file.
2292c22c800SJonas Devlieghere   llvm::StringRef path = *error_or_path;
2302c22c800SJonas Devlieghere   llvm::StringRef parent = llvm::sys::path::parent_path(path);
2312c22c800SJonas Devlieghere   if (parent.empty() || parent == ".")
2322c22c800SJonas Devlieghere     return false;
2332c22c800SJonas Devlieghere 
2342c22c800SJonas Devlieghere   // Make sure that the result exists.
2358f3be7a3SJonas Devlieghere   FileSpec result(*error_or_path);
2362c22c800SJonas Devlieghere   if (!Exists(result))
2372c22c800SJonas Devlieghere     return false;
2382c22c800SJonas Devlieghere 
2392c22c800SJonas Devlieghere   file_spec = result;
2402c22c800SJonas Devlieghere   return true;
2412c22c800SJonas Devlieghere }
242*50bc1ed2SJonas Devlieghere 
243*50bc1ed2SJonas Devlieghere static int OpenWithFS(const FileSystem &fs, const char *path, int flags,
244*50bc1ed2SJonas Devlieghere                       int mode) {
245*50bc1ed2SJonas Devlieghere   return const_cast<FileSystem &>(fs).Open(path, flags, mode);
246*50bc1ed2SJonas Devlieghere }
247*50bc1ed2SJonas Devlieghere 
248*50bc1ed2SJonas Devlieghere static int GetOpenFlags(uint32_t options) {
249*50bc1ed2SJonas Devlieghere   const bool read = options & File::eOpenOptionRead;
250*50bc1ed2SJonas Devlieghere   const bool write = options & File::eOpenOptionWrite;
251*50bc1ed2SJonas Devlieghere 
252*50bc1ed2SJonas Devlieghere   int open_flags = 0;
253*50bc1ed2SJonas Devlieghere   if (write) {
254*50bc1ed2SJonas Devlieghere     if (read)
255*50bc1ed2SJonas Devlieghere       open_flags |= O_RDWR;
256*50bc1ed2SJonas Devlieghere     else
257*50bc1ed2SJonas Devlieghere       open_flags |= O_WRONLY;
258*50bc1ed2SJonas Devlieghere 
259*50bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionAppend)
260*50bc1ed2SJonas Devlieghere       open_flags |= O_APPEND;
261*50bc1ed2SJonas Devlieghere 
262*50bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionTruncate)
263*50bc1ed2SJonas Devlieghere       open_flags |= O_TRUNC;
264*50bc1ed2SJonas Devlieghere 
265*50bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionCanCreate)
266*50bc1ed2SJonas Devlieghere       open_flags |= O_CREAT;
267*50bc1ed2SJonas Devlieghere 
268*50bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionCanCreateNewOnly)
269*50bc1ed2SJonas Devlieghere       open_flags |= O_CREAT | O_EXCL;
270*50bc1ed2SJonas Devlieghere   } else if (read) {
271*50bc1ed2SJonas Devlieghere     open_flags |= O_RDONLY;
272*50bc1ed2SJonas Devlieghere 
273*50bc1ed2SJonas Devlieghere #ifndef _WIN32
274*50bc1ed2SJonas Devlieghere     if (options & File::eOpenOptionDontFollowSymlinks)
275*50bc1ed2SJonas Devlieghere       open_flags |= O_NOFOLLOW;
276*50bc1ed2SJonas Devlieghere #endif
277*50bc1ed2SJonas Devlieghere   }
278*50bc1ed2SJonas Devlieghere 
279*50bc1ed2SJonas Devlieghere #ifndef _WIN32
280*50bc1ed2SJonas Devlieghere   if (options & File::eOpenOptionNonBlocking)
281*50bc1ed2SJonas Devlieghere     open_flags |= O_NONBLOCK;
282*50bc1ed2SJonas Devlieghere   if (options & File::eOpenOptionCloseOnExec)
283*50bc1ed2SJonas Devlieghere     open_flags |= O_CLOEXEC;
284*50bc1ed2SJonas Devlieghere #else
285*50bc1ed2SJonas Devlieghere   open_flags |= O_BINARY;
286*50bc1ed2SJonas Devlieghere #endif
287*50bc1ed2SJonas Devlieghere 
288*50bc1ed2SJonas Devlieghere   return open_flags;
289*50bc1ed2SJonas Devlieghere }
290*50bc1ed2SJonas Devlieghere 
291*50bc1ed2SJonas Devlieghere static mode_t GetOpenMode(uint32_t permissions) {
292*50bc1ed2SJonas Devlieghere   mode_t mode = 0;
293*50bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsUserRead)
294*50bc1ed2SJonas Devlieghere     mode |= S_IRUSR;
295*50bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsUserWrite)
296*50bc1ed2SJonas Devlieghere     mode |= S_IWUSR;
297*50bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsUserExecute)
298*50bc1ed2SJonas Devlieghere     mode |= S_IXUSR;
299*50bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsGroupRead)
300*50bc1ed2SJonas Devlieghere     mode |= S_IRGRP;
301*50bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsGroupWrite)
302*50bc1ed2SJonas Devlieghere     mode |= S_IWGRP;
303*50bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsGroupExecute)
304*50bc1ed2SJonas Devlieghere     mode |= S_IXGRP;
305*50bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsWorldRead)
306*50bc1ed2SJonas Devlieghere     mode |= S_IROTH;
307*50bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsWorldWrite)
308*50bc1ed2SJonas Devlieghere     mode |= S_IWOTH;
309*50bc1ed2SJonas Devlieghere   if (permissions & lldb::eFilePermissionsWorldExecute)
310*50bc1ed2SJonas Devlieghere     mode |= S_IXOTH;
311*50bc1ed2SJonas Devlieghere   return mode;
312*50bc1ed2SJonas Devlieghere }
313*50bc1ed2SJonas Devlieghere 
314*50bc1ed2SJonas Devlieghere Status FileSystem::Open(File &File, const FileSpec &file_spec, uint32_t options,
315*50bc1ed2SJonas Devlieghere                         uint32_t permissions) {
316*50bc1ed2SJonas Devlieghere   if (File.IsValid())
317*50bc1ed2SJonas Devlieghere     File.Close();
318*50bc1ed2SJonas Devlieghere 
319*50bc1ed2SJonas Devlieghere   const int open_flags = GetOpenFlags(options);
320*50bc1ed2SJonas Devlieghere   const mode_t open_mode =
321*50bc1ed2SJonas Devlieghere       (open_flags & O_CREAT) ? GetOpenMode(permissions) : 0;
322*50bc1ed2SJonas Devlieghere   const std::string path = file_spec.GetPath();
323*50bc1ed2SJonas Devlieghere 
324*50bc1ed2SJonas Devlieghere   int descriptor = llvm::sys::RetryAfterSignal(
325*50bc1ed2SJonas Devlieghere       -1, OpenWithFS, *this, path.c_str(), open_flags, open_mode);
326*50bc1ed2SJonas Devlieghere 
327*50bc1ed2SJonas Devlieghere   Status error;
328*50bc1ed2SJonas Devlieghere   if (!File::DescriptorIsValid(descriptor)) {
329*50bc1ed2SJonas Devlieghere     File.SetDescriptor(descriptor, false);
330*50bc1ed2SJonas Devlieghere     error.SetErrorToErrno();
331*50bc1ed2SJonas Devlieghere   } else {
332*50bc1ed2SJonas Devlieghere     File.SetDescriptor(descriptor, true);
333*50bc1ed2SJonas Devlieghere     File.SetOptions(options);
334*50bc1ed2SJonas Devlieghere   }
335*50bc1ed2SJonas Devlieghere   return error;
336*50bc1ed2SJonas Devlieghere }
337