2019-01-30 04:36:38 +08:00
|
|
|
//===-- FileCollector.cpp ---------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-07-25 06:59:20 +08:00
|
|
|
#include "llvm/Support/FileCollector.h"
|
2019-01-30 04:36:38 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2020-06-27 18:16:25 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2019-01-30 04:36:38 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
2019-06-27 02:14:31 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2019-01-30 04:36:38 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2020-10-20 05:29:59 +08:00
|
|
|
FileCollectorBase::FileCollectorBase() = default;
|
|
|
|
FileCollectorBase::~FileCollectorBase() = default;
|
|
|
|
|
|
|
|
void FileCollectorBase::addFile(const Twine &File) {
|
|
|
|
std::lock_guard<std::mutex> lock(Mutex);
|
|
|
|
std::string FileStr = File.str();
|
|
|
|
if (markAsSeen(FileStr))
|
|
|
|
addFileImpl(FileStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileCollectorBase::addDirectory(const Twine &Dir) {
|
|
|
|
assert(sys::fs::is_directory(Dir));
|
|
|
|
std::error_code EC;
|
|
|
|
addDirectoryImpl(Dir, vfs::getRealFileSystem(), EC);
|
|
|
|
}
|
|
|
|
|
2019-07-25 08:17:39 +08:00
|
|
|
static bool isCaseSensitivePath(StringRef Path) {
|
|
|
|
SmallString<256> TmpDest = Path, UpperDest, RealDest;
|
2019-01-30 04:36:38 +08:00
|
|
|
|
|
|
|
// Remove component traversals, links, etc.
|
2019-07-25 08:17:39 +08:00
|
|
|
if (!sys::fs::real_path(Path, TmpDest))
|
2019-01-30 04:36:38 +08:00
|
|
|
return true; // Current default value in vfs.yaml
|
2019-07-25 08:17:39 +08:00
|
|
|
Path = TmpDest;
|
2019-01-30 04:36:38 +08:00
|
|
|
|
|
|
|
// Change path to all upper case and ask for its real path, if the latter
|
|
|
|
// exists and is equal to path, it's not case sensitive. Default to case
|
|
|
|
// sensitive in the absence of real_path, since this is the YAMLVFSWriter
|
|
|
|
// default.
|
2019-07-25 08:17:39 +08:00
|
|
|
UpperDest = Path.upper();
|
|
|
|
if (sys::fs::real_path(UpperDest, RealDest) && Path.equals(RealDest))
|
2019-01-30 04:36:38 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-25 08:17:39 +08:00
|
|
|
FileCollector::FileCollector(std::string Root, std::string OverlayRoot)
|
|
|
|
: Root(std::move(Root)), OverlayRoot(std::move(OverlayRoot)) {
|
2019-01-30 04:36:38 +08:00
|
|
|
}
|
|
|
|
|
2019-07-25 08:17:39 +08:00
|
|
|
bool FileCollector::getRealPath(StringRef SrcPath,
|
|
|
|
SmallVectorImpl<char> &Result) {
|
|
|
|
SmallString<256> RealPath;
|
|
|
|
StringRef FileName = sys::path::filename(SrcPath);
|
|
|
|
std::string Directory = sys::path::parent_path(SrcPath).str();
|
|
|
|
auto DirWithSymlink = SymlinkMap.find(Directory);
|
2019-01-30 04:36:38 +08:00
|
|
|
|
|
|
|
// Use real_path to fix any symbolic link component present in a path.
|
2019-07-25 08:17:39 +08:00
|
|
|
// Computing the real path is expensive, cache the search through the parent
|
|
|
|
// path Directory.
|
|
|
|
if (DirWithSymlink == SymlinkMap.end()) {
|
|
|
|
auto EC = sys::fs::real_path(Directory, RealPath);
|
|
|
|
if (EC)
|
2019-01-30 04:36:38 +08:00
|
|
|
return false;
|
2020-01-29 03:23:46 +08:00
|
|
|
SymlinkMap[Directory] = std::string(RealPath.str());
|
2019-01-30 04:36:38 +08:00
|
|
|
} else {
|
2019-07-25 08:17:39 +08:00
|
|
|
RealPath = DirWithSymlink->second;
|
2019-01-30 04:36:38 +08:00
|
|
|
}
|
|
|
|
|
2019-07-25 08:17:39 +08:00
|
|
|
sys::path::append(RealPath, FileName);
|
|
|
|
Result.swap(RealPath);
|
2019-01-30 04:36:38 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-25 08:17:39 +08:00
|
|
|
void FileCollector::addFileImpl(StringRef SrcPath) {
|
2019-01-30 04:36:38 +08:00
|
|
|
// We need an absolute src path to append to the root.
|
2019-07-25 08:17:39 +08:00
|
|
|
SmallString<256> AbsoluteSrc = SrcPath;
|
|
|
|
sys::fs::make_absolute(AbsoluteSrc);
|
2019-01-30 04:36:38 +08:00
|
|
|
|
|
|
|
// Canonicalize src to a native path to avoid mixed separator styles.
|
2019-07-25 08:17:39 +08:00
|
|
|
sys::path::native(AbsoluteSrc);
|
2019-01-30 04:36:38 +08:00
|
|
|
|
|
|
|
// Remove redundant leading "./" pieces and consecutive separators.
|
2019-07-25 08:17:39 +08:00
|
|
|
AbsoluteSrc = sys::path::remove_leading_dotslash(AbsoluteSrc);
|
2019-01-30 04:36:38 +08:00
|
|
|
|
|
|
|
// Canonicalize the source path by removing "..", "." components.
|
2019-07-25 08:17:39 +08:00
|
|
|
SmallString<256> VirtualPath = AbsoluteSrc;
|
|
|
|
sys::path::remove_dots(VirtualPath, /*remove_dot_dot=*/true);
|
2019-01-30 04:36:38 +08:00
|
|
|
|
|
|
|
// If a ".." component is present after a symlink component, remove_dots may
|
|
|
|
// lead to the wrong real destination path. Let the source be canonicalized
|
|
|
|
// like that but make sure we always use the real path for the destination.
|
2019-07-25 08:17:39 +08:00
|
|
|
SmallString<256> CopyFrom;
|
|
|
|
if (!getRealPath(AbsoluteSrc, CopyFrom))
|
|
|
|
CopyFrom = VirtualPath;
|
2019-01-30 04:36:38 +08:00
|
|
|
|
2019-07-25 08:17:39 +08:00
|
|
|
SmallString<256> DstPath = StringRef(Root);
|
|
|
|
sys::path::append(DstPath, sys::path::relative_path(CopyFrom));
|
2019-01-30 04:36:38 +08:00
|
|
|
|
|
|
|
// Always map a canonical src path to its real path into the YAML, by doing
|
|
|
|
// this we map different virtual src paths to the same entry in the VFS
|
|
|
|
// overlay, which is a way to emulate symlink inside the VFS; this is also
|
|
|
|
// needed for correctness, not doing that can lead to module redefinition
|
|
|
|
// errors.
|
2019-07-25 08:17:39 +08:00
|
|
|
addFileToMapping(VirtualPath, DstPath);
|
2019-01-30 04:36:38 +08:00
|
|
|
}
|
|
|
|
|
2020-03-31 03:58:21 +08:00
|
|
|
llvm::vfs::directory_iterator
|
|
|
|
FileCollector::addDirectoryImpl(const llvm::Twine &Dir,
|
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> FS,
|
|
|
|
std::error_code &EC) {
|
|
|
|
auto It = FS->dir_begin(Dir, EC);
|
|
|
|
if (EC)
|
|
|
|
return It;
|
|
|
|
addFile(Dir);
|
|
|
|
for (; !EC && It != llvm::vfs::directory_iterator(); It.increment(EC)) {
|
|
|
|
if (It->type() == sys::fs::file_type::regular_file ||
|
|
|
|
It->type() == sys::fs::file_type::directory_file ||
|
|
|
|
It->type() == sys::fs::file_type::symlink_file) {
|
|
|
|
addFile(It->path());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (EC)
|
|
|
|
return It;
|
|
|
|
// Return a new iterator.
|
|
|
|
return FS->dir_begin(Dir, EC);
|
|
|
|
}
|
|
|
|
|
2019-06-27 02:14:31 +08:00
|
|
|
/// Set the access and modification time for the given file from the given
|
|
|
|
/// status object.
|
|
|
|
static std::error_code
|
2019-07-25 08:17:39 +08:00
|
|
|
copyAccessAndModificationTime(StringRef Filename,
|
|
|
|
const sys::fs::file_status &Stat) {
|
|
|
|
int FD;
|
2019-06-27 02:14:31 +08:00
|
|
|
|
2019-07-25 08:17:39 +08:00
|
|
|
if (auto EC =
|
|
|
|
sys::fs::openFileForWrite(Filename, FD, sys::fs::CD_OpenExisting))
|
|
|
|
return EC;
|
2019-06-27 02:14:31 +08:00
|
|
|
|
2019-07-25 08:17:39 +08:00
|
|
|
if (auto EC = sys::fs::setLastAccessAndModificationTime(
|
|
|
|
FD, Stat.getLastAccessedTime(), Stat.getLastModificationTime()))
|
|
|
|
return EC;
|
2019-06-27 02:14:31 +08:00
|
|
|
|
2019-07-25 08:17:39 +08:00
|
|
|
if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
|
|
|
|
return EC;
|
2019-06-27 02:14:31 +08:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-07-25 08:17:39 +08:00
|
|
|
std::error_code FileCollector::copyFiles(bool StopOnError) {
|
2020-04-30 02:44:40 +08:00
|
|
|
auto Err = sys::fs::create_directories(Root, /*IgnoreExisting=*/true);
|
|
|
|
if (Err) {
|
|
|
|
return Err;
|
|
|
|
}
|
|
|
|
|
2020-04-29 01:43:17 +08:00
|
|
|
std::lock_guard<std::mutex> lock(Mutex);
|
|
|
|
|
2019-07-25 08:17:39 +08:00
|
|
|
for (auto &entry : VFSWriter.getMappings()) {
|
2019-07-26 05:47:11 +08:00
|
|
|
// Get the status of the original file/directory.
|
|
|
|
sys::fs::file_status Stat;
|
|
|
|
if (std::error_code EC = sys::fs::status(entry.VPath, Stat)) {
|
|
|
|
if (StopOnError)
|
|
|
|
return EC;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-18 02:13:11 +08:00
|
|
|
// Continue if the file doesn't exist.
|
|
|
|
if (Stat.type() == sys::fs::file_type::file_not_found)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Create directory tree.
|
|
|
|
if (std::error_code EC =
|
|
|
|
sys::fs::create_directories(sys::path::parent_path(entry.RPath),
|
|
|
|
/*IgnoreExisting=*/true)) {
|
|
|
|
if (StopOnError)
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
|
2019-07-26 05:47:11 +08:00
|
|
|
if (Stat.type() == sys::fs::file_type::directory_file) {
|
|
|
|
// Construct a directory when it's just a directory entry.
|
|
|
|
if (std::error_code EC =
|
|
|
|
sys::fs::create_directories(entry.RPath,
|
|
|
|
/*IgnoreExisting=*/true)) {
|
|
|
|
if (StopOnError)
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-30 04:36:38 +08:00
|
|
|
// Copy file over.
|
2019-07-25 08:17:39 +08:00
|
|
|
if (std::error_code EC = sys::fs::copy_file(entry.VPath, entry.RPath)) {
|
|
|
|
if (StopOnError)
|
|
|
|
return EC;
|
2019-01-30 04:36:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy over permissions.
|
|
|
|
if (auto perms = sys::fs::getPermissions(entry.VPath)) {
|
2019-07-25 08:17:39 +08:00
|
|
|
if (std::error_code EC = sys::fs::setPermissions(entry.RPath, *perms)) {
|
|
|
|
if (StopOnError)
|
|
|
|
return EC;
|
2019-01-30 04:36:38 +08:00
|
|
|
}
|
|
|
|
}
|
2019-06-27 02:14:31 +08:00
|
|
|
|
|
|
|
// Copy over modification time.
|
2019-07-25 08:17:39 +08:00
|
|
|
copyAccessAndModificationTime(entry.RPath, Stat);
|
2019-01-30 04:36:38 +08:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-03-31 03:58:21 +08:00
|
|
|
std::error_code FileCollector::writeMapping(StringRef MappingFile) {
|
2019-07-25 08:17:39 +08:00
|
|
|
std::lock_guard<std::mutex> lock(Mutex);
|
2019-01-30 04:36:38 +08:00
|
|
|
|
2019-07-25 08:17:39 +08:00
|
|
|
VFSWriter.setOverlayDir(OverlayRoot);
|
|
|
|
VFSWriter.setCaseSensitivity(isCaseSensitivePath(OverlayRoot));
|
|
|
|
VFSWriter.setUseExternalNames(false);
|
2019-01-30 04:36:38 +08:00
|
|
|
|
2019-07-25 08:17:39 +08:00
|
|
|
std::error_code EC;
|
2020-03-31 03:58:21 +08:00
|
|
|
raw_fd_ostream os(MappingFile, EC, sys::fs::OF_Text);
|
2019-07-25 08:17:39 +08:00
|
|
|
if (EC)
|
|
|
|
return EC;
|
2019-01-30 04:36:38 +08:00
|
|
|
|
2019-07-25 08:17:39 +08:00
|
|
|
VFSWriter.write(os);
|
2019-01-30 04:36:38 +08:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
2019-07-30 07:38:30 +08:00
|
|
|
|
2020-03-31 03:58:21 +08:00
|
|
|
namespace llvm {
|
2019-07-30 07:38:30 +08:00
|
|
|
|
|
|
|
class FileCollectorFileSystem : public vfs::FileSystem {
|
|
|
|
public:
|
|
|
|
explicit FileCollectorFileSystem(IntrusiveRefCntPtr<vfs::FileSystem> FS,
|
|
|
|
std::shared_ptr<FileCollector> Collector)
|
|
|
|
: FS(std::move(FS)), Collector(std::move(Collector)) {}
|
|
|
|
|
|
|
|
llvm::ErrorOr<llvm::vfs::Status> status(const Twine &Path) override {
|
|
|
|
auto Result = FS->status(Path);
|
|
|
|
if (Result && Result->exists())
|
|
|
|
Collector->addFile(Path);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
|
|
|
|
openFileForRead(const Twine &Path) override {
|
|
|
|
auto Result = FS->openFileForRead(Path);
|
|
|
|
if (Result && *Result)
|
|
|
|
Collector->addFile(Path);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::vfs::directory_iterator dir_begin(const llvm::Twine &Dir,
|
|
|
|
std::error_code &EC) override {
|
2020-03-31 03:58:21 +08:00
|
|
|
return Collector->addDirectoryImpl(Dir, FS, EC);
|
2019-07-30 07:38:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code getRealPath(const Twine &Path,
|
|
|
|
SmallVectorImpl<char> &Output) const override {
|
|
|
|
auto EC = FS->getRealPath(Path, Output);
|
|
|
|
if (!EC) {
|
|
|
|
Collector->addFile(Path);
|
|
|
|
if (Output.size() > 0)
|
|
|
|
Collector->addFile(Output);
|
|
|
|
}
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code isLocal(const Twine &Path, bool &Result) override {
|
|
|
|
return FS->isLocal(Path, Result);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
|
|
|
|
return FS->getCurrentWorkingDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code setCurrentWorkingDirectory(const llvm::Twine &Path) override {
|
|
|
|
return FS->setCurrentWorkingDirectory(Path);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> FS;
|
|
|
|
std::shared_ptr<FileCollector> Collector;
|
|
|
|
};
|
|
|
|
|
2020-03-31 03:58:21 +08:00
|
|
|
} // namespace llvm
|
2019-07-30 07:38:30 +08:00
|
|
|
|
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem>
|
|
|
|
FileCollector::createCollectorVFS(IntrusiveRefCntPtr<vfs::FileSystem> BaseFS,
|
|
|
|
std::shared_ptr<FileCollector> Collector) {
|
|
|
|
return new FileCollectorFileSystem(std::move(BaseFS), std::move(Collector));
|
|
|
|
}
|