summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCore/FileWatcherMacOS.mm
blob: 760fe6dc510989b47455c4d35cf94db551d9ea30 (plain)
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
/*
 * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "FileWatcher.h"
#include <AK/Debug.h>
#include <AK/LexicalPath.h>
#include <AK/OwnPtr.h>
#include <LibCore/EventLoop.h>
#include <LibCore/Notifier.h>
#include <LibCore/System.h>
#include <errno.h>
#include <limits.h>

#if !defined(AK_OS_MACOS)
static_assert(false, "This file must only be used for macOS");
#endif

#define FixedPoint FixedPointMacOS // AK::FixedPoint conflicts with FixedPoint from MacTypes.h.
#include <CoreServices/CoreServices.h>
#include <dispatch/dispatch.h>
#undef FixedPoint

namespace Core {

struct MonitoredPath {
    DeprecatedString path;
    FileWatcherEvent::Type event_mask { FileWatcherEvent::Type::Invalid };
};

static void on_file_system_event(ConstFSEventStreamRef, void*, size_t, void*, FSEventStreamEventFlags const[], FSEventStreamEventId const[]);

static ErrorOr<ino_t> inode_id_from_path(StringView path)
{
    auto stat = TRY(System::stat(path));
    return stat.st_ino;
}

class FileWatcherMacOS final : public FileWatcher {
    AK_MAKE_NONCOPYABLE(FileWatcherMacOS);

public:
    virtual ~FileWatcherMacOS() override
    {
        close_event_stream();
        dispatch_release(m_dispatch_queue);
    }

    static ErrorOr<NonnullRefPtr<FileWatcherMacOS>> create(FileWatcherFlags)
    {
        auto context = TRY(try_make<FSEventStreamContext>());

        auto queue_name = DeprecatedString::formatted("Serenity.FileWatcher.{:p}", context.ptr());
        auto dispatch_queue = dispatch_queue_create(queue_name.characters(), DISPATCH_QUEUE_SERIAL);
        if (dispatch_queue == nullptr)
            return Error::from_errno(errno);

        // NOTE: This isn't actually used on macOS, but is needed for FileWatcherBase.
        //       Creating it with an FD of -1 will effectively disable the notifier.
        auto notifier = TRY(Notifier::try_create(-1, Notifier::Event::None));

        return adopt_nonnull_ref_or_enomem(new (nothrow) FileWatcherMacOS(move(context), dispatch_queue, move(notifier)));
    }

    ErrorOr<bool> add_watch(DeprecatedString path, FileWatcherEvent::Type event_mask)
    {
        if (m_path_to_inode_id.contains(path)) {
            dbgln_if(FILE_WATCHER_DEBUG, "add_watch: path '{}' is already being watched", path);
            return false;
        }

        auto inode_id = TRY(inode_id_from_path(path));
        TRY(m_path_to_inode_id.try_set(path, inode_id));
        TRY(m_inode_id_to_path.try_set(inode_id, { path, event_mask }));

        TRY(refresh_monitored_paths());

        dbgln_if(FILE_WATCHER_DEBUG, "add_watch: watching path '{}' inode {}", path, inode_id);
        return true;
    }

    ErrorOr<bool> remove_watch(DeprecatedString path)
    {
        auto it = m_path_to_inode_id.find(path);
        if (it == m_path_to_inode_id.end()) {
            dbgln_if(FILE_WATCHER_DEBUG, "remove_watch: path '{}' is not being watched", path);
            return false;
        }

        m_inode_id_to_path.remove(it->value);
        m_path_to_inode_id.remove(it);

        TRY(refresh_monitored_paths());

        dbgln_if(FILE_WATCHER_DEBUG, "remove_watch: stopped watching path '{}'", path);
        return true;
    }

    ErrorOr<MonitoredPath> canonicalize_path(DeprecatedString path)
    {
        LexicalPath lexical_path { move(path) };
        auto parent_path = lexical_path.parent();

        auto inode_id = TRY(inode_id_from_path(parent_path.string()));

        auto it = m_inode_id_to_path.find(inode_id);
        if (it == m_inode_id_to_path.end())
            return Error::from_string_literal("Got an event for a non-existent inode ID");

        return MonitoredPath {
            LexicalPath::join(it->value.path, lexical_path.basename()).string(),
            it->value.event_mask
        };
    }

    void handle_event(FileWatcherEvent event)
    {
        NonnullRefPtr strong_this { *this };

        m_main_event_loop.deferred_invoke(
            [strong_this = move(strong_this), event = move(event)]() {
                strong_this->on_change(event);
            });
    }

private:
    FileWatcherMacOS(NonnullOwnPtr<FSEventStreamContext> context, dispatch_queue_t dispatch_queue, NonnullRefPtr<Notifier> notifier)
        : FileWatcher(-1, move(notifier))
        , m_main_event_loop(EventLoop::current())
        , m_context(move(context))
        , m_dispatch_queue(dispatch_queue)
    {
        m_context->info = this;
    }

    void close_event_stream()
    {
        if (!m_stream)
            return;

        dispatch_sync(m_dispatch_queue, ^{
            FSEventStreamStop(m_stream);
            FSEventStreamInvalidate(m_stream);
            FSEventStreamRelease(m_stream);
            m_stream = nullptr;
        });
    }

    ErrorOr<void> refresh_monitored_paths()
    {
        static constexpr FSEventStreamCreateFlags stream_flags = kFSEventStreamCreateFlagFileEvents | kFSEventStreamCreateFlagUseCFTypes | kFSEventStreamCreateFlagUseExtendedData;
        static constexpr CFAbsoluteTime stream_latency = 0.25;

        close_event_stream();

        if (m_path_to_inode_id.is_empty())
            return {};

        auto monitored_paths = CFArrayCreateMutable(kCFAllocatorDefault, m_path_to_inode_id.size(), &kCFTypeArrayCallBacks);
        if (monitored_paths == nullptr)
            return Error::from_errno(ENOMEM);

        for (auto it : m_path_to_inode_id) {
            auto path = CFStringCreateWithCString(kCFAllocatorDefault, it.key.characters(), kCFStringEncodingUTF8);
            if (path == nullptr)
                return Error::from_errno(ENOMEM);

            CFArrayAppendValue(monitored_paths, static_cast<void const*>(path));
        }

        dispatch_sync(m_dispatch_queue, ^{
            m_stream = FSEventStreamCreate(
                kCFAllocatorDefault,
                &on_file_system_event,
                m_context.ptr(),
                monitored_paths,
                kFSEventStreamEventIdSinceNow,
                stream_latency,
                stream_flags);

            if (m_stream) {
                FSEventStreamSetDispatchQueue(m_stream, m_dispatch_queue);
                FSEventStreamStart(m_stream);
            }
        });

        if (!m_stream)
            return Error::from_string_literal("Could not create an FSEventStream");
        return {};
    }

    EventLoop& m_main_event_loop;

    NonnullOwnPtr<FSEventStreamContext> m_context;
    dispatch_queue_t m_dispatch_queue { nullptr };
    FSEventStreamRef m_stream { nullptr };

    HashMap<DeprecatedString, ino_t> m_path_to_inode_id;
    HashMap<ino_t, MonitoredPath> m_inode_id_to_path;
};

void on_file_system_event(ConstFSEventStreamRef, void* user_data, size_t event_size, void* event_paths, FSEventStreamEventFlags const event_flags[], FSEventStreamEventId const[])
{
    auto& file_watcher = *reinterpret_cast<FileWatcherMacOS*>(user_data);
    auto paths = reinterpret_cast<CFArrayRef>(event_paths);

    for (size_t i = 0; i < event_size; ++i) {
        auto path_dictionary = static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(paths, static_cast<CFIndex>(i)));
        auto path = static_cast<CFStringRef>(CFDictionaryGetValue(path_dictionary, kFSEventStreamEventExtendedDataPathKey));

        char file_path_buffer[PATH_MAX] {};
        if (!CFStringGetFileSystemRepresentation(path, file_path_buffer, sizeof(file_path_buffer))) {
            dbgln_if(FILE_WATCHER_DEBUG, "Could not convert event to a file path");
            continue;
        }

        auto maybe_monitored_path = file_watcher.canonicalize_path(DeprecatedString { file_path_buffer });
        if (maybe_monitored_path.is_error()) {
            dbgln_if(FILE_WATCHER_DEBUG, "Could not canonicalize path {}: {}", file_path_buffer, maybe_monitored_path.error());
            continue;
        }
        auto monitored_path = maybe_monitored_path.release_value();

        FileWatcherEvent event;
        event.event_path = move(monitored_path.path);

        auto flags = event_flags[i];
        if ((flags & kFSEventStreamEventFlagItemCreated) != 0)
            event.type |= FileWatcherEvent::Type::ChildCreated;
        if ((flags & kFSEventStreamEventFlagItemRemoved) != 0)
            event.type |= FileWatcherEvent::Type::ChildDeleted;
        if ((flags & kFSEventStreamEventFlagItemModified) != 0)
            event.type |= FileWatcherEvent::Type::ContentModified;
        if ((flags & kFSEventStreamEventFlagItemInodeMetaMod) != 0)
            event.type |= FileWatcherEvent::Type::MetadataModified;

        if (event.type == FileWatcherEvent::Type::Invalid) {
            dbgln_if(FILE_WATCHER_DEBUG, "Unknown event type {:x} returned by the FS event for {}", flags, path);
            continue;
        }
        if ((event.type & monitored_path.event_mask) == FileWatcherEvent::Type::Invalid) {
            dbgln_if(FILE_WATCHER_DEBUG, "Dropping unwanted FS event {} for {}", flags, path);
            continue;
        }

        file_watcher.handle_event(move(event));
    }
}

ErrorOr<NonnullRefPtr<FileWatcher>> FileWatcher::create(FileWatcherFlags flags)
{
    return TRY(FileWatcherMacOS::create(flags));
}

FileWatcher::FileWatcher(int watcher_fd, NonnullRefPtr<Notifier> notifier)
    : FileWatcherBase(watcher_fd)
    , m_notifier(move(notifier))
{
}

FileWatcher::~FileWatcher() = default;

ErrorOr<bool> FileWatcherBase::add_watch(DeprecatedString path, FileWatcherEvent::Type event_mask)
{
    auto& file_watcher = verify_cast<FileWatcherMacOS>(*this);
    return file_watcher.add_watch(move(path), event_mask);
}

ErrorOr<bool> FileWatcherBase::remove_watch(DeprecatedString path)
{
    auto& file_watcher = verify_cast<FileWatcherMacOS>(*this);
    return file_watcher.remove_watch(move(path));
}

}