summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--llvm/include/llvm/Object/ArchiveWriter.h2
-rw-r--r--llvm/lib/Object/ArchiveWriter.cpp16
-rw-r--r--llvm/tools/llvm-ar/llvm-ar.cpp20
3 files changed, 29 insertions, 9 deletions
diff --git a/llvm/include/llvm/Object/ArchiveWriter.h b/llvm/include/llvm/Object/ArchiveWriter.h
index b5d2ba35808..a3cc64b5216 100644
--- a/llvm/include/llvm/Object/ArchiveWriter.h
+++ b/llvm/include/llvm/Object/ArchiveWriter.h
@@ -42,7 +42,7 @@ public:
std::pair<StringRef, std::error_code>
writeArchive(StringRef ArcName, std::vector<NewArchiveIterator> &NewMembers,
bool WriteSymtab, object::Archive::Kind Kind, bool Deterministic,
- bool Thin);
+ bool Thin, std::unique_ptr<MemoryBuffer> OldArchiveBuf = nullptr);
}
#endif
diff --git a/llvm/lib/Object/ArchiveWriter.cpp b/llvm/lib/Object/ArchiveWriter.cpp
index 1ae14f24178..93e285cf674 100644
--- a/llvm/lib/Object/ArchiveWriter.cpp
+++ b/llvm/lib/Object/ArchiveWriter.cpp
@@ -307,7 +307,8 @@ std::pair<StringRef, std::error_code>
llvm::writeArchive(StringRef ArcName,
std::vector<NewArchiveIterator> &NewMembers,
bool WriteSymtab, object::Archive::Kind Kind,
- bool Deterministic, bool Thin) {
+ bool Deterministic, bool Thin,
+ std::unique_ptr<MemoryBuffer> OldArchiveBuf) {
assert((!Thin || Kind == object::Archive::K_GNU) &&
"Only the gnu format has a thin mode");
SmallString<128> TmpArchive;
@@ -443,6 +444,19 @@ llvm::writeArchive(StringRef ArcName,
Output.keep();
Out.close();
+
+ // At this point, we no longer need whatever backing memory
+ // was used to generate the NewMembers. On Windows, this buffer
+ // could be a mapped view of the file we want to replace (if
+ // we're updating an existing archive, say). In that case, the
+ // rename would still succeed, but it would leave behind a
+ // temporary file (actually the original file renamed) because
+ // a file cannot be deleted while there's a handle open on it,
+ // only renamed. So by freeing this buffer, this ensures that
+ // the last open handle on the destination file, if any, is
+ // closed before we attempt to rename.
+ OldArchiveBuf.reset();
+
sys::fs::rename(TmpArchive, ArcName);
return std::make_pair("", std::error_code());
}
diff --git a/llvm/tools/llvm-ar/llvm-ar.cpp b/llvm/tools/llvm-ar/llvm-ar.cpp
index 45f778b9af6..375b34d9b41 100644
--- a/llvm/tools/llvm-ar/llvm-ar.cpp
+++ b/llvm/tools/llvm-ar/llvm-ar.cpp
@@ -576,7 +576,9 @@ computeNewArchiveMembers(ArchiveOperation Operation,
}
static void
-performWriteOperation(ArchiveOperation Operation, object::Archive *OldArchive,
+performWriteOperation(ArchiveOperation Operation,
+ object::Archive *OldArchive,
+ std::unique_ptr<MemoryBuffer> OldArchiveBuf,
std::vector<NewArchiveIterator> *NewMembersP) {
object::Archive::Kind Kind;
switch (FormatOpt) {
@@ -599,14 +601,16 @@ performWriteOperation(ArchiveOperation Operation, object::Archive *OldArchive,
}
if (NewMembersP) {
std::pair<StringRef, std::error_code> Result = writeArchive(
- ArchiveName, *NewMembersP, Symtab, Kind, Deterministic, Thin);
+ ArchiveName, *NewMembersP, Symtab, Kind, Deterministic, Thin,
+ std::move(OldArchiveBuf));
failIfError(Result.second, Result.first);
return;
}
std::vector<NewArchiveIterator> NewMembers =
computeNewArchiveMembers(Operation, OldArchive);
auto Result =
- writeArchive(ArchiveName, NewMembers, Symtab, Kind, Deterministic, Thin);
+ writeArchive(ArchiveName, NewMembers, Symtab, Kind, Deterministic, Thin,
+ std::move(OldArchiveBuf));
failIfError(Result.second, Result.first);
}
@@ -620,11 +624,12 @@ static void createSymbolTable(object::Archive *OldArchive) {
if (OldArchive->hasSymbolTable())
return;
- performWriteOperation(CreateSymTab, OldArchive, nullptr);
+ performWriteOperation(CreateSymTab, OldArchive, nullptr, nullptr);
}
static void performOperation(ArchiveOperation Operation,
object::Archive *OldArchive,
+ std::unique_ptr<MemoryBuffer> OldArchiveBuf,
std::vector<NewArchiveIterator> *NewMembers) {
switch (Operation) {
case Print:
@@ -637,7 +642,8 @@ static void performOperation(ArchiveOperation Operation,
case Move:
case QuickAppend:
case ReplaceOrInsert:
- performWriteOperation(Operation, OldArchive, NewMembers);
+ performWriteOperation(Operation, OldArchive, std::move(OldArchiveBuf),
+ NewMembers);
return;
case CreateSymTab:
createSymbolTable(OldArchive);
@@ -659,7 +665,7 @@ static int performOperation(ArchiveOperation Operation,
object::Archive Archive(Buf.get()->getMemBufferRef(), EC);
failIfError(EC,
"error loading '" + ArchiveName + "': " + EC.message() + "!");
- performOperation(Operation, &Archive, NewMembers);
+ performOperation(Operation, &Archive, std::move(Buf.get()), NewMembers);
return 0;
}
@@ -674,7 +680,7 @@ static int performOperation(ArchiveOperation Operation,
}
}
- performOperation(Operation, nullptr, NewMembers);
+ performOperation(Operation, nullptr, nullptr, NewMembers);
return 0;
}
OpenPOWER on IntegriCloud