summaryrefslogtreecommitdiff
path: root/llvm/lib/Support/FileCollector.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2020-07-26 19:36:28 +0000
committerDimitry Andric <dim@FreeBSD.org>2020-07-26 19:36:28 +0000
commitcfca06d7963fa0909f90483b42a6d7d194d01e08 (patch)
tree209fb2a2d68f8f277793fc8df46c753d31bc853b /llvm/lib/Support/FileCollector.cpp
parent706b4fc47bbc608932d3b491ae19a3b9cde9497b (diff)
Notes
Diffstat (limited to 'llvm/lib/Support/FileCollector.cpp')
-rw-r--r--llvm/lib/Support/FileCollector.cpp67
1 files changed, 43 insertions, 24 deletions
diff --git a/llvm/lib/Support/FileCollector.cpp b/llvm/lib/Support/FileCollector.cpp
index 47fca64137223..59755556a5a3a 100644
--- a/llvm/lib/Support/FileCollector.cpp
+++ b/llvm/lib/Support/FileCollector.cpp
@@ -8,6 +8,7 @@
#include "llvm/Support/FileCollector.h"
#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/Twine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
@@ -34,7 +35,6 @@ static bool isCaseSensitivePath(StringRef Path) {
FileCollector::FileCollector(std::string Root, std::string OverlayRoot)
: Root(std::move(Root)), OverlayRoot(std::move(OverlayRoot)) {
- sys::fs::create_directories(this->Root, true);
}
bool FileCollector::getRealPath(StringRef SrcPath,
@@ -51,7 +51,7 @@ bool FileCollector::getRealPath(StringRef SrcPath,
auto EC = sys::fs::real_path(Directory, RealPath);
if (EC)
return false;
- SymlinkMap[Directory] = RealPath.str();
+ SymlinkMap[Directory] = std::string(RealPath.str());
} else {
RealPath = DirWithSymlink->second;
}
@@ -61,13 +61,19 @@ bool FileCollector::getRealPath(StringRef SrcPath,
return true;
}
-void FileCollector::addFile(const Twine &file) {
+void FileCollector::addFile(const Twine &File) {
std::lock_guard<std::mutex> lock(Mutex);
- std::string FileStr = file.str();
+ std::string FileStr = File.str();
if (markAsSeen(FileStr))
addFileImpl(FileStr);
}
+void FileCollector::addDirectory(const Twine &Dir) {
+ assert(sys::fs::is_directory(Dir));
+ std::error_code EC;
+ addDirectoryImpl(Dir, vfs::getRealFileSystem(), EC);
+}
+
void FileCollector::addFileImpl(StringRef SrcPath) {
// We need an absolute src path to append to the root.
SmallString<256> AbsoluteSrc = SrcPath;
@@ -101,6 +107,27 @@ void FileCollector::addFileImpl(StringRef SrcPath) {
addFileToMapping(VirtualPath, DstPath);
}
+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);
+}
+
/// Set the access and modification time for the given file from the given
/// status object.
static std::error_code
@@ -123,6 +150,13 @@ copyAccessAndModificationTime(StringRef Filename,
}
std::error_code FileCollector::copyFiles(bool StopOnError) {
+ auto Err = sys::fs::create_directories(Root, /*IgnoreExisting=*/true);
+ if (Err) {
+ return Err;
+ }
+
+ std::lock_guard<std::mutex> lock(Mutex);
+
for (auto &entry : VFSWriter.getMappings()) {
// Create directory tree.
if (std::error_code EC =
@@ -171,7 +205,7 @@ std::error_code FileCollector::copyFiles(bool StopOnError) {
return {};
}
-std::error_code FileCollector::writeMapping(StringRef mapping_file) {
+std::error_code FileCollector::writeMapping(StringRef MappingFile) {
std::lock_guard<std::mutex> lock(Mutex);
VFSWriter.setOverlayDir(OverlayRoot);
@@ -179,7 +213,7 @@ std::error_code FileCollector::writeMapping(StringRef mapping_file) {
VFSWriter.setUseExternalNames(false);
std::error_code EC;
- raw_fd_ostream os(mapping_file, EC, sys::fs::OF_Text);
+ raw_fd_ostream os(MappingFile, EC, sys::fs::OF_Text);
if (EC)
return EC;
@@ -188,7 +222,7 @@ std::error_code FileCollector::writeMapping(StringRef mapping_file) {
return {};
}
-namespace {
+namespace llvm {
class FileCollectorFileSystem : public vfs::FileSystem {
public:
@@ -213,22 +247,7 @@ public:
llvm::vfs::directory_iterator dir_begin(const llvm::Twine &Dir,
std::error_code &EC) override {
- auto It = FS->dir_begin(Dir, EC);
- if (EC)
- return It;
- // Collect everything that's listed in case the user needs it.
- Collector->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) {
- Collector->addFile(It->path());
- }
- }
- if (EC)
- return It;
- // Return a new iterator.
- return FS->dir_begin(Dir, EC);
+ return Collector->addDirectoryImpl(Dir, FS, EC);
}
std::error_code getRealPath(const Twine &Path,
@@ -259,7 +278,7 @@ private:
std::shared_ptr<FileCollector> Collector;
};
-} // end anonymous namespace
+} // namespace llvm
IntrusiveRefCntPtr<vfs::FileSystem>
FileCollector::createCollectorVFS(IntrusiveRefCntPtr<vfs::FileSystem> BaseFS,