Debian Codegraph @

test/src/fsevents_missing_inode_test.cpp

raw
1 /*
2 * Copyright (c) 2014-2026 Enrico M. Crisostomo
3 *
4 * This program is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License as published by the Free Software
6 * Foundation; either version 3, or (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 * details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 #include <libfswatch/libfswatch_config.h>
17
18 #ifdef HAVE_MACOS_GE_10_13
19
20 #include <CoreServices/CoreServices.h>
21 #include <algorithm>
22 #include <cstdlib>
23 #include <iostream>
24 #include <memory>
25 #include <string>
26 #include <vector>
27
28 #include <libfswatch/c++/monitor.hpp>
29
30 // Expose the callback for a focused regression test without changing the API.
31 #define private public
32 #include <libfswatch/c++/fsevents_monitor.hpp>
33 #undef private
34
35 namespace
36 {
37 std::vector<fsw::event> received_events;
38
39 void capture_events(const std::vector<fsw::event>& events, void *)
40 {
41 received_events.insert(received_events.end(), events.begin(), events.end());
42 }
43
44 bool has_flag(const fsw::event& evt, fsw_event_flag flag)
45 {
46 const auto flags = evt.get_flags();
47 return std::find(flags.begin(), flags.end(), flag) != flags.end();
48 }
49 }
50
51 int main()
52 {
53 const std::string expected_path = "/tmp/fswatch-fsevents-missing-inode";
54
55 auto path = CFStringCreateWithCString(nullptr,
56 expected_path.c_str(),
57 kCFStringEncodingUTF8);
58 if (!path)
59 {
60 std::cerr << "Failed to create CFString path." << std::endl;
61 return EXIT_FAILURE;
62 }
63
64 const void *keys[] = {kFSEventStreamEventExtendedDataPathKey};
65 const void *values[] = {path};
66 auto event_info = CFDictionaryCreate(nullptr,
67 keys,
68 values,
69 1,
70 &kCFTypeDictionaryKeyCallBacks,
71 &kCFTypeDictionaryValueCallBacks);
72 CFRelease(path);
73
74 if (!event_info)
75 {
76 std::cerr << "Failed to create FSEvents event dictionary." << std::endl;
77 return EXIT_FAILURE;
78 }
79
80 const void *event_values[] = {event_info};
81 auto event_paths = CFArrayCreate(nullptr,
82 event_values,
83 1,
84 &kCFTypeArrayCallBacks);
85 CFRelease(event_info);
86
87 if (!event_paths)
88 {
89 std::cerr << "Failed to create FSEvents event array." << std::endl;
90 return EXIT_FAILURE;
91 }
92
93 fsw::fsevents_monitor monitor({expected_path}, capture_events);
94 const FSEventStreamEventFlags event_flags[] = {kFSEventStreamEventFlagItemRemoved};
95 const FSEventStreamEventId event_ids[] = {1};
96
97 fsw::fsevents_monitor::fsevents_callback(nullptr,
98 &monitor,
99 1,
100 const_cast<void *>(static_cast<const void *>(event_paths)),
101 event_flags,
102 event_ids);
103 CFRelease(event_paths);
104
105 if (received_events.size() != 1)
106 {
107 std::cerr << "Expected one event, got " << received_events.size() << "." << std::endl;
108 return EXIT_FAILURE;
109 }
110
111 const auto& event = received_events.front();
112 if (event.get_path() != expected_path)
113 {
114 std::cerr << "Expected path " << expected_path << ", got " << event.get_path() << "." << std::endl;
115 return EXIT_FAILURE;
116 }
117
118 if (event.get_correlation_id() != 0)
119 {
120 std::cerr << "Expected missing file ID to produce correlation id 0, got "
121 << event.get_correlation_id() << "." << std::endl;
122 return EXIT_FAILURE;
123 }
124
125 if (!has_flag(event, Removed))
126 {
127 std::cerr << "Expected Removed event flag." << std::endl;
128 return EXIT_FAILURE;
129 }
130
131 return EXIT_SUCCESS;
132 }
133
134 #else
135
136 int main()
137 {
138 return 77;
139 }
140
141 #endif