From 0c33231eff17edbdabdb186e1fae718cf94b58b2 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 28 Jul 2006 22:29:50 +0000 Subject: [PATCH] Modify Path::eraseFromDisk to not throw an exception. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29400 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/FileUtilities.h | 8 ++-- include/llvm/System/Path.h | 10 ++--- lib/Archive/ArchiveWriter.cpp | 11 +++--- lib/Bytecode/Archive/ArchiveWriter.cpp | 11 +++--- lib/System/Unix/Path.inc | 55 ++++++++++++++------------ lib/System/Win32/Path.inc | 21 +++++----- 6 files changed, 58 insertions(+), 58 deletions(-) diff --git a/include/llvm/Support/FileUtilities.h b/include/llvm/Support/FileUtilities.h index 67eb3bb30fb..d0d193deb95 100644 --- a/include/llvm/Support/FileUtilities.h +++ b/include/llvm/Support/FileUtilities.h @@ -43,10 +43,10 @@ namespace llvm { : Filename(filename), DeleteIt(deleteIt) {} ~FileRemover() { - if (DeleteIt) - try { - Filename.eraseFromDisk(); - } catch (...) {} // Ignore problems deleting the file. + if (DeleteIt) { + // Ignore problems deleting the file. + Filename.eraseFromDisk(); + } } /// releaseFile - Take ownership of the file away from the FileRemover so it diff --git a/include/llvm/System/Path.h b/include/llvm/System/Path.h index f8ff8f4233b..14602d7a2ce 100644 --- a/include/llvm/System/Path.h +++ b/include/llvm/System/Path.h @@ -522,17 +522,15 @@ namespace sys { /// \p destroy_contents parameter is ignored. /// @param destroy_contents Indicates whether the contents of a destroyed /// directory should also be destroyed (recursively). - /// @returns true if the file/directory was destroyed, false if the path - /// refers to something that is neither a file nor a directory. - /// @throws std::string if there is an error. + /// @returns false if the file/directory was destroyed, true on error. /// @brief Removes the file or directory from the filesystem. - bool eraseFromDisk(bool destroy_contents = false) const; - + bool eraseFromDisk(bool destroy_contents = false, + std::string *Err = 0) const; /// @} /// @name Data /// @{ private: - mutable std::string path; ///< Storage for the path name. + mutable std::string path; ///< Storage for the path name. /// @} }; diff --git a/lib/Archive/ArchiveWriter.cpp b/lib/Archive/ArchiveWriter.cpp index 52ba99e8054..dc7ef672055 100644 --- a/lib/Archive/ArchiveWriter.cpp +++ b/lib/Archive/ArchiveWriter.cpp @@ -396,7 +396,7 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress, std::ofstream ArchiveFile(TmpArchive.c_str(), io_mode); // Check for errors opening or creating archive file. - if ( !ArchiveFile.is_open() || ArchiveFile.bad() ) { + if (!ArchiveFile.is_open() || ArchiveFile.bad()) { if (TmpArchive.exists()) TmpArchive.eraseFromDisk(); if (error) @@ -415,10 +415,9 @@ 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)) - { + for (MembersList::iterator I = begin(), E = end(); I != E; ++I) { + if (!writeMember(*I, ArchiveFile, CreateSymbolTable, + TruncateNames, Compress, error)) { if (TmpArchive.exists()) TmpArchive.eraseFromDisk(); ArchiveFile.close(); @@ -448,7 +447,7 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress, sys::RemoveFileOnSignal(FinalFilePath); std::ofstream FinalFile(FinalFilePath.c_str(), io_mode); - if ( !FinalFile.is_open() || FinalFile.bad() ) { + if (!FinalFile.is_open() || FinalFile.bad()) { if (TmpArchive.exists()) TmpArchive.eraseFromDisk(); if (error) diff --git a/lib/Bytecode/Archive/ArchiveWriter.cpp b/lib/Bytecode/Archive/ArchiveWriter.cpp index 52ba99e8054..dc7ef672055 100644 --- a/lib/Bytecode/Archive/ArchiveWriter.cpp +++ b/lib/Bytecode/Archive/ArchiveWriter.cpp @@ -396,7 +396,7 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress, std::ofstream ArchiveFile(TmpArchive.c_str(), io_mode); // Check for errors opening or creating archive file. - if ( !ArchiveFile.is_open() || ArchiveFile.bad() ) { + if (!ArchiveFile.is_open() || ArchiveFile.bad()) { if (TmpArchive.exists()) TmpArchive.eraseFromDisk(); if (error) @@ -415,10 +415,9 @@ 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)) - { + for (MembersList::iterator I = begin(), E = end(); I != E; ++I) { + if (!writeMember(*I, ArchiveFile, CreateSymbolTable, + TruncateNames, Compress, error)) { if (TmpArchive.exists()) TmpArchive.eraseFromDisk(); ArchiveFile.close(); @@ -448,7 +447,7 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress, sys::RemoveFileOnSignal(FinalFilePath); std::ofstream FinalFile(FinalFilePath.c_str(), io_mode); - if ( !FinalFile.is_open() || FinalFile.bad() ) { + if (!FinalFile.is_open() || FinalFile.bad()) { if (TmpArchive.exists()) TmpArchive.eraseFromDisk(); if (error) diff --git a/lib/System/Unix/Path.inc b/lib/System/Unix/Path.inc index a0d76b032b4..26f29c0bc0c 100644 --- a/lib/System/Unix/Path.inc +++ b/lib/System/Unix/Path.inc @@ -604,33 +604,38 @@ Path::createTemporaryFileOnDisk(bool reuse_current) { } bool -Path::eraseFromDisk(bool remove_contents) const { - // Make sure we're dealing with a directory +Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const { + // Make sure we're dealing with a directory. if (isFile()) { - if (0 != unlink(path.c_str())) - ThrowErrno(path + ": can't destroy file"); - } else if (isDirectory()) { - if (remove_contents) { - // Recursively descend the directory to remove its content - std::string cmd("/bin/rm -rf "); - cmd += path; - system(cmd.c_str()); - } else { - // Otherwise, try to just remove the one directory - char pathname[MAXPATHLEN]; - path.copy(pathname,MAXPATHLEN); - int lastchar = path.length() - 1 ; - if (pathname[lastchar] == '/') - pathname[lastchar] = 0; - else - pathname[lastchar+1] = 0; - if ( 0 != rmdir(pathname)) - ThrowErrno(std::string(pathname) + ": can't destroy directory"); - } - } - else + if (unlink(path.c_str()) != 0) + return GetErrno(path + ": can't destroy file", ErrStr); return false; - return true; + } + + if (!isDirectory()) { + if (ErrStr) *ErrStr = "not a file or directory"; + return true; + } + if (remove_contents) { + // Recursively descend the directory to remove its contents. + std::string cmd = "/bin/rm -rf " + path; + system(cmd.c_str()); + return false; + } + + // Otherwise, try to just remove the one directory. + char pathname[MAXPATHLEN]; + path.copy(pathname, MAXPATHLEN); + int lastchar = path.length() - 1 ; + if (pathname[lastchar] == '/') + pathname[lastchar] = 0; + else + pathname[lastchar+1] = 0; + + if (rmdir(pathname) != 0) + return GetErrno(std::string(pathname) + ": can't destroy directory", + ErrStr); + return false; } bool diff --git a/lib/System/Win32/Path.inc b/lib/System/Win32/Path.inc index f33654ec8fc..15c686d2640 100644 --- a/lib/System/Win32/Path.inc +++ b/lib/System/Win32/Path.inc @@ -571,19 +571,19 @@ Path::createFileOnDisk() { } bool -Path::eraseFromDisk(bool remove_contents) const { +Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const { if (isFile()) { DWORD attr = GetFileAttributes(path.c_str()); // If it doesn't exist, we're done. if (attr == INVALID_FILE_ATTRIBUTES) - return true; + return false; // Read-only files cannot be deleted on Windows. Must remove the read-only // attribute first. if (attr & FILE_ATTRIBUTE_READONLY) { if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) - ThrowError(path + ": Can't destroy file: "); + return GetError(path + ": Can't destroy file: ", ErrStr); } if (!DeleteFile(path.c_str())) @@ -592,7 +592,7 @@ Path::eraseFromDisk(bool remove_contents) const { } else if (isDirectory()) { // If it doesn't exist, we're done. if (!exists()) - return true; + return false; char *pathname = reinterpret_cast(_alloca(path.length()+3)); int lastchar = path.length() - 1 ; @@ -629,7 +629,7 @@ Path::eraseFromDisk(bool remove_contents) const { FindClose(h); if (err != ERROR_NO_MORE_FILES) { SetLastError(err); - ThrowError(path + ": Can't read directory: "); + return GetError(path + ": Can't read directory: ", ErrStr); } for (std::vector::iterator I = list.begin(); I != list.end(); @@ -639,17 +639,18 @@ Path::eraseFromDisk(bool remove_contents) const { } } else { if (GetLastError() != ERROR_FILE_NOT_FOUND) - ThrowError(path + ": Can't read directory: "); + return GetError(path + ": Can't read directory: ", ErrStr); } } pathname[lastchar] = 0; if (!RemoveDirectory(pathname)) - ThrowError(std::string(pathname) + ": Can't destroy directory: "); - return true; + return GetError(std::string(pathname) + ": Can't destroy directory: ", + ErrStr); + return false; } else { // It appears the path doesn't exist. - return false; + return true; } } @@ -789,5 +790,3 @@ Path::createTemporaryFileOnDisk(bool reuse_current) { } } - -