[llvm] r371103 - [Support] Add writeFileAtomically() to FileUtilities
Jan Korous via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 5 11:10:30 PDT 2019
Author: jkorous
Date: Thu Sep 5 11:10:29 2019
New Revision: 371103
URL: http://llvm.org/viewvc/llvm-project?rev=371103&view=rev
Log:
[Support] Add writeFileAtomically() to FileUtilities
Differential Revision: https://reviews.llvm.org/D66859
Added:
llvm/trunk/unittests/Support/FileUtilitiesTest.cpp
Modified:
llvm/trunk/include/llvm/Support/FileUtilities.h
llvm/trunk/lib/Support/FileUtilities.cpp
llvm/trunk/unittests/Support/CMakeLists.txt
Modified: llvm/trunk/include/llvm/Support/FileUtilities.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/FileUtilities.h?rev=371103&r1=371102&r2=371103&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/FileUtilities.h (original)
+++ llvm/trunk/include/llvm/Support/FileUtilities.h Thu Sep 5 11:10:29 2019
@@ -14,6 +14,8 @@
#ifndef LLVM_SUPPORT_FILEUTILITIES_H
#define LLVM_SUPPORT_FILEUTILITIES_H
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Errc.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
@@ -72,6 +74,11 @@ namespace llvm {
/// will not be removed when the object is destroyed.
void releaseFile() { DeleteIt = false; }
};
+
+ /// 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.
+ llvm::Error writeFileAtomically(StringRef TempPathModel, StringRef FinalPath,
+ StringRef Buffer);
} // End llvm namespace
#endif
Modified: llvm/trunk/lib/Support/FileUtilities.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/FileUtilities.cpp?rev=371103&r1=371102&r2=371103&view=diff
==============================================================================
--- llvm/trunk/lib/Support/FileUtilities.cpp (original)
+++ llvm/trunk/lib/Support/FileUtilities.cpp Thu Sep 5 11:10:29 2019
@@ -15,6 +15,7 @@
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <cctype>
#include <cmath>
@@ -264,3 +265,37 @@ int llvm::DiffFilesWithTolerance(StringR
return CompareFailed;
}
+
+Error llvm::writeFileAtomically(StringRef TempPathModel, StringRef FinalPath,
+ StringRef Buffer) {
+ SmallString<128> GeneratedUniqPath;
+ int TempFD;
+ if (const std::error_code Error = sys::fs::createUniqueFile(
+ TempPathModel.str(), TempFD, GeneratedUniqPath)) {
+ return createStringError(
+ Error, "failed to create temporary file with model \"%s\"",
+ TempPathModel.str().c_str());
+ }
+
+ raw_fd_ostream OS(TempFD, /*shouldClose=*/true);
+ OS.write(Buffer.data(), Buffer.size());
+ OS.close();
+ TempFD = -1;
+
+ if (OS.has_error()) {
+ const std::error_code Error = OS.error();
+ OS.clear_error();
+ return createStringError(Error, "failed to write to \"%s\"",
+ GeneratedUniqPath.c_str());
+ }
+
+ if (const std::error_code Error =
+ sys::fs::rename(/*from=*/GeneratedUniqPath.c_str(),
+ /*to=*/FinalPath.str().c_str())) {
+ return createStringError(Error, "failed to rename file \"%s\" to \"%s\"",
+ GeneratedUniqPath.c_str(),
+ FinalPath.str().c_str());
+ }
+
+ return Error::success();
+}
Modified: llvm/trunk/unittests/Support/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/CMakeLists.txt?rev=371103&r1=371102&r2=371103&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/CMakeLists.txt (original)
+++ llvm/trunk/unittests/Support/CMakeLists.txt Thu Sep 5 11:10:29 2019
@@ -33,6 +33,7 @@ add_llvm_unittest(SupportTests
FileCheckTest.cpp
FileCollectorTest.cpp
FileOutputBufferTest.cpp
+ FileUtilitiesTest.cpp
FormatVariadicTest.cpp
GlobPatternTest.cpp
Host.cpp
Added: llvm/trunk/unittests/Support/FileUtilitiesTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/FileUtilitiesTest.cpp?rev=371103&view=auto
==============================================================================
--- llvm/trunk/unittests/Support/FileUtilitiesTest.cpp (added)
+++ llvm/trunk/unittests/Support/FileUtilitiesTest.cpp Thu Sep 5 11:10:29 2019
@@ -0,0 +1,52 @@
+//===- 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 "gtest/gtest.h"
+#include <fstream>
+
+using namespace llvm;
+using namespace llvm::sys;
+
+#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
+ SmallString<128> RootTestDirectory;
+ ASSERT_NO_ERROR(
+ fs::createUniqueDirectory("writeFileAtomicallyTest", RootTestDirectory));
+
+ SmallString<128> FinalTestfilePath(RootTestDirectory);
+ sys::path::append(FinalTestfilePath, "foo.txt");
+ const std::string TempUniqTestFileModel = FinalTestfilePath.str().str() + "-%%%%%%%%";
+ const std::string TestfileContent = "fooFOOfoo";
+
+ llvm::Error Err = llvm::writeFileAtomically(TempUniqTestFileModel, FinalTestfilePath, TestfileContent);
+ ASSERT_FALSE(static_cast<bool>(Err));
+
+ std::ifstream FinalFileStream(FinalTestfilePath.str());
+ std::string FinalFileContent;
+ FinalFileStream >> FinalFileContent;
+ ASSERT_EQ(FinalFileContent, TestfileContent);
+}
+} // anonymous namespace
More information about the llvm-commits
mailing list