-
Notifications
You must be signed in to change notification settings - Fork 992
/
stat.cpp
279 lines (258 loc) · 9.08 KB
/
stat.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* Copyright 2012-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
#include "InMemoryView.h"
#include "watchman_error_category.h"
namespace watchman {
/* The purpose of this function is to help us decide whether we should
* update the parent directory when a non-directory directory entry
* is changed. If so, we schedule re-examining the parent.
* Not all systems report the containing directory as changed in that
* situation, so we decide this based on the capabilities of the watcher.
* If the directory is added to the PendingCollection, this function
* returns true. Otherwise, this function returns false.
*/
bool InMemoryView::propagateToParentDirIfAppropriate(
const std::shared_ptr<w_root_t>& root,
PendingCollection::LockedPtr& coll,
struct timeval now,
const FileInformation& entryStat,
const w_string& dirName,
const watchman_dir* parentDir,
bool isUnlink) {
if ((watcher_->flags & WATCHER_HAS_PER_FILE_NOTIFICATIONS) &&
dirName != root->root_path && !entryStat.isDir() &&
parentDir->last_check_existed) {
/* We're deliberately not propagating any of the flags through
* from statPath() (which calls us); we
* definitely don't want this to be a recursive evaluation.
* Previously, we took pains to avoid turning on VIA_NOTIFY
* here to avoid spuriously marking the node as changed when
* only its atime was changed to avoid tickling some behavior
* in the Pants build system:
* https://github.com/facebook/watchman/issues/305 and
* https://github.com/facebook/watchman/issues/307, but
* unfortunately we do need to set it here because eg:
* Linux doesn't send an inotify event for the parent
* directory for an unlink, and if we rely on stat()
* alone, the filesystem mtime granularity may be too
* low for us to detect that the parent has changed.
* As a compromize, if we're told that the change was due
* to an unlink, then we force delivery of a change event,
* otherwise we'll only do so if the directory has
* observably changed via stat().
*/
coll->add(dirName, now, isUnlink ? W_PENDING_VIA_NOTIFY : 0);
return true;
}
return false;
}
void InMemoryView::statPath(
const std::shared_ptr<w_root_t>& root,
SyncView::LockedPtr& view,
PendingCollection::LockedPtr& coll,
const w_string& full_path,
struct timeval now,
int flags,
const watchman_dir_ent* pre_stat) {
watchman::FileInformation st;
std::error_code errcode;
char path[WATCHMAN_NAME_MAX];
bool recursive = flags & W_PENDING_RECURSIVE;
bool via_notify = flags & W_PENDING_VIA_NOTIFY;
if (root->ignore.isIgnoreDir(full_path)) {
logf(DBG, "{} matches ignore_dir rules\n", full_path);
return;
}
if (full_path.size() > sizeof(path) - 1) {
logf(FATAL, "path {} is too big\n", full_path);
}
memcpy(path, full_path.data(), full_path.size());
path[full_path.size()] = 0;
auto dir_name = full_path.dirName();
auto file_name = full_path.baseName();
auto parentDir = resolveDir(view, dir_name, true);
auto file = parentDir->getChildFile(file_name);
auto dir_ent = parentDir->getChildDir(file_name);
if (pre_stat && pre_stat->has_stat) {
st = pre_stat->stat;
} else {
try {
st = getFileInformation(path, root->case_sensitive);
log(DBG,
"getFileInformation(",
path,
") file=",
fmt::ptr(file),
" dir=",
fmt::ptr(dir_ent),
"\n");
} catch (const std::system_error& exc) {
errcode = exc.code();
log(DBG,
"getFileInformation(",
path,
") file=",
fmt::ptr(file),
" dir=",
fmt::ptr(dir_ent),
" failed: ",
exc.what(),
"\n");
}
}
if (errcode == watchman::error_code::no_such_file_or_directory ||
errcode == watchman::error_code::not_a_directory) {
/* it's not there, update our state */
if (dir_ent) {
markDirDeleted(view, dir_ent, now, true);
watchman::log(
watchman::DBG,
"getFileInformation(",
path,
") -> ",
errcode.message(),
" so stopping watch\n");
}
if (file) {
if (file->exists) {
watchman::log(
watchman::DBG,
"getFileInformation(",
path,
") -> ",
errcode.message(),
" so marking ",
file->getName(),
" deleted\n");
file->exists = false;
markFileChanged(view, file, now);
}
} else {
// It was created and removed before we could ever observe it
// in the filesystem. We need to generate a deleted file
// representation of it now, so that subscription clients can
// be notified of this event
file = getOrCreateChildFile(view, parentDir, file_name, now);
log(DBG,
"getFileInformation(",
path,
") -> ",
errcode.message(),
" and file node was NULL. "
"Generating a deleted node.\n");
file->exists = false;
markFileChanged(view, file, now);
}
if (!propagateToParentDirIfAppropriate(
root,
coll,
now,
file->stat,
dir_name,
parentDir,
/* isUnlink= */ true) &&
root->case_sensitive == CaseSensitivity::CaseInSensitive &&
!w_string_equal(dir_name, root->root_path) &&
parentDir->last_check_existed) {
/* If we rejected the name because it wasn't canonical,
* we need to ensure that we look in the parent dir to discover
* the new item(s) */
logf(
DBG,
"we're case insensitive, and {} is ENOENT, "
"speculatively look at parent dir {}\n",
path,
dir_name);
coll->add(dir_name, now, W_PENDING_CRAWL_ONLY);
}
} else if (errcode.value()) {
log(ERR,
"getFileInformation(",
path,
") failed and not handled! -> ",
errcode.message(),
" value=",
errcode.value(),
" category=",
errcode.category().name(),
"\n");
} else {
if (!file) {
file = getOrCreateChildFile(view, parentDir, file_name, now);
}
if (!file->exists) {
/* we're transitioning from deleted to existing,
* so we're effectively new again */
file->ctime.ticks = mostRecentTick_;
file->ctime.timestamp = now.tv_sec;
/* if a dir was deleted and now exists again, we want
* to crawl it again */
recursive = true;
}
if (!file->exists || via_notify || did_file_change(&file->stat, &st)) {
logf(
DBG,
"file changed exists={} via_notify={} stat-changed={} isdir={} {}\n",
file->exists,
via_notify,
file->exists && !via_notify,
st.isDir(),
path);
file->exists = true;
markFileChanged(view, file, now);
// If the inode number changed then we definitely need to recursively
// examine any children because we cannot assume that the kernel will
// have given us the correct hints about this change. BTRFS is one
// example of a filesystem where this has been observed to happen.
if (file->stat.ino != st.ino) {
recursive = true;
}
}
memcpy(&file->stat, &st, sizeof(file->stat));
// check for symbolic link
if (st.isSymlink() && root->config.getBool("watch_symlinks", false)) {
root->inner.pending_symlink_targets.lock()->add(full_path, now, 0);
}
if (st.isDir()) {
if (dir_ent == NULL) {
recursive = true;
} else {
// Ensure that we believe that this node exists
dir_ent->last_check_existed = true;
}
// Don't recurse if our parent is an ignore dir
if (!root->ignore.isIgnoreVCS(dir_name) ||
// but do if we're looking at the cookie dir (stat_path is never
// called for the root itself)
w_string_equal(full_path, root->cookies.cookieDir())) {
if (!(watcher_->flags & WATCHER_HAS_PER_FILE_NOTIFICATIONS)) {
/* we always need to crawl, but may not need to be fully recursive */
coll->add(
full_path,
now,
W_PENDING_CRAWL_ONLY | (recursive ? W_PENDING_RECURSIVE : 0));
} else {
/* we get told about changes on the child, so we only
* need to crawl if we've never seen the dir before.
* An exception is that fsevents will only report the root
* of a dir rename and not a rename event for all of its
* children. */
if (recursive) {
coll->add(
full_path, now, W_PENDING_RECURSIVE | W_PENDING_CRAWL_ONLY);
}
}
}
} else if (dir_ent) {
// We transitioned from dir to file (see fishy.php), so we should prune
// our former tree here
markDirDeleted(view, dir_ent, now, true);
}
propagateToParentDirIfAppropriate(
root, coll, now, st, dir_name, parentDir, /* isUnlink= */ false);
}
}
} // namespace watchman
/* vim:ts=2:sw=2:et:
*/