diff options
author | Rafael Espindola <rafael.espindola@gmail.com> | 2017-11-13 18:33:44 +0000 |
---|---|---|
committer | Rafael Espindola <rafael.espindola@gmail.com> | 2017-11-13 18:33:44 +0000 |
commit | 58fe67a96537cfc50f891101e7c578c0cd73bab6 (patch) | |
tree | 44763b6d9e05516c66795074fe22e8a57757ec2a /llvm/lib/Support/Path.cpp | |
parent | b78ac6e3220bb8d3124947dc3cec6a3a377b2f67 (diff) | |
download | bcm5719-llvm-58fe67a96537cfc50f891101e7c578c0cd73bab6.tar.gz bcm5719-llvm-58fe67a96537cfc50f891101e7c578c0cd73bab6.zip |
Create a TempFile class.
This just adds a TempFile class and replaces the use in
FileOutputBuffer with it.
The only difference for now is better error handling. Followup work includes:
- Convert other user of temporary files to it.
- Add support for automatically deleting on windows.
- Add a createUnnamed method that returns a potentially unnamed
file. It would be actually unnamed on modern linux and have a
unknown name on windows.
llvm-svn: 318069
Diffstat (limited to 'llvm/lib/Support/Path.cpp')
-rw-r--r-- | llvm/lib/Support/Path.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp index 9692acb5283..54ea76b0f92 100644 --- a/llvm/lib/Support/Path.cpp +++ b/llvm/lib/Support/Path.cpp @@ -18,6 +18,7 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Process.h" +#include "llvm/Support/Signals.h" #include <cctype> #include <cstring> @@ -759,6 +760,58 @@ std::error_code createUniqueFile(const Twine &Model, return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name); } +TempFile::TempFile(StringRef Name, int FD) : TmpName(Name), FD(FD) {} +TempFile::TempFile(TempFile &&Other) { + TmpName = std::move(Other.TmpName); + FD = Other.FD; + Other.Done = true; +} + +TempFile::~TempFile() { assert(Done); } + +Error TempFile::discard() { + if (Done) + return Error::success(); + Done = true; + // Always try to close and remove. + std::error_code RemoveEC = fs::remove(TmpName); + sys::DontRemoveFileOnSignal(TmpName); + if (close(FD) == -1) { + std::error_code EC = std::error_code(errno, std::generic_category()); + return errorCodeToError(EC); + } + return errorCodeToError(RemoveEC); +} + +Error TempFile::keep(const Twine &Name) { + assert(!Done); + Done = true; + // Always try to close and rename. + std::error_code RenameEC = fs::rename(TmpName, Name); + sys::DontRemoveFileOnSignal(TmpName); + if (close(FD) == -1) { + std::error_code EC(errno, std::generic_category()); + return errorCodeToError(EC); + } + return errorCodeToError(RenameEC); +} + +Expected<TempFile> TempFile::create(const Twine &Model, unsigned Mode) { + int FD; + SmallString<128> ResultPath; + if (std::error_code EC = createUniqueFile(Model, FD, ResultPath, Mode)) + return errorCodeToError(EC); + + // Make sure we delete the file when RemoveFileOnSignal fails. + TempFile Ret(ResultPath, FD); + if (sys::RemoveFileOnSignal(ResultPath)) { + consumeError(Ret.discard()); + std::error_code EC(errc::operation_not_permitted); + return errorCodeToError(EC); + } + return std::move(Ret); +} + static std::error_code createTemporaryFile(const Twine &Model, int &ResultFD, llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) { |