w1146869587
2021-11-05 cbf39419a8299e7d119618e5e8e1b1eb35f72f45
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
#include <efsw/WatcherFSEvents.hpp>
#include <efsw/FileWatcherFSEvents.hpp>
#include <efsw/FileSystem.hpp>
#include <efsw/Debug.hpp>
 
#if EFSW_PLATFORM == EFSW_PLATFORM_FSEVENTS
 
namespace efsw {
 
WatcherFSEvents::WatcherFSEvents() :
    Watcher(),
    FWatcher( NULL ),
    FSStream( NULL ),
    WatcherGen( NULL ),
    initializedAsync( false )
{
}
 
WatcherFSEvents::WatcherFSEvents( WatchID id, std::string directory, FileWatchListener * listener, bool recursive, WatcherFSEvents * parent ) :
    Watcher( id, directory, listener, recursive ),
    FWatcher( NULL ),
    FSStream( NULL ),
    WatcherGen( NULL ),
    initializedAsync( false )
{
}
 
WatcherFSEvents::~WatcherFSEvents()
{
    if ( NULL != FSStream )
    {
        if ( initializedAsync )
        {
            FSEventStreamStop( FSStream );
        }
 
        FSEventStreamInvalidate( FSStream );
        FSEventStreamRelease( FSStream );
    }
 
    efSAFE_DELETE( WatcherGen );
}
 
void WatcherFSEvents::init()
{
    CFStringRef CFDirectory            = CFStringCreateWithCString( NULL, Directory.c_str(), kCFStringEncodingUTF8 );
    CFArrayRef    CFDirectoryArray    = CFArrayCreate( NULL, (const void **)&CFDirectory, 1, NULL );
    
    Uint32 streamFlags = kFSEventStreamCreateFlagNone;
    
    if ( FileWatcherFSEvents::isGranular() )
    {
        streamFlags = efswFSEventStreamCreateFlagFileEvents;
    }
    else
    {
        WatcherGen = new WatcherGeneric( ID, Directory, Listener, FWatcher, Recursive );
    }
    
    FSEventStreamContext ctx;
    /* Initialize context */
    ctx.version = 0;
    ctx.info = this;
    ctx.retain = NULL;
    ctx.release = NULL;
    ctx.copyDescription = NULL;
 
    FSStream = FSEventStreamCreate( kCFAllocatorDefault, &FileWatcherFSEvents::FSEventCallback, &ctx, CFDirectoryArray, kFSEventStreamEventIdSinceNow, 0.25, streamFlags );
 
    FWatcher->mNeedInit.push_back( this );
 
    CFRelease( CFDirectoryArray );
    CFRelease( CFDirectory );
}
 
void WatcherFSEvents::initAsync()
{
    FSEventStreamScheduleWithRunLoop( FSStream, FWatcher->mRunLoopRef, kCFRunLoopDefaultMode );
    FSEventStreamStart( FSStream );
    initializedAsync = true;
}
 
void WatcherFSEvents::sendFileAction( WatchID watchid, const std::string& dir, const std::string& filename, Action action, std::string oldFilename )
{
    Listener->handleFileAction( watchid, FileSystem::precomposeFileName( dir ), FileSystem::precomposeFileName( filename ), action, oldFilename );
}
 
void WatcherFSEvents::handleAddModDel( const Uint32& flags, const std::string& path, std::string& dirPath, std::string& filePath )
{
    if ( flags & efswFSEventStreamEventFlagItemCreated )
    {
        if ( FileInfo::exists( path ) )
        {
            sendFileAction( ID, dirPath, filePath, Actions::Add );
        }
    }
 
    if ( flags & efswFSEventsModified )
    {
        sendFileAction( ID, dirPath, filePath, Actions::Modified );
    }
 
    if ( flags & efswFSEventStreamEventFlagItemRemoved )
    {
        // Since i don't know the order, at least i try to keep the data consistent with the real state
        if ( !FileInfo::exists( path ) )
        {
            sendFileAction( ID, dirPath, filePath, Actions::Delete );
        }
    }
}
 
void WatcherFSEvents::handleActions( std::vector<FSEvent>& events )
{
    size_t esize = events.size();
 
    for ( size_t i = 0; i < esize; i++ )
    {
        FSEvent& event = events[i];
 
        if ( event.Flags & (    kFSEventStreamEventFlagUserDropped |
                                kFSEventStreamEventFlagKernelDropped |
                                kFSEventStreamEventFlagEventIdsWrapped |
                                kFSEventStreamEventFlagHistoryDone |
                                kFSEventStreamEventFlagMount |
                                kFSEventStreamEventFlagUnmount |
                                kFSEventStreamEventFlagRootChanged ) )
        {
            continue;
        }
 
        if ( !Recursive )
        {
            /** In case that is not recursive the watcher, ignore the events from subfolders */
            if ( event.Path.find_last_of( FileSystem::getOSSlash() ) != Directory.size() - 1 )
            {
                continue;
            }
        }
 
        if ( FileWatcherFSEvents::isGranular() )
        {
            std::string dirPath( FileSystem::pathRemoveFileName( event.Path ) );
            std::string filePath( FileSystem::fileNameFromPath( event.Path ) );
 
            if ( event.Flags & (    efswFSEventStreamEventFlagItemCreated |
                                    efswFSEventStreamEventFlagItemRemoved |
                                    efswFSEventStreamEventFlagItemRenamed )
            )
            {
                if ( dirPath != Directory )
                {
                    DirsChanged.insert( dirPath );
                }
            }
 
            // This is a mess. But it's FSEvents faults, because shrinks events from the same file in one single event ( so there's no order for them )
            // For example a file could have been added modified and erased, but i can't know if first was erased and then added and modified, or added, then modified and then erased.
            // I don't know what they were thinking by doing this...
            efDEBUG( "Event in: %s - flags: %ld\n", event.Path.c_str(), event.Flags );
 
            if ( event.Flags & efswFSEventStreamEventFlagItemRenamed )
            {
                if (    ( i + 1 < esize ) &&
                        ( events[ i + 1 ].Flags & efswFSEventStreamEventFlagItemRenamed ) &&
                        ( events[ i + 1 ].Id == event.Id + 1 )
                    )
                {
                    FSEvent& nEvent = events[ i + 1 ];
                    std::string newDir( FileSystem::pathRemoveFileName( nEvent.Path ) );
                    std::string newFilepath( FileSystem::fileNameFromPath( nEvent.Path ) );
 
                    if ( event.Path != nEvent.Path )
                    {
                        if ( dirPath == newDir )
                        {
                            if ( !FileInfo::exists( event.Path ) )
                            {
                                sendFileAction( ID, dirPath, newFilepath, Actions::Moved, filePath );
                            }
                            else
                            {
                                sendFileAction( ID, dirPath, filePath, Actions::Moved, newFilepath );
                            }
                        }
                        else
                        {
                            sendFileAction( ID, dirPath, filePath, Actions::Delete );
                            sendFileAction( ID, newDir, newFilepath, Actions::Add );
 
                            if ( nEvent.Flags & efswFSEventsModified )
                            {
                                sendFileAction( ID, newDir, newFilepath, Actions::Modified );
                            }
                        }
                    }
                    else
                    {
                        handleAddModDel( nEvent.Flags, nEvent.Path, dirPath, filePath );
                    }
 
                    if ( nEvent.Flags & (    efswFSEventStreamEventFlagItemCreated |
                                            efswFSEventStreamEventFlagItemRemoved |
                                            efswFSEventStreamEventFlagItemRenamed )
                    )
                    {
                        if ( newDir != Directory )
                        {
                            DirsChanged.insert( newDir );
                        }
                    }
 
                    // Skip the renamed file
                    i++;
                }
                else if ( FileInfo::exists( event.Path ) )
                {
                    sendFileAction( ID, dirPath, filePath, Actions::Add );
 
                    if ( event.Flags & efswFSEventsModified )
                    {
                        sendFileAction( ID, dirPath, filePath, Actions::Modified );
                    }
                }
                else
                {
                    sendFileAction( ID, dirPath, filePath, Actions::Delete );
                }
            }
            else
            {
                handleAddModDel( event.Flags, event.Path, dirPath, filePath );
            }
        }
        else
        {
            efDEBUG( "Directory: %s changed\n", event.Path.c_str() );
            DirsChanged.insert( event.Path );
        }
    }
}
 
void WatcherFSEvents::process()
{
    std::set<std::string>::iterator it = DirsChanged.begin();
 
    for ( ; it != DirsChanged.end(); it++ )
    {
        if ( !FileWatcherFSEvents::isGranular() )
        {
            WatcherGen->watchDir( (*it) );
        }
        else
        {
            sendFileAction( ID, FileSystem::pathRemoveFileName( (*it) ), FileSystem::fileNameFromPath( (*it) ), Actions::Modified );
        }
    }
 
    DirsChanged.clear();
}
 
}
 
#endif