[PATCH] D40606: [Support/TarWriter] - Don't allow TarWriter to add the same file more than once.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 29 08:00:33 PST 2017


grimar created this revision.

This is WIP patch for PR35460.

Currently when LLD adds files to TarWriter it may pass the same file multiple times.
For example it happens for clang reproduce file which specifies archive (.a) files more
than once in command line. Though that can be catched on LLD side, I think it is reasonable
to restrict TarWriter to add the same file more than once, I believe current behavior does not
make much sence.

Patch does not yet have testcases and I am posting it to hear opinions first. 
I'll update it if we deside this approach is fine.


https://reviews.llvm.org/D40606

Files:
  include/llvm/Support/TarWriter.h
  lib/Support/TarWriter.cpp


Index: lib/Support/TarWriter.cpp
===================================================================
--- lib/Support/TarWriter.cpp
+++ lib/Support/TarWriter.cpp
@@ -169,10 +169,15 @@
     : OS(FD, /*shouldClose=*/true, /*unbuffered=*/false), BaseDir(BaseDir) {}
 
 // Append a given file to an archive.
-void TarWriter::append(StringRef Path, StringRef Data) {
+bool TarWriter::append(StringRef Path, StringRef Data) {
   // Write Path and Data.
   std::string Fullpath = BaseDir + "/" + sys::path::convert_to_slash(Path);
 
+  bool Exist;
+  std::tie(std::ignore, Exist) = Files.insert(Fullpath);
+  if (Exist)
+    return false;
+
   StringRef Prefix;
   StringRef Name;
   if (splitUstar(Fullpath, Prefix, Name)) {
@@ -192,4 +197,6 @@
   OS << std::string(BlockSize * 2, '\0');
   OS.seek(Pos);
   OS.flush();
+
+  return true;
 }
Index: include/llvm/Support/TarWriter.h
===================================================================
--- include/llvm/Support/TarWriter.h
+++ include/llvm/Support/TarWriter.h
@@ -13,19 +13,21 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/raw_ostream.h"
+#include <set>
 
 namespace llvm {
 class TarWriter {
 public:
   static Expected<std::unique_ptr<TarWriter>> create(StringRef OutputPath,
                                                      StringRef BaseDir);
 
-  void append(StringRef Path, StringRef Data);
+  bool append(StringRef Path, StringRef Data);
 
 private:
   TarWriter(int FD, StringRef BaseDir);
   raw_fd_ostream OS;
   std::string BaseDir;
+  std::set<std::string> Files;
 };
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40606.124755.patch
Type: text/x-patch
Size: 1593 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171129/19bfee51/attachment.bin>


More information about the llvm-commits mailing list