[PATCH] D65297: [FileCollector] add support for recording empty directories

Alex Lorenz via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 25 13:07:41 PDT 2019


arphaman created this revision.
arphaman added reviewers: JDevlieghere, bruno.
Herald added subscribers: dexonsmith, jkorous, hiraditya.
Herald added a project: LLVM.

The file collector class is useful for constructing reproducers by creating a snapshot of the files that are accessed. Sometimes it might also be important to construct directories that don't necessarily have files, but are still accessed by some tool that we want to make a reproducer for. This is useful for instance for modeling the behavior of Clang's header search, which scans through a number of directories it doesn't actually access when looking for framework headers. This patch extends the file collector to allow it to work with paths that are just directories, by constructing them as the files are copied over.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D65297

Files:
  llvm/lib/Support/FileCollector.cpp
  llvm/unittests/Support/FileCollectorTest.cpp


Index: llvm/unittests/Support/FileCollectorTest.cpp
===================================================================
--- llvm/unittests/Support/FileCollectorTest.cpp
+++ llvm/unittests/Support/FileCollectorTest.cpp
@@ -148,6 +148,37 @@
   EXPECT_FALSE(ec);
 }
 
+TEST(FileCollectorTest, recordAndConstructDirectory) {
+  ScopedDir file_root("dir_root", true);
+  ScopedDir subdir(file_root + "/subdir");
+  ScopedDir subdir2(file_root + "/subdir2");
+  ScopedFile a(subdir2 + "/a");
+
+  // Create file collector and add files.
+  ScopedDir root("copy_files_root", true);
+  std::string root_fs = root.Path.str();
+  TestingFileCollector FileCollector(root_fs, root_fs);
+  FileCollector.addFile(a.Path);
+
+  // The empty directory isn't seen until we add it.
+  EXPECT_TRUE(FileCollector.hasSeen(a.Path));
+  EXPECT_FALSE(FileCollector.hasSeen(subdir.Path));
+
+  FileCollector.addFile(subdir.Path);
+  EXPECT_TRUE(FileCollector.hasSeen(subdir.Path));
+
+  // Make sure we can construct the directory.
+  std::error_code ec = FileCollector.copyFiles(true);
+  EXPECT_FALSE(ec);
+  bool IsDirectory = false;
+  llvm::SmallString<128> SubdirInRoot = root.Path;
+  llvm::sys::path::append(SubdirInRoot,
+                          llvm::sys::path::relative_path(subdir.Path));
+  ec = sys::fs::is_directory(SubdirInRoot, IsDirectory);
+  EXPECT_FALSE(ec);
+  ASSERT_TRUE(IsDirectory);
+}
+
 #ifndef _WIN32
 TEST(FileCollectorTest, Symlinks) {
   // Root where the original files live.
Index: llvm/lib/Support/FileCollector.cpp
===================================================================
--- llvm/lib/Support/FileCollector.cpp
+++ llvm/lib/Support/FileCollector.cpp
@@ -132,6 +132,26 @@
         return EC;
     }
 
+    // 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;
+    }
+
+    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;
+    }
+
     // Copy file over.
     if (std::error_code EC = sys::fs::copy_file(entry.VPath, entry.RPath)) {
       if (StopOnError)
@@ -147,12 +167,6 @@
     }
 
     // Copy over modification time.
-    sys::fs::file_status Stat;
-    if (std::error_code EC = sys::fs::status(entry.VPath, Stat)) {
-      if (StopOnError)
-        return EC;
-      continue;
-    }
     copyAccessAndModificationTime(entry.RPath, Stat);
   }
   return {};


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65297.211804.patch
Type: text/x-patch
Size: 2756 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190725/ebd3a2c7/attachment.bin>


More information about the llvm-commits mailing list