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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
content / browser / file_system_access / file_path_watcher / file_path_watcher_change_tracker.cc [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/file_system_access/file_path_watcher/file_path_watcher_change_tracker.h"
#include <windows.h>
#include <winnt.h>
#include "base/files/file_util.h"
#include "content/browser/file_system_access/file_path_watcher/file_path_watcher.h"
namespace content {
namespace {
enum class PathRelation {
kSelf,
kAncestor,
kDescendant,
kDirectChild,
kOther,
};
// Finds `related_path`'s relationship to `self_path` from `self_path`'s
// perspective.
PathRelation FindPathRelation(const base::FilePath& self_path,
const base::FilePath& related_path) {
const auto self_components = self_path.GetComponents();
const auto related_components = related_path.GetComponents();
for (size_t i = 0;
i < self_components.size() && i < related_components.size(); ++i) {
if (self_components[i] != related_components[i]) {
return PathRelation::kOther;
}
}
if (self_components.size() + 1 == related_components.size()) {
return PathRelation::kDirectChild;
}
if (self_components.size() < related_components.size()) {
return PathRelation::kDescendant;
}
if (self_components.size() > related_components.size()) {
return PathRelation::kAncestor;
}
return PathRelation::kSelf;
}
FilePathWatcher::FilePathType GetFilePathType(const base::FilePath& file) {
base::File::Info file_info;
if (!GetFileInfo(file, &file_info)) {
return FilePathWatcher::FilePathType::kUnknown;
}
return file_info.is_directory ? FilePathWatcher::FilePathType::kDirectory
: FilePathWatcher::FilePathType::kFile;
}
FilePathWatcher::ChangeType ToChangeType(DWORD win_change_type) {
switch (win_change_type) {
case FILE_ACTION_ADDED:
return FilePathWatcher::ChangeType::kCreated;
case FILE_ACTION_REMOVED:
return FilePathWatcher::ChangeType::kDeleted;
case FILE_ACTION_MODIFIED:
return FilePathWatcher::ChangeType::kModified;
case FILE_ACTION_RENAMED_OLD_NAME:
case FILE_ACTION_RENAMED_NEW_NAME:
return FilePathWatcher::ChangeType::kMoved;
default:
NOTREACHED();
}
}
// Returns the path `descendant_path` would be in if `ancestor_path` was moved
// to or from `moved_path`.
base::FilePath GetMovedPathOfDescendant(const base::FilePath& descendant_path,
const base::FilePath& ancestor_path,
const base::FilePath& moved_path) {
const auto ancestor_components = ancestor_path.GetComponents();
const auto descendant_components = descendant_path.GetComponents();
auto moved_path_of_descendant = moved_path;
for (size_t i = ancestor_components.size(); i < descendant_components.size();
++i) {
moved_path_of_descendant =
moved_path_of_descendant.Append(descendant_components[i]);
}
return moved_path_of_descendant;
}
} // namespace
FilePathWatcherChangeTracker::FilePathWatcherChangeTracker(
base::FilePath target_path,
FilePathWatcher::Type type)
: target_path_(target_path), type_(type) {
// Need to check for target existence in case the first change is a
// FILE_ACTION_RENAMED_OLD_NAME event.
target_status_ =
GetFilePathType(target_path_) == FilePathWatcher::FilePathType::kUnknown
? ExistenceStatus::kGone
: ExistenceStatus::kExists;
}
FilePathWatcherChangeTracker::FilePathWatcherChangeTracker(
FilePathWatcherChangeTracker&&) = default;
FilePathWatcherChangeTracker::~FilePathWatcherChangeTracker() = default;
void FilePathWatcherChangeTracker::AddChange(base::FilePath path,
DWORD win_change_type) {
// Attempt to coalesce an overwrite into a single move event.
//
// Windows reports overwrites as a delete then a move.
if (last_deleted_change_) {
bool is_overwrite_event = win_change_type == FILE_ACTION_RENAMED_NEW_NAME &&
last_deleted_change_->modified_path == path;
bool next_event_could_be_overwrite_event =
win_change_type == FILE_ACTION_RENAMED_OLD_NAME;
// If it's not an overwrite and the next event couldn't be an overwrite
// event, then report the `last_deleted_change_` event instead of
// coalescing.
if (!is_overwrite_event && !next_event_could_be_overwrite_event) {
changes_.push_back(*std::exchange(last_deleted_change_, std::nullopt));
}
// Coalesce the overwrite event by dropping the last deleted change.
if (!next_event_could_be_overwrite_event) {
last_deleted_change_ = std::nullopt;
}
}
// Attempt to coalesce move.
if (win_change_type == FILE_ACTION_RENAMED_OLD_NAME) {
last_moved_from_path_ = path;
return;
}
PathRelation path_relation = FindPathRelation(target_path_, path);
ChangeInfo change;
change.change_type = ToChangeType(win_change_type);
change.modified_path = std::move(path);
if (win_change_type == FILE_ACTION_RENAMED_NEW_NAME) {
change.moved_from_path = std::move(last_moved_from_path_);
}
switch (path_relation) {
case PathRelation::kSelf:
HandleSelfChange(std::move(change));
break;
case PathRelation::kAncestor:
HandleAncestorChange(std::move(change));
break;
case PathRelation::kDescendant:
HandleDescendantChange(std::move(change), /*is_direct_child=*/false);
break;
case PathRelation::kDirectChild:
HandleDescendantChange(std::move(change), /*is_direct_child=*/true);
break;
case PathRelation::kOther:
HandleOtherChange(std::move(change));
break;
}
}
void FilePathWatcherChangeTracker::MayHaveMissedChanges() {
target_status_ =
GetFilePathType(target_path_) == FilePathWatcher::FilePathType::kUnknown
? ExistenceStatus::kGone
: ExistenceStatus::kExists;
}
std::vector<FilePathWatcher::ChangeInfo>
FilePathWatcherChangeTracker::PopChanges(bool next_change_soon) {
if (!next_change_soon) {
if (target_status_ == ExistenceStatus::kMayHaveMovedIntoPlace) {
// Decide whether the target moved into place or not.
ExistenceStatus status = GetFilePathType(target_path_) ==
FilePathWatcher::FilePathType::kUnknown
? ExistenceStatus::kGone
: ExistenceStatus::kExists;
HandleChangeEffect(status, status);
}
if (last_deleted_change_) {
changes_.push_back(*std::exchange(last_deleted_change_, std::nullopt));
}
}
return std::move(changes_);
}
bool FilePathWatcherChangeTracker::KnowTargetExists() {
return target_status_ == ExistenceStatus::kExists;
}
void FilePathWatcherChangeTracker::ConvertMoveToCreateIfOutOfScope(
ChangeInfo& change) {
CHECK_EQ(change.change_type, ChangeType::kMoved);
CHECK(change.moved_from_path.has_value());
if (FindPathRelation(target_path_, *change.moved_from_path) ==
PathRelation::kOther) {
change.moved_from_path = std::nullopt;
change.change_type = ChangeType::kCreated;
}
}
void FilePathWatcherChangeTracker::HandleChangeEffect(
ExistenceStatus before_action,
ExistenceStatus after_action) {
// If the target may have moved into place and we now know that it exists,
// then it definitely moved into place.
if (target_status_ == ExistenceStatus::kMayHaveMovedIntoPlace &&
before_action == ExistenceStatus::kExists) {
CHECK(!last_move_change_.moved_from_path.has_value());
last_move_change_.modified_path = target_path_;
last_move_change_.file_path_type = GetFilePathType(target_path_);
changes_.push_back(std::move(last_move_change_));
}
target_status_ = after_action;
}
void FilePathWatcherChangeTracker::HandleSelfChange(ChangeInfo change) {
switch (change.change_type) {
case ChangeType::kCreated:
HandleChangeEffect(ExistenceStatus::kGone, ExistenceStatus::kExists);
change.file_path_type = GetFilePathType(target_path_);
changes_.push_back(std::move(change));
break;
case ChangeType::kMoved:
HandleChangeEffect(ExistenceStatus::kGone, ExistenceStatus::kExists);
change.file_path_type = GetFilePathType(target_path_);
ConvertMoveToCreateIfOutOfScope(change);
changes_.push_back(std::move(change));
break;
case ChangeType::kDeleted:
HandleChangeEffect(ExistenceStatus::kExists, ExistenceStatus::kGone);
last_deleted_change_ = std::move(change);
break;
case ChangeType::kModified:
HandleChangeEffect(ExistenceStatus::kExists, ExistenceStatus::kExists);
change.file_path_type = GetFilePathType(target_path_);
// Don't report modifications on directories.
if (change.file_path_type != FilePathWatcher::FilePathType::kDirectory) {
changes_.push_back(std::move(change));
}
break;
case ChangeType::kUnknown:
// All changes passed into here come from `ToChangeType` which doesn't
// return `kUnknown`.
NOTREACHED();
}
}
void FilePathWatcherChangeTracker::HandleDescendantChange(
ChangeInfo change,
bool is_direct_child) {
// Any notification on a descendant means the target existed before and
// after.
HandleChangeEffect(ExistenceStatus::kExists, ExistenceStatus::kExists);
// Only report descendants that are direct children in non recursive types.
if (type_ == FilePathWatcher::Type::kNonRecursive && !is_direct_child) {
return;
}
// Get the `file_path_type` if it's not a deleted event. If it's a deleted
// event, then there's no way to know.
change.file_path_type = change.change_type == ChangeType::kDeleted
? FilePathWatcher::FilePathType::kUnknown
: GetFilePathType(change.modified_path);
// Don't report modifications on directories.
if (change.change_type == ChangeType::kModified &&
change.file_path_type == FilePathWatcher::FilePathType::kDirectory) {
return;
}
if (change.change_type == ChangeType::kDeleted) {
last_deleted_change_ = std::move(change);
return;
}
if (change.change_type == ChangeType::kMoved) {
ConvertMoveToCreateIfOutOfScope(change);
}
changes_.push_back(std::move(change));
}
void FilePathWatcherChangeTracker::HandleAncestorChange(ChangeInfo change) {
switch (change.change_type) {
case ChangeType::kDeleted:
case ChangeType::kCreated:
// Can't delete a directory until all its children are gone. Can't create
// a directory with existing children.
HandleChangeEffect(ExistenceStatus::kGone, ExistenceStatus::kGone);
break;
case ChangeType::kMoved:
HandleChangeEffect(ExistenceStatus::kGone,
ExistenceStatus::kMayHaveMovedIntoPlace);
ConvertMoveToCreateIfOutOfScope(change);
last_move_change_ = std::move(change);
break;
case ChangeType::kModified:
// This tells us nothing.
break;
case ChangeType::kUnknown:
// All changes passed into here come from `ToChangeType` which doesn't
// return `kUnknown`.
NOTREACHED();
}
}
void FilePathWatcherChangeTracker::HandleOtherChange(ChangeInfo change) {
// Only moved types have the possibility of being in scope.
if (change.change_type != ChangeType::kMoved) {
return;
}
CHECK(change.moved_from_path.has_value());
base::FilePath moved_from_path = *std::move(change.moved_from_path);
base::FilePath moved_to_path = std::move(change.modified_path);
// Move out of scopes are reported as deleted.
change.change_type = ChangeType::kDeleted;
change.moved_from_path = std::nullopt;
switch (FindPathRelation(target_path_, moved_from_path)) {
case PathRelation::kSelf:
HandleChangeEffect(ExistenceStatus::kExists, ExistenceStatus::kGone);
change.file_path_type = GetFilePathType(moved_to_path);
change.modified_path = std::move(moved_from_path);
changes_.push_back(std::move(change));
break;
case PathRelation::kAncestor: {
bool target_exists_before_move =
target_status_ == ExistenceStatus::kExists;
HandleChangeEffect(target_status_, ExistenceStatus::kGone);
// If the target wasn't there before the move, then it wasn't moved.
if (!target_exists_before_move) {
break;
}
// Figure out where the target was exists now so that you can get its
// `file_path_type`.
base::FilePath target_move_to_path = GetMovedPathOfDescendant(
target_path_, moved_from_path, moved_to_path);
change.file_path_type = GetFilePathType(target_move_to_path);
change.modified_path = target_path_;
changes_.push_back(std::move(change));
break;
}
case PathRelation::kDescendant:
case PathRelation::kDirectChild:
// Any notification on a descendant means the target existed before and
// after.
HandleChangeEffect(ExistenceStatus::kExists, ExistenceStatus::kExists);
change.file_path_type = GetFilePathType(moved_to_path);
change.modified_path = std::move(moved_from_path);
changes_.push_back(std::move(change));
break;
case PathRelation::kOther:
// We don't care about files moving around out of scope.
break;
}
}
} // namespace content