| 47 | } |
| 48 | |
| 49 | FolderWatcher::FolderWatcher(const std::filesystem::path &path, bool recursive, DWORD filter, callback_t callback, void *context) : |
| 50 | m_Overlapped { |
| 51 | .hEvent = context // not used for ReadDirectoryChanges so we can stash the user context pointer in it. |
| 52 | }, |
| 53 | m_Recursive(recursive), |
| 54 | m_Filter(filter), |
| 55 | m_Callback(callback), |
| 56 | m_BufferSize() |
| 57 | { |
| 58 | m_FolderHandle.reset(CreateFile( |
| 59 | path.c_str(), FILE_LIST_DIRECTORY, |
| 60 | FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE, nullptr, |
| 61 | OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, nullptr |
| 62 | )); |
| 63 | |
| 64 | if (m_FolderHandle) |
| 65 | { |
| 66 | SYSTEM_INFO info; |
| 67 | GetSystemInfo(&info); |
| 68 | m_BufferSize = std::max(info.dwPageSize, info.dwAllocationGranularity); |
| 69 | |
| 70 | m_Buffer.reset(static_cast<char *>(VirtualAlloc(nullptr, m_BufferSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE))); |
| 71 | if (m_Buffer) |
| 72 | { |
| 73 | rearm(); |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | m_FolderHandle.reset(); |
| 78 | LastErrorHandle(spdlog::level::warn, L"Failed to allocate overlapped IO buffer"); |
| 79 | } |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | LastErrorHandle(spdlog::level::warn, L"Failed to open folder handle"); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | FolderWatcher::~FolderWatcher() |
| 88 | { |