使用readdirectorychangesw监视文件 [英] Monitor file with readdirectorychangesw

查看:152
本文介绍了使用readdirectorychangesw监视文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我在win32 c ++中执行文件监控任务。



有一个软件,每个调用一些WriteFile()和一个FlushFileBuffer() 12 secondes.So,文件在一分钟内有五处变化。



软件调用WriteFile()在文件的某个位置写一个增量时间戳,然后在文件的其他位置调用WriteFile(),最后调用FlushFileBuffer。

以下记录是带有过程监视器的节目,可以解释该软件的功能:



14:20:11.299 WriteFile抵消:1153长度:63724

14:20:11.304 WriteFile偏移量:64877长度:63724

14:20:23.304 FlushFileBuffer

14: 20:23.327 WriteFile偏移量:1153长度:63724

14:20:23.306 WriteFile偏移量:64877长度:63724

14:20:23.204 FlushFileBuffer

14:20:35.348 WriteFile偏移量:1153长度:63724

14:20:35.325 WriteFile偏移量:64877长度:63724

14:20:35.268 FlushFileBuffer



我的工作是监控文件更改,当其上下文发生变化时,我读取文件,并从文件的某个位置获取时间戳。



我使用带有完成例程的ReadDirectoryChangesW()。一旦文件改变,就会调用完成例程,所以我可以确定文件的每次更改都是c适合。





但是在我捕获文件的更改后,我开始读取文件,因为上面提到的时间戳是增量,正常情况应该是:



...

文件更改在第N,读取文件,时间戳的值是time_n

文件在第(N + 1)处更改,读取文件,时间戳的值为time_n + 1

...



但有时情况是这样的情况



...

文件更改在Nth,读取文件,读取时间戳的值是time_n

文件在第(N + 1)处更改,读取文件,时间戳读取的值是time_n

...





...

文件更改在第N,读取文件,时间戳读取的值是time_n + 1

文件在第(N + 1)处更改,读取文件,时间戳读取的值是time_n + 1

...





我怀疑ReadDirectoryChangesW只能告诉我们文件已经改变以及其他一些信息。但是当我收到文件更改时我无法保证回复,FlushFileBuffer将文件的内容彻底刷新到磁盘。



你对这个问题有什么意见吗?



我尝试了什么:



我尝试了CDirectoryChangeWatcher,也有这种情况。

Recently, I was doing a task on file monitoring in win32 c++.

There is a software,which call some WriteFile() and a FlushFileBuffer() every 12 secondes.So,there is five changes to the file in one minute.

The software calls WriteFile() to write an incremental timestamp at some position of the file,then call WriteFile() at other position of the file,at last, call FlushFileBuffer.
The following record is the show with "process monitor",which can explain what the software does:

14:20:11.299 WriteFile Offset:1153 Length:63724
14:20:11.304 WriteFile Offset:64877 Length:63724
14:20:23.304 FlushFileBuffer
14:20:23.327 WriteFile Offset:1153 Length:63724
14:20:23.306 WriteFile Offset:64877 Length:63724
14:20:23.204 FlushFileBuffer
14:20:35.348 WriteFile Offset:1153 Length:63724
14:20:35.325 WriteFile Offset:64877 Length:63724
14:20:35.268 FlushFileBuffer

My job is monitor the file changes,when its context changes,I read the file,and get a timestamp from some position of the file.

I use the ReadDirectoryChangesW() with a completion routine.And once the file changes,the completion routine is called, so I can be sure that every change of the file is captured.


But after I captured the change of the file, I begin to read the file,because the timestamp which refered above is incremental, the normal situation should be :

...
file changes at The Nth,read the file,the timestamp's value is time_n
file changes at The (N+1)th,read the file,the timestamp's value is time_n+1
...

but sometimes, the situation is

...
file changes at The Nth,read the file,the timestamp's value read is time_n
file changes at The (N+1)th,read the file,the timestamp's value read is time_n
...

or
...
file changes at The Nth,read the file,the timestamp's value read is time_n+1
file changes at The (N+1)th,read the file,the timestamp's value read is time_n+1
...


I doubt that ReadDirectoryChangesW can only tell us that the file has changed and some other information.But it's not guaranteed that when I receive the file change respond, the content of the file is thoroughly flushed to disk by FlushFileBuffer.

Do you have any comments on this question?

What I have tried:

I have tried the "CDirectoryChangeWatcher",and there is also the situation.

推荐答案

我不能保证当我收到文件更改响应时,FlushFileBuffer会将文件内容彻底刷新到磁盘上。



是的,这是真的。您可能会在第一次写作时得到通知。有几种方法可以处理这种情况。一种是在收到通知时总是睡一会儿(如500ms或一秒),然后读取时间戳。你也可以跟踪你读到的最后一个并继续阅读(在通知之后),两者之间有一点延迟,直到你看到它与你最后一个不同为止。



这本质上是一个异步过程,因此您必须执行此类操作才能阅读更改。如果您可以控制写入文件的进程,那么您可以通过使用事件来调整事物以使其更加同步。
"it's not guaranteed that when I receive the file change respond, the content of the file is thoroughly flushed to disk by FlushFileBuffer."

Yes, that is true. You are getting probably notified at the first write. There are a few ways you can deal with the situation. One is to always sleep for a little while (like a 500ms or a second) when you get the notification, then read the time stamp. You could also keep track of the last one you read and continue reading (after the notification) with a small delay in between until you see it differ from the last one you.

This is by nature an asynchronous process so you have to do this kind of thing to read the changes. If you have control over the process that writes to the file you could adjust things to make it much more synchronous if you wanted to by using events, as one possibility.


这篇关于使用readdirectorychangesw监视文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