summaryrefslogtreecommitdiff
path: root/lib/Archive
diff options
context:
space:
mode:
authorReid Spencer <rspencer@reidspencer.com>2006-08-24 23:45:08 +0000
committerReid Spencer <rspencer@reidspencer.com>2006-08-24 23:45:08 +0000
commit0ff2d31766209ce1a69d4d2c5d35761ef57362aa (patch)
treecc950d878d90c6ccb0836cf35ac004f32bc4fba1 /lib/Archive
parentc82b3aab6502a9766ddf42b45faeca3d6fa0ad65 (diff)
For PR797:
Remove exception handling from the bytecode archiver and adjust the llvm-ar tool to accommodate the new interfaces. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29866 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Archive')
-rw-r--r--lib/Archive/Archive.cpp28
-rw-r--r--lib/Archive/ArchiveReader.cpp8
-rw-r--r--lib/Archive/ArchiveWriter.cpp86
3 files changed, 65 insertions, 57 deletions
diff --git a/lib/Archive/Archive.cpp b/lib/Archive/Archive.cpp
index d5b56ca85e2..a661a4e992a 100644
--- a/lib/Archive/Archive.cpp
+++ b/lib/Archive/Archive.cpp
@@ -61,7 +61,7 @@ ArchiveMember::ArchiveMember(Archive* PAR)
// This method allows an ArchiveMember to be replaced with the data for a
// different file, presumably as an update to the member. It also makes sure
// the flags are reset correctly.
-void ArchiveMember::replaceWith(const sys::Path& newFile) {
+bool ArchiveMember::replaceWith(const sys::Path& newFile, std::string* ErrMsg) {
assert(newFile.exists() && "Can't replace with a non-existent file");
data = 0;
path = newFile;
@@ -110,8 +110,8 @@ void ArchiveMember::replaceWith(const sys::Path& newFile) {
path.getMagicNumber(magic,4);
signature = magic.c_str();
std::string err;
- if (path.getFileStatus(info, &err))
- throw err;
+ if (path.getFileStatus(info, ErrMsg))
+ return true;
}
// Determine what kind of file it is
@@ -127,23 +127,27 @@ void ArchiveMember::replaceWith(const sys::Path& newFile) {
flags &= ~(BytecodeFlag|CompressedBytecodeFlag);
break;
}
+ return false;
}
// Archive constructor - this is the only constructor that gets used for the
// Archive class. Everything else (default,copy) is deprecated. This just
// initializes and maps the file into memory, if requested.
-Archive::Archive(const sys::Path& filename, bool map )
+Archive::Archive(const sys::Path& filename)
: archPath(filename), members(), mapfile(0), base(0), symTab(), strtab(),
symTabSize(0), firstFileOffset(0), modules(), foreignST(0)
{
- if (map) {
- std::string ErrMsg;
- mapfile = new sys::MappedFile();
- if (mapfile->open(filename, sys::MappedFile::READ_ACCESS, &ErrMsg))
- throw ErrMsg;
- if (!(base = (char*) mapfile->map(&ErrMsg)))
- throw ErrMsg;
- }
+}
+
+bool
+Archive::mapToMemory(std::string* ErrMsg)
+{
+ mapfile = new sys::MappedFile();
+ if (mapfile->open(archPath, sys::MappedFile::READ_ACCESS, ErrMsg))
+ return true;
+ if (!(base = (char*) mapfile->map(ErrMsg)))
+ return true;
+ return false;
}
void Archive::cleanUpMemory() {
diff --git a/lib/Archive/ArchiveReader.cpp b/lib/Archive/ArchiveReader.cpp
index c8dd17c8f11..38aa07243c7 100644
--- a/lib/Archive/ArchiveReader.cpp
+++ b/lib/Archive/ArchiveReader.cpp
@@ -330,7 +330,9 @@ Archive::loadArchive(std::string* error) {
Archive*
Archive::OpenAndLoad(const sys::Path& file, std::string* ErrorMessage)
{
- std::auto_ptr<Archive> result ( new Archive(file, true));
+ std::auto_ptr<Archive> result ( new Archive(file));
+ if (result->mapToMemory(ErrorMessage))
+ return 0;
if (!result->loadArchive(ErrorMessage))
return 0;
return result.release();
@@ -437,7 +439,9 @@ Archive::loadSymbolTable(std::string* ErrorMsg) {
// Open the archive and load just the symbol tables
Archive*
Archive::OpenAndLoadSymbols(const sys::Path& file, std::string* ErrorMessage) {
- std::auto_ptr<Archive> result ( new Archive(file, true) );
+ std::auto_ptr<Archive> result ( new Archive(file) );
+ if (result->mapToMemory(ErrorMessage))
+ return 0;
if (!result->loadSymbolTable(ErrorMessage))
return 0;
return result.release();
diff --git a/lib/Archive/ArchiveWriter.cpp b/lib/Archive/ArchiveWriter.cpp
index 86da17cc77f..3746dbff8a1 100644
--- a/lib/Archive/ArchiveWriter.cpp
+++ b/lib/Archive/ArchiveWriter.cpp
@@ -64,7 +64,7 @@ inline unsigned numVbrBytes(unsigned num) {
// Create an empty archive.
Archive*
Archive::CreateEmpty(const sys::Path& FilePath ) {
- Archive* result = new Archive(FilePath,false);
+ Archive* result = new Archive(FilePath);
return result;
}
@@ -151,17 +151,17 @@ Archive::fillHeader(const ArchiveMember &mbr, ArchiveMemberHeader& hdr,
// Insert a file into the archive before some other member. This also takes care
// of extracting the necessary flags and information from the file.
-void
-Archive::addFileBefore(const sys::Path& filePath, iterator where) {
+bool
+Archive::addFileBefore(const sys::Path& filePath, iterator where,
+ std::string* ErrMsg) {
assert(filePath.exists() && "Can't add a non-existent file");
ArchiveMember* mbr = new ArchiveMember(this);
mbr->data = 0;
mbr->path = filePath;
- std::string err;
- if (mbr->path.getFileStatus(mbr->info, &err))
- throw err;
+ if (mbr->path.getFileStatus(mbr->info, ErrMsg))
+ return true;
unsigned flags = 0;
bool hasSlash = filePath.toString().find('/') != std::string::npos;
@@ -183,6 +183,7 @@ Archive::addFileBefore(const sys::Path& filePath, iterator where) {
}
mbr->flags = flags;
members.insert(where,mbr);
+ return false;
}
// Write one member out to the file.
@@ -193,7 +194,7 @@ Archive::writeMember(
bool CreateSymbolTable,
bool TruncateNames,
bool ShouldCompress,
- std::string* error
+ std::string* ErrMsg
) {
unsigned filepos = ARFile.tellp();
@@ -205,12 +206,11 @@ Archive::writeMember(
const char* data = (const char*)member.getData();
sys::MappedFile* mFile = 0;
if (!data) {
- std::string ErrMsg;
mFile = new sys::MappedFile();
- if (mFile->open(member.getPath(), sys::MappedFile::READ_ACCESS, &ErrMsg))
- throw ErrMsg;
- if (!(data = (const char*) mFile->map(&ErrMsg)))
- throw ErrMsg;
+ if (mFile->open(member.getPath(), sys::MappedFile::READ_ACCESS, ErrMsg))
+ return true;
+ if (!(data = (const char*) mFile->map(ErrMsg)))
+ return true;
fSize = mFile->size();
}
@@ -246,8 +246,9 @@ Archive::writeMember(
mFile->close();
delete mFile;
}
- if (error)
- *error = "Can't parse bytecode member: " + member.getPath().toString();
+ if (ErrMsg)
+ *ErrMsg = "Can't parse bytecode member: " + member.getPath().toString();
+ return true;
}
}
@@ -274,9 +275,9 @@ Archive::writeMember(
data +=4;
fSize -= 4;
}
- fSize = Compressor::compressToNewBuffer(data,fSize,output,error);
+ fSize = Compressor::compressToNewBuffer(data,fSize,output,ErrMsg);
if (fSize == 0)
- return false;
+ return true;
data = output;
if (member.isBytecode())
hdrSize = -fSize-4;
@@ -320,7 +321,7 @@ Archive::writeMember(
mFile->close();
delete mFile;
}
- return true;
+ return false;
}
// Write out the LLVM symbol table as an archive member to the file.
@@ -380,7 +381,7 @@ Archive::writeSymbolTable(std::ofstream& ARFile) {
// compressing each archive member.
bool
Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
- std::string* error)
+ std::string* ErrMsg)
{
// Make sure they haven't opened up the file, not loaded it,
// but are now trying to write it which would wipe out the file.
@@ -389,8 +390,8 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
// Create a temporary file to store the archive in
sys::Path TmpArchive = archPath;
- if (TmpArchive.createTemporaryFileOnDisk(error))
- return false;
+ if (TmpArchive.createTemporaryFileOnDisk(ErrMsg))
+ return true;
// Make sure the temporary gets removed if we crash
sys::RemoveFileOnSignal(TmpArchive);
@@ -404,9 +405,9 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
if (!ArchiveFile.is_open() || ArchiveFile.bad()) {
if (TmpArchive.exists())
TmpArchive.eraseFromDisk();
- if (error)
- *error = "Error opening archive file: " + archPath.toString();
- return false;
+ if (ErrMsg)
+ *ErrMsg = "Error opening archive file: " + archPath.toString();
+ return true;
}
// If we're creating a symbol table, reset it now
@@ -421,12 +422,12 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
// Loop over all member files, and write them out. Note that this also
// builds the symbol table, symTab.
for (MembersList::iterator I = begin(), E = end(); I != E; ++I) {
- if (!writeMember(*I, ArchiveFile, CreateSymbolTable,
- TruncateNames, Compress, error)) {
+ if (writeMember(*I, ArchiveFile, CreateSymbolTable,
+ TruncateNames, Compress, ErrMsg)) {
if (TmpArchive.exists())
TmpArchive.eraseFromDisk();
ArchiveFile.close();
- return false;
+ return true;
}
}
@@ -443,27 +444,26 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
// Map in the archive we just wrote.
sys::MappedFile arch;
- std::string ErrMsg;
- if (arch.open(TmpArchive, sys::MappedFile::READ_ACCESS, &ErrMsg))
- throw ErrMsg;
+ if (arch.open(TmpArchive, sys::MappedFile::READ_ACCESS, ErrMsg))
+ return true;
const char* base;
- if (!(base = (const char*) arch.map(&ErrMsg)))
- throw ErrMsg;
+ if (!(base = (const char*) arch.map(ErrMsg)))
+ return true;
// Open another temporary file in order to avoid invalidating the
// mmapped data
sys::Path FinalFilePath = archPath;
- if (FinalFilePath.createTemporaryFileOnDisk(error))
- return false;
+ if (FinalFilePath.createTemporaryFileOnDisk(ErrMsg))
+ return true;
sys::RemoveFileOnSignal(FinalFilePath);
std::ofstream FinalFile(FinalFilePath.c_str(), io_mode);
if (!FinalFile.is_open() || FinalFile.bad()) {
if (TmpArchive.exists())
TmpArchive.eraseFromDisk();
- if (error)
- *error = "Error opening archive file: " + FinalFilePath.toString();
- return false;
+ if (ErrMsg)
+ *ErrMsg = "Error opening archive file: " + FinalFilePath.toString();
+ return true;
}
// Write the file magic number
@@ -475,11 +475,11 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
// compatibility with other ar(1) implementations as well as allowing the
// archive to store both native .o and LLVM .bc files, both indexed.
if (foreignST) {
- if (!writeMember(*foreignST, FinalFile, false, false, false, error)) {
+ if (writeMember(*foreignST, FinalFile, false, false, false, ErrMsg)) {
FinalFile.close();
if (TmpArchive.exists())
TmpArchive.eraseFromDisk();
- return false;
+ return true;
}
}
@@ -496,8 +496,8 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
arch.close();
// Move the final file over top of TmpArchive
- if (FinalFilePath.renamePathOnDisk(TmpArchive, error))
- return false;
+ if (FinalFilePath.renamePathOnDisk(TmpArchive, ErrMsg))
+ return true;
}
// Before we replace the actual archive, we need to forget all the
@@ -505,8 +505,8 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
// this because we cannot replace an open file on Windows.
cleanUpMemory();
- if (TmpArchive.renamePathOnDisk(archPath, error))
- return false;
+ if (TmpArchive.renamePathOnDisk(archPath, ErrMsg))
+ return true;
- return true;
+ return false;
}