For PR797:
authorReid Spencer <rspencer@reidspencer.com>
Wed, 23 Aug 2006 07:30:48 +0000 (07:30 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Wed, 23 Aug 2006 07:30:48 +0000 (07:30 +0000)
Eliminate exception throwing from Path::renamePathOnDisk and adjust its
users correspondingly.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29843 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/System/Path.h
lib/Archive/ArchiveWriter.cpp
lib/Bytecode/Archive/ArchiveWriter.cpp
lib/System/Unix/Path.inc
lib/System/Unix/Unix.h
lib/System/Win32/Path.inc
lib/System/Win32/Win32.h
tools/llvm-ld/llvm-ld.cpp

index 5609f61cb09e12a1bf1014444123017da2f5e9aa..368fe202316092ff35d045cd7702b0c8d94293e1 100644 (file)
@@ -492,10 +492,9 @@ namespace sys {
       /// This method renames the file referenced by \p this as \p newName. The
       /// file referenced by \p this must exist. The file referenced by
       /// \p newName does not need to exist.
-      /// @returns true
-      /// @throws std::string if there is an file system error.
+      /// @returns true on error, false otherwise
       /// @brief Rename one file as another.
-      bool renamePathOnDisk(const Path& newName);
+      bool renamePathOnDisk(const Path& newName, std::string* ErrMsg);
 
       /// This method attempts to destroy the file or directory named by the
       /// last component of the Path. If the Path refers to a directory and the
index c3fda5fe1a94d8634409af0b5627e3b1765e5fe8..86da17cc77f0d97c52e43e4abf1c43410a975099 100644 (file)
@@ -496,7 +496,8 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
     arch.close();
     
     // Move the final file over top of TmpArchive
-    FinalFilePath.renamePathOnDisk(TmpArchive);
+    if (FinalFilePath.renamePathOnDisk(TmpArchive, error))
+      return false;
   }
   
   // Before we replace the actual archive, we need to forget all the
@@ -504,7 +505,8 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
   // this because we cannot replace an open file on Windows.
   cleanUpMemory();
   
-  TmpArchive.renamePathOnDisk(archPath);
+  if (TmpArchive.renamePathOnDisk(archPath, error))
+    return false;
 
   return true;
 }
index c3fda5fe1a94d8634409af0b5627e3b1765e5fe8..86da17cc77f0d97c52e43e4abf1c43410a975099 100644 (file)
@@ -496,7 +496,8 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
     arch.close();
     
     // Move the final file over top of TmpArchive
-    FinalFilePath.renamePathOnDisk(TmpArchive);
+    if (FinalFilePath.renamePathOnDisk(TmpArchive, error))
+      return false;
   }
   
   // Before we replace the actual archive, we need to forget all the
@@ -504,7 +505,8 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
   // this because we cannot replace an open file on Windows.
   cleanUpMemory();
   
-  TmpArchive.renamePathOnDisk(archPath);
+  if (TmpArchive.renamePathOnDisk(archPath, error))
+    return false;
 
   return true;
 }
index 2b9f1263ee34501bda35063009f593820d10e073..fd992505311569fbc535c4955f31315470ec2846 100644 (file)
@@ -391,36 +391,28 @@ static bool AddPermissionBits(const Path &File, int bits) {
 }
 
 bool Path::makeReadableOnDisk(std::string* ErrMsg) {
-  if (!AddPermissionBits(*this, 0444)) {
-    MakeErrMsg(ErrMsg, path + ": can't make file readable");
-    return true;
-  }
+  if (!AddPermissionBits(*this, 0444)) 
+    return MakeErrMsg(ErrMsg, path + ": can't make file readable");
   return false;
 }
 
 bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
-  if (!AddPermissionBits(*this, 0222)) {
-    MakeErrMsg(ErrMsg, path + ": can't make file writable");
-    return true;
-  }
+  if (!AddPermissionBits(*this, 0222))
+    return MakeErrMsg(ErrMsg, path + ": can't make file writable");
   return false;
 }
 
 bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
-  if (!AddPermissionBits(*this, 0111)) {
-    MakeErrMsg(ErrMsg, path + ": can't make file executable");
-    return true;
-  }
+  if (!AddPermissionBits(*this, 0111))
+    return MakeErrMsg(ErrMsg, path + ": can't make file executable");
   return false;
 }
 
 bool
 Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
   DIR* direntries = ::opendir(path.c_str());
-  if (direntries == 0) {
-    MakeErrMsg(ErrMsg, path + ": can't open directory");
-    return true;
-  }
+  if (direntries == 0)
+    return MakeErrMsg(ErrMsg, path + ": can't open directory");
 
   std::string dirPath = path;
   if (!lastIsSlash(dirPath))
@@ -435,8 +427,8 @@ Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
       if (0 != lstat(aPath.path.c_str(), &st)) {
         if (S_ISLNK(st.st_mode))
           continue; // dangling symlink -- ignore
-        MakeErrMsg(ErrMsg, aPath.path +  ": can't determine file object type");
-        return true;
+        return MakeErrMsg(ErrMsg, 
+                          aPath.path +  ": can't determine file object type");
       }
       result.insert(aPath);
     }
