forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CookieSync.cpp
150 lines (124 loc) · 3.72 KB
/
CookieSync.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
/* Copyright 2012-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman_system.h"
#include <exception>
#include "watchman.h"
namespace watchman {
CookieSync::Cookie::Cookie(w_string name) : fileName(name) {}
CookieSync::Cookie::~Cookie() {
// The file may not exist at this point; we're just taking this
// opportunity to remove it if nothing else has done so already.
// We don't care about the return code; best effort is fine.
unlink(fileName.c_str());
}
CookieSync::CookieSync(const w_string& dir) {
setCookieDir(dir);
}
CookieSync::~CookieSync() {
// Wake up anyone that might have been waiting on us
abortAllCookies();
}
void CookieSync::setCookieDir(const w_string& dir) {
cookieDir_ = dir;
char hostname[256];
gethostname(hostname, sizeof(hostname));
hostname[sizeof(hostname) - 1] = '\0';
cookiePrefix_ = w_string::printf(
"%.*s/" WATCHMAN_COOKIE_PREFIX "%s-%d-",
int(cookieDir_.size()),
cookieDir_.data(),
hostname,
int(getpid()));
}
Future<Unit> CookieSync::sync() {
/* generate a cookie name: cookie prefix + id */
auto path_str = w_string::printf(
"%.*s%" PRIu32,
int(cookiePrefix_.size()),
cookiePrefix_.data(),
serial_++);
auto cookie = make_unique<Cookie>(path_str);
auto future = cookie->promise.getFuture();
/* insert our cookie in the map */
{
auto wlock = cookies_.wlock();
auto& map = *wlock;
map[path_str] = std::move(cookie);
}
/* then touch the file */
auto file = w_stm_open(
path_str.c_str(), O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0700);
if (!file) {
auto errcode = errno;
// The erase will unlink the file
cookies_.wlock()->erase(path_str);
throw std::system_error(
errcode,
std::generic_category(),
to<std::string>(
"sync: creat(", path_str, ") failed: ", strerror(errcode)));
}
log(DBG, "sync created cookie file ", path_str, "\n");
return future;
}
void CookieSync::syncToNow(std::chrono::milliseconds timeout) {
/* compute deadline */
using namespace std::chrono;
auto deadline = system_clock::now() + timeout;
while (true) {
auto cookie = sync();
if (!cookie.wait_for(timeout)) {
auto why = to<std::string>(
"syncToNow: timed out waiting for cookie file to be "
"observed by watcher within ",
timeout.count(),
" milliseconds");
log(ERR, why, "\n");
throw std::system_error(ETIMEDOUT, std::generic_category(), why);
}
if (cookie.result().hasValue()) {
// Success!
return;
}
// Sync was aborted by a recrawl; recompute the timeout
// and wait again if we still have time
timeout = duration_cast<milliseconds>(deadline - system_clock::now());
if (timeout.count() <= 0) {
cookie.result().throwIfError();
}
}
}
void CookieSync::abortAllCookies() {
std::unordered_map<w_string, std::unique_ptr<Cookie>> cookies;
{
auto map = cookies_.wlock();
std::swap(*map, cookies);
}
for (auto& it : cookies) {
log(ERR, "syncToNow: aborting cookie ", it.first, "\n");
it.second->promise.setException(
std::make_exception_ptr(CookieSyncAborted()));
}
}
void CookieSync::notifyCookie(const w_string& path) {
std::unique_ptr<Cookie> cookie;
{
auto map = cookies_.wlock();
auto cookie_iter = map->find(path);
log(DBG,
"cookie for ",
path,
"? ",
cookie_iter != map->end() ? "yes" : "no",
"\n");
if (cookie_iter != map->end()) {
cookie = std::move(cookie_iter->second);
map->erase(cookie_iter);
}
}
if (cookie) {
cookie->promise.setValue(Unit{});
// cookie file will be unlinked when we exit this scope
}
}
}