forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.cpp
189 lines (160 loc) · 4.35 KB
/
state.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* Copyright 2012-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
#include <folly/String.h>
#include <folly/Synchronized.h>
#include "Logging.h"
#include "watchman_error_category.h"
using namespace watchman;
std::string watchman_state_file;
int dont_save_state = 0;
/** The state saving thread is responsible for writing out the
* persistent information about the users watches.
* It runs in its own thread so that we avoid the possibility
* of self deadlock if various threads were to immediately
* save the state when things are changing.
*
* This uses a simple condition variable to wait for and be
* notified of state changes.
*/
namespace {
struct state {
bool needsSave{false};
};
folly::Synchronized<state, std::mutex> saveState;
std::condition_variable stateCond;
std::thread state_saver_thread;
} // namespace
static bool do_state_save(void);
static void state_saver() noexcept {
bool do_save;
w_set_thread_name("statesaver");
while (!w_is_stopping()) {
{
auto state = saveState.lock();
if (!state->needsSave) {
stateCond.wait(state.getUniqueLock());
}
do_save = state->needsSave;
state->needsSave = false;
}
if (do_save) {
do_state_save();
}
}
}
void w_state_shutdown(void) {
if (dont_save_state) {
return;
}
stateCond.notify_one();
state_saver_thread.join();
}
bool w_state_load(void) {
if (dont_save_state) {
return true;
}
state_saver_thread = std::thread(state_saver);
json_ref state;
try {
state = json_load_file(watchman_state_file.c_str(), 0);
} catch (const std::system_error& exc) {
if (exc.code() == watchman::error_code::no_such_file_or_directory) {
// No need to alarm anyone if we've never written a state file
return false;
}
logf(
ERR,
"failed to load json from {}: {}\n",
watchman_state_file,
folly::exceptionStr(exc).toStdString());
return false;
} catch (const std::exception& exc) {
logf(
ERR,
"failed to parse json from {}: {}\n",
watchman_state_file,
folly::exceptionStr(exc).toStdString());
return false;
}
if (!w_root_load_state(state)) {
return false;
}
return true;
}
#if defined(HAVE_MKOSTEMP) && defined(sun)
// Not guaranteed to be defined in stdlib.h
extern int mkostemp(char*, int);
#endif
std::unique_ptr<watchman_stream> w_mkstemp(char* templ) {
#if defined(_WIN32)
char* name = _mktemp(templ);
if (!name) {
return nullptr;
}
// Most annoying aspect of windows is the latency around
// file handle exclusivity. We could avoid this dumb loop
// by implementing our own mkostemp, but this is the most
// expedient option for the moment.
for (size_t attempts = 0; attempts < 10; ++attempts) {
auto stm = w_stm_open(name, O_RDWR | O_CLOEXEC | O_CREAT | O_TRUNC, 0600);
if (stm) {
return stm;
}
if (errno == EACCES) {
/* sleep override */ std::this_thread::sleep_for(
std::chrono::microseconds(2000));
continue;
}
return nullptr;
}
return nullptr;
#else
FileDescriptor fd;
#ifdef HAVE_MKOSTEMP
fd = FileDescriptor(
mkostemp(templ, O_CLOEXEC), FileDescriptor::FDType::Generic);
#else
fd = FileDescriptor(mkstemp(templ), FileDescriptor::FDType::Generic);
#endif
if (!fd) {
return nullptr;
}
fd.setCloExec();
return w_stm_fdopen(std::move(fd));
#endif
}
static bool do_state_save(void) {
w_jbuffer_t buffer;
auto state = json_object();
auto file = w_stm_open(
watchman_state_file.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0600);
if (!file) {
log(ERR,
"save_state: unable to open ",
watchman_state_file,
" for write: ",
folly::errnoStr(errno),
"\n");
return false;
}
state.set("version", typed_string_to_json(PACKAGE_VERSION, W_STRING_UNICODE));
/* now ask the different subsystems to fill out the state */
if (!w_root_save_state(state)) {
return false;
}
/* we've prepared what we're going to save, so write it out */
buffer.jsonEncodeToStream(state, file.get(), JSON_INDENT(4));
return true;
}
/** Arranges for the state to be saved.
* Does not immediately save the state. */
void w_state_save(void) {
if (dont_save_state) {
return;
}
saveState.lock()->needsSave = true;
stateCond.notify_one();
}
/* vim:ts=2:sw=2:et:
*/