forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheden.cpp
1251 lines (1095 loc) · 43.3 KB
/
eden.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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2016-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
#include <folly/String.h>
#include <folly/futures/Future.h>
#include <folly/io/async/EventBase.h>
#include <folly/io/async/EventBaseManager.h>
#include <thrift/lib/cpp/async/TAsyncSocket.h>
#include <thrift/lib/cpp2/async/HeaderClientChannel.h>
#include <thrift/lib/cpp2/async/RocketClientChannel.h>
#include <algorithm>
#include <chrono>
#include <iterator>
#include <thread>
#include "eden/fs/service/gen-cpp2/StreamingEdenService.h"
#include "thirdparty/wildmatch/wildmatch.h"
#include "watchman_error_category.h"
#include "ChildProcess.h"
#include "LRUCache.h"
#include "QueryableView.h"
#include "ThreadPool.h"
using apache::thrift::async::TAsyncSocket;
using facebook::eden::EdenError;
using facebook::eden::FileDelta;
using facebook::eden::FileInformation;
using facebook::eden::FileInformationOrError;
using facebook::eden::Glob;
using facebook::eden::GlobParams;
using facebook::eden::JournalPosition;
using facebook::eden::SHA1Result;
using facebook::eden::StreamingEdenServiceAsyncClient;
using folly::Optional;
using std::make_unique;
namespace {
/** Represents a cache key for getFilesChangedBetweenCommits()
* It is unfortunately a bit boilerplate-y due to the requirements
* of unordered_map<>. */
struct BetweenCommitKey {
std::string sinceCommit;
std::string toCommit;
bool operator==(const BetweenCommitKey& other) const {
return sinceCommit == other.sinceCommit && toCommit == other.toCommit;
}
std::size_t hashValue() const {
using namespace watchman;
return hash_128_to_64(
w_hash_bytes(sinceCommit.data(), sinceCommit.size(), 0),
w_hash_bytes(toCommit.data(), toCommit.size(), 0));
}
};
} // namespace
namespace std {
/** Ugly glue for unordered_map to hash BetweenCommitKey items */
template <>
struct hash<BetweenCommitKey> {
std::size_t operator()(BetweenCommitKey const& key) const {
return key.hashValue();
}
};
} // namespace std
namespace watchman {
namespace {
struct NameAndDType {
std::string name;
DType dtype;
explicit NameAndDType(const std::string& name, DType dtype = DType::Unknown)
: name(name), dtype(dtype) {}
};
/** This is a helper for settling out subscription events.
* We have a single instance of the callback object that we schedule
* each time we get an update from the eden server. If we are already
* scheduled we will cancel it and reschedule it.
*/
class SettleCallback : public folly::HHWheelTimer::Callback {
public:
SettleCallback(folly::EventBase* eventBase, std::shared_ptr<w_root_t> root)
: eventBase_(eventBase), root_(std::move(root)) {}
void timeoutExpired() noexcept override {
try {
auto settledPayload = json_object({{"settled", json_true()}});
root_->unilateralResponses->enqueue(std::move(settledPayload));
} catch (const std::exception& exc) {
watchman::log(
watchman::ERR,
"error while dispatching settle payload; cancel watch: ",
exc.what(),
"\n");
eventBase_->terminateLoopSoon();
}
}
void callbackCanceled() noexcept override {
// We must override this because the default is to call timeoutExpired().
// We don't want that to happen because we're only canceled in the case
// where we want to delay the timeoutExpired() callback.
}
private:
folly::EventBase* eventBase_;
std::shared_ptr<w_root_t> root_;
};
/** Execute a functor, retrying it if we encounter an ESTALE exception.
* Ideally ESTALE wouldn't happen but we've been unable to figure out
* exactly what is happening on the Eden side so far, and it is more
* expedient to add a basic retry to ensure smoother operation
* for watchman clients. */
template <typename FUNC>
auto retryEStale(FUNC&& func) {
constexpr size_t kNumRetries = 5;
std::chrono::milliseconds backoff(1);
for (size_t retryAttemptsRemaining = kNumRetries; retryAttemptsRemaining >= 0;
--retryAttemptsRemaining) {
try {
return func();
} catch (const std::system_error& exc) {
if (exc.code() != error_code::stale_file_handle ||
retryAttemptsRemaining == 0) {
throw;
}
// Try again
log(ERR,
"Got ESTALE error from eden; will retry ",
retryAttemptsRemaining,
" more times. (",
exc.what(),
")\n");
/* sleep override */ std::this_thread::sleep_for(backoff);
backoff *= 2;
continue;
}
}
throw std::runtime_error(
"unreachable line reached; should have thrown an ESTALE");
}
folly::SocketAddress getEdenSocketAddress(w_string_piece rootPath) {
// Resolve the eden socket; we use the .eden dir that is present in
// every dir of an eden mount.
folly::SocketAddress addr;
auto path = watchman::to<std::string>(rootPath, "/.eden/socket");
// It is important to resolve the link because the path in the eden mount
// may exceed the maximum permitted unix domain socket path length.
// This is actually how things our in our integration test environment.
auto socketPath = readSymbolicLink(path.c_str());
addr.setFromPath(watchman::to<std::string>(socketPath));
return addr;
}
/** Create a thrift client that will connect to the eden server associated
* with the current user. */
std::unique_ptr<StreamingEdenServiceAsyncClient> getEdenClient(
w_string_piece rootPath,
folly::EventBase* eb = folly::EventBaseManager::get()->getEventBase()) {
return retryEStale([&] {
auto addr = getEdenSocketAddress(rootPath);
return make_unique<StreamingEdenServiceAsyncClient>(
apache::thrift::HeaderClientChannel::newChannel(
TAsyncSocket::newSocket(eb, addr)));
});
}
/** Create a thrift client that will connect to the eden server associated
* with the current user.
* This particular client uses the RocketClientChannel channel that
* is required to use the new thrift streaming protocol. */
std::unique_ptr<StreamingEdenServiceAsyncClient> getRocketEdenClient(
w_string_piece rootPath,
folly::EventBase* eb = folly::EventBaseManager::get()->getEventBase()) {
return retryEStale([&] {
auto addr = getEdenSocketAddress(rootPath);
return make_unique<StreamingEdenServiceAsyncClient>(
apache::thrift::RocketClientChannel::newChannel(
TAsyncSocket::UniquePtr(new TAsyncSocket(eb, addr))));
});
}
class EdenFileResult : public FileResult {
public:
EdenFileResult(
const w_string& rootPath,
const w_string& fullName,
JournalPosition* position = nullptr,
bool isNew = false,
DType dtype = DType::Unknown)
: root_path_(rootPath), fullName_(fullName), dtype_(dtype) {
otime_.ticks = ctime_.ticks = 0;
otime_.timestamp = ctime_.timestamp = 0;
if (position) {
otime_.ticks = position->sequenceNumber;
if (isNew) {
// the "ctime" in the context of FileResult represents the point
// in time that we saw the file transition !exists -> exists.
// We don't strictly know the point at which that happened for results
// returned from eden, but it will tell us whether that happened in
// a given since query window by listing the file in the created files
// set. We set the isNew flag in this case. The goal here is to
// ensure that the code in query/eval.cpp considers us to be new too,
// and that works because we set the created time ticks == the last
// change tick. The logic in query/eval.cpp will consider this to
// be new because the ctime > lower bound in the since query.
// When isNew is not set our ctime tick value is initialized to
// zero which always fails that is_new check.
ctime_.ticks = otime_.ticks;
}
}
}
Optional<FileInformation> stat() override {
if (!stat_.has_value()) {
accessorNeedsProperties(FileResult::Property::FullFileInformation);
return folly::none;
}
return stat_;
}
Optional<watchman::DType> dtype() override {
// We're using Unknown as the default value to avoid also wrapping
// this value up in an Optional in our internal storage.
// In theory this is ambiguous, but in practice Eden will never
// return Unknown for dtype values so this is safe to use with
// impunity.
if (dtype_ != DType::Unknown) {
return dtype_;
}
if (stat_.has_value()) {
return stat_->dtype();
}
accessorNeedsProperties(FileResult::Property::FileDType);
return folly::none;
}
Optional<size_t> size() override {
if (!stat_.has_value()) {
accessorNeedsProperties(FileResult::Property::Size);
return folly::none;
}
return stat_->size;
}
Optional<struct timespec> accessedTime() override {
if (!stat_.has_value()) {
accessorNeedsProperties(FileResult::Property::StatTimeStamps);
return folly::none;
}
return stat_->atime;
}
Optional<struct timespec> modifiedTime() override {
if (!stat_.has_value()) {
accessorNeedsProperties(FileResult::Property::StatTimeStamps);
return folly::none;
}
return stat_->mtime;
}
Optional<struct timespec> changedTime() override {
if (!stat_.has_value()) {
accessorNeedsProperties(FileResult::Property::StatTimeStamps);
return folly::none;
}
return stat_->ctime;
}
w_string_piece baseName() override {
return fullName_.piece().baseName();
}
w_string_piece dirName() override {
return fullName_.piece().dirName();
}
void setExists(bool exists) noexcept {
exists_ = exists;
if (!exists) {
stat_ = FileInformation::makeDeletedFileInformation();
}
}
Optional<bool> exists() override {
if (!exists_.has_value()) {
accessorNeedsProperties(FileResult::Property::Exists);
return folly::none;
}
return exists_;
}
Optional<w_string> readLink() override {
if (symlinkTarget_.has_value()) {
return symlinkTarget_;
}
accessorNeedsProperties(FileResult::Property::SymlinkTarget);
return folly::none;
}
Optional<w_clock_t> ctime() override {
if (!stat_.has_value()) {
accessorNeedsProperties(FileResult::Property::CTime);
return folly::none;
}
return ctime_;
}
Optional<w_clock_t> otime() override {
if (!stat_.has_value()) {
accessorNeedsProperties(FileResult::Property::OTime);
return folly::none;
}
return otime_;
}
Optional<FileResult::ContentHash> getContentSha1() override {
if (!sha1_.has_value()) {
accessorNeedsProperties(FileResult::Property::ContentSha1);
return folly::none;
}
switch (sha1_->getType()) {
// Copy thrift SHA1Result aka (std::string) into
// watchman FileResult::ContentHash aka (std::array<uint8_t, 20>)
case SHA1Result::Type::sha1: {
auto& hash = sha1_->get_sha1();
FileResult::ContentHash result;
std::copy(hash.begin(), hash.end(), result.begin());
return result;
}
// Thrift error occured
case SHA1Result::Type::error: {
auto& err = sha1_->get_error();
throw std::system_error(
err.errorCode_ref().value_unchecked(),
std::generic_category(),
err.message);
}
// Something is wrong with type union
default:
throw std::runtime_error(
"Unknown thrift data for EdenFileResult::getContentSha1");
}
}
void batchFetchProperties(
const std::vector<std::unique_ptr<FileResult>>& files) override {
std::vector<EdenFileResult*> getFileInformationFiles;
std::vector<std::string> getFileInformationNames;
std::vector<EdenFileResult*> getShaFiles;
std::vector<std::string> getShaNames;
std::vector<EdenFileResult*> getSymlinkFiles;
for (auto& f : files) {
auto& edenFile = dynamic_cast<EdenFileResult&>(*f.get());
auto relName = edenFile.fullName_.piece();
// Strip off the mount point prefix for the names we're going
// to pass to eden. The +1 is its trailing slash.
relName.advance(root_path_.size() + 1);
if (edenFile.neededProperties() & FileResult::Property::SymlinkTarget) {
// We need to know if the node is a symlink
edenFile.accessorNeedsProperties(FileResult::Property::FileDType);
getSymlinkFiles.emplace_back(&edenFile);
}
if (edenFile.neededProperties() &
(FileResult::Property::FileDType | FileResult::Property::CTime |
FileResult::Property::OTime | FileResult::Property::Exists |
FileResult::Property::Size | FileResult::Property::StatTimeStamps |
FileResult::Property::FullFileInformation)) {
getFileInformationFiles.emplace_back(&edenFile);
getFileInformationNames.emplace_back(relName.data(), relName.size());
}
if (edenFile.neededProperties() & FileResult::Property::ContentSha1) {
getShaFiles.emplace_back(&edenFile);
getShaNames.emplace_back(relName.data(), relName.size());
}
// If we were to throw later in this method, we will have forgotten
// the input set of properties, but it is ok: if we do decide to
// re-evaluate after throwing, the accessors will set the mask up
// accordingly and we'll end up calling back in here if needed.
edenFile.clearNeededProperties();
}
auto client = getEdenClient(root_path_);
loadFileInformation(
client.get(), getFileInformationNames, getFileInformationFiles);
// TODO: add eden bulk readlink call
loadSymlinkTargets(client.get(), getSymlinkFiles);
if (!getShaFiles.empty()) {
std::vector<SHA1Result> sha1s;
client->sync_getSHA1(sha1s, to<std::string>(root_path_), getShaNames);
if (sha1s.size() != getShaFiles.size()) {
watchman::log(
ERR,
"Requested SHA-1 of ",
getShaFiles.size(),
" but Eden returned ",
sha1s.size(),
" results -- ignoring");
} else {
auto sha1Iter = sha1s.begin();
for (auto& edenFile : getShaFiles) {
edenFile->sha1_ = *sha1Iter++;
}
}
}
}
private:
w_string root_path_;
w_string fullName_;
Optional<FileInformation> stat_;
Optional<bool> exists_;
w_clock_t ctime_;
w_clock_t otime_;
Optional<SHA1Result> sha1_;
Optional<w_string> symlinkTarget_;
DType dtype_{DType::Unknown};
// Read the symlink targets for each of the provided `files`. The files
// had SymlinkTarget set in neededProperties prior to clearing it in
// the batchFetchProperties() method that calls us, so we know that
// we unconditionally need to read these links.
void loadSymlinkTargets(
StreamingEdenServiceAsyncClient* client,
const std::vector<EdenFileResult*>& files) {
for (auto& edenFile : files) {
if (!edenFile->stat_->isSymlink()) {
// If this file is not a symlink then we immediately yield
// a nullptr w_string instance rather than propagating an error.
// This behavior is relied upon by the field rendering code and
// checked in test_symlink.py.
edenFile->symlinkTarget_ = w_string();
continue;
}
edenFile->symlinkTarget_ = readSymbolicLink(edenFile->fullName_.c_str());
}
}
void loadFileInformation(
StreamingEdenServiceAsyncClient* client,
const std::vector<std::string>& names,
const std::vector<EdenFileResult*>& outFiles) {
w_assert(
names.size() == outFiles.size(), "names.size must == outFiles.size");
if (names.empty()) {
return;
}
std::vector<FileInformationOrError> info;
client->sync_getFileInformation(info, to<std::string>(root_path_), names);
if (names.size() != info.size()) {
watchman::log(
ERR,
"Requested file information of ",
names.size(),
" files but Eden returned information for ",
info.size(),
" files. Treating missing entries as missing files.");
}
auto infoIter = info.begin();
for (auto& edenFileResult : outFiles) {
if (infoIter == info.end()) {
FileInformationOrError missingInfo;
missingInfo.set_error(EdenError("Missing info"));
edenFileResult->applyFileInformationOrError(missingInfo);
} else {
edenFileResult->applyFileInformationOrError(*infoIter);
infoIter++;
}
}
}
void applyFileInformationOrError(const FileInformationOrError& infoOrErr) {
if (infoOrErr.getType() == FileInformationOrError::Type::info) {
FileInformation stat;
stat.size = infoOrErr.get_info().size;
stat.mode = infoOrErr.get_info().mode;
stat.mtime.tv_sec = infoOrErr.get_info().mtime.seconds;
stat.mtime.tv_nsec = infoOrErr.get_info().mtime.nanoSeconds;
otime_.timestamp = ctime_.timestamp = stat.mtime.tv_sec;
stat_ = std::move(stat);
setExists(true);
} else {
setExists(false);
}
}
};
static std::string escapeGlobSpecialChars(w_string_piece str) {
std::string result;
for (size_t i = 0; i < str.size(); ++i) {
auto c = str[i];
switch (c) {
case '*':
case '?':
case '[':
case ']':
case '\\':
result.append("\\");
break;
}
result.append(&c, 1);
}
return result;
}
/** filter out paths that are ignored or that are not part of the
* relative_root restriction in a query.
* Ideally we'd pass this information into eden so that it doesn't
* have to walk those paths and return the data to us, but for the
* moment we have to filter it out of the results.
* We need to respect the ignore_dirs configuration setting and
* also remove anything that doesn't match the relative_root constraint
* in the query. */
void filterOutPaths(std::vector<NameAndDType>& files, w_query_ctx* ctx) {
files.erase(
std::remove_if(
files.begin(),
files.end(),
[ctx](const NameAndDType& item) {
auto full = w_string::pathCat({ctx->root->root_path, item.name});
if (!ctx->fileMatchesRelativeRoot(full)) {
// Not in the desired area, so filter it out
return true;
}
return ctx->root->ignore.isIgnored(full.data(), full.size());
}),
files.end());
}
/** Wraps around the raw SCM to acclerate certain things for Eden
*/
class EdenWrappedSCM : public SCM {
std::unique_ptr<SCM> inner_;
std::string mountPoint_;
public:
explicit EdenWrappedSCM(std::unique_ptr<SCM> inner)
: SCM(inner->getRootPath(), inner->getSCMRoot()),
inner_(std::move(inner)),
mountPoint_(to<std::string>(getRootPath())) {}
w_string mergeBaseWith(w_string_piece commitId, w_string requestId = nullptr)
const override {
return inner_->mergeBaseWith(commitId, requestId);
}
std::vector<w_string> getFilesChangedSinceMergeBaseWith(
w_string_piece commitId,
w_string requestId = nullptr) const override {
return inner_->getFilesChangedSinceMergeBaseWith(commitId, requestId);
}
SCM::StatusResult getFilesChangedBetweenCommits(
w_string_piece commitA,
w_string_piece commitB,
w_string /* requestId */ = nullptr) const override {
return inner_->getFilesChangedBetweenCommits(commitA, commitB);
}
std::chrono::time_point<std::chrono::system_clock> getCommitDate(
w_string_piece commitId,
w_string requestId = nullptr) const override {
return inner_->getCommitDate(commitId, requestId);
}
std::vector<w_string> getCommitsPriorToAndIncluding(
w_string_piece commitId,
int numCommits,
w_string requestId = nullptr) const override {
return inner_->getCommitsPriorToAndIncluding(
commitId, numCommits, requestId);
}
static std::unique_ptr<EdenWrappedSCM> wrap(std::unique_ptr<SCM> inner) {
if (!inner) {
return nullptr;
}
return make_unique<EdenWrappedSCM>(std::move(inner));
}
};
void appendGlobResultToNameAndDTypeVec(
std::vector<NameAndDType>& results,
Glob&& glob) {
size_t i = 0;
size_t numDTypes = glob.get_dtypes().size();
for (auto& name : glob.get_matchingFiles()) {
// The server may not support dtypes, so this list may be empty.
// This cast is OK because eden returns the system dependent bits to us, and
// our DType enum is declared in terms of those bits
auto dtype = i < numDTypes ? static_cast<DType>(glob.get_dtypes()[i])
: DType::Unknown;
results.emplace_back(name, dtype);
++i;
}
}
/** Returns the files that match the glob. */
std::vector<NameAndDType> globNameAndDType(
StreamingEdenServiceAsyncClient* client,
const std::string& mountPoint,
const std::vector<std::string>& globPatterns,
bool includeDotfiles) {
// edenfs had a bug where, for a globFiles request like ["foo/*", "foo/*/*"],
// edenfs would effectively ignore the second pattern. edenfs diff D15078089
// fixed this bug, but that diff hasn't been deployed widely yet. Work around
// this bug here by not calling globFiles with more than one pattern.
// TODO(T44365385): Remove this workaround when a newer version of edenfs is
// deployed everywhere.
bool isEdenFSGlobFilesBuggy = true;
if (isEdenFSGlobFilesBuggy && globPatterns.size() > 1) {
folly::DrivableExecutor* executor =
folly::EventBaseManager::get()->getEventBase();
std::vector<folly::Future<Glob>> globFutures;
globFutures.reserve(globPatterns.size());
for (const std::string& globPattern : globPatterns) {
GlobParams params;
params.set_mountPoint(mountPoint);
params.set_globs(std::vector<std::string>{globPattern});
params.set_includeDotfiles(includeDotfiles);
params.set_wantDtype(true);
globFutures.emplace_back(
client->semifuture_globFiles(params).via(executor));
}
std::vector<NameAndDType> allResults;
for (folly::Future<Glob>& globFuture : globFutures) {
appendGlobResultToNameAndDTypeVec(
allResults, std::move(globFuture).getVia(executor));
}
return allResults;
} else {
GlobParams params;
params.set_mountPoint(mountPoint);
params.set_globs(globPatterns);
params.set_includeDotfiles(includeDotfiles);
params.set_wantDtype(true);
Glob glob;
client->sync_globFiles(glob, params);
std::vector<NameAndDType> result;
appendGlobResultToNameAndDTypeVec(result, std::move(glob));
return result;
}
}
class EdenView : public QueryableView {
w_string root_path_;
// The source control system that we detected during initialization
mutable std::unique_ptr<EdenWrappedSCM> scm_;
folly::EventBase subscriberEventBase_;
mutable LRUCache<BetweenCommitKey, SCM::StatusResult>
filesBetweenCommitCache_;
JournalPosition lastCookiePosition_;
std::string mountPoint_;
std::promise<void> subscribeReadyPromise_;
std::shared_future<void> subscribeReadyFuture_;
public:
explicit EdenView(w_root_t* root)
: root_path_(root->root_path),
scm_(EdenWrappedSCM::wrap(SCM::scmForPath(root->root_path))),
// Allow for 32 pairs of revs, with errors cached for 10 seconds
filesBetweenCommitCache_(32, std::chrono::seconds(10)),
mountPoint_(to<std::string>(root->root_path)),
subscribeReadyFuture_(subscribeReadyPromise_.get_future()) {
// Get the current journal position so that we can keep track of
// cookie file changes
auto client = getEdenClient(root_path_);
client->sync_getCurrentJournalPosition(lastCookiePosition_, mountPoint_);
}
void timeGenerator(w_query* query, struct w_query_ctx* ctx) const override {
auto client = getEdenClient(root_path_);
FileDelta delta;
JournalPosition resultPosition;
if (ctx->since.is_timestamp) {
throw QueryExecError(
"timestamp based since queries are not supported with eden");
}
// This is the fall back for a fresh instance result set.
// There are two different code paths that may need this, so
// it is broken out as a lambda.
auto getAllFiles = [this,
ctx,
&client,
includeDotfiles =
(query->glob_flags & WM_PERIOD) == 0]() {
if (ctx->query->empty_on_fresh_instance) {
// Avoid a full tree walk if we don't need it!
return std::vector<NameAndDType>();
}
std::string globPattern;
if (ctx->query->relative_root) {
w_string_piece rel(ctx->query->relative_root);
rel.advance(ctx->root->root_path.size() + 1);
globPattern.append(rel.data(), rel.size());
globPattern.append("/");
}
globPattern.append("**");
return globNameAndDType(
client.get(),
mountPoint_,
std::vector<std::string>{globPattern},
includeDotfiles);
};
std::vector<NameAndDType> fileInfo;
// We use the list of created files to synthesize the "new" field
// in the file results
std::unordered_set<std::string> createdFileNames;
if (ctx->since.clock.is_fresh_instance) {
// Earlier in the processing flow, we decided that the rootNumber
// didn't match the current root which means that eden was restarted.
// We need to translate this to a fresh instance result set and
// return a list of all possible matching files.
client->sync_getCurrentJournalPosition(resultPosition, mountPoint_);
fileInfo = getAllFiles();
} else {
// Query eden to fill in the mountGeneration field.
JournalPosition position;
client->sync_getCurrentJournalPosition(position, mountPoint_);
// dial back to the sequence number from the query
position.sequenceNumber = ctx->since.clock.ticks;
// Now we can get the change journal from eden
try {
client->sync_getFilesChangedSince(delta, mountPoint_, position);
createdFileNames.insert(
delta.createdPaths.begin(), delta.createdPaths.end());
// The list of changed files is the union of the created, added,
// and removed sets returned from eden in list form.
for (auto& name : delta.changedPaths) {
fileInfo.emplace_back(NameAndDType(std::move(name)));
}
for (auto& name : delta.removedPaths) {
fileInfo.emplace_back(NameAndDType(std::move(name)));
}
for (auto& name : delta.createdPaths) {
fileInfo.emplace_back(NameAndDType(std::move(name)));
}
if (scm_ &&
delta.fromPosition.snapshotHash != delta.toPosition.snapshotHash) {
// Either they checked out a new commit or reset the commit to
// a different hash. We interrogate source control to discover
// the set of changed files between those hashes, and then
// add in any paths that may have changed around snapshot hash
// changes events; These are files whose status cannot be
// determined purely from source control operations
std::unordered_set<std::string> mergedFileList;
for (auto& info : fileInfo) {
mergedFileList.insert(info.name);
}
auto fromHash = folly::hexlify(delta.fromPosition.snapshotHash);
auto toHash = folly::hexlify(delta.toPosition.snapshotHash);
log(ERR,
"since ",
position.sequenceNumber,
" we changed commit hashes from ",
fromHash,
" to ",
toHash,
"\n");
auto changedBetweenCommits =
getFilesChangedBetweenCommits(fromHash, toHash);
for (auto& fileName : changedBetweenCommits.changedFiles) {
mergedFileList.insert(to<std::string>(fileName));
}
for (auto& fileName : changedBetweenCommits.removedFiles) {
mergedFileList.insert(to<std::string>(fileName));
}
for (auto& fileName : changedBetweenCommits.addedFiles) {
mergedFileList.insert(to<std::string>(fileName));
createdFileNames.insert(to<std::string>(fileName));
}
// We don't know whether the unclean paths are added, removed
// or just changed. We're going to treat them as changed.
mergedFileList.insert(
std::make_move_iterator(delta.uncleanPaths.begin()),
std::make_move_iterator(delta.uncleanPaths.end()));
// Replace the list of fileNames with the de-duped set
// of names we've extracted from source control
fileInfo.clear();
for (auto name : mergedFileList) {
fileInfo.emplace_back(std::move(name));
}
}
resultPosition = delta.toPosition;
watchman::log(
watchman::DBG,
"wanted from ",
position.sequenceNumber,
" result delta from ",
delta.fromPosition.sequenceNumber,
" to ",
delta.toPosition.sequenceNumber,
" with ",
fileInfo.size(),
" changed files\n");
} catch (const EdenError& err) {
// ERANGE: mountGeneration differs
// EDOM: journal was truncated.
// For other situations we let the error propagate.
if (err.errorCode_ref().value_unchecked() != ERANGE &&
err.errorCode_ref().value_unchecked() != EDOM) {
throw;
}
// mountGeneration differs, or journal was truncated,
// so treat this as equivalent to a fresh instance result
ctx->since.clock.is_fresh_instance = true;
client->sync_getCurrentJournalPosition(resultPosition, mountPoint_);
fileInfo = getAllFiles();
}
}
// Filter out any ignored files
filterOutPaths(fileInfo, ctx);
for (auto& item : fileInfo) {
// a file is considered new if it was present in the created files
// set returned from eden.
bool isNew = createdFileNames.find(item.name) != createdFileNames.end();
auto file = make_unique<EdenFileResult>(
root_path_,
w_string::pathCat({mountPoint_, item.name}),
&resultPosition,
isNew,
item.dtype);
if (ctx->since.clock.is_fresh_instance) {
// Fresh instance queries only return data about files
// that currently exist, and we know this to be true
// here because our list of files comes from evaluating
// a glob.
file->setExists(true);
}
w_query_process_file(ctx->query, ctx, std::move(file));
}
ctx->bumpNumWalked(fileInfo.size());
}
void suffixGenerator(w_query* query, struct w_query_ctx* ctx) const override {
// If the query is anchored to a relative_root, use that that
// avoid sucking down a massive list of files from eden
w_string_piece rel;
if (query->relative_root) {
rel = query->relative_root;
rel.advance(ctx->root->root_path.size() + 1);
}
std::vector<std::string> globStrings;
// Translate the suffix list into a list of globs
for (auto& suff : *query->suffixes) {
globStrings.emplace_back(to<std::string>(w_string::pathCat(
{rel, to<std::string>("**/*.", escapeGlobSpecialChars(suff))})));
}
executeGlobBasedQuery(globStrings, query, ctx);
}
void syncToNow(const std::shared_ptr<w_root_t>&, std::chrono::milliseconds)
override {}
void executeGlobBasedQuery(
const std::vector<std::string>& globStrings,
w_query* query,
struct w_query_ctx* ctx) const {
auto client = getEdenClient(ctx->root->root_path);
auto includeDotfiles = (query->glob_flags & WM_PERIOD) == 0;
auto fileInfo = globNameAndDType(
client.get(), mountPoint_, globStrings, includeDotfiles);
// Filter out any ignored files
filterOutPaths(fileInfo, ctx);
for (auto& item : fileInfo) {
auto file = make_unique<EdenFileResult>(
root_path_,
w_string::pathCat({mountPoint_, item.name}),
/* position=*/nullptr,
/*isNew=*/false,
item.dtype);
// The results of a glob are known to exist
file->setExists(true);
w_query_process_file(ctx->query, ctx, std::move(file));
}
ctx->bumpNumWalked(fileInfo.size());
}
// Helper for computing a relative path prefix piece.
// The returned piece is owned by the supplied context object!
w_string_piece computeRelativePathPiece(struct w_query_ctx* ctx) const {
w_string_piece rel;
if (ctx->query->relative_root) {
rel = ctx->query->relative_root;
rel.advance(ctx->root->root_path.size() + 1);
}
return rel;
}
/** Walks files that match the supplied set of paths */
void pathGenerator(w_query* query, struct w_query_ctx* ctx) const override {
// If the query is anchored to a relative_root, use that that
// avoid sucking down a massive list of files from eden
auto rel = computeRelativePathPiece(ctx);
std::vector<std::string> globStrings;
// Translate the path list into a list of globs
for (auto& path : *query->paths) {
if (path.depth > 0) {
// We don't have an easy way to express depth constraints
// in the existing glob API, so we just punt for the moment.
// I believe that this sort of query is quite rare anyway.
throw QueryExecError(
"the eden watcher only supports depth 0 or depth -1");
}
// -1 depth is infinite which we can translate to a recursive
// glob. 0 depth is direct descendant which we can translate
// to a simple * wildcard.
auto glob = path.depth == -1 ? "**/*" : "*";
globStrings.emplace_back(to<std::string>(
w_string::pathCat({rel, escapeGlobSpecialChars(path.name), glob})));
}
executeGlobBasedQuery(globStrings, query, ctx);
}
void globGenerator(w_query* query, struct w_query_ctx* ctx) const override {
// If the query is anchored to a relative_root, use that that
// avoid sucking down a massive list of files from eden
auto rel = computeRelativePathPiece(ctx);
std::vector<std::string> globStrings;
// Use the glob array provided by the query_spec.
// The InMemoryView uses the compiled glob tree but we just want to
// pass this list through to eden to evaluate. Note that we're
// relying on parse_globs() to have already checked that the glob
// looks sane during query parsing, and that eden itself will
// sanity check and throw an error if there is still something it
// doesn't like about it when we call sync_glob() below.
for (auto& glob : query->query_spec.get("glob").array()) {
globStrings.emplace_back(