简体   繁体   English

检测文件是否被修改,即使在最后一秒内,也没有散列?

[英]Detect if a file was modified, even within the last second, without hashing?

This applies to Mac, but it may also apply to Linux, so I've tagged it as such. 这适用于Mac,但它也可能适用于Linux,所以我已经标记了它。

I'm recursing through a directory and getting a tree, with filesystem attributes. 我正在通过目录递归并获取具有文件系统属性的树。

I then recurse again (and again, and again) each time the window comes back into focus. 然后每当窗口重新聚焦时我再次(再次)再次递归。 When I read the directory, I'm looking for any files that got modified and need to act on them. 当我读取目录时,我正在寻找任何已修改并需要对其进行操作的文件。

The obvious thing that came to mind was to compare the modification dates of each file, however my unit tests proved that this not reliable since the tests themselves execute in less than a second... the result being that files my tests changed go considered unmodified (as it's only accurate to 1 second). 想到的显而易见的事情是比较每个文件的修改日期,但是我的单元测试证明这不可靠,因为测试本身在不到一秒的时间内执行...结果是我的测试更改的文件被视为未经修改(因为它只精确到1秒)。

My fix thus far has been to also compare the file size, but this runs the risk that if the file was changed, and the resulting size is the same, it will also go undetected. 到目前为止,我的修复方法还是比较了文件大小,但这样做的风险是,如果文件被更改,并且结果大小相同,它也将无法检测到。

Far from hashing each file, which is just not feasible (too slow when recursing a whole directory tree), is there something else I can use that HFS/HFS+ offers? 远非散列每个文件,这是不可行的(在递归整个目录树时太慢),我还可以使用HFS / HFS +提供的其他东西吗? Like some sort of version number/modification count attached to the file? 喜欢附加到文件的某种版本号/修改计数? I'm fearing not, although in reality the edge case of files changing as fast as my unit tests are changing them isn't likely to be an issue in a real use case. 我担心没有,虽然实际上文件的边缘情况变化与我的单元测试一样快,但是在实际用例中不太可能是一个问题。

Real time, least load solutions: 实时,最少负载的解决方案:

Note that your issue regarding modification time resolution is only specific to HFS+ . 请注意,有关修改时间分辨率的问题仅适用于HFS +

The most common cross-platform solution used is File Alteration Monitor . 最常用的跨平台解决方案是File Alteration Monitor

Additional links: 其他链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM