blob: b0e67ec0d6541eb98351b25a9b863a735ec6eac2 [file] [log] [blame]
Jonas Devlieghere46575172019-01-29 20:36:38 +00001//===-- FileCollector.cpp ---------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
Alex Lorenz86814bf2019-07-24 22:59:20 +00009#include "llvm/Support/FileCollector.h"
Jonas Devlieghere46575172019-01-29 20:36:38 +000010#include "llvm/ADT/SmallString.h"
11#include "llvm/Support/FileSystem.h"
12#include "llvm/Support/Path.h"
Jonas Devlieghere27789ce2019-06-26 18:14:31 +000013#include "llvm/Support/Process.h"
Jonas Devlieghere46575172019-01-29 20:36:38 +000014
Jonas Devlieghere46575172019-01-29 20:36:38 +000015using namespace llvm;
16
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +000017static bool isCaseSensitivePath(StringRef Path) {
18 SmallString<256> TmpDest = Path, UpperDest, RealDest;
Jonas Devlieghere46575172019-01-29 20:36:38 +000019
20 // Remove component traversals, links, etc.
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +000021 if (!sys::fs::real_path(Path, TmpDest))
Jonas Devlieghere46575172019-01-29 20:36:38 +000022 return true; // Current default value in vfs.yaml
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +000023 Path = TmpDest;
Jonas Devlieghere46575172019-01-29 20:36:38 +000024
25 // Change path to all upper case and ask for its real path, if the latter
26 // exists and is equal to path, it's not case sensitive. Default to case
27 // sensitive in the absence of real_path, since this is the YAMLVFSWriter
28 // default.
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +000029 UpperDest = Path.upper();
30 if (sys::fs::real_path(UpperDest, RealDest) && Path.equals(RealDest))
Jonas Devlieghere46575172019-01-29 20:36:38 +000031 return false;
32 return true;
33}
34
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +000035FileCollector::FileCollector(std::string Root, std::string OverlayRoot)
36 : Root(std::move(Root)), OverlayRoot(std::move(OverlayRoot)) {
37 sys::fs::create_directories(this->Root, true);
Jonas Devlieghere46575172019-01-29 20:36:38 +000038}
39
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +000040bool FileCollector::getRealPath(StringRef SrcPath,
41 SmallVectorImpl<char> &Result) {
42 SmallString<256> RealPath;
43 StringRef FileName = sys::path::filename(SrcPath);
44 std::string Directory = sys::path::parent_path(SrcPath).str();
45 auto DirWithSymlink = SymlinkMap.find(Directory);
Jonas Devlieghere46575172019-01-29 20:36:38 +000046
47 // Use real_path to fix any symbolic link component present in a path.
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +000048 // Computing the real path is expensive, cache the search through the parent
49 // path Directory.
50 if (DirWithSymlink == SymlinkMap.end()) {
51 auto EC = sys::fs::real_path(Directory, RealPath);
52 if (EC)
Jonas Devlieghere46575172019-01-29 20:36:38 +000053 return false;
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +000054 SymlinkMap[Directory] = RealPath.str();
Jonas Devlieghere46575172019-01-29 20:36:38 +000055 } else {
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +000056 RealPath = DirWithSymlink->second;
Jonas Devlieghere46575172019-01-29 20:36:38 +000057 }
58
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +000059 sys::path::append(RealPath, FileName);
60 Result.swap(RealPath);
Jonas Devlieghere46575172019-01-29 20:36:38 +000061 return true;
62}
63
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +000064void FileCollector::addFile(const Twine &file) {
65 std::lock_guard<std::mutex> lock(Mutex);
66 std::string FileStr = file.str();
67 if (markAsSeen(FileStr))
68 addFileImpl(FileStr);
Jonas Devlieghere46575172019-01-29 20:36:38 +000069}
70
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +000071void FileCollector::addFileImpl(StringRef SrcPath) {
Jonas Devlieghere46575172019-01-29 20:36:38 +000072 // We need an absolute src path to append to the root.
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +000073 SmallString<256> AbsoluteSrc = SrcPath;
74 sys::fs::make_absolute(AbsoluteSrc);
Jonas Devlieghere46575172019-01-29 20:36:38 +000075
76 // Canonicalize src to a native path to avoid mixed separator styles.
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +000077 sys::path::native(AbsoluteSrc);
Jonas Devlieghere46575172019-01-29 20:36:38 +000078
79 // Remove redundant leading "./" pieces and consecutive separators.
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +000080 AbsoluteSrc = sys::path::remove_leading_dotslash(AbsoluteSrc);
Jonas Devlieghere46575172019-01-29 20:36:38 +000081
82 // Canonicalize the source path by removing "..", "." components.
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +000083 SmallString<256> VirtualPath = AbsoluteSrc;
84 sys::path::remove_dots(VirtualPath, /*remove_dot_dot=*/true);
Jonas Devlieghere46575172019-01-29 20:36:38 +000085
86 // If a ".." component is present after a symlink component, remove_dots may
87 // lead to the wrong real destination path. Let the source be canonicalized
88 // like that but make sure we always use the real path for the destination.
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +000089 SmallString<256> CopyFrom;
90 if (!getRealPath(AbsoluteSrc, CopyFrom))
91 CopyFrom = VirtualPath;
Jonas Devlieghere46575172019-01-29 20:36:38 +000092
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +000093 SmallString<256> DstPath = StringRef(Root);
94 sys::path::append(DstPath, sys::path::relative_path(CopyFrom));
Jonas Devlieghere46575172019-01-29 20:36:38 +000095
96 // Always map a canonical src path to its real path into the YAML, by doing
97 // this we map different virtual src paths to the same entry in the VFS
98 // overlay, which is a way to emulate symlink inside the VFS; this is also
99 // needed for correctness, not doing that can lead to module redefinition
100 // errors.
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +0000101 addFileToMapping(VirtualPath, DstPath);
Jonas Devlieghere46575172019-01-29 20:36:38 +0000102}
103
Jonas Devlieghere27789ce2019-06-26 18:14:31 +0000104/// Set the access and modification time for the given file from the given
105/// status object.
106static std::error_code
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +0000107copyAccessAndModificationTime(StringRef Filename,
108 const sys::fs::file_status &Stat) {
109 int FD;
Jonas Devlieghere27789ce2019-06-26 18:14:31 +0000110
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +0000111 if (auto EC =
112 sys::fs::openFileForWrite(Filename, FD, sys::fs::CD_OpenExisting))
113 return EC;
Jonas Devlieghere27789ce2019-06-26 18:14:31 +0000114
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +0000115 if (auto EC = sys::fs::setLastAccessAndModificationTime(
116 FD, Stat.getLastAccessedTime(), Stat.getLastModificationTime()))
117 return EC;
Jonas Devlieghere27789ce2019-06-26 18:14:31 +0000118
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +0000119 if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
120 return EC;
Jonas Devlieghere27789ce2019-06-26 18:14:31 +0000121
122 return {};
123}
124
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +0000125std::error_code FileCollector::copyFiles(bool StopOnError) {
126 for (auto &entry : VFSWriter.getMappings()) {
Jonas Devlieghere46575172019-01-29 20:36:38 +0000127 // Create directory tree.
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +0000128 if (std::error_code EC =
Jonas Devlieghere46575172019-01-29 20:36:38 +0000129 sys::fs::create_directories(sys::path::parent_path(entry.RPath),
130 /*IgnoreExisting=*/true)) {
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +0000131 if (StopOnError)
132 return EC;
Jonas Devlieghere46575172019-01-29 20:36:38 +0000133 }
134
135 // Copy file over.
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +0000136 if (std::error_code EC = sys::fs::copy_file(entry.VPath, entry.RPath)) {
137 if (StopOnError)
138 return EC;
Jonas Devlieghere46575172019-01-29 20:36:38 +0000139 }
140
141 // Copy over permissions.
142 if (auto perms = sys::fs::getPermissions(entry.VPath)) {
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +0000143 if (std::error_code EC = sys::fs::setPermissions(entry.RPath, *perms)) {
144 if (StopOnError)
145 return EC;
Jonas Devlieghere46575172019-01-29 20:36:38 +0000146 }
147 }
Jonas Devlieghere27789ce2019-06-26 18:14:31 +0000148
149 // Copy over modification time.
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +0000150 sys::fs::file_status Stat;
151 if (std::error_code EC = sys::fs::status(entry.VPath, Stat)) {
152 if (StopOnError)
153 return EC;
Jonas Devlieghere27789ce2019-06-26 18:14:31 +0000154 continue;
155 }
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +0000156 copyAccessAndModificationTime(entry.RPath, Stat);
Jonas Devlieghere46575172019-01-29 20:36:38 +0000157 }
158 return {};
159}
160
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +0000161std::error_code FileCollector::writeMapping(StringRef mapping_file) {
162 std::lock_guard<std::mutex> lock(Mutex);
Jonas Devlieghere46575172019-01-29 20:36:38 +0000163
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +0000164 VFSWriter.setOverlayDir(OverlayRoot);
165 VFSWriter.setCaseSensitivity(isCaseSensitivePath(OverlayRoot));
166 VFSWriter.setUseExternalNames(false);
Jonas Devlieghere46575172019-01-29 20:36:38 +0000167
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +0000168 std::error_code EC;
169 raw_fd_ostream os(mapping_file, EC, sys::fs::F_Text);
170 if (EC)
171 return EC;
Jonas Devlieghere46575172019-01-29 20:36:38 +0000172
Jonas Devlieghereeb1b4c52019-07-25 00:17:39 +0000173 VFSWriter.write(os);
Jonas Devlieghere46575172019-01-29 20:36:38 +0000174
175 return {};
176}