diff --git a/llvm/include/llvm/Support/FileUtilities.h b/llvm/include/llvm/Support/FileUtilities.h index c9a72d5d14ec2..9707724d63170 100644 --- a/llvm/include/llvm/Support/FileUtilities.h +++ b/llvm/include/llvm/Support/FileUtilities.h @@ -76,41 +76,6 @@ namespace llvm { void releaseFile() { DeleteIt = false; } }; - enum class atomic_write_error { - failed_to_create_uniq_file = 0, - output_stream_error, - failed_to_rename_temp_file - }; - - class AtomicFileWriteError : public llvm::ErrorInfo { - public: - AtomicFileWriteError(atomic_write_error Error) : Error(Error) {} - - void log(raw_ostream &OS) const override; - - const atomic_write_error Error; - static char ID; - - private: - // Users are not expected to use error_code. - std::error_code convertToErrorCode() const override { - return llvm::inconvertibleErrorCode(); - } - }; - - // atomic_write_error + whatever the Writer can return - - /// Creates a unique file with name according to the given \p TempPathModel, - /// writes content of \p Buffer to the file and renames it to \p FinalPath. - /// - /// \returns \c AtomicFileWriteError in case of error. - llvm::Error writeFileAtomically(StringRef TempPathModel, StringRef FinalPath, - StringRef Buffer); - - llvm::Error - writeFileAtomically(StringRef TempPathModel, StringRef FinalPath, - std::function Writer); - /// FilePermssionsApplier helps to copy permissions from an input file to /// an output one. It memorizes the status of the input file and can apply /// permissions and dates to the output file. diff --git a/llvm/lib/Support/FileUtilities.cpp b/llvm/lib/Support/FileUtilities.cpp index 5d4ebb9e43c39..dbd6c324cf4dc 100644 --- a/llvm/lib/Support/FileUtilities.cpp +++ b/llvm/lib/Support/FileUtilities.cpp @@ -267,64 +267,6 @@ int llvm::DiffFilesWithTolerance(StringRef NameA, return CompareFailed; } -void llvm::AtomicFileWriteError::log(raw_ostream &OS) const { - OS << "atomic_write_error: "; - switch (Error) { - case atomic_write_error::failed_to_create_uniq_file: - OS << "failed_to_create_uniq_file"; - return; - case atomic_write_error::output_stream_error: - OS << "output_stream_error"; - return; - case atomic_write_error::failed_to_rename_temp_file: - OS << "failed_to_rename_temp_file"; - return; - } - llvm_unreachable("unknown atomic_write_error value in " - "failed_to_rename_temp_file::log()"); -} - -llvm::Error llvm::writeFileAtomically(StringRef TempPathModel, - StringRef FinalPath, StringRef Buffer) { - return writeFileAtomically(TempPathModel, FinalPath, - [&Buffer](llvm::raw_ostream &OS) { - OS.write(Buffer.data(), Buffer.size()); - return llvm::Error::success(); - }); -} - -llvm::Error llvm::writeFileAtomically( - StringRef TempPathModel, StringRef FinalPath, - std::function Writer) { - SmallString<128> GeneratedUniqPath; - int TempFD; - if (sys::fs::createUniqueFile(TempPathModel, TempFD, GeneratedUniqPath)) { - return llvm::make_error( - atomic_write_error::failed_to_create_uniq_file); - } - llvm::FileRemover RemoveTmpFileOnFail(GeneratedUniqPath); - - raw_fd_ostream OS(TempFD, /*shouldClose=*/true); - if (llvm::Error Err = Writer(OS)) { - return Err; - } - - OS.close(); - if (OS.has_error()) { - OS.clear_error(); - return llvm::make_error( - atomic_write_error::output_stream_error); - } - - if (sys::fs::rename(/*from=*/GeneratedUniqPath, /*to=*/FinalPath)) { - return llvm::make_error( - atomic_write_error::failed_to_rename_temp_file); - } - - RemoveTmpFileOnFail.releaseFile(); - return Error::success(); -} - Expected FilePermissionsApplier::create(StringRef InputFilename) { sys::fs::file_status Status; @@ -389,5 +331,3 @@ Error FilePermissionsApplier::apply( return Error::success(); } - -char llvm::AtomicFileWriteError::ID; diff --git a/llvm/unittests/Support/CMakeLists.txt b/llvm/unittests/Support/CMakeLists.txt index e828640451cae..92aca0010186b 100644 --- a/llvm/unittests/Support/CMakeLists.txt +++ b/llvm/unittests/Support/CMakeLists.txt @@ -41,7 +41,6 @@ add_llvm_unittest(SupportTests ExtensibleRTTITest.cpp FileCollectorTest.cpp FileOutputBufferTest.cpp - FileUtilitiesTest.cpp FormatVariadicTest.cpp FSUniqueIDTest.cpp GlobPatternTest.cpp diff --git a/llvm/unittests/Support/FileUtilitiesTest.cpp b/llvm/unittests/Support/FileUtilitiesTest.cpp deleted file mode 100644 index ff973e235965f..0000000000000 --- a/llvm/unittests/Support/FileUtilitiesTest.cpp +++ /dev/null @@ -1,54 +0,0 @@ -//===- llvm/unittest/Support/FileUtilitiesTest.cpp - unit tests -----------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "llvm/Support/FileUtilities.h" -#include "llvm/Support/Errc.h" -#include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/FileSystem.h" -#include "llvm/Support/MemoryBuffer.h" -#include "llvm/Support/Path.h" -#include "llvm/Testing/Support/SupportHelpers.h" -#include "gtest/gtest.h" -#include - -using namespace llvm; -using namespace llvm::sys; - -using llvm::unittest::TempDir; - -#define ASSERT_NO_ERROR(x) \ - if (std::error_code ASSERT_NO_ERROR_ec = x) { \ - SmallString<128> MessageStorage; \ - raw_svector_ostream Message(MessageStorage); \ - Message << #x ": did not return errc::success.\n" \ - << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ - << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ - GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ - } else { \ - } - -namespace { -TEST(writeFileAtomicallyTest, Test) { - // Create unique temporary directory for these tests - TempDir RootTestDirectory("writeFileAtomicallyTest", /*Unique*/ true); - - SmallString<128> FinalTestfilePath(RootTestDirectory.path()); - sys::path::append(FinalTestfilePath, "foo.txt"); - const std::string TempUniqTestFileModel = - std::string(FinalTestfilePath) + "-%%%%%%%%"; - const std::string TestfileContent = "fooFOOfoo"; - - llvm::Error Err = llvm::writeFileAtomically(TempUniqTestFileModel, FinalTestfilePath, TestfileContent); - ASSERT_FALSE(static_cast(Err)); - - std::ifstream FinalFileStream(std::string(FinalTestfilePath.str())); - std::string FinalFileContent; - FinalFileStream >> FinalFileContent; - ASSERT_EQ(FinalFileContent, TestfileContent); -} -} // anonymous namespace