Repository navigation
Expand file tree
/
Copy pathFileEvent.cpp
More file actions
786 lines (726 loc) · 26.3 KB
/
Copy pathFileEvent.cpp
File metadata and controls
786 lines (726 loc) · 26.3 KB
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
#include "prx/libkernel/include/Api.hpp"
#include "prx/libkernel/Equeue/Equeue.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libkernel/File/include/File.hpp"
#include "prx/libkernel/Socket/include/SocketRuntime.hpp"
#include <atomic>
#include <cerrno>
#include <cstdint>
#include <memory>
#include <stdexcept>
#include <string>
#include <thread>
#include <utility>
#include <vector>
#ifdef _WIN32
#include <windows.h>
#include <io.h>
#include "prx/libkernel/File/include/DirectoryDescriptor.hpp"
#elif defined(__APPLE__)
#include <fcntl.h>
#include <sys/event.h>
#include <sys/stat.h>
#include <unistd.h>
#else
#include <dirent.h>
#include <fcntl.h>
#include <poll.h>
#include <sys/eventfd.h>
#include <sys/inotify.h>
#include <sys/stat.h>
#include <unistd.h>
#include <unordered_map>
#endif
namespace {
constexpr int GUEST_EBADF = 9;
constexpr int GUEST_EINVAL = 22;
#ifdef __APPLE__
static_assert(NOTE_DELETE == 0x0001 && NOTE_WRITE == 0x0002 && NOTE_EXTEND == 0x0004 && NOTE_ATTRIB == 0x0008 &&
NOTE_LINK == 0x0010 && NOTE_RENAME == 0x0020);
#else
constexpr std::uint32_t NOTE_DELETE = 0x0001;
constexpr std::uint32_t NOTE_WRITE = 0x0002;
constexpr std::uint32_t NOTE_EXTEND = 0x0004;
constexpr std::uint32_t NOTE_ATTRIB = 0x0008;
constexpr std::uint32_t NOTE_LINK = 0x0010;
constexpr std::uint32_t NOTE_RENAME = 0x0020;
#endif
constexpr std::uint32_t MAPPED_NOTES = NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | NOTE_LINK | NOTE_RENAME;
class VnodeWatch {
public:
VnodeWatch(const KernelEqueueRef& owner, std::uintptr_t descriptor, std::uint32_t notes) : queue(owner), ident(descriptor), interest(notes) {
#ifdef _WIN32
stopEvent = ::CreateEventW(nullptr, TRUE, FALSE, nullptr);
if (stopEvent == nullptr) {
throw std::runtime_error("EVFILT_VNODE: CreateEvent failed, error=" + std::to_string(::GetLastError()));
}
#elif defined(__APPLE__)
int ends[2];
if (::pipe(ends) != 0) {
throw std::runtime_error("EVFILT_VNODE: pipe failed, errno=" + std::to_string(errno));
}
stopEvent = ends[0];
stopSignal = ends[1];
::fcntl(stopEvent, F_SETFD, FD_CLOEXEC);
::fcntl(stopSignal, F_SETFD, FD_CLOEXEC);
#else
stopEvent = ::eventfd(0, EFD_CLOEXEC);
if (stopEvent < 0) {
throw std::runtime_error("EVFILT_VNODE: eventfd failed, errno=" + std::to_string(errno));
}
#endif
}
~VnodeWatch() {
#ifdef _WIN32
::CloseHandle(stopEvent);
#elif defined(__APPLE__)
::close(stopEvent);
::close(stopSignal);
#else
::close(stopEvent);
#endif
}
VnodeWatch(const VnodeWatch&) = delete;
VnodeWatch& operator=(const VnodeWatch&) = delete;
void Stop() {
stopped.store(true);
#ifdef _WIN32
::SetEvent(stopEvent);
#elif defined(__APPLE__)
const char one = 1;
const auto written = ::write(stopSignal, &one, sizeof(one));
static_cast<void>(written);
#else
const std::uint64_t one = 1;
const auto written = ::write(stopEvent, &one, sizeof(one));
static_cast<void>(written);
#endif
}
void Deliver(std::uint32_t hint);
std::uint32_t Interest() const { return interest.load(); }
void SetInterest(std::uint32_t notes) { interest.store(notes); }
#ifdef _WIN32
HANDLE StopEvent() const { return stopEvent; }
#else
int StopEvent() const { return stopEvent; }
#endif
private:
std::weak_ptr<KernelEqueuePrivate> queue;
std::uintptr_t ident;
std::atomic<std::uint32_t> interest;
std::atomic<bool> stopped{false};
#ifdef _WIN32
HANDLE stopEvent = nullptr;
#elif defined(__APPLE__)
int stopEvent = -1;
int stopSignal = -1;
#else
int stopEvent = -1;
#endif
};
struct VnodeHint {
const VnodeWatch* watch;
std::uint32_t hint;
};
void VnodeWatch::Deliver(std::uint32_t hint) {
if (hint == 0 || stopped.load()) {
return;
}
const auto owner = queue.lock();
if (!owner) {
return;
}
VnodeHint message{this, hint};
owner->TriggerEvent(ident, EVFILT_VNODE, &message);
}
#ifdef _WIN32
constexpr DWORD VNODE_POLL_MILLISECONDS = 50;
constexpr DWORD PARENT_CHANGES = FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES |
FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_CREATION | FILE_NOTIFY_CHANGE_SECURITY;
struct Snapshot {
std::uint64_t size = 0;
std::uint32_t links = 0;
std::uint32_t attributes = 0;
std::int64_t creation = 0;
std::int64_t lastWrite = 0;
std::int64_t change = 0;
bool deletePending = false;
std::wstring path;
};
bool TakeSnapshot(HANDLE handle, Snapshot& snapshot) {
std::wstring path(MAX_PATH, L'\0');
auto length = ::GetFinalPathNameByHandleW(handle, path.data(), static_cast<DWORD>(path.size()), FILE_NAME_NORMALIZED);
if (length >= path.size()) {
path.resize(length);
length = ::GetFinalPathNameByHandleW(handle, path.data(), length, FILE_NAME_NORMALIZED);
}
if (length != 0 && length < path.size()) {
path.resize(length);
snapshot.path = std::move(path);
}
FILE_BASIC_INFO basic{};
FILE_STANDARD_INFO standard{};
if (!::GetFileInformationByHandleEx(handle, FileBasicInfo, &basic, sizeof(basic)) ||
!::GetFileInformationByHandleEx(handle, FileStandardInfo, &standard, sizeof(standard))) {
return false;
}
snapshot.size = static_cast<std::uint64_t>(standard.EndOfFile.QuadPart);
snapshot.links = standard.NumberOfLinks;
snapshot.deletePending = standard.DeletePending != FALSE;
snapshot.attributes = basic.FileAttributes;
snapshot.creation = basic.CreationTime.QuadPart;
snapshot.lastWrite = basic.LastWriteTime.QuadPart;
snapshot.change = basic.ChangeTime.QuadPart;
return true;
}
bool IsDeleted(const Snapshot& snapshot) {
return snapshot.deletePending || snapshot.links == 0 || snapshot.path.find(L"\\$Extend\\$Deleted\\") != std::wstring::npos;
}
std::vector<std::uint32_t> SnapshotHints(const Snapshot& before, const Snapshot& after, bool directory) {
std::vector<std::uint32_t> hints;
if (!directory) {
if (after.size > before.size) {
hints.push_back(NOTE_WRITE | NOTE_EXTEND);
} else if (after.size == before.size && after.lastWrite != before.lastWrite) {
hints.push_back(NOTE_WRITE);
}
if (after.links > before.links) {
hints.push_back(NOTE_LINK);
}
}
if (after.size < before.size || after.attributes != before.attributes || after.creation != before.creation) {
hints.push_back(NOTE_ATTRIB);
} else if (!directory && hints.empty() && after.change != before.change) {
hints.push_back(NOTE_ATTRIB);
}
if ((after.deletePending && !before.deletePending) || after.links < before.links || (!IsDeleted(before) && IsDeleted(after))) {
hints.push_back(NOTE_DELETE);
}
if (!after.path.empty() && after.path != before.path && !IsDeleted(after)) {
hints.push_back(NOTE_RENAME);
}
return hints;
}
std::string hostPathText(const std::wstring& path) {
const int size = ::WideCharToMultiByte(CP_UTF8, 0, path.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (size <= 0) {
return "<unprintable host path>";
}
std::string text(static_cast<std::size_t>(size), '\0');
if (::WideCharToMultiByte(CP_UTF8, 0, path.c_str(), -1, text.data(), size, nullptr, nullptr) != size) {
return "<unprintable host path>";
}
text.pop_back();
return text;
}
std::string changeNotificationFailure(const char* watcher, const std::wstring& path, DWORD error) {
return "EVFILT_VNODE: the " + std::string(watcher) + " watch of \"" + hostPathText(path) +
"\" failed, FindFirstChangeNotification error=" + std::to_string(error);
}
HANDLE WatchParent(const std::wstring& path) {
const auto separator = path.find_last_of(L"\\/");
if (separator == std::wstring::npos) {
return INVALID_HANDLE_VALUE;
}
const auto parent = path.substr(0, separator + 1);
const HANDLE handle = ::FindFirstChangeNotificationW(parent.c_str(), FALSE, PARENT_CHANGES);
const DWORD error = handle == INVALID_HANDLE_VALUE ? ::GetLastError() : ERROR_SUCCESS;
if (handle == INVALID_HANDLE_VALUE && error != ERROR_INVALID_FUNCTION) {
throw std::runtime_error(changeNotificationFailure("parent", parent, error));
}
return handle;
}
HANDLE WatchContents(const std::wstring& path, DWORD changes, const char* watcher) {
const HANDLE handle = ::FindFirstChangeNotificationW(path.c_str(), FALSE, changes);
if (handle == INVALID_HANDLE_VALUE) {
const DWORD error = ::GetLastError();
throw std::runtime_error(changeNotificationFailure(watcher, path, error));
}
return handle;
}
struct HostWatch {
HANDLE target = INVALID_HANDLE_VALUE;
HANDLE parent = INVALID_HANDLE_VALUE;
HANDLE entries = INVALID_HANDLE_VALUE;
HANDLE subdirectories = INVALID_HANDLE_VALUE;
bool directory = false;
Snapshot last;
};
void CloseChange(HANDLE& handle) {
if (handle != INVALID_HANDLE_VALUE) {
::FindCloseChangeNotification(handle);
handle = INVALID_HANDLE_VALUE;
}
}
void CloseHostWatch(HostWatch& host) {
CloseChange(host.parent);
CloseChange(host.entries);
CloseChange(host.subdirectories);
if (host.target != INVALID_HANDLE_VALUE) {
::CloseHandle(host.target);
host.target = INVALID_HANDLE_VALUE;
}
}
bool Signaled(HANDLE handle) {
return handle != INVALID_HANDLE_VALUE && ::WaitForSingleObject(handle, 0) == WAIT_OBJECT_0;
}
void RunHostWatch(std::shared_ptr<VnodeWatch> watch, HostWatch host) {
for (;;) {
std::vector<HANDLE> handles{watch->StopEvent()};
for (const HANDLE handle : {host.parent, host.entries, host.subdirectories}) {
if (handle != INVALID_HANDLE_VALUE) {
handles.push_back(handle);
}
}
const DWORD result = ::WaitForMultipleObjects(static_cast<DWORD>(handles.size()), handles.data(), FALSE, VNODE_POLL_MILLISECONDS);
if (result == WAIT_OBJECT_0) {
break;
}
if (result == WAIT_FAILED) {
CloseHostWatch(host);
throw std::runtime_error("EVFILT_VNODE: WaitForMultipleObjects failed, error=" + std::to_string(::GetLastError()));
}
std::vector<std::uint32_t> hints;
if (Signaled(host.entries)) {
hints.push_back(NOTE_WRITE);
::FindNextChangeNotification(host.entries);
}
if (Signaled(host.subdirectories)) {
hints.push_back(NOTE_WRITE | NOTE_LINK);
::FindNextChangeNotification(host.subdirectories);
}
if (Signaled(host.parent)) {
::FindNextChangeNotification(host.parent);
}
Snapshot current = host.last;
if (TakeSnapshot(host.target, current)) {
for (const auto hint : SnapshotHints(host.last, current, host.directory)) {
hints.push_back(hint);
}
if (current.path != host.last.path) {
CloseChange(host.parent);
if (!IsDeleted(current)) {
host.parent = WatchParent(current.path);
}
}
host.last = std::move(current);
}
for (const auto hint : hints) {
watch->Deliver(hint);
}
}
CloseHostWatch(host);
}
int OpenHostWatch(int descriptor, std::uint32_t notes, HostWatch& host) {
if (const auto directory = File::DirectoryDescriptorPath(descriptor)) {
host.directory = true;
host.target = ::CreateFileW(directory->wstring().c_str(), FILE_LIST_DIRECTORY | FILE_READ_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
} else {
const auto handle = reinterpret_cast<HANDLE>(::_get_osfhandle(descriptor));
if (handle == INVALID_HANDLE_VALUE) {
return GUEST_EBADF;
}
const DWORD type = ::GetFileType(handle);
if (type == FILE_TYPE_PIPE) {
return GUEST_EINVAL;
}
if (type != FILE_TYPE_DISK) {
NotImplemented_nid_no_patch("EVFILT_VNODE: a descriptor that is not a file or a directory");
}
host.target = ::ReOpenFile(handle, FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 0);
}
if (host.target == INVALID_HANDLE_VALUE) {
throw std::runtime_error("EVFILT_VNODE: opening the watched file failed, error=" + std::to_string(::GetLastError()));
}
if (!TakeSnapshot(host.target, host.last) || host.last.path.empty()) {
const auto error = ::GetLastError();
CloseHostWatch(host);
throw std::runtime_error("EVFILT_VNODE: reading the watched file failed, error=" + std::to_string(error));
}
try {
if (!IsDeleted(host.last)) {
host.parent = WatchParent(host.last.path);
}
if (host.directory) {
host.entries = WatchContents(host.last.path, FILE_NOTIFY_CHANGE_FILE_NAME, "entries");
host.subdirectories = WatchContents(host.last.path, FILE_NOTIFY_CHANGE_DIR_NAME, "subdirectories");
}
} catch (...) {
CloseHostWatch(host);
throw;
}
return 0;
}
#elif defined(__APPLE__)
struct HostWatch {
int notify = -1;
int target = -1;
};
void CloseHostWatch(HostWatch& host) {
for (int* descriptor : {&host.notify, &host.target}) {
if (*descriptor >= 0) {
::close(*descriptor);
*descriptor = -1;
}
}
}
void RunHostWatch(std::shared_ptr<VnodeWatch> watch, HostWatch host) {
struct kevent stop {};
EV_SET(&stop, watch->StopEvent(), EVFILT_READ, EV_ADD, 0, 0, nullptr);
if (::kevent(host.notify, &stop, 1, nullptr, 0, nullptr) < 0) {
const int error = errno;
CloseHostWatch(host);
throw std::runtime_error("EVFILT_VNODE: registering the stop event failed, errno=" + std::to_string(error));
}
for (;;) {
struct kevent event {};
if (::kevent(host.notify, nullptr, 0, &event, 1, nullptr) < 0) {
if (errno == EINTR) {
continue;
}
const int error = errno;
CloseHostWatch(host);
throw std::runtime_error("EVFILT_VNODE: kevent failed, errno=" + std::to_string(error));
}
if (event.filter != EVFILT_VNODE) {
break;
}
auto notes = event.fflags & MAPPED_NOTES;
if ((notes & NOTE_DELETE) != 0) {
notes &= ~NOTE_LINK;
}
watch->Deliver(notes);
}
CloseHostWatch(host);
}
int OpenHostWatch(int descriptor, std::uint32_t, HostWatch& host) {
struct stat status {};
if (::fstat(descriptor, &status) != 0) {
return GUEST_EBADF;
}
if (S_ISFIFO(status.st_mode) || S_ISSOCK(status.st_mode)) {
return GUEST_EINVAL;
}
if (!S_ISREG(status.st_mode) && !S_ISDIR(status.st_mode)) {
NotImplemented_nid_no_patch("EVFILT_VNODE: a descriptor that is not a file or a directory");
}
host.target = ::fcntl(descriptor, F_DUPFD_CLOEXEC, 0);
if (host.target < 0) {
const int error = errno;
CloseHostWatch(host);
throw std::runtime_error("EVFILT_VNODE: duplicating the watched descriptor failed, errno=" + std::to_string(error));
}
host.notify = ::kqueue();
struct kevent change {};
EV_SET(&change, host.target, EVFILT_VNODE, EV_ADD | EV_CLEAR, MAPPED_NOTES, 0, nullptr);
if (host.notify < 0 || ::kevent(host.notify, &change, 1, nullptr, 0, nullptr) < 0) {
const int error = errno;
CloseHostWatch(host);
throw std::runtime_error("EVFILT_VNODE: kqueue failed, errno=" + std::to_string(error));
}
return 0;
}
#else
struct HostWatch {
int notify = -1;
int target = -1;
int entries = -1;
bool directory = false;
struct stat last {};
};
struct HostEvent {
std::uint32_t mask;
std::uint32_t cookie;
};
void CloseHostWatch(HostWatch& host) {
for (int* descriptor : {&host.notify, &host.target, &host.entries}) {
if (*descriptor >= 0) {
::close(*descriptor);
*descriptor = -1;
}
}
}
std::uint32_t AttributeHint(HostWatch& host) {
struct stat current {};
if (::fstat(host.target, ¤t) != 0) {
return NOTE_ATTRIB;
}
std::uint32_t hint = NOTE_ATTRIB;
if (current.st_nlink > host.last.st_nlink) {
hint = NOTE_LINK;
} else if (current.st_nlink < host.last.st_nlink) {
hint = host.directory && current.st_nlink != 0 ? NOTE_LINK : NOTE_DELETE;
}
host.last = current;
return hint;
}
std::uint32_t ModifyHint(HostWatch& host) {
struct stat current {};
if (::fstat(host.target, ¤t) != 0) {
return NOTE_WRITE;
}
std::uint32_t hint = NOTE_WRITE;
if (current.st_size > host.last.st_size) {
hint = NOTE_WRITE | NOTE_EXTEND;
} else if (current.st_size < host.last.st_size) {
hint = NOTE_ATTRIB;
}
host.last = current;
return hint;
}
int ReadHostEvents(const HostWatch& host, std::vector<HostEvent>& events) {
alignas(inotify_event) char buffer[16384];
for (;;) {
const auto length = ::read(host.notify, buffer, sizeof(buffer));
if (length < 0) {
if (errno == EINTR) {
continue;
}
return errno == EAGAIN ? 0 : errno;
}
if (length == 0) {
return 0;
}
for (char* cursor = buffer; cursor < buffer + length;) {
const auto* event = reinterpret_cast<const inotify_event*>(cursor);
events.push_back({event->mask, event->cookie});
cursor += sizeof(inotify_event) + event->len;
}
}
}
std::unordered_map<std::uint32_t, int> CountMoves(const std::vector<HostEvent>& events) {
std::unordered_map<std::uint32_t, int> moves;
for (const auto& event : events) {
if ((event.mask & (IN_MOVED_FROM | IN_MOVED_TO)) != 0) {
++moves[event.cookie];
}
}
return moves;
}
bool HasUnpairedMove(const std::vector<HostEvent>& events, std::size_t first) {
const auto moves = CountMoves(events);
for (std::size_t index = first; index < events.size(); ++index) {
if ((events[index].mask & IN_MOVED_FROM) != 0 && moves.at(events[index].cookie) == 1) {
return true;
}
}
return false;
}
int WaitForRenamesInProgress(const HostWatch& host) {
char entries[1024];
if (::getdents64(host.entries, entries, sizeof(entries)) < 0 && errno != ENOENT) {
return errno;
}
return 0;
}
int ReadSettledHostEvents(const HostWatch& host, std::vector<HostEvent>& events) {
if (const int error = ReadHostEvents(host, events)) {
return error;
}
for (std::size_t settled = 0; HasUnpairedMove(events, settled);) {
settled = events.size();
if (const int error = WaitForRenamesInProgress(host)) {
return error;
}
if (const int error = ReadHostEvents(host, events)) {
return error;
}
}
return 0;
}
void RunHostWatch(std::shared_ptr<VnodeWatch> watch, HostWatch host) {
pollfd waits[2] = {{host.notify, POLLIN, 0}, {watch->StopEvent(), POLLIN, 0}};
bool watching = true;
while (watching) {
if (::poll(waits, 2, -1) < 0) {
if (errno == EINTR) {
continue;
}
CloseHostWatch(host);
throw std::runtime_error("EVFILT_VNODE: poll failed, errno=" + std::to_string(errno));
}
if (waits[1].revents != 0) {
break;
}
std::vector<HostEvent> events;
if (const int error = ReadSettledHostEvents(host, events)) {
CloseHostWatch(host);
throw std::runtime_error("EVFILT_VNODE: reading inotify failed, errno=" + std::to_string(error));
}
const auto moves = CountMoves(events);
std::vector<std::uint32_t> hints;
for (const auto& event : events) {
const std::uint32_t link = (event.mask & IN_ISDIR) != 0 ? NOTE_LINK : 0;
if ((event.mask & IN_MODIFY) != 0) {
hints.push_back(ModifyHint(host));
}
if ((event.mask & IN_ATTRIB) != 0) {
hints.push_back(AttributeHint(host));
}
if ((event.mask & (IN_CREATE | IN_DELETE)) != 0) {
hints.push_back(NOTE_WRITE | link);
}
if ((event.mask & (IN_MOVED_FROM | IN_MOVED_TO)) != 0) {
hints.push_back(moves.at(event.cookie) > 1 ? NOTE_WRITE : NOTE_WRITE | NOTE_EXTEND | link);
}
if ((event.mask & IN_MOVE_SELF) != 0) {
hints.push_back(NOTE_RENAME);
}
if ((event.mask & IN_DELETE_SELF) != 0) {
hints.push_back(NOTE_DELETE);
}
if ((event.mask & IN_IGNORED) != 0) {
watching = false;
}
}
for (const auto hint : hints) {
watch->Deliver(hint);
}
}
CloseHostWatch(host);
}
int OpenHostWatch(int descriptor, std::uint32_t, HostWatch& host) {
struct stat status {};
if (::fstat(descriptor, &status) != 0) {
return GUEST_EBADF;
}
if (S_ISFIFO(status.st_mode) || S_ISSOCK(status.st_mode)) {
return GUEST_EINVAL;
}
if (!S_ISREG(status.st_mode) && !S_ISDIR(status.st_mode)) {
NotImplemented_nid_no_patch("EVFILT_VNODE: a descriptor that is not a file or a directory");
}
host.directory = S_ISDIR(status.st_mode);
const auto path = "/proc/self/fd/" + std::to_string(descriptor);
host.target = ::open(path.c_str(), O_PATH | O_CLOEXEC);
if (host.target < 0 || ::fstat(host.target, &host.last) != 0) {
const int error = errno;
CloseHostWatch(host);
throw std::runtime_error("EVFILT_VNODE: opening the watched file failed, errno=" + std::to_string(error));
}
if (host.directory) {
host.entries = ::open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
if (host.entries < 0) {
const int error = errno;
CloseHostWatch(host);
throw std::runtime_error("EVFILT_VNODE: opening the watched directory failed, errno=" + std::to_string(error));
}
}
host.notify = ::inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
std::uint32_t mask = IN_MODIFY | IN_ATTRIB | IN_MOVE_SELF | IN_DELETE_SELF;
if (host.directory) {
mask |= IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO;
}
if (host.notify < 0 || ::inotify_add_watch(host.notify, path.c_str(), mask) < 0) {
const int error = errno;
CloseHostWatch(host);
throw std::runtime_error("EVFILT_VNODE: inotify failed, errno=" + std::to_string(error));
}
return 0;
}
#endif
void TriggerVnode(KernelEqueueEvent* event, void* data) {
const auto* message = static_cast<const VnodeHint*>(data);
if (event->filter.data != message->watch) {
return;
}
if ((message->watch->Interest() & message->hint) != 0) {
event->event.fflags |= message->hint;
}
event->triggered = event->event.fflags != 0;
}
void StopVnode(KernelEqueue, KernelEqueueEvent* event) {
static_cast<VnodeWatch*>(event->filter.data)->Stop();
}
int VnodeDescriptorError(int descriptor) {
if (descriptor < 0) {
return GUEST_EBADF;
}
if (GuestSockets::IsOpen(descriptor)) {
return GUEST_EINVAL;
}
if (!DescriptorIsOpen_nid_no_patch(descriptor)) {
return GUEST_EBADF;
}
return IsKqueueDescriptor(descriptor) ? GUEST_EINVAL : 0;
}
}
void RequireVnodeNotes(std::uint32_t fflags) {
if ((fflags & ~MAPPED_NOTES) != 0) {
NotImplemented_nid_no_patch(("EVFILT_VNODE: fflags " + std::to_string(fflags & ~MAPPED_NOTES)).c_str());
}
}
void SetVnodeNotes(KernelEqueueEvent& event, std::uint32_t fflags) {
static_cast<VnodeWatch*>(event.filter.data)->SetInterest(fflags);
}
int AddVnodeEvent(const KernelEqueueRef& queue, int descriptor, std::uint32_t fflags, std::uint16_t flags, void* udata) {
RequireVnodeNotes(fflags);
if (const int error = VnodeDescriptorError(descriptor)) {
return error;
}
HostWatch host;
if (const int error = OpenHostWatch(descriptor, fflags, host)) {
return error;
}
std::shared_ptr<VnodeWatch> watch;
try {
watch = std::make_shared<VnodeWatch>(queue, static_cast<std::uintptr_t>(descriptor), fflags);
} catch (...) {
CloseHostWatch(host);
throw;
}
KernelEqueueEvent event{};
event.event.ident = static_cast<std::uintptr_t>(descriptor);
event.event.filter = EVFILT_VNODE;
event.event.flags = flags;
event.event.udata = udata;
event.filter.data = watch.get();
event.filter.owner = watch;
event.filter.triggerFunc = TriggerVnode;
event.filter.deleteEventFunc = StopVnode;
const int result = queue->AddEvent(event);
bool registered = false;
if (result == EQUEUE_OK) {
queue->EditEvent(event.event.ident, EVFILT_VNODE, [®istered, &watch](KernelEqueueEvent& added) {
registered = added.filter.data == watch.get();
});
}
if (!registered) {
CloseHostWatch(host);
return result == EQUEUE_OK ? 0 : result & 0xffff;
}
std::thread(RunHostWatch, watch, std::move(host)).detach();
return 0;
}
extern "C" {
int APS5_VABI sceKernelAddFileEvent(KernelEqueue eq, int fd, int watch, void* udata) {
const auto queue = EqueuePin_nid_postfix(eq);
if (!queue) {
return SCE_KERNEL_ERROR_EBADF;
}
const auto notes = static_cast<std::uint32_t>(watch);
RequireVnodeNotes(notes);
if (const int error = VnodeDescriptorError(fd)) {
return SceKernelError(error);
}
const int edited = queue->EditEvent(static_cast<std::uintptr_t>(fd), EVFILT_VNODE, [notes, udata](KernelEqueueEvent& event) {
event.event.udata = udata;
SetVnodeNotes(event, notes);
});
if (edited != SCE_KERNEL_ERROR_ENOENT) {
return edited;
}
const int error = AddVnodeEvent(queue, fd, notes, EV_CLEAR, udata);
return error == 0 ? EQUEUE_OK : SceKernelError(error);
}
int APS5_VABI sceKernelDeleteFileEvent(KernelEqueue eq, int fd) {
if (!EqueuePin_nid_postfix(eq)) {
return SCE_KERNEL_ERROR_EBADF;
}
if (fd < 0 || (!GuestSockets::IsOpen(fd) && !DescriptorIsOpen_nid_no_patch(fd))) {
return SCE_KERNEL_ERROR_EBADF;
}
return EqueueDeleteEvent_nid_postfix(eq, static_cast<std::uintptr_t>(fd), EVFILT_VNODE);
}
}