LLVM 19.0.0git
FileCollector.cpp
Go to the documentation of this file.
1//===-- 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
11#include "llvm/ADT/Twine.h"
13#include "llvm/Support/Path.h"
15
16using namespace llvm;
17
20
22 std::lock_guard<std::mutex> lock(Mutex);
23 std::string FileStr = File.str();
24 if (markAsSeen(FileStr))
25 addFileImpl(FileStr);
26}
27
30 std::error_code EC;
32}
33
34static bool isCaseSensitivePath(StringRef Path) {
35 SmallString<256> TmpDest = Path, UpperDest, RealDest;
36
37 // Remove component traversals, links, etc.
38 if (sys::fs::real_path(Path, TmpDest))
39 return true; // Current default value in vfs.yaml
40 Path = TmpDest;
41
42 // Change path to all upper case and ask for its real path, if the latter
43 // exists and is equal to path, it's not case sensitive. Default to case
44 // sensitive in the absence of real_path, since this is the YAMLVFSWriter
45 // default.
46 UpperDest = Path.upper();
47 if (!sys::fs::real_path(UpperDest, RealDest) && Path.equals(RealDest))
48 return false;
49 return true;
50}
51
52FileCollector::FileCollector(std::string Root, std::string OverlayRoot)
53 : Root(Root), OverlayRoot(OverlayRoot) {
54 assert(sys::path::is_absolute(Root) && "Root not absolute");
55 assert(sys::path::is_absolute(OverlayRoot) && "OverlayRoot not absolute");
56}
57
58void FileCollector::PathCanonicalizer::updateWithRealPath(
60 StringRef SrcPath(Path.begin(), Path.size());
61 StringRef Filename = sys::path::filename(SrcPath);
62 StringRef Directory = sys::path::parent_path(SrcPath);
63
64 // Use real_path to fix any symbolic link component present in the directory
65 // part of the path, caching the search because computing the real path is
66 // expensive.
67 SmallString<256> RealPath;
68 auto DirWithSymlink = CachedDirs.find(Directory);
69 if (DirWithSymlink == CachedDirs.end()) {
70 // FIXME: Should this be a call to FileSystem::getRealpath(), in some
71 // cases? What if there is nothing on disk?
72 if (sys::fs::real_path(Directory, RealPath))
73 return;
74 CachedDirs[Directory] = std::string(RealPath);
75 } else {
76 RealPath = DirWithSymlink->second;
77 }
78
79 // Finish recreating the path by appending the original filename, since we
80 // don't need to resolve symlinks in the filename.
81 //
82 // FIXME: If we can cope with this, maybe we can cope without calling
83 // getRealPath() at all when there's no ".." component.
84 sys::path::append(RealPath, Filename);
85
86 // Swap to create the output.
87 Path.swap(RealPath);
88}
89
90/// Make Path absolute.
92 // We need an absolute src path to append to the root.
94
95 // Canonicalize src to a native path to avoid mixed separator styles.
97
98 // Remove redundant leading "./" pieces and consecutive separators.
99 Path.erase(Path.begin(), sys::path::remove_leading_dotslash(
100 StringRef(Path.begin(), Path.size()))
101 .begin());
102}
103
106 PathStorage Paths;
107 Paths.VirtualPath = SrcPath;
109
110 // If a ".." component is present after a symlink component, remove_dots may
111 // lead to the wrong real destination path. Let the source be canonicalized
112 // like that but make sure we always use the real path for the destination.
113 Paths.CopyFrom = Paths.VirtualPath;
114 updateWithRealPath(Paths.CopyFrom);
115
116 // Canonicalize the virtual path by removing "..", "." components.
117 sys::path::remove_dots(Paths.VirtualPath, /*remove_dot_dot=*/true);
118
119 return Paths;
120}
121
124
127
128 // Always map a canonical src path to its real path into the YAML, by doing
129 // this we map different virtual src paths to the same entry in the VFS
130 // overlay, which is a way to emulate symlink inside the VFS; this is also
131 // needed for correctness, not doing that can lead to module redefinition
132 // errors.
133 addFileToMapping(Paths.VirtualPath, DstPath);
134}
135
139 std::error_code &EC) {
140 auto It = FS->dir_begin(Dir, EC);
141 if (EC)
142 return It;
143 addFile(Dir);
144 for (; !EC && It != llvm::vfs::directory_iterator(); It.increment(EC)) {
145 if (It->type() == sys::fs::file_type::regular_file ||
147 It->type() == sys::fs::file_type::symlink_file) {
148 addFile(It->path());
149 }
150 }
151 if (EC)
152 return It;
153 // Return a new iterator.
154 return FS->dir_begin(Dir, EC);
155}
156
157/// Set the access and modification time for the given file from the given
158/// status object.
159static std::error_code
161 const sys::fs::file_status &Stat) {
162 int FD;
163
164 if (auto EC =
166 return EC;
167
169 FD, Stat.getLastAccessedTime(), Stat.getLastModificationTime()))
170 return EC;
171
173 return EC;
174
175 return {};
176}
177
178std::error_code FileCollector::copyFiles(bool StopOnError) {
179 auto Err = sys::fs::create_directories(Root, /*IgnoreExisting=*/true);
180 if (Err) {
181 return Err;
182 }
183
184 std::lock_guard<std::mutex> lock(Mutex);
185
186 for (auto &entry : VFSWriter.getMappings()) {
187 // Get the status of the original file/directory.
189 if (std::error_code EC = sys::fs::status(entry.VPath, Stat)) {
190 if (StopOnError)
191 return EC;
192 continue;
193 }
194
195 // Continue if the file doesn't exist.
196 if (Stat.type() == sys::fs::file_type::file_not_found)
197 continue;
198
199 // Create directory tree.
200 if (std::error_code EC =
202 /*IgnoreExisting=*/true)) {
203 if (StopOnError)
204 return EC;
205 }
206
207 if (Stat.type() == sys::fs::file_type::directory_file) {
208 // Construct a directory when it's just a directory entry.
209 if (std::error_code EC =
210 sys::fs::create_directories(entry.RPath,
211 /*IgnoreExisting=*/true)) {
212 if (StopOnError)
213 return EC;
214 }
215 continue;
216 }
217
218 // Copy file over.
219 if (std::error_code EC = sys::fs::copy_file(entry.VPath, entry.RPath)) {
220 if (StopOnError)
221 return EC;
222 }
223
224 // Copy over permissions.
225 if (auto perms = sys::fs::getPermissions(entry.VPath)) {
226 if (std::error_code EC = sys::fs::setPermissions(entry.RPath, *perms)) {
227 if (StopOnError)
228 return EC;
229 }
230 }
231
232 // Copy over modification time.
233 copyAccessAndModificationTime(entry.RPath, Stat);
234 }
235 return {};
236}
237
238std::error_code FileCollector::writeMapping(StringRef MappingFile) {
239 std::lock_guard<std::mutex> lock(Mutex);
240
244
245 std::error_code EC;
246 raw_fd_ostream os(MappingFile, EC, sys::fs::OF_TextWithCRLF);
247 if (EC)
248 return EC;
249
250 VFSWriter.write(os);
251
252 return {};
253}
254
255namespace llvm {
256
258public:
260 std::shared_ptr<FileCollector> Collector)
261 : FS(std::move(FS)), Collector(std::move(Collector)) {}
262
264 auto Result = FS->status(Path);
265 if (Result && Result->exists())
266 Collector->addFile(Path);
267 return Result;
268 }
269
271 openFileForRead(const Twine &Path) override {
272 auto Result = FS->openFileForRead(Path);
273 if (Result && *Result)
274 Collector->addFile(Path);
275 return Result;
276 }
277
279 std::error_code &EC) override {
280 return Collector->addDirectoryImpl(Dir, FS, EC);
281 }
282
283 std::error_code getRealPath(const Twine &Path,
284 SmallVectorImpl<char> &Output) const override {
285 auto EC = FS->getRealPath(Path, Output);
286 if (!EC) {
287 Collector->addFile(Path);
288 if (Output.size() > 0)
289 Collector->addFile(Output);
290 }
291 return EC;
292 }
293
294 std::error_code isLocal(const Twine &Path, bool &Result) override {
295 return FS->isLocal(Path, Result);
296 }
297
299 return FS->getCurrentWorkingDirectory();
300 }
301
302 std::error_code setCurrentWorkingDirectory(const llvm::Twine &Path) override {
303 return FS->setCurrentWorkingDirectory(Path);
304 }
305
306private:
308 std::shared_ptr<FileCollector> Collector;
309};
310
311} // namespace llvm
312
315 std::shared_ptr<FileCollector> Collector) {
316 return new FileCollectorFileSystem(std::move(BaseFS), std::move(Collector));
317}
static std::error_code copyAccessAndModificationTime(StringRef Filename, const sys::fs::file_status &Stat)
Set the access and modification time for the given file from the given status object.
static void makeAbsolute(SmallVectorImpl< char > &Path)
Make Path absolute.
static bool isCaseSensitivePath(StringRef Path)
Provides a library for accessing information about this process and other processes on the operating ...
Register Usage Information Collector
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallString class.
Represents either an error or a value T.
Definition: ErrorOr.h:56
void addDirectory(const Twine &Dir)
void addFile(const Twine &file)
virtual llvm::vfs::directory_iterator addDirectoryImpl(const llvm::Twine &Dir, IntrusiveRefCntPtr< vfs::FileSystem > FS, std::error_code &EC)=0
bool markAsSeen(StringRef Path)
Definition: FileCollector.h:31
virtual void addFileImpl(StringRef SrcPath)=0
FileCollectorFileSystem(IntrusiveRefCntPtr< vfs::FileSystem > FS, std::shared_ptr< FileCollector > Collector)
std::error_code getRealPath(const Twine &Path, SmallVectorImpl< char > &Output) const override
Gets real path of Path e.g.
std::error_code setCurrentWorkingDirectory(const llvm::Twine &Path) override
Set the working directory.
std::error_code isLocal(const Twine &Path, bool &Result) override
Is the file mounted on a local filesystem?
llvm::ErrorOr< llvm::vfs::Status > status(const Twine &Path) override
Get the status of the entry at Path, if one exists.
llvm::ErrorOr< std::unique_ptr< llvm::vfs::File > > openFileForRead(const Twine &Path) override
Get a File object for the file at Path, if one exists.
llvm::vfs::directory_iterator dir_begin(const llvm::Twine &Dir, std::error_code &EC) override
Get a directory_iterator for Dir.
llvm::ErrorOr< std::string > getCurrentWorkingDirectory() const override
Get the working directory of this file system.
PathStorage canonicalize(StringRef SrcPath)
Canonicalize a pair of virtual and real paths.
llvm::vfs::directory_iterator addDirectoryImpl(const llvm::Twine &Dir, IntrusiveRefCntPtr< vfs::FileSystem > FS, std::error_code &EC) override
std::error_code writeMapping(StringRef MappingFile)
Write the yaml mapping (for the VFS) to the given file.
FileCollector(std::string Root, std::string OverlayRoot)
Root is the directory where collected files are will be stored.
std::error_code copyFiles(bool StopOnError=true)
Copy the files into the root directory.
const std::string OverlayRoot
The root directory where the VFS overlay lives.
PathCanonicalizer Canonicalizer
Helper utility for canonicalizing paths.
const std::string Root
The directory where collected files are copied to in copyFiles().
vfs::YAMLVFSWriter VFSWriter
The yaml mapping writer.
void addFileImpl(StringRef SrcPath) override
static IntrusiveRefCntPtr< vfs::FileSystem > createCollectorVFS(IntrusiveRefCntPtr< vfs::FileSystem > BaseFS, std::shared_ptr< FileCollector > Collector)
Create a VFS that uses Collector to collect files accessed via BaseFS.
A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
iterator end()
Definition: StringMap.h:221
iterator find(StringRef Key)
Definition: StringMap.h:234
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
iterator begin() const
Definition: StringRef.h:111
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:470
static std::error_code SafelyCloseFileDescriptor(int FD)
Represents the result of a call to sys::fs::status().
Definition: FileSystem.h:226
The virtual file system interface.
void setCaseSensitivity(bool CaseSensitive)
void setOverlayDir(StringRef OverlayDirectory)
const std::vector< YAMLVFSEntry > & getMappings() const
void write(llvm::raw_ostream &OS)
void setUseExternalNames(bool UseExtNames)
An input iterator over the entries in a virtual path, similar to llvm::sys::fs::directory_iterator.
void make_absolute(const Twine &current_directory, SmallVectorImpl< char > &path)
Make path an absolute path.
Definition: Path.cpp:907
std::error_code real_path(const Twine &path, SmallVectorImpl< char > &output, bool expand_tilde=false)
Collapse all .
ErrorOr< perms > getPermissions(const Twine &Path)
Get file permissions.
Definition: Path.cpp:1152
std::error_code setLastAccessAndModificationTime(int FD, TimePoint<> AccessTime, TimePoint<> ModificationTime)
Set the file modification and access time.
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition: FileSystem.h:768
@ CD_OpenExisting
CD_OpenExisting - When opening a file:
Definition: FileSystem.h:741
std::error_code openFileForWrite(const Twine &Name, int &ResultFD, CreationDisposition Disp=CD_CreateAlways, OpenFlags Flags=OF_None, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
Definition: FileSystem.h:1063
std::error_code create_directories(const Twine &path, bool IgnoreExisting=true, perms Perms=owner_all|group_all)
Create all the non-existent directories in path.
Definition: Path.cpp:968
std::error_code copy_file(const Twine &From, const Twine &To)
Copy the contents of From to To.
Definition: Path.cpp:1017
std::error_code setPermissions(const Twine &Path, perms Permissions)
Set file permissions.
bool is_directory(const basic_file_status &status)
Does status represent a directory?
Definition: Path.cpp:1093
bool remove_dots(SmallVectorImpl< char > &path, bool remove_dot_dot=false, Style style=Style::native)
In-place remove any '.
Definition: Path.cpp:716
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:578
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
Definition: Path.cpp:468
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition: Path.cpp:672
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:457
StringRef relative_path(StringRef path, Style style=Style::native)
Get relative path.
Definition: Path.cpp:414
StringRef remove_leading_dotslash(StringRef path, Style style=Style::native)
Remove redundant leading "./" pieces and consecutive separators.
Definition: Path.cpp:704
IntrusiveRefCntPtr< FileSystem > getRealFileSystem()
Gets an vfs::FileSystem for the 'real' file system, as seen by the operating system.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1858
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858