aboutsummaryrefslogtreecommitdiff
path: root/lib/Archive
diff options
context:
space:
mode:
authorMichael J. Spencer <bigcheesegs@gmail.com>2011-01-15 21:43:37 +0000
committerMichael J. Spencer <bigcheesegs@gmail.com>2011-01-15 21:43:37 +0000
commit770772e831d1ff1dfbc0e94fb3212437e597d7a5 (patch)
tree50966268373286a7876d42cd9ba308a5a6dcec3b /lib/Archive
parent36852d3681ec239df82e1fd86d93a91a9f49e592 (diff)
Archive: Replace all internal uses of PathV1 with PathV2. The external API still uses PathV1.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123551 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Archive')
-rw-r--r--lib/Archive/ArchiveWriter.cpp72
1 files changed, 36 insertions, 36 deletions
diff --git a/lib/Archive/ArchiveWriter.cpp b/lib/Archive/ArchiveWriter.cpp
index 8fcc7aa29c..59fb7bdd3f 100644
--- a/lib/Archive/ArchiveWriter.cpp
+++ b/lib/Archive/ArchiveWriter.cpp
@@ -18,6 +18,7 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Process.h"
+#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/system_error.h"
#include <fstream>
@@ -27,7 +28,7 @@ using namespace llvm;
// Write an integer using variable bit rate encoding. This saves a few bytes
// per entry in the symbol table.
-static inline void writeInteger(unsigned num, std::ofstream& ARFile) {
+static inline void writeInteger(unsigned num, raw_ostream& ARFile) {
while (1) {
if (num < 0x80) { // done?
ARFile << (unsigned char)num;
@@ -201,14 +202,14 @@ Archive::addFileBefore(const sys::Path& filePath, iterator where,
bool
Archive::writeMember(
const ArchiveMember& member,
- std::ofstream& ARFile,
+ raw_ostream& ARFile,
bool CreateSymbolTable,
bool TruncateNames,
bool ShouldCompress,
std::string* ErrMsg
) {
- unsigned filepos = ARFile.tellp();
+ unsigned filepos = ARFile.tell();
filepos -= 8;
// Get the data and its size either from the
@@ -281,7 +282,7 @@ Archive::writeMember(
ARFile.write(data,fSize);
// Make sure the member is an even length
- if ((ARFile.tellp() & 1) == 1)
+ if ((ARFile.tell() & 1) == 1)
ARFile << ARFILE_PAD;
// Close the mapped file if it was opened
@@ -291,7 +292,7 @@ Archive::writeMember(
// Write out the LLVM symbol table as an archive member to the file.
void
-Archive::writeSymbolTable(std::ofstream& ARFile) {
+Archive::writeSymbolTable(raw_ostream& ARFile) {
// Construct the symbol table's header
ArchiveMemberHeader Hdr;
@@ -315,7 +316,7 @@ Archive::writeSymbolTable(std::ofstream& ARFile) {
#ifndef NDEBUG
// Save the starting position of the symbol tables data content.
- unsigned startpos = ARFile.tellp();
+ unsigned startpos = ARFile.tell();
#endif
// Write out the symbols sequentially
@@ -332,7 +333,7 @@ Archive::writeSymbolTable(std::ofstream& ARFile) {
#ifndef NDEBUG
// Now that we're done with the symbol table, get the ending file position
- unsigned endpos = ARFile.tellp();
+ unsigned endpos = ARFile.tell();
#endif
// Make sure that the amount we wrote is what we pre-computed. This is
@@ -361,25 +362,19 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
}
// Create a temporary file to store the archive in
- sys::Path TmpArchive = archPath;
- if (TmpArchive.createTemporaryFileOnDisk(ErrMsg))
+ SmallString<128> TempArchivePath;
+ int ArchFD;
+ if (error_code ec = sys::fs::unique_file("%%-%%-%%-%%" + archPath.str(),
+ ArchFD, TempArchivePath)) {
+ if (ErrMsg) *ErrMsg = ec.message();
return true;
+ }
// Make sure the temporary gets removed if we crash
- sys::RemoveFileOnSignal(TmpArchive);
+ sys::RemoveFileOnSignal(sys::Path(TempArchivePath.str()));
// Create archive file for output.
- std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
- std::ios::binary;
- std::ofstream ArchiveFile(TmpArchive.c_str(), io_mode);
-
- // Check for errors opening or creating archive file.
- if (!ArchiveFile.is_open() || ArchiveFile.bad()) {
- TmpArchive.eraseFromDisk();
- if (ErrMsg)
- *ErrMsg = "Error opening archive file: " + archPath.str();
- return true;
- }
+ raw_fd_ostream ArchiveFile(ArchFD, true);
// If we're creating a symbol table, reset it now
if (CreateSymbolTable) {
@@ -395,8 +390,9 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
for (MembersList::iterator I = begin(), E = end(); I != E; ++I) {
if (writeMember(*I, ArchiveFile, CreateSymbolTable,
TruncateNames, Compress, ErrMsg)) {
- TmpArchive.eraseFromDisk();
ArchiveFile.close();
+ bool existed;
+ sys::fs::remove(TempArchivePath.str(), existed);
return true;
}
}
@@ -411,12 +407,12 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
// ensure compatibility with other archivers we need to put the symbol
// table first in the file. Unfortunately, this means mapping the file
// we just wrote back in and copying it to the destination file.
- sys::Path FinalFilePath = archPath;
+ SmallString<128> TempArchiveWithSymbolTablePath;
// Map in the archive we just wrote.
{
OwningPtr<MemoryBuffer> arch;
- if (error_code ec = MemoryBuffer::getFile(TmpArchive.c_str(), arch)) {
+ if (error_code ec = MemoryBuffer::getFile(TempArchivePath.c_str(), arch)) {
if (ErrMsg)
*ErrMsg = ec.message();
return true;
@@ -425,17 +421,14 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
// Open another temporary file in order to avoid invalidating the
// mmapped data
- if (FinalFilePath.createTemporaryFileOnDisk(ErrMsg))
- return true;
- sys::RemoveFileOnSignal(FinalFilePath);
-
- std::ofstream FinalFile(FinalFilePath.c_str(), io_mode);
- if (!FinalFile.is_open() || FinalFile.bad()) {
- TmpArchive.eraseFromDisk();
- if (ErrMsg)
- *ErrMsg = "Error opening archive file: " + FinalFilePath.str();
+ if (error_code ec = sys::fs::unique_file("%%-%%-%%-%%" + archPath.str(),
+ ArchFD, TempArchiveWithSymbolTablePath)) {
+ if (ErrMsg) *ErrMsg = ec.message();
return true;
}
+ sys::RemoveFileOnSignal(sys::Path(TempArchiveWithSymbolTablePath.str()));
+
+ raw_fd_ostream FinalFile(ArchFD, true);
// Write the file magic number
FinalFile << ARFILE_MAGIC;
@@ -448,7 +441,8 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
if (foreignST) {
if (writeMember(*foreignST, FinalFile, false, false, false, ErrMsg)) {
FinalFile.close();
- TmpArchive.eraseFromDisk();
+ bool existed;
+ sys::fs::remove(TempArchiveWithSymbolTablePath.str(), existed);
return true;
}
}
@@ -466,8 +460,11 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
} // free arch.
// Move the final file over top of TmpArchive
- if (FinalFilePath.renamePathOnDisk(TmpArchive, ErrMsg))
+ if (error_code ec = sys::fs::rename(TempArchiveWithSymbolTablePath.str(),
+ TempArchivePath.str())) {
+ if (ErrMsg) *ErrMsg = ec.message();
return true;
+ }
}
// Before we replace the actual archive, we need to forget all the
@@ -475,8 +472,11 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
// this because we cannot replace an open file on Windows.
cleanUpMemory();
- if (TmpArchive.renamePathOnDisk(archPath, ErrMsg))
+ if (error_code ec = sys::fs::rename(TempArchivePath.str(),
+ archPath.str())) {
+ if (ErrMsg) *ErrMsg = ec.message();
return true;
+ }
// Set correct read and write permissions after temporary file is moved
// to final destination path.