ReadDirectoryChangesW:异步使用时如何检测缓冲区溢出?

发布于 2024-12-19 07:04:04 字数 112 浏览 2 评论 0原文

我将 ReadDirectoryChangesW (Windows API) 与 GetQueuedCompletionStatus 结合异步使用。如何检测可能的缓冲区溢出以了解至少一个文件系统更改事件已丢失?

I'm using ReadDirectoryChangesW (Windows API) asynchronously in combination with GetQueuedCompletionStatus. How can I detect a possible buffer overflow to understand that at least one file system change event has been lost?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

┾廆蒐ゝ 2024-12-26 07:04:04

当异步使用ReadDirectoryChangesW时,您将获得第一组事件,然后您必须再次调用它以获取更多事件。事件数量超过您的缓冲区的数量并不是一个错误。错误情况是事件数量超出了操作系统级缓冲区的容纳范围,您会发现如下情况:

  1. 发生了某些事件。
  2. ReadDirectoryChangesW 启动的异步操作成功完成。您的缓冲区已填满,您的事件句柄已设置或 IOCP 已触发。
  3. 发生其他事件,这些事件存储在操作系统级缓冲区中。
  4. 更多其他事件发生,导致操作系统级缓冲区溢出。这不会更改重叠操作的状态,该操作已在步骤 2 中成功。
  5. 您等待事件句柄,或处理 IOCP,并发现已完成的 OVERLAPPED 调用。
  6. 您再次调用 ReadDirectoryChangesW 以开始异步重叠操作,检查自步骤 2 以来发生的任何事件。此调用同步失败,GetLastError() = = ERROR_NOTIFY_ENUM_DIR,或成功并返回 dwBytesTransferred == 0,因为 文档说这也意味着重新-枚举目录

如果传输的字节数为零,则缓冲区太大,系统无法分配,或者太小,无法提供目录或子树中发生的所有更改的详细信息。在这种情况下,您应该通过枚举目录或子树来计算更改。

When using ReadDirectoryChangesW asynchronously, you will get the first group of events, then you have to call it again for more events. Having more events than fit in your buffer is not an error. Having more events than fit in the OS-level buffer is the error condition, and you find out like so:

  1. Some event happens.
  2. The asynchronous operation started by ReadDirectoryChangesW completes successfully. Your buffer is filled, your event handle is set or the IOCP is triggered.
  3. Additional events happen, which are stored in the OS-level buffer.
  4. More additional events happen, which overflow the OS-level buffer. This doesn't change the status of the overlapped operation, which already succeeded at step 2.
  5. You wait on the event handle, or process the IOCP, and discover the completed OVERLAPPED call.
  6. You call ReadDirectoryChangesW again to begin an asynchronous overlapped operation checking for any events that happened since step 2. This call fails synchronously, with GetLastError() == ERROR_NOTIFY_ENUM_DIR, or succeeds with dwBytesTransferred == 0, since the documentation says this also means to re-enumerate the directory

If the number of bytes transferred is zero, the buffer was either too large for the system to allocate or too small to provide detailed information on all the changes that occurred in the directory or subtree. In this case, you should compute the changes by enumerating the directory or subtree.

韵柒 2024-12-26 07:04:04

您可能无法通过这种方式完成检测,但是此处是一个很棒的教程,可能会有所帮助。

您还可以查看这个其他问题的答案。

You may not be able to accomplish your detections that way, but here is a great tutorial that may help.

You might also check out the answer to this other question.

千柳 2024-12-26 07:04:04

这里来看,似乎没有异步返回这样的错误代码

建议:同步监视更改,但在专用线程中,并注意 ERROR_NOTIFY_ENUM_DIR

Judging from here, it seems like there is no such error code returned asynchronously.

Suggestion: Monitor for changes synchronously, but in a dedicated thread, and watch out for ERROR_NOTIFY_ENUM_DIR.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文