Revert PathV2 changes, as sys::fs::unique_file is not finished yet.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@126773 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dan Gohman 2011-03-01 19:50:55 +00:00
parent bdcd766028
commit 898fd7fd61
2 changed files with 38 additions and 41 deletions

View File

@ -25,7 +25,6 @@
namespace llvm { namespace llvm {
class MemoryBuffer; class MemoryBuffer;
class raw_ostream;
// Forward declare classes // Forward declare classes
class Module; // From VMCore class Module; // From VMCore
@ -483,7 +482,7 @@ class Archive {
bool loadSymbolTable(std::string* ErrMessage); bool loadSymbolTable(std::string* ErrMessage);
/// @brief Write the symbol table to an ofstream. /// @brief Write the symbol table to an ofstream.
void writeSymbolTable(raw_ostream& ARFile); void writeSymbolTable(std::ofstream& ARFile);
/// Writes one ArchiveMember to an ofstream. If an error occurs, returns /// Writes one ArchiveMember to an ofstream. If an error occurs, returns
/// false, otherwise true. If an error occurs and error is non-null then /// false, otherwise true. If an error occurs and error is non-null then
@ -492,7 +491,7 @@ class Archive {
/// @returns true Writing member failed, \p error set to error message /// @returns true Writing member failed, \p error set to error message
bool writeMember( bool writeMember(
const ArchiveMember& member, ///< The member to be written const ArchiveMember& member, ///< The member to be written
raw_ostream& ARFile, ///< The file to write member onto std::ofstream& ARFile, ///< The file to write member onto
bool CreateSymbolTable, ///< Should symbol table be created? bool CreateSymbolTable, ///< Should symbol table be created?
bool TruncateNames, ///< Should names be truncated to 11 chars? bool TruncateNames, ///< Should names be truncated to 11 chars?
bool ShouldCompress, ///< Should the member be compressed? bool ShouldCompress, ///< Should the member be compressed?

View File

@ -18,7 +18,6 @@
#include "llvm/Support/FileSystem.h" #include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Process.h" #include "llvm/Support/Process.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Signals.h" #include "llvm/Support/Signals.h"
#include "llvm/Support/system_error.h" #include "llvm/Support/system_error.h"
#include <fstream> #include <fstream>
@ -28,7 +27,7 @@ using namespace llvm;
// Write an integer using variable bit rate encoding. This saves a few bytes // Write an integer using variable bit rate encoding. This saves a few bytes
// per entry in the symbol table. // per entry in the symbol table.
static inline void writeInteger(unsigned num, raw_ostream& ARFile) { static inline void writeInteger(unsigned num, std::ofstream& ARFile) {
while (1) { while (1) {
if (num < 0x80) { // done? if (num < 0x80) { // done?
ARFile << (unsigned char)num; ARFile << (unsigned char)num;
@ -202,14 +201,14 @@ Archive::addFileBefore(const sys::Path& filePath, iterator where,
bool bool
Archive::writeMember( Archive::writeMember(
const ArchiveMember& member, const ArchiveMember& member,
raw_ostream& ARFile, std::ofstream& ARFile,
bool CreateSymbolTable, bool CreateSymbolTable,
bool TruncateNames, bool TruncateNames,
bool ShouldCompress, bool ShouldCompress,
std::string* ErrMsg std::string* ErrMsg
) { ) {
unsigned filepos = ARFile.tell(); unsigned filepos = ARFile.tellp();
filepos -= 8; filepos -= 8;
// Get the data and its size either from the // Get the data and its size either from the
@ -282,7 +281,7 @@ Archive::writeMember(
ARFile.write(data,fSize); ARFile.write(data,fSize);
// Make sure the member is an even length // Make sure the member is an even length
if ((ARFile.tell() & 1) == 1) if ((ARFile.tellp() & 1) == 1)
ARFile << ARFILE_PAD; ARFile << ARFILE_PAD;
// Close the mapped file if it was opened // Close the mapped file if it was opened
@ -292,7 +291,7 @@ Archive::writeMember(
// Write out the LLVM symbol table as an archive member to the file. // Write out the LLVM symbol table as an archive member to the file.
void void
Archive::writeSymbolTable(raw_ostream& ARFile) { Archive::writeSymbolTable(std::ofstream& ARFile) {
// Construct the symbol table's header // Construct the symbol table's header
ArchiveMemberHeader Hdr; ArchiveMemberHeader Hdr;
@ -316,7 +315,7 @@ Archive::writeSymbolTable(raw_ostream& ARFile) {
#ifndef NDEBUG #ifndef NDEBUG
// Save the starting position of the symbol tables data content. // Save the starting position of the symbol tables data content.
unsigned startpos = ARFile.tell(); unsigned startpos = ARFile.tellp();
#endif #endif
// Write out the symbols sequentially // Write out the symbols sequentially
@ -333,7 +332,7 @@ Archive::writeSymbolTable(raw_ostream& ARFile) {
#ifndef NDEBUG #ifndef NDEBUG
// Now that we're done with the symbol table, get the ending file position // Now that we're done with the symbol table, get the ending file position
unsigned endpos = ARFile.tell(); unsigned endpos = ARFile.tellp();
#endif #endif
// Make sure that the amount we wrote is what we pre-computed. This is // Make sure that the amount we wrote is what we pre-computed. This is
@ -362,20 +361,25 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
} }
// Create a temporary file to store the archive in // Create a temporary file to store the archive in
SmallString<128> TempArchivePath; sys::Path TmpArchive = archPath;
int ArchFD; if (TmpArchive.createTemporaryFileOnDisk(ErrMsg))
if (error_code ec =
sys::fs::unique_file("%%-%%-%%-%%-" + sys::path::filename(archPath.str()),
ArchFD, TempArchivePath)) {
if (ErrMsg) *ErrMsg = ec.message();
return true; return true;
}
// Make sure the temporary gets removed if we crash // Make sure the temporary gets removed if we crash
sys::RemoveFileOnSignal(sys::Path(TempArchivePath.str())); sys::RemoveFileOnSignal(TmpArchive);
// Create archive file for output. // Create archive file for output.
raw_fd_ostream ArchiveFile(ArchFD, true); 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;
}
// If we're creating a symbol table, reset it now // If we're creating a symbol table, reset it now
if (CreateSymbolTable) { if (CreateSymbolTable) {
@ -391,9 +395,8 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
for (MembersList::iterator I = begin(), E = end(); I != E; ++I) { for (MembersList::iterator I = begin(), E = end(); I != E; ++I) {
if (writeMember(*I, ArchiveFile, CreateSymbolTable, if (writeMember(*I, ArchiveFile, CreateSymbolTable,
TruncateNames, Compress, ErrMsg)) { TruncateNames, Compress, ErrMsg)) {
TmpArchive.eraseFromDisk();
ArchiveFile.close(); ArchiveFile.close();
bool existed;
sys::fs::remove(TempArchivePath.str(), existed);
return true; return true;
} }
} }
@ -408,12 +411,12 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
// ensure compatibility with other archivers we need to put the symbol // ensure compatibility with other archivers we need to put the symbol
// table first in the file. Unfortunately, this means mapping the file // table first in the file. Unfortunately, this means mapping the file
// we just wrote back in and copying it to the destination file. // we just wrote back in and copying it to the destination file.
SmallString<128> TempArchiveWithSymbolTablePath; sys::Path FinalFilePath = archPath;
// Map in the archive we just wrote. // Map in the archive we just wrote.
{ {
OwningPtr<MemoryBuffer> arch; OwningPtr<MemoryBuffer> arch;
if (error_code ec = MemoryBuffer::getFile(TempArchivePath.c_str(), arch)) { if (error_code ec = MemoryBuffer::getFile(TmpArchive.c_str(), arch)) {
if (ErrMsg) if (ErrMsg)
*ErrMsg = ec.message(); *ErrMsg = ec.message();
return true; return true;
@ -422,15 +425,17 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
// Open another temporary file in order to avoid invalidating the // Open another temporary file in order to avoid invalidating the
// mmapped data // mmapped data
if (error_code ec = if (FinalFilePath.createTemporaryFileOnDisk(ErrMsg))
sys::fs::unique_file("%%-%%-%%-%%-" + sys::path::filename(archPath.str()), return true;
ArchFD, TempArchiveWithSymbolTablePath)) { sys::RemoveFileOnSignal(FinalFilePath);
if (ErrMsg) *ErrMsg = ec.message();
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();
return true; return true;
} }
sys::RemoveFileOnSignal(sys::Path(TempArchiveWithSymbolTablePath.str()));
raw_fd_ostream FinalFile(ArchFD, true);
// Write the file magic number // Write the file magic number
FinalFile << ARFILE_MAGIC; FinalFile << ARFILE_MAGIC;
@ -443,8 +448,7 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
if (foreignST) { if (foreignST) {
if (writeMember(*foreignST, FinalFile, false, false, false, ErrMsg)) { if (writeMember(*foreignST, FinalFile, false, false, false, ErrMsg)) {
FinalFile.close(); FinalFile.close();
bool existed; TmpArchive.eraseFromDisk();
sys::fs::remove(TempArchiveWithSymbolTablePath.str(), existed);
return true; return true;
} }
} }
@ -462,11 +466,8 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
} // free arch. } // free arch.
// Move the final file over top of TmpArchive // Move the final file over top of TmpArchive
if (error_code ec = sys::fs::rename(TempArchiveWithSymbolTablePath.str(), if (FinalFilePath.renamePathOnDisk(TmpArchive, ErrMsg))
TempArchivePath.str())) {
if (ErrMsg) *ErrMsg = ec.message();
return true; return true;
}
} }
// Before we replace the actual archive, we need to forget all the // Before we replace the actual archive, we need to forget all the
@ -474,11 +475,8 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
// this because we cannot replace an open file on Windows. // this because we cannot replace an open file on Windows.
cleanUpMemory(); cleanUpMemory();
if (error_code ec = sys::fs::rename(TempArchivePath.str(), if (TmpArchive.renamePathOnDisk(archPath, ErrMsg))
archPath.str())) {
if (ErrMsg) *ErrMsg = ec.message();
return true; return true;
}
// Set correct read and write permissions after temporary file is moved // Set correct read and write permissions after temporary file is moved
// to final destination path. // to final destination path.