[PATCH] D55417: [clangd] Change disk-backed storage to be atomic
Kadir Cetinkaya via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 7 04:57:07 PST 2018
kadircet updated this revision to Diff 177181.
kadircet marked 2 inline comments as done.
kadircet added a comment.
- Address comments
Repository:
rCTE Clang Tools Extra
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D55417/new/
https://reviews.llvm.org/D55417
Files:
clangd/index/BackgroundIndexStorage.cpp
Index: clangd/index/BackgroundIndexStorage.cpp
===================================================================
--- clangd/index/BackgroundIndexStorage.cpp
+++ clangd/index/BackgroundIndexStorage.cpp
@@ -9,6 +9,8 @@
#include "Logger.h"
#include "index/Background.h"
+#include "llvm/ADT/ScopeExit.h"
+#include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
@@ -33,6 +35,29 @@
return ShardRootSS.str();
}
+template <typename WriterFunction>
+llvm::Error writeAtomically(llvm::StringRef OutPath, WriterFunction &&Writer) {
+ // Write to a temporary file first.
+ llvm::Twine TempPath(OutPath, ".tmp.");
+ TempPath.concat(std::to_string(rand()));
+ std::error_code EC;
+ llvm::raw_fd_ostream OS(TempPath.str(), EC);
+ if (EC)
+ return llvm::errorCodeToError(EC);
+ // Make sure temp file is destroyed at exit.
+ auto _ =
+ llvm::make_scope_exit([TempPath] { llvm::sys::fs::remove(TempPath); });
+ Writer(OS);
+ OS.close();
+ if (OS.has_error())
+ return llvm::errorCodeToError(OS.error());
+ // Then move to real location.
+ EC = llvm::sys::fs::rename(TempPath, OutPath);
+ if (EC)
+ return llvm::errorCodeToError(EC);
+ return llvm::ErrorSuccess();
+}
+
// Uses disk as a storage for index shards. Creates a directory called
// ".clangd-index/" under the path provided during construction.
class DiskBackedIndexStorage : public BackgroundIndexStorage {
@@ -70,14 +95,9 @@
llvm::Error storeShard(llvm::StringRef ShardIdentifier,
IndexFileOut Shard) const override {
- auto ShardPath = getShardPathFromFilePath(DiskShardRoot, ShardIdentifier);
- std::error_code EC;
- llvm::raw_fd_ostream OS(ShardPath, EC);
- if (EC)
- return llvm::errorCodeToError(EC);
- OS << Shard;
- OS.close();
- return llvm::errorCodeToError(OS.error());
+ return writeAtomically(
+ getShardPathFromFilePath(DiskShardRoot, ShardIdentifier),
+ [&Shard](llvm::raw_ostream &OS) { OS << Shard; });
}
};
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55417.177181.patch
Type: text/x-patch
Size: 2079 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181207/d85e50bd/attachment.bin>
More information about the cfe-commits
mailing list