Remove this file; the code that it went with is no longer
[oota-llvm.git] / lib / Archive / ArchiveWriter.cpp
index f5c412609ac8dbc3e75f8fa2effc6c29e0e69752..2269464c6c5351915e8e90f192612431d1f7ad07 100644 (file)
@@ -13,6 +13,8 @@
 
 #include "ArchiveInternals.h"
 #include "llvm/Bitcode/ReaderWriter.h"
+#include "llvm/ADT/OwningPtr.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include "llvm/System/Signals.h"
 #include "llvm/System/Process.h"
 #include "llvm/ModuleProvider.h"
@@ -23,7 +25,7 @@ using namespace llvm;
 
 // Write an integer using variable bit rate encoding. This saves a few bytes
 // per entry in the symbol table.
-inline void writeInteger(unsigned num, std::ofstream& ARFile) {
+static inline void writeInteger(unsigned num, std::ofstream& ARFile) {
   while (1) {
     if (num < 0x80) { // done?
       ARFile << (unsigned char)num;
@@ -39,7 +41,7 @@ inline void writeInteger(unsigned num, std::ofstream& ARFile) {
 
 // Compute how many bytes are taken by a given VBR encoded value. This is needed
 // to pre-compute the size of the symbol table.
-inline unsigned numVbrBytes(unsigned num) {
+static inline unsigned numVbrBytes(unsigned num) {
 
   // Note that the following nested ifs are somewhat equivalent to a binary
   // search. We split it in half by comparing against 2^14 first. This allows
@@ -47,11 +49,12 @@ inline unsigned numVbrBytes(unsigned num) {
   // small ones and four for large ones. We expect this to access file offsets
   // in the 2^10 to 2^24 range and symbol lengths in the 2^0 to 2^8 range,
   // so this approach is reasonable.
-  if (num < 1<<14)
+  if (num < 1<<14) {
     if (num < 1<<7)
       return 1;
     else
       return 2;
+  }
   if (num < 1<<21)
     return 3;
 
@@ -206,15 +209,14 @@ Archive::writeMember(
   // Get the data and its size either from the
   // member's in-memory data or directly from the file.
   size_t fSize = member.getSize();
-  const chardata = (const char*)member.getData();
-  sys::MappedFile* mFile = 0;
+  const char *data = (const char*)member.getData();
+  MemoryBuffer *mFile = 0;
   if (!data) {
-    mFile = new sys::MappedFile();
-    if (mFile->open(member.getPath(), sys::MappedFile::READ_ACCESS, ErrMsg))
-      return true;
-    if (!(data = (const char*) mFile->map(ErrMsg)))
+    mFile = MemoryBuffer::getFile(member.getPath().c_str(), ErrMsg);
+    if (mFile == 0)
       return true;
-    fSize = mFile->size();
+    data = mFile->getBufferStart();
+    fSize = mFile->getBufferSize();
   }
 
   // Now that we have the data in memory, update the
@@ -245,10 +247,7 @@ Archive::writeMember(
       // We don't need this module any more.
       delete MP;
     } else {
-      if (mFile != 0) {
-        mFile->close();
-        delete mFile;
-      }
+      delete mFile;
       if (ErrMsg)
         *ErrMsg = "Can't parse bitcode member: " + member.getPath().toString()
           + ": " + *ErrMsg;
@@ -279,10 +278,7 @@ Archive::writeMember(
     ARFile << ARFILE_PAD;
 
   // Close the mapped file if it was opened
-  if (mFile != 0) {
-    mFile->close();
-    delete mFile;
-  }
+  delete mFile;
   return false;
 }
 
@@ -347,7 +343,7 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
 {
   // 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.
-  if (members.empty() && mapfile->size() > 8) {
+  if (members.empty() && mapfile && mapfile->getBufferSize() > 8) {
     if (ErrMsg)
       *ErrMsg = "Can't write an archive not opened for writing";
     return true;
@@ -406,18 +402,16 @@ 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;
 
     // Map in the archive we just wrote.
-    sys::MappedFile arch;
-    if (arch.open(TmpArchive, sys::MappedFile::READ_ACCESS, ErrMsg))
-      return true;
-    const char* base;
-    if (!(base = (const char*) arch.map(ErrMsg)))
-      return true;
+    {
+    OwningPtr<MemoryBuffer> arch(MemoryBuffer::getFile(TmpArchive.c_str()));
+    if (arch == 0) return true;
+    const char* base = arch->getBufferStart();
 
     // Open another temporary file in order to avoid invalidating the 
     // mmapped data
-    sys::Path FinalFilePath = archPath;
     if (FinalFilePath.createTemporaryFileOnDisk(ErrMsg))
       return true;
     sys::RemoveFileOnSignal(FinalFilePath);
@@ -454,11 +448,11 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
     // Copy the temporary file contents being sure to skip the file's magic
     // number.
     FinalFile.write(base + sizeof(ARFILE_MAGIC)-1,
-      arch.size()-sizeof(ARFILE_MAGIC)+1);
+      arch->getBufferSize()-sizeof(ARFILE_MAGIC)+1);
 
     // Close up shop
     FinalFile.close();
-    arch.close();
+    } // free arch.
     
     // Move the final file over top of TmpArchive
     if (FinalFilePath.renamePathOnDisk(TmpArchive, ErrMsg))
@@ -473,5 +467,12 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
   if (TmpArchive.renamePathOnDisk(archPath, ErrMsg))
     return true;
 
+  // Set correct read and write permissions after temporary file is moved
+  // to final destination path.
+  if (archPath.makeReadableOnDisk(ErrMsg))
+    return true;
+  if (archPath.makeWriteableOnDisk(ErrMsg))
+    return true;
+
   return false;
 }