@@ -544,9 +536,8 @@ Path::createDirectoryOnDisk( bool create_parents, std::string* ErrMsg ) {
       *next = 0;
       if (0 != access(pathname, F_OK | R_OK | W_OK))
         if (0 != mkdir(pathname, S_IRWXU | S_IRWXG)) {
-          MakeErrMsg(ErrMsg, 
-            std::string(pathname) + ": can't create directory");
-          return true;
+          return MakeErrMsg(ErrMsg, 
+                            std::string(pathname) + ": can't create directory");
         }
       char* save = next;
       next = strchr(next+1,'/');
@@ -556,8 +547,8 @@ Path::createDirectoryOnDisk( bool create_parents, std::string* ErrMsg ) {
 
   if (0 != access(pathname, F_OK | R_OK))
     if (0 != mkdir(pathname, S_IRWXU | S_IRWXG)) {
-      MakeErrMsg(ErrMsg, std::string(pathname) + ": can't create directory");
-      return true;
+      return MakeErrMsg(ErrMsg, 
+                        std::string(pathname) + ": can't create directory");
     }
   return false;
 }
@@ -566,10 +557,8 @@ bool
 Path::createFileOnDisk(std::string* ErrMsg) {
   // Create the file
   int fd = ::creat(path.c_str(), S_IRUSR | S_IWUSR);
-  if (fd < 0) {
-    MakeErrMsg(ErrMsg, path + ": can't create file");
-    return true;
-  }
+  if (fd < 0)
+    return MakeErrMsg(ErrMsg, path + ": can't create file");
   ::close(fd);
   return false;
 }
@@ -581,10 +570,8 @@ Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
 
   // create the file
   int fd = ::open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
-  if (fd < 0) {
-    MakeErrMsg(ErrMsg, path + ": can't create temporary file");
-    return true;
-  }
+  if (fd < 0) 
+    return MakeErrMsg(ErrMsg, path + ": can't create temporary file");
   ::close(fd);
   return false;
 }
@@ -633,11 +620,11 @@ Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
 }
 
 bool
-Path::renamePathOnDisk(const Path& newName) {
+Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
   if (0 != ::rename(path.c_str(), newName.c_str()))
-    ThrowErrno(std::string("can't rename '") + path + "' as '" + 
+    return MakeErrMsg(ErrMsg, std::string("can't rename '") + path + "' as '" + 
                newName.toString() + "' ");
-  return true;
+  return false;
 }
 
 bool
index 1516d453dd91e1926421b91b2ca39d028130471b..0f9b96adca092e642bc00f70f4b4f788fe3ee3ff 100644 (file)
@@ -123,10 +123,10 @@ inline void ThrowErrno(const std::string& prefix, int errnum = -1) {
 /// string and the Unix error number given by \p errnum. If errnum is -1, the
 /// default then the value of errno is used.
 /// @brief Make an error message
-inline void MakeErrMsg(
+inline bool MakeErrMsg(
   std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
   if (!ErrMsg)
-    return;
+    return true;
   char buffer[MAXPATHLEN];
   buffer[0] = 0;
   if (errnum == -1)
@@ -148,6 +148,7 @@ inline void MakeErrMsg(
   sprintf(buffer, "Error #%d", errnum);
 #endif
   *ErrMsg = buffer;
+  return true;
 }
 
 #endif
index 913a4091aca050a50f9077d9c7425098c6243ff0..ece5727151b969946c599a4097b15258fc58f469 100644 (file)
@@ -651,10 +651,10 @@ bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
 }
 
 bool
-Path::renamePathOnDisk(const Path& newName) {
+Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
   if (!MoveFileEx(path.c_str(), newName.c_str(), MOVEFILE_REPLACE_EXISTING))
-    ThrowError("Can't move '" + path +
-               "' to '" + newName.path + "': ");
+    return MakeErrMsg(ErrMsg, "Can't move '" + path + "' to '" + newName.path 
+        + "': ");
   return true;
 }
 
index 7243548cda48960095418e3f2d522ca8f8af5f76..2238faa8485b63dfdf3be3216cd1a5933a27e4de 100644 (file)
@@ -44,14 +44,15 @@ inline void ThrowError(const std::string& msg) {
   throw s;
 }
 
-inline void MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
+inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
   if (!ErrMsg)
-    return;
+    return true;
   char *buffer = NULL;
   FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
       NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
   ErrMsg = prefix + buffer;
   LocalFree(buffer);
+  return true;
 }
 
 inline void ThrowErrno(const std::string& prefix) {
index 53f04e6954207f986fcaa89002f945128a671af2..6b9adaff5a4b481db8949d5db4fc375b4a30a207 100644 (file)
@@ -505,7 +505,10 @@ int main(int argc, char **argv, char **envp) {
             if (tmp_output.isBytecodeFile()) {
               sys::Path target(RealBytecodeOutput);
               target.eraseFromDisk();
-              tmp_output.renamePathOnDisk(target);
+              if (tmp_output.renamePathOnDisk(target, &ErrMsg)) {
+                std::cerr << argv[0] << ": " << ErrMsg << "\n";
+                return 2;
+              }
             } else
               return PrintAndReturn(
                 "Post-link optimization output is not bytecode");