Skip to content

Commit

Permalink
Use ReadDirectoryChangesW as fallback.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpartanJ committed Aug 10, 2024
1 parent 621939d commit 2f90a44
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
19 changes: 15 additions & 4 deletions src/efsw/WatcherWin32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void CALLBACK WatchCallback( DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOve
}

// Fork watch depending on the Windows API supported
if ( pReadDirectoryChangesExW ) {
if ( pWatch->Extended ) {
WatchCallbackEx( pWatch );
} else {
WatchCallbackOld( pWatch );
Expand All @@ -181,29 +181,40 @@ void CALLBACK WatchCallback( DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOve
}

/// Refreshes the directory monitoring.
bool RefreshWatch( WatcherStructWin32* pWatch ) {
RefreshResult RefreshWatch( WatcherStructWin32* pWatch ) {
initReadDirectoryChangesEx();

bool bRet = false;
RefreshResult ret = RefreshResult::Failed;
pWatch->Watch->Extended = false;

if ( pReadDirectoryChangesExW ) {
bRet = pReadDirectoryChangesExW( pWatch->Watch->DirHandle, pWatch->Watch->Buffer.data(),
(DWORD)pWatch->Watch->Buffer.size(), pWatch->Watch->Recursive,
pWatch->Watch->NotifyFilter, NULL, &pWatch->Overlapped,
NULL, EFSW_ReadDirectoryNotifyExtendedInformation ) != 0;
} else {
if ( bRet ) {
ret = RefreshResult::SucessEx;
pWatch->Watch->Extended = true;
}
}

if ( !bRet ) {
bRet = ReadDirectoryChangesW( pWatch->Watch->DirHandle, pWatch->Watch->Buffer.data(),
(DWORD)pWatch->Watch->Buffer.size(), pWatch->Watch->Recursive,
pWatch->Watch->NotifyFilter, NULL, &pWatch->Overlapped,
NULL ) != 0;

if ( bRet )
ret = RefreshResult::Success;
}

if ( !bRet ) {
std::string error = std::to_string( GetLastError() );
Errors::Log::createLastError( Errors::WatcherFailed, error );
}

return bRet;
return ret;
}

/// Stops monitoring a directory.
Expand Down
6 changes: 5 additions & 1 deletion src/efsw/WatcherWin32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace efsw {

class WatcherWin32;

enum RefreshResult { Success, SucessEx, Failed };

/// Internal watch data
struct WatcherStructWin32 {
OVERLAPPED Overlapped;
Expand All @@ -33,7 +35,7 @@ struct sLastModifiedEvent {
std::string fileName;
};

bool RefreshWatch( WatcherStructWin32* pWatch );
RefreshResult RefreshWatch( WatcherStructWin32* pWatch );

void CALLBACK WatchCallback( DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOverlapped );

Expand All @@ -51,6 +53,7 @@ class WatcherWin32 : public Watcher {
lParam( 0 ),
NotifyFilter( 0 ),
StopNow( false ),
Extended( false ),
Watch( NULL ),
DirName( NULL ) {
Buffer.resize(dwBufferSize);
Expand All @@ -62,6 +65,7 @@ class WatcherWin32 : public Watcher {
LPARAM lParam;
DWORD NotifyFilter;
bool StopNow;
bool Extended;
FileWatcherImpl* Watch;
char* DirName;
sLastModifiedEvent LastModifiedEvent;
Expand Down

0 comments on commit 2f90a44

Please sign in to comment.