Skip to content

Commit

Permalink
Try to make writeSnapshot atomic for watchman backend
Browse files Browse the repository at this point in the history
  • Loading branch information
yamadapc committed May 28, 2024
1 parent 8dfd994 commit e36eb50
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/watchman/WatchmanBackend.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <string>
#include <fstream>
#include <stdlib.h>
#include <filesystem>
#include <algorithm>
#include "../DirTree.hh"
#include "../Event.hh"
Expand Down Expand Up @@ -241,8 +242,14 @@ void WatchmanBackend::writeSnapshot(Watcher &watcher, std::string *snapshotPath)
std::unique_lock<std::mutex> lock(mMutex);
watchmanWatch(watcher.mDir);

std::ofstream ofs(*snapshotPath);
ofs << clock(watcher);
auto clockValue = clock(watcher);
auto temporaryDirectory = std::filesystem::temp_directory_path();
auto temporaryFilePath = temporaryDirectory.append("tmp-parcel-snapshot.txt");
{
std::ofstream ofs(temporaryFilePath);
ofs << clockValue;
}
std::filesystem::rename(temporaryFilePath, *snapshotPath);
}

void WatchmanBackend::getEventsSince(Watcher &watcher, std::string *snapshotPath) {
Expand Down

0 comments on commit e36eb50

Please sign in to comment.